Skip to content

Commit

Permalink
OrFail no longer relies on a specific version of Objection
Browse files Browse the repository at this point in the history
  • Loading branch information
snlamm committed Aug 31, 2017
1 parent 063cdd6 commit 2ac0560
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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:

Expand Down
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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.
`)
}
Expand All @@ -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 {
Expand Down
26 changes: 23 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
})

Expand Down

0 comments on commit 2ac0560

Please sign in to comment.