From 2ac0560bf4e0cee40a8f91b87cfdaae3d9e28ac5 Mon Sep 17 00:00:00 2001 From: snlamm Date: Thu, 31 Aug 2017 10:48:31 -0400 Subject: [PATCH] OrFail no longer relies on a specific version of Objection --- README.md | 4 ++-- index.js | 22 ++++++++++++++++++++-- test/index.js | 26 +++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d18ead6..06b8e75 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Person.query().finder.isDisabledOrStatus(true, 'failed') // => Person.query().where('is_disabled', true).orWhere('status', 'failed') Person.query().finder.firstNameOrFail('Jane') -// If no model is returned, throws a NotFoundError. +// If no model is returned, throws an error (uses throwIfNotFound() in Objection > 0.8.1) Person.query().finder.firstNameAndNonExistingField('foo') // => NotFoundError 'Querying invalid field: non_existing_field' @@ -29,7 +29,7 @@ Person.query().avg('income').finder.lastNameAndCountry('Smith', 'USA').where('ag ``` ## Installation -Due to [Babel not transpiling](http://babeljs.io/learn-es2015/#ecmascript-2015-features-proxies) the Proxy object, this plugin is only compatible with Node versions >= 6.0.0. +Due to [Babel not handling](http://babeljs.io/learn-es2015/#ecmascript-2015-features-proxies) the Proxy object, this plugin is only compatible with Node versions >= 6.0.0. Add the `objection-dynamic-finder` package via your preferred package manager: diff --git a/index.js b/index.js index e177a20..91fc304 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ module.exports = Model => { // For queryStrings that end in 'OrFail', fail if no models are found ex. firstNameOrFail if(queryString.slice(-6) === 'OrFail') { - this.throwIfNotFound() + this._failIfNotFound() queryString = queryString.slice(0, -6) } @@ -45,7 +45,7 @@ module.exports = Model => { // If a jsonSchema is defined on the model, use it to validate that the queried fields exist if(hasSchema) { if((schema.properties[searchField] === void 0) && (schema.properties[cameled] === void 0)) { - throw new Model.NotFoundError(` + throw new Error(` Querying invalid field: ${searchField}. Please fix the query or update your jsonSchema. `) } @@ -69,6 +69,24 @@ module.exports = Model => { // Returns the proxy to allow accces to the getter return proxy } + + // Use throwIfNotFound on Objection >= 0.8.1. Else mimic its basic functionality. + _failIfNotFound() { + if(typeof this.throwIfNotFound === 'function') { + return this.throwIfNotFound() + } + + return this.runAfter(result => { + if(Array.isArray(result) && result.length === 0) { + throw new Error('No models found') + } else if([ null, undefined, 0 ].includes(result)) { + throw new Error('No models found') + } + + return result + }) + } + } return class extends Model { diff --git a/test/index.js b/test/index.js index 44bbf3d..eb5ccc8 100644 --- a/test/index.js +++ b/test/index.js @@ -80,17 +80,37 @@ test('Find or fail', t => { .catch(err => { t.is(err.message, 'NotFoundError') }) +}) + +test('Find or fail. Stub Objection version < 0.8.1', t => { + const throwIfNotFound = Person.QueryBuilder.prototype.throwIfNotFound + Person.QueryBuilder.prototype.throwIfNotFound = null + const personsQuery = Person.query().finder.firstNameOrFail('Jim') + const personQuery = Person.query().finder.firstNameOrFail('Jim').first() + + return personsQuery.then(() => { + Person.QueryBuilder.prototype.throwIfNotFound = throwIfNotFound + t.fail() + }).catch(err => { + t.is(err.message, 'No models found') + }).then(() => { + return personQuery.then(() => { + Person.QueryBuilder.prototype.throwIfNotFound = throwIfNotFound + t.fail() + }).catch(err => { + Person.QueryBuilder.prototype.throwIfNotFound = throwIfNotFound + t.is(err.message, 'No models found') + }) + }) }) test('Querying on a non-existing field fails', t => { - try { Person.query().finder.asdfead('Jane') t.fail() } catch(err) { - t.is(err.message, 'NotFoundError') - t.is(err.data.trim(), 'Querying invalid field: asdfead. Please fix the query or update your jsonSchema.') + t.is(err.message.trim(), 'Querying invalid field: asdfead. Please fix the query or update your jsonSchema.') } })