npm

Check out our latest tech talk, "JavaScript Supply Chain Security" presented by VP of Security, Adam Baldwin.Watch it here »

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'
});

install

npm i mongoose-minute

Downloadsweekly downloads

2

version

1.0.0

license

MIT

repository

Gitbitbucket

last publish

collaborators

  • avatar
  • avatar
Report a vulnerability