Registry / database / mongo-base-crud

mongo-base-crud

JSON →
library0.4.4jsnpmunverified

mongo-base-crud (v0.4.4) is a TypeScript library that simplifies MongoDB CRUD operations with a class-based Singleton pattern, supporting multi-tenancy via dynamic database names and collection prefixes. It provides methods like save, update, partialUpdate, delete, find, findAll, getById, and aggregate. Key differentiators: built-in Singleton management (via getInstance or .instance()), automatic collection pluralization control, environment-based configuration, and full TypeScript type support. It requires Node.js >=14 and MongoDB >=4.x.

npm install mongo-base-crud
INSTALL
IMPORT
SIG · MONGO-BASE-CRUD
M
mongo-base-crud
databasejavascriptv0.4.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BaseCrud
import { BaseCrud } from 'mongo-base-crud'
const BaseCrud = require('mongo-base-crud')
Package is ESM-only; CommonJS require may cause issues if not using a bundler.
BaseCrud.getInstance()
import { BaseCrud } from 'mongo-base-crud'; const crud = BaseCrud.getInstance(collection, dbName, indexes);
import { getInstance } from 'mongo-base-crud'
getInstance is a static method, not a named export.
interface DocumentWithId
import { DocumentWithId } from 'mongo-base-crud'
Type-only import: use import type { DocumentWithId } from 'mongo-base-crud' if not using runtime.

Demonstrates basic usage: getInstance to obtain a CRUD instance, save a document, and fetch it by ID.

import { BaseCrud } from 'mongo-base-crud'; interface User { id?: string; name: string; } async function main() { const crud = BaseCrud.getInstance<User>('users', 'mydb', { name: 1 }); const saved = await crud.save({ name: 'Alice' }); console.log('Saved:', saved); const found = await crud.getById(saved.id); console.log('Found:', found); } main().catch(console.error);
Debug
Known issues
gotchaSingleton keys are based on dbName and collectionName; using same parameters returns the same instance, which may lead to unintended shared state across requests in serverless environments.
fix
Use .instance() with a unique alias per tenant or request context to avoid singleton conflicts.
affects: >=0.1.0
gotchaThe 'save' method performs an upsert if an 'id' field is present; this may overwrite existing documents unintentionally if 'id' is not unique or expected.
fix
Always validate or generate 'id' properly; consider using 'insertOne' from the underlying MongoDB driver for strict inserts.
affects: >=0.1.0
breakingIn v0.2.0, the 'update' method signature changed from (filter, data) to (data: { id: string, ... }), making direct filter passing invalid.
fix
Pass an object with 'id' key instead of a filter; e.g., { id: 'abc', name: 'new' }.
affects: >=0.2.0
deprecatedThe 'MONGO_DISABLE_PLURAL' environment variable may be deprecated in future releases; use the 'disablePlural' configuration option passed to getInstance.
fix
Pass { disablePlural: true } in the config parameter of getInstance.
affects: <1.0.0
Errors
Common errors & fixes
TypeError: BaseCrud is not a constructor
Using require instead of import in ESM environment, or forgetting to call getInstance() as a static method.
fix
Use import { BaseCrud } from 'mongo-base-crud'; then call BaseCrud.getInstance(...).
ReferenceError: exports is not defined in ES module scope
Package is ESM-only, but project is using CommonJS (require).
fix
Set "type": "module" in package.json, or use dynamic import() within a CommonJS file.
Error: Cannot find module 'typescript-singleton'
Missing peer dependency 'typescript-singleton'.
fix
Run npm install typescript-singleton.
Upgrade
Version history
0.4.4latest on npm
Audit
Dependencies
typescript-singletonrequiredSingleton pattern implementation for instance management
mongodbrequiredMongoDB driver for database operations
Agent activity
2 hits · last 30 days
node
2
Resources
mongo-base-crud — npm install mongo-base-crud · libregistry