From d655e586e2b7c69aed171348be92accc8eaf7af6 Mon Sep 17 00:00:00 2001 From: artemave Date: Tue, 27 Mar 2018 16:06:55 +0100 Subject: [PATCH] Serialize Date objects when using better-sqlite3 To keep the API in sync with sqlite3. --- .travis.yml | 3 ++- sqliteDriver.js | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 81c4f06..fed0ea7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: node_js -node_js: node +node_js: + - node cache: directories: - node_modules diff --git a/sqliteDriver.js b/sqliteDriver.js index e2ce270..89379c6 100644 --- a/sqliteDriver.js +++ b/sqliteDriver.js @@ -29,11 +29,21 @@ 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) { @@ -41,10 +51,10 @@ function betterSqliteInit(Database) { } 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); }