lodash-id
makes it easy to manipulate id-based resources with lodash or lowdb
getById
insert
upsert
updateById
updateWhere
replaceById
removeById
removeWhere
createId
Install
# with lodash npm install lodash lodash-id --save # with lowdb npm install lowdb lodash-id --save
Note lodash-id
is also compatible with underscore
API
In the API examples, we're assuming db
to be:
const db = posts: id: 1 body: 'one' published: false id: 2 body: 'two' published: true comments: id: 1 body: 'foo' postId: 1 id: 2 body: 'bar' postId: 2
getById(collection, id)
Finds and returns document by id or undefined.
const post = _
insert(collection, document)
Adds document to collection, sets an id and returns created document.
const post = _
If the document already has an id, and it is the same as an existing document in the collection, an error is thrown.
__ // Throws an error
upsert(collection, document)
Adds document to collection, sets an id and returns created document.
const post = _
If the document already has an id, it will be used to insert or replace.
___ // { id: 1, title: 'New title' }
updateById(collection, id, attrs)
Finds document by id, copies properties to it and returns updated document or undefined.
const post = _
updateWhere(collection, whereAttrs, attrs)
Finds documents using _.where
, updates documents and returns updated documents or an empty array.
// Publish all unpublished postsconst posts = _
replaceById(collection, id, attrs)
Finds document by id, replaces properties and returns document or undefined.
const post = _
removeById(collection, id)
Removes document from collection and returns it or undefined.
const comment = _
removeWhere(collection, whereAttrs)
Removes documents from collection using _.where
and returns removed documents or an empty array.
const comments = _
id
Overwrite it if you want to use another id property.
_id = '_id'
createId(collectionName, doc)
Called by lodash-id when a document is inserted. Overwrite it if you want to change id generation algorithm.
_ `--`
Changelog
See details changes for each version in the release notes.
License
MIT - Typicode 🌵