0.14.1 • Published 3 years ago

lodash-id v0.14.1

Weekly downloads
152,170
License
MIT
Repository
github
Last release
3 years ago

lodash-id Build Status NPM version

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 = _.getById(db.posts, 1)

insert(collection, document)

Adds document to collection, sets an id and returns created document.

const post = _.insert(db.posts, { body: 'New post' })

If the document already has an id, and it is the same as an existing document in the collection, an error is thrown.

_.insert(db.posts, { id: 1, body: 'New post' })
_.insert(db.posts, { id: 1, title: 'New title' }) // Throws an error

upsert(collection, document)

Adds document to collection, sets an id and returns created document.

const post = _.upsert(db.posts, { body: 'New post' })

If the document already has an id, it will be used to insert or replace.

_.upsert(db.posts, { id: 1, body: 'New post' })
_.upsert(db.posts, { id: 1, title: 'New title' })
_.getById(db.posts, 1) // { 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 = _.updateById(db.posts, 1, { body: 'Updated body' })

updateWhere(collection, whereAttrs, attrs)

Finds documents using _.where, updates documents and returns updated documents or an empty array.

// Publish all unpublished posts
const posts = _.updateWhere(db.posts, { published: false }, { published: true })

replaceById(collection, id, attrs)

Finds document by id, replaces properties and returns document or undefined.

const post = _.replaceById(db.posts, 1, { foo: 'bar' })

removeById(collection, id)

Removes document from collection and returns it or undefined.

const comment = _.removeById(db.comments, 1)

removeWhere(collection, whereAttrs)

Removes documents from collection using _.where and returns removed documents or an empty array.

const comments = _.removeWhere(db.comments, { postId: 1 })

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.

_.createId = (collectionName, item) => `${collectionName}-${item.prop}-${Date.now()}`

Changelog

See details changes for each version in the release notes.

License

MIT - Typicode :cactus:

swagger-api-tracking-webpack-plugin2swagger-api-tracking-webpack-plugin3swagger-api-tracking-webpack-plugin4swagger-api-tracking-webpack-plugin6swagger-api-tracking-webpack-plugin7swagger-api-tracking-webpack-plugin9@src-works/npm-ts-gulpanother-json-server@r35007/mock-server-litejson-server-custom-routereoapi-core@everything-registry/sub-chunk-2094devkubedialog-differcozy-konnector-libsem-pipeeasy-element-adminegg-react-typescript-boilerplateegg-react-webpack-asset-boilerplateegg-react-webpack-awesome-boilerplateegg-react-webpack-boilerplateegg-react-webpack-html-boilerplateegg-react-webpack-mpa-boilerplateegg-react-webpack-spa-boilerplateegg-vue-typescript-boilerplateegg-vue-webpack-boilerplateegg-vue-webpack-ssregg-vue-asset-boilerplateegg-vue-element-admin-boilerplateegg-vue-html-boilerplateecg-at-uploaderves-adminves-blogemo-json-server@rsf/json-serverdartonian-json-server@sizhi/client@sizhi/domainhai-server@nodox/gatsby@picgo/storeguserver-db-lowdbzapcms-cligoogle-cloud-guivuex-orm-lowdborigami-store-lowdbpochta-mjml@jmilanes/hotbarsxl-json-servergatsby-songwang@masscode/json-server@thg303/json-server@r35007/mock-server@quick-qui/data-providerfds-json-server@mockswitch/cli@lrnwebcomponents/glossary-serviceflat-jsondbwind-core-dao@json-server/core@jxding/agvmaprtv-server@andrkrn/picgo@wll8/json-serverjson-server-cnjson-server-forkjson-server-masterjson-server-plusjson-server-relationshipjson-server-s3json-server-sinnbojson-server-testejson-server-visopjson-server-with-batch-insertionjson-server2jump-json-serverjv-accountke-vekegapilang-live-recimemento-json-server@vbots/session-storagehull-connectorhull-connector-devres-awesome@abstractball/json-serverrest-entity-handler@weaming/picgosomoy@dony435/json-serverauthkeybanner-saleslink-line-corelowserverlowdb-middlewarelowdb-api@gift-adv/json-server@zalastax/nolb-lodajohnnycachead-json-server
0.14.1

3 years ago

0.14.0

7 years ago

0.13.0

7 years ago