From 9a2b9a35f4104cd38fd6da7f46ed4c6751dae3a6 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Fri, 15 May 2020 15:18:08 +0100 Subject: [PATCH] Fix knex getter when undefined on model class --- index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 8dced37..eecf02b 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ module.exports = options => { $beforeInsert(context) { const parent = super.$beforeInsert(context); - return this.queryResolver(parent); + return this.queryResolver(parent, false, {}, context); } /** @@ -46,16 +46,16 @@ module.exports = options => { throw new Error('Unique validation at update only works with queries started with $query.'); } - return this.queryResolver(parent, true, queryOptions); + return this.queryResolver(parent, true, queryOptions, context); } /** * Query resolver. */ - queryResolver(parent, update = false, queryOptions = {}) { + queryResolver(parent, update = false, queryOptions = {}, context) { return Promise.resolve(parent) - .then(() => Promise.all(this.getQuery(update, queryOptions))) + .then(() => Promise.all(this.getQuery(update, queryOptions, context))) .then(rows => { const errors = this.parseErrors(rows); @@ -73,9 +73,9 @@ module.exports = options => { * Get select query. */ - getQuery(update, queryOptions) { + getQuery(update, queryOptions, context) { return options.fields.reduce((queries, field, index) => { - const knex = Model.knex(); + const knex = Model.knex() || context.transaction; const collection = knex(this.constructor.tableName); const fields = castArray(field);