Skip to content

Commit 94cc07e

Browse files
author
Theo Gravity
authored
Merge pull request #30 from kriasoft/flex-database
Add ability to use custom sqlite3 driver
2 parents d917018 + a98feb5 commit 94cc07e

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ $ npm install sqlite --save
2525

2626
### How to Use
2727

28+
**NOTE**: For Node.js v5 and below use `var db = require('sqlite/legacy');`.
29+
2830
This module has the same API as the original `sqlite3` library ([docs](https://github.com/mapbox/node-sqlite3/wiki/API)),
2931
except that all its API methods return ES6 Promises and do not accept callback arguments.
3032

@@ -58,8 +60,13 @@ Promise.resolve()
5860
.finally(() => app.listen(port));
5961
```
6062

61-
**NOTE**: For Node.js v5 and below use `var db = require('sqlite/legacy');`.
63+
### Cached DB Driver
6264

65+
If you want to enable the (database object cache)[https://github.com/mapbox/node-sqlite3/wiki/Caching]
66+
67+
```
68+
db.open('./database.sqlite', { cached: true }))
69+
```
6370

6471
### Migrations
6572

src/main.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,33 @@ const db = new Database(null, { Promise: promise });
1818
*
1919
* @returns Promise<Database> A promise that resolves to an instance of SQLite database client.
2020
*/
21-
db.open = (filename, { mode = null, verbose = false, Promise = promise } = {}) => {
21+
db.open = (filename, {
22+
mode = null,
23+
verbose = false,
24+
Promise = promise,
25+
cached = false } = {}) => {
2226
let driver;
27+
let DBDriver = sqlite3.Database;
28+
29+
if (cached) {
30+
DBDriver = sqlite3.cached.Database;
31+
}
2332

2433
if (verbose) {
2534
sqlite3.verbose();
2635
}
2736

2837
return new Promise((resolve, reject) => {
2938
if (mode !== null) {
30-
driver = new sqlite3.Database(filename, mode, (err) => {
39+
driver = new DBDriver(filename, mode, (err) => {
3140
if (err) {
3241
reject(err);
3342
} else {
3443
resolve();
3544
}
3645
});
3746
} else {
38-
driver = new sqlite3.Database(filename, (err) => {
47+
driver = new DBDriver(filename, (err) => {
3948
if (err) {
4049
reject(err);
4150
} else {

test/spec.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,35 @@
1010
const db = require('../build/main');
1111
const expect = require('chai').expect;
1212

13-
it('Should open a database connection', (done) => {
14-
let p = db.open(':memory:');
15-
p = p.then(() => db.exec('CREATE TABLE tbl (col TEXT)'));
16-
p = p.then(() => db.exec('INSERT INTO tbl VALUES ("test")'));
17-
p = p.then(() => db.get('SELECT col FROM tbl').then((result) => {
18-
expect(result).to.be.deep.equal({ col: 'test' });
19-
}));
20-
p = p.then(() => db.all('SELECT col FROM tbl').then((result) => {
21-
expect(result).to.be.deep.equal([{ col: 'test' }]);
22-
}));
23-
p = p.then(() => db.all('SELECT * FROM tbl WHERE col = ?', 'test').then((result) => {
24-
expect(result).to.have.length(1);
25-
}));
26-
p = p.then(() => db.run('UPDATE tbl SET col = ? WHERE col = ?', 'foo', 'test')).then((stmt) => {
27-
// Cannot use deep equals because stmt is a Statement instance
28-
expect(stmt.lastID).to.equal(1);
29-
expect(stmt.changes).to.equal(1);
30-
expect(stmt.sql).to.equal('UPDATE tbl SET col = ? WHERE col = ?');
13+
// enable the sqlite cached database or not
14+
const cache = [false, true];
15+
16+
cache.forEach((c) => {
17+
it(`Should open a database connection; cached = ${c}`, (done) => {
18+
let p = db.open(':memory:', { cached: c });
19+
p = p.then(() => db.exec('CREATE TABLE tbl (col TEXT)'));
20+
p = p.then(() => db.exec('INSERT INTO tbl VALUES ("test")'));
21+
p = p.then(() => db.get('SELECT col FROM tbl').then((result) => {
22+
expect(result).to.be.deep.equal({ col: 'test' });
23+
}));
24+
p = p.then(() => db.all('SELECT col FROM tbl').then((result) => {
25+
expect(result).to.be.deep.equal([{ col: 'test' }]);
26+
}));
27+
p = p.then(() => db.all('SELECT * FROM tbl WHERE col = ?', 'test').then((result) => {
28+
expect(result).to.have.length(1);
29+
}));
30+
p = p.then(() => db.run('UPDATE tbl SET col = ? WHERE col = ?', 'foo', 'test')).then((stmt) => {
31+
// Cannot use deep equals because stmt is a Statement instance
32+
expect(stmt.lastID).to.equal(1);
33+
expect(stmt.changes).to.equal(1);
34+
expect(stmt.sql).to.equal('UPDATE tbl SET col = ? WHERE col = ?');
35+
});
36+
p = p.then(() => db.get('SELECT col FROM tbl').then((result) => {
37+
expect(result).to.be.deep.equal({ col: 'foo' });
38+
}));
39+
p = p.then(() => db.close());
40+
p.then(done, done);
3141
});
32-
p = p.then(() => db.get('SELECT col FROM tbl').then((result) => {
33-
expect(result).to.be.deep.equal({ col: 'foo' });
34-
}));
35-
p = p.then(() => db.close());
36-
p.then(done, done);
3742
});
3843

3944
it('Should allow named parameters to be used', (done) => {

0 commit comments

Comments
 (0)