Registry /
database / pip-services3-mongodb-nodex
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.fixUse 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.fixAlways 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.fixOverride 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.fixUpdate 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
fixAlways use getCollection callback pattern: this.getCollection((err, collection) => { if (err) return; ... }) Error: Cannot find module 'pip-services3-commons-nodex'
Missing runtime dependency
fixnpm install pip-services3-commons-nodex
TypeError: (intermediate value).(...) is not iterable
FilterParams methods return null for missing keys, not empty string
fixCheck for null before using result: const key = filter.getAsNullableString('key') ?? ''; 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.