mongoose-minute

1.0.0 • Public • Published

mongoose-minute

Mongoose 4.0 plugin for setting created and updated timestamps.

npm install mongoose-minute

Usage

By default two additional properties are added to the schema createdAt and updatedAt. The timestamps are updated for each call to save, update and findOneAndUpdate. The module uses hooks only available in Mongoose 4.0, and the MongoDB $setOnInsert operator.

Pass the schema to the module.

var mongoose = require('mongoose');
var minute = require('mongoose-minute');
 
var schema = new mongoose.Schema({
    counter: { type: Number, default: 0 }
});
 
minute(schema);
 
var Model = db.model('Model', schema);
 
// Set createdAt and updatedAt on model instance
var model = new Model();
model.save();
 
Model.findOneAndUpdate(
    { _id: mongoose.Types.ObjectId() },
    {},
    {
        'new': true,
        upsert: true,
        setDefaultsOnInsert: true
    },
    function(err, model) {
        // model has createdAt and updatedAt
    });
 
Model.update(
    { _id: mongoose.Types.ObjectId() },
    { counter: 0 },
    { upsert: true },
    function(err, model) {
        // model has createdAt and updatedAt
    });

Rename the properties using options.

minute(schema, {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
});

Package Sidebar

Install

npm i mongoose-minute

Weekly Downloads

17

Version

1.0.0

License

MIT

Last publish

Collaborators

  • kapetan
  • kasperkronborg