# lodash-id

> Use JavaScript objects as databases

Latest version **0.14.1** (published 2021-06-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install lodash-id
pnpm add lodash-id
yarn add lodash-id
bun add lodash-id
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.14.1 |
| Published | 2021-06-09 |
| First published | 2017-02-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 4 |
| Dependencies | 0 |
| Unpacked size | 15 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 475 |
| Author | Typicode |
| Maintainers | typicode |
| Keywords | lodash, lowdb, underscore, id, resource, mixin |

## Links

- npm: https://www.npmjs.com/package/lodash-id
- Repository: https://github.com/typicode/lodash-id
- Homepage: https://github.com/typicode/lodash-id#readme
- Issues: https://github.com/typicode/lodash-id/issues
- npm.io page: https://npm.io/package/lodash-id

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.14.1 (latest) — 2021-06-09
- 0.14.0 — 2017-03-14
- 0.13.0 — 2017-02-20

## README

# lodash-id [![Build Status](https://travis-ci.org/typicode/lodash-id.svg)](https://travis-ci.org/typicode/lodash-id) [![NPM version](https://badge.fury.io/js/lodash-id.svg)](http://badge.fury.io/js/lodash-id)

> `lodash-id` makes it easy to manipulate id-based resources with [lodash](https://lodash.com/) or [lowdb](https://github.com/typicode/lowdb)

* `getById`
* `insert`
* `upsert`
* `updateById`
* `updateWhere`
* `replaceById`
* `removeById`
* `removeWhere`
* `createId`

## Install

```bash
# with lodash
npm install lodash lodash-id --save

# with lowdb
npm install lowdb lodash-id --save
```

__Note__ `lodash-id` is also compatible with [underscore](http://underscorejs.org/)

## API

In the API examples, we're assuming `db` to be:

```js
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.

```js
const post = _.getById(db.posts, 1)
```

__insert(collection, document)__

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

```js
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.

```js
_.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.

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

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

```js
_.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.

```js
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.

```js
// 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.

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

__removeById(collection, id)__

Removes document from collection and returns it or undefined.

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

__removeWhere(collection, whereAttrs)__

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

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

__id__

Overwrite it if you want to use another id property.

```js
_.id = '_id'
```

__createId(collectionName, doc)__

Called by lodash-id when a document is inserted. Overwrite it if you want to change id generation algorithm.

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

## Changelog

See details changes for each version in the [release notes](https://github.com/typicode/lodash-id/releases).

## License

MIT - [Typicode :cactus:](https://github.com/typicode)

---
_Source: https://npm.io/package/lodash-id · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
