Skip to content

Commit

Permalink
add model query logguer
Browse files Browse the repository at this point in the history
  • Loading branch information
BibiFock committed Jun 5, 2020
1 parent c1c0e44 commit 95d11c9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { concatValues } from './helpers';

export { concatValues };

export default ({ filename }) => {
export default ({ filename, queryLogger }) => {
const dbParams = {
filename,
driver: sqlite3.Database
Expand All @@ -29,7 +29,7 @@ export default ({ filename }) => {

return {
connect,
model: model(connect),
model: model({ connect, log: queryLogger }),
migrate
};
};
14 changes: 10 additions & 4 deletions src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ');
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 5 additions & 8 deletions tests/features/model/getter/getter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand All @@ -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([
Expand Down

0 comments on commit 95d11c9

Please sign in to comment.