@lgslabs/bits-mongo-crud v4.0.1
BITS MongoDB CRUD
The MongoDB-backed implementation of CRUD for BITS. This package extends the functionality provided in bits-memory-crud. All items in the collection are expected to be properly-formatted JSON objects. Mongo automatically adds an _id field in the form of its ObjectId. For documentation on using MongoDB's, queries and options, refer to the page about CRUD operations.
Use
Importing
npm install @lgslabs/bits-mongo-crudconst {MongoDbCrudService, MongoDbCrudManager, MongoDbCrudMessenger, MongoDbCrudApi, MongoDbCrudRouter} = require('@lgslabs/bits-mongo-crud');Service
The Service is responsible for creating, loading, and unloading the manager, messenger, router (if desired), a public API (if desired), and any resources the subsystem may need.
load
Loads the Service, thereby creating and loading the other subsystem components with the provided options
load({messageCenter, tag, scopes, readScopes, writeScopes, filter, routePath, routerFilepath, apiExport, apiData, collectionName, dbName, url, port, ...args})messageCenterMessageCenter The MessageCenter to usetagString The tag to use for requests/eventsscopesArray The scopes to use on requests/eventsreadScopesArray The scopes for read-type requests/eventswriteScopesArray The scopes for write-type requestsfilterBoolean True if the Messenger should filter CRUD events, false otherwiseroutePathString The path to use for the router (optional)routerFilepathString The path to the Router class to use (optional)apiExportSymbol(API_EXPORT) How to export the API. This must be a value from API_EXPORT object in order to exportapiDataObject The data to supply for export ({name, filepath}forGLOBAL,{topic, value}forRESOURCE)collectionNameString The name of the collection within the database.dbNameString The name of the DB to use. By default, thebitsDB is used.urlString The Mongo process URL to use. By default, the URL ismongodb://127.0.0.1portString The Mongo process port to use. By default, the port is27017...argsAny number of additional key/value pairs- Returns Promise resolving to request or rejecting to error
createManager
Called in the Service's load function to create the manager. By default, it creates an instance of the base implementation of the MongoDB CRUD manager. If the subsystem needs additional functionality, it is recommended to override this function.
createManager()- Returns Promise resolving to request or rejecting to error
Manager
The Manager is responsible for performing any operations related to the subsystem. It has default implementations for all CRUD operations. The manager is an EventEmitter to allow communication with the Messenger. To change the Manager class being used, override the createManager function in the Service to create and return the desired Manager implementation. The loadManager and unloadManager functions can be overridden in the event more customization is required.
_createIndexes
The load function of the Manager will automatically call _createIndexes after creating the CollectionManager.
The user should override this function and return a Promise to provide specific implementation of indexes. NOTE: The
MongoCrudManager's instance of CollectionManager supports the Mongo Driver's implementation of createIndex
and createIndexes.
/**
* @override
* @return {Promise}
*/
_createIndexes(options) {
return Promise.all([
this._collectionManager.createIndex({bitsId: 1}),
this._collectionManager.createIndex({customField: -1}, {unique: true}),
]);
}
// or using the array specification
_createIndexes(options) {
return this._collectionManager.createIndexes([{key: {bitsId: 1}}]);
}
// or based on a field in the load options
_createIndexes({somethignFromLoad}) {
if (somethingFromLoad) {
return this._collectionManager.createIndexes([{key: {bitsId: 1}}]);
}
}_upsertFilter
Helper function that will upsert the provided item, returning the result in the form:
{
value: <Object>,
lastErrorObject: {updatedExisting: <Boolean>},
}By default, the matcher is the _id property.
_upsertFilter(item)itemObject the item to upsert- Returns Promise resolving to an object, or rejecting to error
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago