mongoose-schema-lastmodifiedfields

mongoose schema plugin to create last modified fields for each field and update them automatically

npm install mongoose-schema-lastmodifiedfields
4 downloads in the last week
4 downloads in the last month

mongoose-schema-lastmodifiedfields

A mongoose plugin that automatically adds last modified fields for each user defined property in a mongoose schema. Each time a document is saved, any modified properties that have corresponding last modified fields will be updated with the current timestamp. This can be useful when syncing data with clients on a per-field basis.

The inspiration for this was a result to extend my mongoose schemas to support real-time and per field data syncing discussed in @brentsimmons post about Vesper syncing here

Install

Install via NPM

    npm install mongoose-schema-lastmodifiedfields

Usage

var mongoose = require("mongoose"),
    lastModifiedFields = require("lastModifiedFields");

var CarSchema = new mongoose.Schema({
    make:String,
    model:String,
    vin:String
    miles:Number
});

CarSchema.plugin(lastModifiedFields, {fieldSuffix:"_lastModified",
                                      omittedFields:["make", "model"]});

Options

The plugin currently has the following options

  • fieldSuffix

    A string to append to the end of each path to determine the name of each timestamp field. Defaults to "_lastModifiedDate"

  • omittedFields

    An array of field names that should not have timestamp fields created for them. Defaults:

    • Standard id key "_id"
    • Discriminator Key (typically "__t")
    • Version key (typically "__v")
  • purgeFromJSON

    Boolean that transforms the schema's toJSON method to remove the timestamp fields from its JSON representation. Defaults to "false"

  • purgeFromObject

    Boolean that transforms the schema's toObject method to remove the timestamp fields from its Object representation. Defaults to "false"

Tests

Test can be run simply by installing and running mocha

npm install -g mocha
mocha

Authors

Mike Sabatini @mikesabatini

License

Copyright Mike Sabatini 2014 Licensed under the MIT License. Enjoy

npm loves you