From 95d11c93d4251bed70006c77dc1226de2d3fd6c5 Mon Sep 17 00:00:00 2001 From: bibifock Date: Thu, 4 Jun 2020 21:24:51 +0200 Subject: [PATCH] add model query logguer --- src/index.js | 4 ++-- src/model/model.js | 14 ++++++++++---- tests/features/model/getter/getter.test.js | 13 +++++-------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 97b60cb..59adb42 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ import { concatValues } from './helpers'; export { concatValues }; -export default ({ filename }) => { +export default ({ filename, queryLogger }) => { const dbParams = { filename, driver: sqlite3.Database @@ -29,7 +29,7 @@ export default ({ filename }) => { return { connect, - model: model(connect), + model: model({ connect, log: queryLogger }), migrate }; }; diff --git a/src/model/model.js b/src/model/model.js index b5b4c47..d917159 100644 --- a/src/model/model.js +++ b/src/model/model.js @@ -2,8 +2,14 @@ import SQL from 'sql-template-strings'; import { concatValues } from '../helpers'; -const makeExec = (connect) => (action, query) => connect() - .then(db => db[action](query)); +const makeExec = ({ connect, log }) => (action, query) => connect() + .then(db => { + if (log) { + log({ db, action, query }); + } + + return db[action](query) + }); const makeQueries = ({ fields, table }) => { const fieldsList = fields.join(', '); @@ -71,13 +77,13 @@ const makeStore = ({ exec, queries, fields }) => { * * @return {object} */ -const init = (connect) => (config) => { +const init = ({ connect, log }) => (config) => { const { fields, table, primaryKey = 'rowid', } = config; - const exec = makeExec(connect) + const exec = makeExec({ connect, log }) const queries = makeQueries({ fields, table }); return { diff --git a/tests/features/model/getter/getter.test.js b/tests/features/model/getter/getter.test.js index 1bfa60b..dc70358 100644 --- a/tests/features/model/getter/getter.test.js +++ b/tests/features/model/getter/getter.test.js @@ -7,6 +7,11 @@ describe('Model->getters', () => { beforeAll(async () => { vegeData = VegeData({ filename: getTmpDb() }); + model = vegeData.model.init({ + table: 'characters', + fields: ['name', 'type'] + }); + db = await vegeData.connect(); await db.run(` @@ -26,14 +31,6 @@ describe('Model->getters', () => { `); }); - beforeEach(async () => { - - model = vegeData.model.init({ - table: 'characters', - fields: ['name', 'type'] - }); - }); - describe('all', () => { it('should return all elements in db', async () => { return Promise.all([