-
Notifications
You must be signed in to change notification settings - Fork 6
/
example.js
30 lines (23 loc) · 909 Bytes
/
example.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
var sqlite3 = require("sqlite3"),
TransactionDatabase = require("sqlite3-transactions").TransactionDatabase;
// Wrap sqlite3 database
var db = new TransactionDatabase(
new sqlite3.Database("test.sqlite", sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE)
);
// Use db as normal sqlite3.Database object.
db.exec("CREATE TABLE ...", function(err) {
// table created
});
// Begin a transaction.
db.beginTransaction(function(err, transaction) {
// Now we are inside a transaction.
// Use transaction as normal sqlite3.Database object.
transaction.run("INSERT ...");
// All calls db.exec(), db.run(), db.beginTransaction(), ... are
// queued and executed after you do transaction.commit() or transaction.rollback()
// This will be executed after the transaction is finished.
database.run("INSERT ...");
// Remember to .commit() or .rollback()
transaction.commit();
// or transaction.rollback()
});