Registry / database / pip-services3-mongodb-node

pip-services3-mongodb-node

JSON →
library3.5.0jsnpmunverified

A module of the Pip.Services polyglot microservice toolkit providing MongoDB persistence components for Node.js. Version 3.5.0 includes factory, connection, and abstract persistence classes for CRUD operations against MongoDB collections. It requires pip-services3-commons-node and pip-services3-mongodb-node as peer dependencies. Differentiators: integrates with the broader Pip.Services framework for microservice configuration, dependency injection, and telemetry; provides type-safe persistence through abstract base classes; supports callback-based async patterns common in earlier Node.js ecosystems.

npm install pip-services3-mongodb-node
INSTALL
IMPORT
SIG · PIP-SERVICES3-MONG
P
pip-services3-mongodb-node
databasejavascriptv3.5.0
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-node'
import IdentifiableMongoDbPersistence from 'pip-services3-mongodb-node'
Named export; no default export exists.
MongoDbConnection
import { MongoDbConnection } from 'pip-services3-mongodb-node'
import * as MongoDbConnection from 'pip-services3-mongodb-node'
Named export; do not use namespace import.
DefaultMongoDbFactory
import { DefaultMongoDbFactory } from 'pip-services3-mongodb-node'
const DefaultMongoDbFactory = require('pip-services3-mongodb-node').DefaultMongoDbFactory
Require pattern works but named import is preferred for TypeScript.

Defines a custom persistence class extending IdentifiableMongoDbPersistence with a composed filter for paginated queries.

import { IdentifiableMongoDbPersistence } from 'pip-services3-mongodb-node'; import { IIdentifiable, FilterParams, PagingParams, DataPage } from 'pip-services3-commons-node'; interface MyObject extends IIdentifiable { key: string; value: number; } class MyMongoDbPersistence extends IdentifiableMongoDbPersistence<MyObject> { public constructor() { super('myobjects'); this.ensureIndex({ key: 1 }, { unique: true }); } 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); } private composeFilter(filter: FilterParams): any { filter = filter || new FilterParams(); let criteria: any[] = []; let id = filter.getAsNullableString('id'); if (id != null) criteria.push({ _id: id }); let tempIds = filter.getAsNullableString('ids'); if (tempIds != null) { let ids = tempIds.split(','); criteria.push({ _id: { $in: ids } }); } let key = filter.getAsNullableString('key'); if (key != null) criteria.push({ key: key }); return criteria.length > 0 ? { $and: criteria } : null; } }
Debug
Known issues
gotchaCallbacks must always handle error; otherwise uncaught exceptions may crash the process.
fix
Always check the error parameter in callbacks before using the result.
affects: >=3.0.0
deprecatedUsing default export from pip-services3-mongodb-node will not work; only named exports are available.
fix
Use named imports: import { IdentifiableMongoDbPersistence } from 'pip-services3-mongodb-node'
affects: >=3.0.0
gotchaThe _id field is automatically mapped to the id property in IdentifiableMongoDbPersistence. Do not define a separate _id field in your data object.
fix
Ensure your data object's id property is used; the base class handles the mapping to MongoDB _id.
affects: >=3.0.0
Errors
Common errors & fixes
Cannot find module 'pip-services3-mongodb-node'
Package not installed or not in node_modules.
fix
Run: npm install pip-services3-mongodb-node --save
Property '_collection' does not exist on type 'IdentifiableMongoDbPersistence'.
Accessing protected property outside subclass.
fix
Access '_collection' only within a class that extends IdentifiableMongoDbPersistence.
TypeError: this.ensureIndex is not a function
Calling ensureIndex before calling base constructor or in wrong context.
fix
Call ensureIndex in the constructor after super() call.
Upgrade
Version history
3.5.0latest on npm
Audit
Dependencies
pip-services3-commons-noderequiredCore Pip.Services types and interfaces (IIdentifiable, FilterParams, PagingParams, DataPage)
mongodbrequiredNative MongoDB driver used internally for database operations
Agent activity
2 hits · last 30 days
node
2
Resources
pip-services3-mongodb-node — npm install pip-services3-mongodb-node · libregistry