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 d655e58
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 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
18 changes: 14 additions & 4 deletions sqliteDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,32 @@ 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
}, {})

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 d655e58

Please sign in to comment.