Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better-sqlite3 as sqlite module
Browse files Browse the repository at this point in the history
artemave committed Mar 27, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent efaf537 commit 852b928
Showing 5 changed files with 1,368 additions and 347 deletions.
1,551 changes: 1,238 additions & 313 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
"underscore": "1.8.3"
},
"devDependencies": {
"better-sqlite3": "^4.1.0",
"chai": "4.0.2",
"chai-as-promised": "7.0.0",
"electron": "1.6.11",
@@ -32,7 +33,7 @@
"test-mysql": "mocha test/mysqlSpec.js",
"test-postgres": "mocha test/postgresSpec.js",
"test-mssql": "mocha test/mssqlSpec.js",
"test-sqlite": "mocha test/sqliteSpec.js"
"test-sqlite": "DRIVER_MODULE=sqlite3 mocha test/sqliteSpec.js && DRIVER_MODULE=better-sqlite3 mocha test/sqliteSpec.js"
},
"repository": {
"type": "git",
69 changes: 66 additions & 3 deletions sqliteDriver.js
Original file line number Diff line number Diff line change
@@ -4,8 +4,71 @@ var debug = require('debug')('sworm:sqlite');
var urlUtils = require('url')

module.exports = function() {
var sqlite = optionalRequire('sqlite3');
var Database;

try {
Database = require('better-sqlite3');
return betterSqliteInit(Database);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
try {
Database = require('sqlite3').Database;
return sqliteInit(Database);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new Error("Driver not found, please install either better-sqlite3 or sqlite3 modules");
}
throw e;
}
}
throw e;
}
};

function betterSqliteInit(Database) {
return {
query: function(query, params, options) {
debug(query, params);
if (options.statement || options.insert) {
var statement = this.connection.prepare(query);
var res = statement.run(params);
return Promise.resolve({
id: params.hasOwnProperty(options.id) ? params[options.id] : res.lastInsertROWID,
changes: res.changes
});
} else if (options.exec || options.multiline) {
return Promise.resolve(this.connection.exec(query));
} else {
var statement = this.connection.prepare(query);
try {
return Promise.resolve(statement.all(params));
} catch (e) {
if (e.message === 'This statement does not return data. Use run() instead') {
return Promise.resolve(statement.run(params));
}
return Promise.reject(e);
}
}
},

insert: function(query, params, options) {
return this.query(query, params, options)
},

connect: function(options) {
var config = parseConfig(options)
this.connection = new Database(config.filename, config);
return Promise.resolve()
},

close: function() {
this.connection.close();
return Promise.resolve();
}
};
};

function sqliteInit(Database) {
return {
query: function(query, params, options) {
var self = this;
@@ -54,9 +117,9 @@ module.exports = function() {

return promisify(function(cb) {
if (options.mode) {
self.connection = new sqlite.Database(config.filename, config.mode, cb);
self.connection = new Database(config.filename, config.mode, cb);
} else {
self.connection = new sqlite.Database(config.filename, cb);
self.connection = new Database(config.filename, cb);
}
});
},
90 changes: 61 additions & 29 deletions test/describeDatabase.js
Original file line number Diff line number Diff line change
@@ -10,9 +10,41 @@ require('es6-promise').polyfill();

module.exports = function(name, config, database, otherTests) {
describe(name, function() {
var driverModuleName;

if (database.driverModuleName instanceof Array) {
var modulesToDisable = database.driverModuleName.filter(function(moduleName) {
return moduleName !== process.env.DRIVER_MODULE;
});

before(function() {
return Promise.all(
modulesToDisable.map(function(moduleName) {
var modulePath = process.cwd() + "/node_modules/" + moduleName;
return fs.rename(modulePath, modulePath + ".disabled");
})
);
});
after(function() {
return Promise.all(
modulesToDisable.map(function(moduleName) {
var modulePath = process.cwd() + "/node_modules/" + moduleName;
return fs.rename(modulePath + ".disabled", modulePath);
})
);
});

driverModuleName = process.env.DRIVER_MODULE;
} else {
driverModuleName = database.driverModuleName;
}
describeDatabase(driverModuleName);
});

function describeDatabase (driverModuleName) {
if (!database.noModule) {
describe("missing modules", function() {
var moduleName = __dirname + "/../node_modules/" + database.driverModuleName;
describe("missing module", function() {
var moduleName = __dirname + "/../node_modules/" + driverModuleName;

beforeEach(function() {
return fs.rename(moduleName, moduleName + ".missing");
@@ -25,7 +57,7 @@ module.exports = function(name, config, database, otherTests) {
it("throws an exception if the driver module is not present", function() {
return expect(function() {
sworm.db(config).connect();
}).to.throw("npm install " + database.driverModuleName);
}).to.throw(driverModuleName);
});
});
}
@@ -119,7 +151,7 @@ module.exports = function(name, config, database, otherTests) {

it("can insert", function() {
var p = person({
name: "bob"
name: "bob"
});
return p.save().then(function() {
expect(p.id).to.exist;
@@ -158,7 +190,7 @@ module.exports = function(name, config, database, otherTests) {
table: 'people'
});
var p = person({
name: "bob"
name: "bob"
});
return p.save().then(function() {
expect(p.id).to.exist;
@@ -472,10 +504,10 @@ module.exports = function(name, config, database, otherTests) {
return p.save().then(function() {
return db.query("select * from people").then(function(people) {
expect(database.clean(people)).to.eql([{
id: p.id,
name: "jane",
address_id: null,
photo: null
id: p.id,
name: "jane",
address_id: null,
photo: null
}]);
});
});
@@ -664,9 +696,9 @@ module.exports = function(name, config, database, otherTests) {

it("can update an entity with compound keys", function() {
var pa = personAddress({
person_id: 12,
address_id: 34,
rating: 1
person_id: 12,
address_id: 34,
rating: 1
});

return pa.save().then(function() {
@@ -752,10 +784,10 @@ module.exports = function(name, config, database, otherTests) {
return describe("model queries", function() {
it("can pass parameters to a query", function() {
return person({
name: "bob"
name: "bob"
}).save().then(function() {
return person({
name: "jane"
name: "jane"
}).save().then(function() {
return person.query("select name from people where name = @name", {
name: "jane"
@@ -805,10 +837,10 @@ module.exports = function(name, config, database, otherTests) {
describe("foreign keys", function() {
it("can save a many to one relationship", function() {
var bob = person({
name: "bob",
address: address({
address: "15, Rue d'Essert"
})
name: "bob",
address: address({
address: "15, Rue d'Essert"
})
});

return bob.save().then(function() {
@@ -847,9 +879,9 @@ module.exports = function(name, config, database, otherTests) {
it("can save a many to one relationship with function that returns undefined", function() {
var bobsAddress;
var bob = person({
name: "bob",
address: function () {
}
name: "bob",
address: function () {
}
});

return bob.save().then(function() {
@@ -977,8 +1009,8 @@ module.exports = function(name, config, database, otherTests) {

return db.query("select * from addresses").then(function(addresses) {
expect(database.clean(addresses)).to.eql([ {
id: bob.address_id,
address: "15, Rue d'Essert"
id: bob.address_id,
address: "15, Rue d'Essert"
} ]);

return db.query("select * from people order by name").then(function(people) {
@@ -1055,16 +1087,16 @@ module.exports = function(name, config, database, otherTests) {
}

var bob = person({
name: "bob"
name: "bob"
});
var jane = person({
name: "jane"
name: "jane"
});
var fremantle = address({
address: "Fremantle"
address: "Fremantle"
});
var essert = address({
address: "15 Rue d'Essert"
address: "15 Rue d'Essert"
});

livesIn(bob, fremantle);
@@ -1573,7 +1605,7 @@ module.exports = function(name, config, database, otherTests) {
name: 'name'
})
})

return db.queryGraph(def,
'select ' +
'people.id as person_id, name, addresses.id as address_id, address ' +
@@ -1757,5 +1789,5 @@ module.exports = function(name, config, database, otherTests) {
});
});
});
});
}
};
2 changes: 1 addition & 1 deletion test/sqliteSpec.js
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ var database = {
return records;
},

driverModuleName: "sqlite3"
driverModuleName: ["sqlite3", "better-sqlite3"]
};

var config = {

0 comments on commit 852b928

Please sign in to comment.