Model-Nav
A simple library to navigate serialized flat models on your JS projects.
Usage
Install it as usual:
npm install @gluedigital/model-nav
Use it from your code:
import Model from '@gluedigital/model-nav'
// ...
const model = new Model(myData)
const someUser = model.get('user', 42)
const userOffice = model.getRelated(someUser, 'office')
API docs
Below you can check all the available methods:
Method | Example | Description |
constructor |
js const model = new Model(data) |
Creates a new model definition for the given data. If called multiple times with the same data, the internal structures will be shared between instances, providing much greater speed. |
get(type, id) |
const model = new Model(data) const user = model.get('user', 42) |
Returns the entity from the specified type and id. Returns undefined if not found. |
getOne(type) |
const model = new Model(data) const user = model.getOne('user') |
Returns the first entity from the specified type. Returns undefined if there isn't any. |
getAll(type) |
const model = new Model(data) const users = model.getAll('user') |
Returns an array with all the entities from the given type. |
getRelated(entity, relatedType[, relationName]) |
const model = new Model(data) const user = model.get('user', 42) const office = model.getRelated(user, 'office') |
Navigates a relationship (1..1, 1..N, or N..N) with the given settings. See tests for more info. |