From e3a8a239c83326f1b6bd3597a99242994afda6e3 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 ++- package.json | 2 +- sqliteDriver.js | 20 +++++++++++++++----- 3 files changed, 18 insertions(+), 7 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/package.json b/package.json index 0fbb15b..bf083b6 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/sqliteDriver.js b/sqliteDriver.js index e2ce270..214cdfd 100644 --- a/sqliteDriver.js +++ b/sqliteDriver.js @@ -28,12 +28,22 @@ 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) { @@ -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); }