0.5.2 • Published 7 years ago

node-data-handling v0.5.2

Weekly downloads
36
License
For internal use ...
Repository
github
Last release
7 years ago

Data Handling

Usage

  1. Set following environment variables:
  • MONGOLAB_URI or MONGODB_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
  1. Add node-data-handling dependency to your package.json
    "dependencies": {
      "node-data-handling": "0.5.2"
    }
  2. Call dataHandling = require("node-data-handling")();
  3. Create a mongoose schema

    Schema = dataHandling.Schema
    jsonSchema = {
      name: {
        "type": "string",
        "default": null
      }
    }
    schema = Schema(jsonSchema);
  4. Add your mongoose plugins

    schema.plugin(awesomePlugin);

    By default, all schemas are created with these plugins:

  1. Create model object

    Model = dataHandling.Model
    objModel = Model(schema, 'collectionName'); // `schema` is the schema created before
  2. Use model's methods, for example:

    objModel.find(query, function(error, response) {
      // Use response object and handle errors here
    });
  3. 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.

  1. 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 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