4.0.1 • Published 5 years ago

@lgslabs/bits-mongo-crud v4.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 years ago

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-crud
const {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})
  • messageCenter MessageCenter The MessageCenter to use
  • tag String The tag to use for requests/events
  • scopes Array The scopes to use on requests/events
  • readScopes Array The scopes for read-type requests/events
  • writeScopes Array The scopes for write-type requests
  • filter Boolean True if the Messenger should filter CRUD events, false otherwise
  • routePath String The path to use for the router (optional)
  • routerFilepath String The path to the Router class to use (optional)
  • apiExport Symbol(API_EXPORT) How to export the API. This must be a value from API_EXPORT object in order to export
  • apiData Object The data to supply for export ({name, filepath} for GLOBAL, {topic, value} for RESOURCE)
  • collectionName String The name of the collection within the database.
  • dbName String The name of the DB to use. By default, the bits DB is used.
  • url String The Mongo process URL to use. By default, the URL is mongodb://127.0.0.1
  • port String The Mongo process port to use. By default, the port is 27017
  • ...args Any 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)
  • item Object the item to upsert
  • Returns Promise resolving to an object, or rejecting to error
4.0.1

5 years ago

4.0.0

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.2.5

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.1

5 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago