Skip to content

Commit

Permalink
Serialize Date objects when using better-sqlite3
Browse files Browse the repository at this point in the history
To keep the API in sync with sqlite3.
  • Loading branch information
artemave committed Mar 27, 2018
1 parent 6ad919f commit e3a8a23
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js: node
node_js:
- node
cache:
directories:
- node_modules
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sworm",
"version": "3.7.1",
"version": "3.8.0",
"description": "a lightweight write-only ORM for MSSQL, MySQL, PostgreSQL, Oracle, Sqlite 3",
"main": "index.js",
"dependencies": {
Expand Down
20 changes: 15 additions & 5 deletions sqliteDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,33 @@ module.exports = function() {
function betterSqliteInit(Database) {
return {
query: function(query, params, options) {
debug(query, params);
var sqliteParams = Object.keys(params).reduce(function(result, key) {
if (params[key] instanceof Date) {
result[key] = params[key].getTime()
} else {
result[key] = params[key]
}
return result
}, {})

debug(query, sqliteParams);

if (options.statement || options.insert) {
var statement = this.connection.prepare(query);
var res = statement.run(params);
var res = statement.run(sqliteParams);
return Promise.resolve({
id: params.hasOwnProperty(options.id) ? params[options.id] : res.lastInsertROWID,
id: sqliteParams.hasOwnProperty(options.id) ? sqliteParams[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));
return Promise.resolve(statement.all(sqliteParams));
} catch (e) {
if (e.message === 'This statement does not return data. Use run() instead') {
return Promise.resolve(statement.run(params));
return Promise.resolve(statement.run(sqliteParams));
}
return Promise.reject(e);
}
Expand Down

0 comments on commit e3a8a23

Please sign in to comment.