diff --git a/README.md b/README.md index 48ba0d9..621cbda 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ $ npm install sqlite --save ### How to Use +**NOTE**: For Node.js v5 and below use `var db = require('sqlite/legacy');`. + This module has the same API as the original `sqlite3` library ([docs](https://github.com/mapbox/node-sqlite3/wiki/API)), except that all its API methods return ES6 Promises and do not accept callback arguments. @@ -58,8 +60,13 @@ Promise.resolve() .finally(() => app.listen(port)); ``` -**NOTE**: For Node.js v5 and below use `var db = require('sqlite/legacy');`. +### Cached DB Driver +If you want to enable the (database object cache)[https://github.com/mapbox/node-sqlite3/wiki/Caching] + +``` +db.open('./database.sqlite', { cached: true })) +``` ### Migrations diff --git a/src/main.js b/src/main.js index 2a9b52c..a52e321 100644 --- a/src/main.js +++ b/src/main.js @@ -18,8 +18,17 @@ const db = new Database(null, { Promise: promise }); * * @returns Promise A promise that resolves to an instance of SQLite database client. */ -db.open = (filename, { mode = null, verbose = false, Promise = promise } = {}) => { +db.open = (filename, { + mode = null, + verbose = false, + Promise = promise, + cached = false } = {}) => { let driver; + let DBDriver = sqlite3.Database; + + if (cached) { + DBDriver = sqlite3.cached.Database; + } if (verbose) { sqlite3.verbose(); @@ -27,7 +36,7 @@ db.open = (filename, { mode = null, verbose = false, Promise = promise } = {}) = return new Promise((resolve, reject) => { if (mode !== null) { - driver = new sqlite3.Database(filename, mode, (err) => { + driver = new DBDriver(filename, mode, (err) => { if (err) { reject(err); } else { @@ -35,7 +44,7 @@ db.open = (filename, { mode = null, verbose = false, Promise = promise } = {}) = } }); } else { - driver = new sqlite3.Database(filename, (err) => { + driver = new DBDriver(filename, (err) => { if (err) { reject(err); } else { diff --git a/test/spec.js b/test/spec.js index 96c02a0..93950f9 100644 --- a/test/spec.js +++ b/test/spec.js @@ -10,30 +10,35 @@ const db = require('../build/main'); const expect = require('chai').expect; -it('Should open a database connection', (done) => { - let p = db.open(':memory:'); - p = p.then(() => db.exec('CREATE TABLE tbl (col TEXT)')); - p = p.then(() => db.exec('INSERT INTO tbl VALUES ("test")')); - p = p.then(() => db.get('SELECT col FROM tbl').then((result) => { - expect(result).to.be.deep.equal({ col: 'test' }); - })); - p = p.then(() => db.all('SELECT col FROM tbl').then((result) => { - expect(result).to.be.deep.equal([{ col: 'test' }]); - })); - p = p.then(() => db.all('SELECT * FROM tbl WHERE col = ?', 'test').then((result) => { - expect(result).to.have.length(1); - })); - p = p.then(() => db.run('UPDATE tbl SET col = ? WHERE col = ?', 'foo', 'test')).then((stmt) => { - // Cannot use deep equals because stmt is a Statement instance - expect(stmt.lastID).to.equal(1); - expect(stmt.changes).to.equal(1); - expect(stmt.sql).to.equal('UPDATE tbl SET col = ? WHERE col = ?'); +// enable the sqlite cached database or not +const cache = [false, true]; + +cache.forEach((c) => { + it(`Should open a database connection; cached = ${c}`, (done) => { + let p = db.open(':memory:', { cached: c }); + p = p.then(() => db.exec('CREATE TABLE tbl (col TEXT)')); + p = p.then(() => db.exec('INSERT INTO tbl VALUES ("test")')); + p = p.then(() => db.get('SELECT col FROM tbl').then((result) => { + expect(result).to.be.deep.equal({ col: 'test' }); + })); + p = p.then(() => db.all('SELECT col FROM tbl').then((result) => { + expect(result).to.be.deep.equal([{ col: 'test' }]); + })); + p = p.then(() => db.all('SELECT * FROM tbl WHERE col = ?', 'test').then((result) => { + expect(result).to.have.length(1); + })); + p = p.then(() => db.run('UPDATE tbl SET col = ? WHERE col = ?', 'foo', 'test')).then((stmt) => { + // Cannot use deep equals because stmt is a Statement instance + expect(stmt.lastID).to.equal(1); + expect(stmt.changes).to.equal(1); + expect(stmt.sql).to.equal('UPDATE tbl SET col = ? WHERE col = ?'); + }); + p = p.then(() => db.get('SELECT col FROM tbl').then((result) => { + expect(result).to.be.deep.equal({ col: 'foo' }); + })); + p = p.then(() => db.close()); + p.then(done, done); }); - p = p.then(() => db.get('SELECT col FROM tbl').then((result) => { - expect(result).to.be.deep.equal({ col: 'foo' }); - })); - p = p.then(() => db.close()); - p.then(done, done); }); it('Should allow named parameters to be used', (done) => {