node-data-handling
v0.5.2
Published
Data Handling component for Nodejs and MongoDB
Keywords
Readme
Data Handling
Usage
- Set following environment variables:
MONGOLAB_URIorMONGODB_URL: path for MongoDB database. Default:192.168.59.103:27017/dbMONGODB_BULK_PAYLOAD: number with maximum quantity for a bulk batch. Default:1000MONGODB_BULK_TIMEOUT: timeout for saving a bulk batch, in milliseconds. Default:500MEMCACHE_SERVERS: path for memcache. Default:192.168.59.103:11211/memcacheMEMCACHE_USERNAME: memcache username. Default:adminMEMCACHE_PASSWORD: memcache password. Default:admin
- Add
node-data-handlingdependency to yourpackage.json
"dependencies": {
"node-data-handling": "0.5.2"
}- Call
dataHandling = require("node-data-handling")(); - Create a mongoose schema
Schema = dataHandling.Schema
jsonSchema = {
name: {
"type": "string",
"default": null
}
}
schema = Schema(jsonSchema);- Add your mongoose plugins
schema.plugin(awesomePlugin);By default, all schemas are created with these plugins:
- Create model object
Model = dataHandling.Model
objModel = Model(schema, 'collectionName'); // `schema` is the schema created before- Use model's methods, for example:
objModel.find(query, function(error, response) {
// Use response object and handle errors here
});- Get object for update or bulk operation using alias
// schema definition as example
MyModelSchema = Schema({
maf: {type: String, default: null, alias: 'my.alias.field'}
nested:
f: {type: String, default: null, alias: 'nested.field'}
})
// object to be used for update that only knows alias
var obj = {
'my.alias.field': 'value'
'nested.field': 'value2'
}
// newObj will be transformed to an object that can be used in updates/bulk operations
var newObj = MyModel.toAliasObject(obj);
// {'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 = dataHandling.Schema
jsonSchema = {
name: {
"type": "string",
"default": null
}
}
options = {
"mode": "softDelete",
"index": [{ "deleted": 1, "name": 1 }] // Optional: You can send an index array
}
schema = Schema(jsonSchema, options);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 testcaveat
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
