{"id":16427,"library":"lodash-id","title":"lodash-id: ID-based Resource Manipulation for Lodash/Lowdb","description":"lodash-id is a utility package that extends Lodash and Underscore with methods for manipulating collections of JavaScript objects based on a unique `id` property, effectively turning them into simple in-memory databases. It provides a set of CRUD-like operations such as `getById`, `insert`, `upsert`, `updateById`, `removeById`, and `removeWhere`. The package normalizes ID comparisons, allowing both string and integer IDs to be treated equivalently since version `0.12.0`. The current stable version is `0.14.1`. Releases are infrequent but address compatibility and feature refinements. Its primary differentiator is its integration as a Lodash mixin, simplifying common data management patterns within existing Lodash-based applications or in conjunction with `lowdb`. It does not handle data persistence directly since `v0.14.0`, delegating that responsibility to `lowdb` or custom implementations.","status":"active","version":"0.14.1","language":"javascript","source_language":"en","source_url":"https://github.com/typicode/lodash-id","tags":["javascript","lodash","lowdb","underscore","id","resource","mixin"],"install":[{"cmd":"npm install lodash-id","lang":"bash","label":"npm"},{"cmd":"yarn add lodash-id","lang":"bash","label":"yarn"},{"cmd":"pnpm add lodash-id","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"lodash-id extends Lodash's utility belt as a mixin. Required at runtime.","package":"lodash","optional":false},{"reason":"lodash-id can be used with lowdb for persistence, which typically bundles its own Lodash-like utilities or similar methods.","package":"lowdb","optional":true}],"imports":[{"note":"lodash-id is designed as a CommonJS module to be mixed into the Lodash (or Underscore) `_` object, not as a direct ESM import for individual functions.","wrong":"import { getById } from 'lodash-id';","symbol":"lodash-id mixin","correct":"const _ = require('lodash');\n_.mixin(require('lodash-id'));"},{"note":"All functions provided by lodash-id become methods of the Lodash `_` object after `_.mixin` is called. They are not standalone exports.","wrong":"const { getById } = require('lodash-id');\nconst post = getById(db.posts, 1);","symbol":"getById (and other methods)","correct":"const post = _.getById(db.posts, 1);"},{"note":"The `id` property for customization is a direct property of the mixed-in Lodash `_` object, not an export of the module itself.","wrong":"lodashId.id = '_id';","symbol":"id (custom ID property)","correct":"_.id = '_id';"}],"quickstart":{"code":"const _ = require('lodash');\n_.mixin(require('lodash-id'));\n\nconst db = {\n  posts: [\n    { id: 1, title: 'First Post', published: false },\n    { id: 2, title: 'Second Post', published: true }\n  ]\n};\n\n// Insert a new document\nconst newPost = _.insert(db.posts, { title: 'Third Post' });\nconsole.log('Inserted post:', newPost); // { id: 3, title: 'Third Post' }\n\n// Get a document by ID\nconst post1 = _.getById(db.posts, 1);\nconsole.log('Got post by ID 1:', post1); // { id: 1, title: 'First Post', published: false }\n\n// Update a document by ID\n_.updateById(db.posts, newPost.id, { published: true });\nconsole.log('Updated post:', _.getById(db.posts, newPost.id)); // { id: 3, title: 'Third Post', published: true }\n\n// Upsert a document (replaces if ID exists, inserts if not)\n_.upsert(db.posts, { id: 1, title: 'First Post Updated', category: 'News' });\nconsole.log('Upserted post 1:', _.getById(db.posts, 1)); // { id: 1, title: 'First Post Updated', category: 'News' }\n\n// Remove a document by ID\nconst removedPost = _.removeById(db.posts, 2);\nconsole.log('Removed post by ID 2:', removedPost); // { id: 2, title: 'Second Post', published: true }\nconsole.log('Remaining posts:', db.posts); // [{ id: 1, title: 'First Post Updated', category: 'News' }, { id: 3, title: 'Third Post', published: true }]\n","lang":"javascript","description":"Demonstrates how to initialize lodash-id and perform basic CRUD operations like insert, get, update, upsert, and remove on an in-memory collection."},"warnings":[{"fix":"Migrate persistence logic to `lowdb` or implement custom `save` and `load` functions that interact with your storage mechanism (e.g., file system, database, browser storage).","message":"As of v0.14.0, `lodash-id` no longer provides `localStorage` or `file` functions for data persistence. Users previously relying on these must now implement custom persistence logic or integrate with `lowdb` directly to handle data saving and loading.","severity":"breaking","affected_versions":">=0.14.0"},{"fix":"If you intend to replace a document when its ID already exists, use `_.upsert(collection, document)` instead of `_.insert(collection, document)`. If you strictly want to add only new documents and fail on duplicates, `_.insert` is appropriate.","message":"The behavior of the `insert` method changed significantly. In v0.8.0, `insert` would replace existing documents with the same ID. However, in current versions (post-v0.8.0, likely with the introduction of `upsert`), `insert` now throws an error if a document with the provided ID already exists in the collection. To replace documents, use the `upsert` method instead.","severity":"breaking","affected_versions":">=0.8.1 (or post-v0.8.0 change)"},{"fix":"Replace any calls to `_.mixWith(require('lodash-id'))` with `_.mixin(require('lodash-id'))`.","message":"The `mixWith` function was removed in v0.7.0. Developers must now use `_.mixin` directly to integrate `lodash-id` functionalities into their Lodash instance.","severity":"breaking","affected_versions":">=0.7.0"},{"fix":"Be aware of ID normalization when interacting with collections. Ensure your application logic accounts for this behavior and does not rely on strict type differences for IDs in `lodash-id` operations.","message":"Since v0.12.0, `lodash-id` normalizes IDs, treating string and integer representations of the same ID (e.g., `1` and `'1'`) as equivalent in methods like `getById`, `updateById`, and `removeById`. While this simplifies usage, it may lead to unexpected behavior if strict type-based ID comparisons were previously relied upon.","severity":"gotcha","affected_versions":">=0.12.0"},{"fix":"Set the `_.id` property to your custom ID key immediately after mixing in `lodash-id`: `_.id = '_id';`","message":"By default, `lodash-id` assumes the unique identifier property for documents is `id`. If your documents use a different property (e.g., `_id`, `uuid`), you must explicitly configure `lodash-id` to use that property.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure you have called `_.mixin(require('lodash-id'))` after importing both `lodash` and `lodash-id`.","cause":"The lodash-id mixin has not been properly applied to the Lodash instance.","error":"TypeError: _.getById is not a function"},{"fix":"If you intend to replace the existing document, use `_.upsert(collection, document)` instead of `_.insert`. If you wish to only insert new unique documents, ensure the `id` is unique or generated dynamically (e.g., by letting `insert` create one if no ID is provided).","cause":"You are attempting to use `_.insert` to add a document with an `id` that already exists in the collection.","error":"Error: Document with id 'X' already exists."},{"fix":"Integrate your `lodash-id` usage with a dedicated persistence layer like `lowdb` (which is often used with `lodash-id`) or implement your own `save` and `load` mechanisms for the data collections.","cause":"You might be expecting `lodash-id` to handle data persistence, but it only provides in-memory array manipulation. Furthermore, `localStorage` and `file` functions were removed in v0.14.0.","error":"Data not saved/loaded after application restart or page refresh."}],"ecosystem":"npm"}