forked from mariadb-developers/nodejs-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_insert.js
40 lines (32 loc) · 1.08 KB
/
batch_insert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const db = require("./db");
async function asyncFunction() {
let conn;
try {
// Insert values
const contacts = [
["Bob", "Hardy", "[email protected]"],
["Karen", "Smith", "[email protected]"],
["Katie", "Johnson", "[email protected]"]
];
// Acquire a connection from the connection pool
conn = await db.pool.getConnection();
// Start a new transaction
await conn.beginTransaction();
// Insert query with parameter placeholders
const insertQuery = "INSERT INTO demo.contacts (first_name, last_name, email) VALUES (?, ?, ?)";
// Insert new customer record using pool.query function
const result = await conn.batch(insertQuery, contacts);
// Commit the transaction
await conn.commit();
// Print result
console.log(result);
} catch (err) {
// Print errors
console.log(err);
// Roll back the transaction
conn.rollback();
} finally {
if (conn) await conn.release();
}
}
asyncFunction();