Registry / database / pip-services3-mongodb-nodex

pip-services3-mongodb-nodex

JSON →
library1.1.3jsnpmunverified

Provides MongoDB persistence components for the Pip.Services microservice toolkit. Current stable version is 1.1.3, part of the Pip.Services NodeX ecosystem. It offers abstract base classes like IdentifiableMongoDbPersistence for CRUD operations, connection management via MongoDbConnection, and factory patterns for building persistence. Supports ES2017+ and TypeScript. Release cycle tied to Pip.Services updates. Differentiates by providing a standardized, configurable persistence layer for microservices with built-in filtering, paging, and indexing support.

npm install pip-services3-mongodb-nodex
INSTALL
IMPORT
SIG · PIP-SERVICES3-MONG
P
pip-services3-mongodb-nodex
databasejavascriptv1.1.3
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.

IdentifiableMongoDbPersistence
import { IdentifiableMongoDbPersistence } from 'pip-services3-mongodb-nodex'
import IdentifiableMongoDbPersistence from 'pip-services3-mongodb-nodex'
This is a named export, not default. Must use named import syntax.
MongoDbConnection
import { MongoDbConnection } from 'pip-services3-mongodb-nodex'
Used for configuring and managing MongoDB connections via Pip.Services connection parameters.
DefaultMongoDbFactory
import { DefaultMongoDbFactory } from 'pip-services3-mongodb-nodex'
Factory class to register MongoDB persistence components in Pip.Services container.
IdentifiableJsonMongoDbPersistence
import { IdentifiableJsonMongoDbPersistence } from 'pip-services3-mongodb-nodex'
Variant that stores documents as JSON strings, useful for versioning.

Demonstrates creating a custom MongoDB persistence class extending IdentifiableMongoDbPersistence with filtering and custom methods.

import { IdentifiableMongoDbPersistence } from 'pip-services3-mongodb-nodex'; import { IIdentifiable } from 'pip-services3-commons-nodex'; import { FilterParams, PagingParams, DataPage } from 'pip-services3-commons-nodex'; interface MyObject extends IIdentifiable { key: string; value: number; } class MyMongoDbPersistence extends IdentifiableMongoDbPersistence<MyObject> { constructor() { super('myobjects'); this.ensureIndex({ key: 1 }, { unique: true }); } private composeFilter(filter: FilterParams): any { filter = filter || new FilterParams(); const criteria: any[] = []; const id = filter.getAsNullableString('id'); if (id != null) criteria.push({ _id: id }); const ids = filter.getAsNullableString('ids'); if (ids != null) criteria.push({ _id: { $in: ids.split(',') } }); const key = filter.getAsNullableString('key'); if (key != null) criteria.push({ key: key }); return criteria.length > 0 ? { $and: criteria } : null; } public getPageByFilter(correlationId: string, filter: FilterParams, paging: PagingParams, callback: (err: any, page: DataPage<MyObject>) => void): void { super.getPageByFilter(correlationId, this.composeFilter(filter), paging, '_id', null, callback); } public getOneByKey(correlationId: string, key: string, callback: (err: any, item: MyObject) => void): void { const filter = { key: key }; this.getCollection((err, collection) => { if (err) return callback(err, null); collection.findOne(filter, (err, item) => callback(err, item)); }); } } // Usage example (requires configured component context) // persistence.setReferences(references); // persistence.open(null, (err) => { // persistence.create(null, { id: '1', key: 'foo', value: 123 }, (err, item) => console.log(item)); // });
Debug
Known issues
gotchaBase class methods use callbacks, not Promises. Async/await may require wrapping.
fix
Use promisify from utils or wrap calls in new Promise. Consider using MongoDbPersistence which returns promises in newer versions.
affects: >=1.0.0
deprecatedIdentifiableMongoDbPersistence expects callback parameters; some derived classes may not pass them correctly.
fix
Always provide callbacks to base methods even if you ignore errors.
affects: >=1.0.0
gotchaIndex creation in constructor may fail if called before database is open. ensureIndex must be called after connection is established.
fix
Override ensureIndex() method or call it after open() callback.
affects: >=1.0.0
breakingSwitched from MongoDB driver v3 to v4 in version 1.0.0, breaking some internal type expectations.
fix
Update to work with mongodb driver 4.x, which uses new types (e.g., Collection, Db).
affects: <1.0.0 to >=1.0.0
Errors
Common errors & fixes
TypeError: collection.findOne is not a function
getCollection() returns null or error not handled before using collection
fix
Always use getCollection callback pattern: this.getCollection((err, collection) => { if (err) return; ... })
Error: Cannot find module 'pip-services3-commons-nodex'
Missing runtime dependency
fix
npm install pip-services3-commons-nodex
TypeError: (intermediate value).(...) is not iterable
FilterParams methods return null for missing keys, not empty string
fix
Check for null before using result: const key = filter.getAsNullableString('key') ?? '';
Upgrade
Version history
1.1.3latest on npm
Audit
Dependencies
pip-services3-commons-nodexrequiredCore Pip.Services types (IIdentifiable, FilterParams, PagingParams, DataPage) required for persistence implementations.
mongodbrequiredOfficial MongoDB Node.js driver, version 4.x or 5.x compatible.
Agent activity
9 hits · last 30 days
node
8
Resources
pip-services3-mongodb-nodex — npm install pip-services3-mongodb-nodex · libregistry