node-bits-mongo
v0.0.20
Published
Provides the logic for connecting bits to a mongo database
Downloads
2
Readme
node-bits-mongo
node-bits-mongo allows a node-bits app access to a mongo database. node-bits-mongo will use the schema defined by other bits in the loadSchema step as its schema definition.
Install
npm install node-bits-mongo --save
or
yarn add node-bits-mongo
Configuration
nodeBitsMongo({
connection: 'connection-string',
hooks: [
nodeBitsPassword(),
],
}),
connection
This is the connection string to the mongo database.
runSeeds
If runSeeds is included and set to true, node-bits-mongo will look for seeds as defined in the schema, and insert them into the database
hooks
hooks is an array of functions that accepts a single args parameter. The property values passed to args and optional actions varies by operation and are described below:
Before execution
- name: the name of the model
- schema: the defined schema object for this model
- action: QUERY, INSERT, UPDATE, DELETE
- stage: BEFORE, AFTER
- options: these are the options passed to the database on the query
Any value returned will be used as the options forward. If you do not want this effect, return null.
After Execution
- name: the name of the model
- schema: the defined schema object for this model
- action: QUERY, INSERT, UPDATE, DELETE
- stage: BEFORE, AFTER
- options: these are the options passed to the database on the query
- results: the results returned by the database
node-bits-password
node-bits-password implements the logic for the PASSWORD type fields and is a common hook. See the bit's documentation for more information.
Methods
connect
This will open a connection to the database.
rawConnection
Sometimes you need the raw mongo connection to do something that node-bits-mongo hasn't exposed. This method will return the mongoosejs connection to you.
getModel
getModel(name)
This will return to you the mongoosejs model.
findById
findById(name, id)
The name of the model, the id to search for.
Will return an object if found, if not will return null.
find
find(name, options)
The name of the model, the options to use for searching.
Will return the results for the options supplied, null is supplied, it will return all records for the model
Options
All options are not required and can be used in any combination.
- select: an array of field names to include in the results
- orderby: an array of objects that define the order of the result. The format of is item is as follows:
{field: '', direction: ''}
. - start: the index of the result set to start from (alternatively parameter can be named skip)
- max: the number of records to return in the results (alternatively parameter can be named limit)
- where: a complex object that contains the
- includeMetaData: an array of options to return wrapped around the result set. If supplied the format of the result will be
value:[rows], ...{keys as supplied}
Where
The where clause is specified as a complex object made up of key, value pairs. Many times the values for a key are a complex object themselves, representing the operations and values.
node-bits-mongo supports the following operators: eq, ne, gt, ge, lt, le, like, startsWith, endsWith, and, or.
Example (all orders with a total greater than or equal to $5.00):
database.find('order', {
where: {
total: { ge: 5 },
},
};ß
Include Meta Data
The following options are understood for metadata, and the constants can be found in node-bits.
- COUNT
- START
- MAX
To allow for different return keys, the format of each item in the array is as follows: {key: '', value: ''}
create
create(name, data)
The name of the model, the data to insert.
Will return the object inserted with all autogenerated fields
update
update(name, id, data)
The name of the model, the id of the record to update, the data to use as the new version of the object.
Will return the object updated with all autogenerated fields
delete
delete(name, id)
The name of the model, the id of the object to delete.