Socket
Socket
Sign inDemoInstall

node-data-handling

Package Overview
Dependencies
53
Maintainers
4
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-data-handling

Data Handling component for Nodejs and MongoDB


Version published
Maintainers
4
Created

Readme

Source

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"
}
  1. Call dataHandling = require("node-data-handling")();
  2. Create a mongoose schema
Schema = dataHandling.Schema
jsonSchema = {
  name: {
    "type": "string",
    "default": null
  }
}
schema = Schema(jsonSchema);
  1. Add your mongoose plugins
schema.plugin(awesomePlugin);

By default, all schemas are created with these plugins:

  • mongoose-aliasfield
  1. Create model object
Model = dataHandling.Model
objModel = Model(schema, 'collectionName'); // `schema` is the schema created before
  1. Use model's methods, for example:
objModel.find(query, function(error, response) {
  // Use response object and handle errors here
});
  1. 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

FAQs

Last updated on 18 May 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc