mongoose-updated_at

0.0.2 • Public • Published

Mongoose updated_at Plugin

A simple Mongoose plugin which adds updatedAt date attribute to models that include our plugin.

Installation

npm install mongoose-updated_at --save

Usage

First of all, add to your model a createdAt attribute.

createdAt: { type: Date, default: Date.now }

Then require mongoose-updated_at plugin on your schema file.

var updatedTimestamp = require('mongoose-updated_at');
 
var UserSchema = new Schema({
    name: String
  , createdAt: { type: Date, default: Date.now }
});
 
UserSchema.plugin(updatedTimestamp);
 
module.exports = mongoose.model('User', UserSchema);

The User model will now have an updatedAt attribute, which will be updated every time you save your document.

If you already have timestamps defined on your models with different names, you can setup the plugin on these attributes and work with them instead of adding new ones.

mongoose.plugin(updatedTimestamp,  {
  createdAtOn: 'created_at',
  updatedAtOn: 'modified_at'
});

Package Sidebar

Install

npm i mongoose-updated_at

Weekly Downloads

0

Version

0.0.2

License

MIT

Last publish

Collaborators

  • aanfuso