GithubHelp home page GithubHelp logo

gabrielelana / mongoose-trackable Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 4.0 287 KB

Keeps track of when the document has been created, updated and optionally when some fields has changed

License: MIT License

Vim Script 3.28% JavaScript 96.72%

mongoose-trackable's Introduction

What

It's a mongoose plugin that automatically keeps track of when the document has been created, updated and, optionally, when some fields have been modified

Build Status

Install

npm install mongoose-trackable

Usage

// see examples/track_model.js

var OrderSchema = new mongoose.Schema({products: Array}).plugin(trackable)

var Order = mongoose.model('Order', OrderSchema)

Order.create({products: ['apple']}, function(err, order) {
  // order now has createdAt and updatedAt fields

  order.products.push('cucumber')
  order.save(function(err, order) {
    // updatedAt has been updated with the last save
  })
})

Order is a trackable model, by default a trackable model will track the creation time in the createdAt field and last update time in the updatedAt field

Moreover you can keep track of all the changes of a certain field using the option fieldsToTrack

// see examples/track_field.js

var OrderSchema = new mongoose.Schema({products: Array, status: String})
  .plugin(trackable, {fieldsToTrack: ['status']})

var Order = mongoose.model('Order', OrderSchema)

Order.create({status: 'placed', products: ['apple', 'cucumber']}, function(err, order) {
  // order now has __updates field that contains something like
  // [
  //    {"field":"status","changedTo":"placed","at":"2014-01-18T13:51:04.780Z"}
  // ]

  order.status = 'shipped'
  order.save(function(err, order) {
    // now __updates contains also the last status update
    // [
    //    {"field":"status","changedTo":"placed","at":"2014-01-18T13:51:04.780Z"},
    //    {"field":"status","changedTo":"shipped","at":"2014-01-18T13:51:04.808Z"}
    // ]
  })
})

For more options and more use cases see spec/acceptance.js

TODO

  • more and better documentation
  • skiptToTrackDocumentUpdates vs skipToTrackFieldsUpdates

mongoose-trackable's People

Contributors

gabrielelana avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mongoose-trackable's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.