Data Handling
Usage
- Set following environment variables:
MONGOLAB_URI
orMONGODB_URL
: path for MongoDB database. Default:192.168.59.103:27017/db
MONGODB_BULK_PAYLOAD
: number with maximum quantity for a bulk batch. Default:1000
MONGODB_BULK_TIMEOUT
: timeout for saving a bulk batch, in milliseconds. Default:500
MEMCACHE_SERVERS
: path for memcache. Default:192.168.59.103:11211/memcache
MEMCACHE_USERNAME
: memcache username. Default:admin
MEMCACHE_PASSWORD
: memcache password. Default:admin
- Add
node-data-handling
dependency to yourpackage.json
"dependencies":
- Call
dataHandling = require("node-data-handling")();
- Create a mongoose schema
Schema = dataHandlingSchemajsonSchema = name: "type": "string" "default": null schema = ;
- Add your mongoose plugins
schema;
By default, all schemas are created with these plugins:
- Create model object
Model = dataHandlingModelobjModel = ; // `schema` is the schema created before
- Use model's methods, for example:
objModel;
- Get object for update or bulk operation using alias
// schema definition as exampleMyModelSchema = // object to be used for update that only knows aliasvar obj = 'my.alias.field': 'value' 'nested.field': 'value2' // newObj will be transformed to an object that can be used in updates/bulk operationsvar newObj = MyModel;// {'maf': 'value', nested: {f: 'value2'} }
Soft Delete Mode
On mongo schema creation you can pass an options object setting softDelete mode.
- Create a mongoose schema
Schema = dataHandlingSchemajsonSchema = name: "type": "string" "default": null options = "mode": "softDelete" "index": "deleted": 1 "name": 1 // Optional: You can send an index array schema = ;
Once softDelete mode is set, it will add 2 fields to your schema:
deleted = type: 'boolean' default: false deleted_at = type: 'number' default: null
The node-data-handling and the following mongoose methods will consider only the deleted: false values on results:
- count
- find
- findOne
- findOneAndUpdate
- update
Test
$ npm install
$ npm test
caveat
Travis build will execute npm run travis
, so environment variables can be used exclusively for CI, depending on the needs
npm shrinkwrap
To add new dependencies, remove npm-shrinkwrap.json
before $ npm install
, otherwise it will not be installed