Skip to content

Commit

Permalink
Added mustFind and mustGet
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Jun 9, 2017
1 parent dc2b9cf commit c197efe
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Changelog

## `2.1.7`

:tophat:

- Added `mustGet` and `mustFind` as whinny versions of `get` and `find`

## `2.1.6`

:nail_care:

- Added flow types in the build process
- Updated dependencies

## `2.1.5`

:nail_care:

- Improved error handling (kudos to @p3drosola)

## `2.1.4`

:bug: Bugfix:

- Stop publishing `babelrc` since it breaks React native

## `2.1.3`

:bug: Bugfix:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-rest",
"version": "2.1.6",
"version": "2.1.7",
"description": "REST conventions for mobx.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-jest": "^20.0.3",
"babel-plugin-transform-async-to-generator": "^6.24.1",
Expand Down
25 changes: 25 additions & 0 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ export default class Collection<T: Model> {
return this.models.find(item => item.id === id)
}

/**
* The whinny version of the `get` method
*/
mustGet (id: Id): T {
const model = this.get(id)

if (!model) throw Error(`Invariant: Model must be found with id: ${id}`)

return model
}

/**
* Get resources matching criteria
*/
Expand All @@ -113,6 +124,20 @@ export default class Collection<T: Model> {
})
}

/**
* The whinny version of `find`
*/
mustFind (query: { [key: string]: mixed }): T {
const model = this.find(query)

if (!model) {
const conditions = JSON.stringify(query)
throw Error(`Invariant: Model must be found with: ${conditions}`)
}

return model
}

/**
* Adds a collection of models.
* Returns the added models.
Expand Down

0 comments on commit c197efe

Please sign in to comment.