Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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

Expand Down
15 changes: 12 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,33 @@ const db = new Database(null, { Promise: promise });
*
* @returns Promise<Database> 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();
}

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 {
resolve();
}
});
} else {
driver = new sqlite3.Database(filename, (err) => {
driver = new DBDriver(filename, (err) => {
if (err) {
reject(err);
} else {
Expand Down
51 changes: 28 additions & 23 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down