• Stars
    star
    467
  • Rank 90,341 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Makes it easy to manipulate id-based resources with lodash or lowdb

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 🌵

More Repositories

1

json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
JavaScript
70,794
star
2

husky

Git hooks made easy 🐶 woof!
JavaScript
31,302
star
3

lowdb

Simple and fast JSON database
JavaScript
20,699
star
4

hotel

🏩 A simple process manager for developers. Start apps from your browser and access them using local domains
JavaScript
9,953
star
5

jsonplaceholder

A simple online fake REST API server
HTML
4,945
star
6

tlapse

📷 Create a timelapse of your web development... or just automatically take screenshots of your hard work ;)
JavaScript
2,026
star
7

xv

🙅‍♀️ ✌️ fastest test runner
JavaScript
829
star
8

pegasus

Load JSON while still loading other scripts
JavaScript
702
star
9

katon

(use hotel instead)
JavaScript
683
star
10

steno

Super fast async file writer with atomic write ⚡
JavaScript
664
star
11

react-fake-props

🔮 Magically generate fake props for your React tests
JavaScript
628
star
12

fetchival

Easy window.fetch requests
JavaScript
517
star
13

react-lodash

⚛️ 🔧 Lodash as React components
JavaScript
356
star
14

stop-server

📱 Shut down your computer with your phone
JavaScript
341
star
15

demo

A demo repository for My JSON Server (Alpha)
326
star
16

pinst

🍺 dev only postinstall hooks (package.json)
JavaScript
256
star
17

please-upgrade-node

💁 Show a message to your users to upgrade Node instead of a stacktrace
JavaScript
238
star
18

jsop

JSON file reader/writer (powered by Object.observe)
JavaScript
205
star
19

mistcss

Write atomic React components using only CSS! (JS-from-CSS™) 🌬️
JavaScript
168
star
20

husky-4-to-8

Quickly migrate your hooks from husky v4 to husky@latest
JavaScript
126
star
21

user-startup

Auto start commands when you log in (cross-platform)
JavaScript
123
star
22

cult

cult monitors gulpfile changes and reloads gulp
JavaScript
118
star
23

logan

Mini template system for the console and colors
JavaScript
80
star
24

bg.nvim

Automatically sync your terminal background with your colorscheme 🎆
Lua
70
star
25

minihost

Easily start and access servers
JavaScript
45
star
26

ghwn

Get desktop notifications for new issues, comments, stars... (no installation required)
HTML
42
star
27

shoutjs

Make your ShellJS commands explicit and get a beautiful output
JavaScript
29
star
28

homerun

Turn npm package scripts into CLI commands
JavaScript
24
star
29

server-ready

Know when a server is ready to receive requests
JavaScript
20
star
30

husky-init

JavaScript
20
star
31

backbone-pegasus

Load models and collections data while loading Backbone
JavaScript
19
star
32

server-ready-cli

Run commands only when a server is available
JavaScript
18
star
33

typicode.github.io

HTML
7
star
34

eslint-config

JavaScript
7
star
35

coc.nvim

Intellisense engine for vim8 & neovim, full language server protocol support as VSCode
TypeScript
2
star