Skip to content

Commit

Permalink
Fix knex getter when undefined on model class
Browse files Browse the repository at this point in the history
  • Loading branch information
nuno-vieira-deel committed May 15, 2020
1 parent 8713e82 commit 3fa3750
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = options => {
$beforeInsert(context) {
const parent = super.$beforeInsert(context);

return this.queryResolver(parent);
return this.queryResolver(context, parent);
}

/**
Expand All @@ -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(context, parent, true, queryOptions);
}

/**
* Query resolver.
*/

queryResolver(parent, update = false, queryOptions = {}) {
queryResolver(context, parent, update = false, queryOptions = {}) {
return Promise.resolve(parent)
.then(() => Promise.all(this.getQuery(update, queryOptions)))
.then(() => Promise.all(this.getQuery(context, update, queryOptions)))
.then(rows => {
const errors = this.parseErrors(rows);

Expand All @@ -73,9 +73,14 @@ module.exports = options => {
* Get select query.
*/

getQuery(update, queryOptions) {
getQuery(context, update, queryOptions) {
return options.fields.reduce((queries, field, index) => {
const knex = Model.knex();
let knex = Model.knex();

if (!knex) {
knex = context.transaction;
}

const collection = knex(this.constructor.tableName);
const fields = castArray(field);

Expand Down

0 comments on commit 3fa3750

Please sign in to comment.