goodeggs-mongoose-timestamps
v2.1.0
Published
Adds createdAt and updatedAt fields to any schema
Downloads
374
Keywords
Readme
goodeggs-mongoose-timestamps
Adds createdAt and updatedAt fields to any schema using Mongoose's built-in timestamps schema option.
Usage
npm install goodeggs-mongoose-timestampstimestamps = require 'goodeggs-mongoose-timestamps'
schema = new mongoose.Schema {}
schema.plugin timestampsIndexes
By default, it creates an index for updatedAt with spec {updatedAt: 1}. To override
this behavior:
# Creates indexes for updatedAt by default
schema.plugin timestamps
# Does not create indexes
schema.plugin timestamps, createIndexes: false
# Only create createdAt index, descending order
schema.plugin timestamps, createIndexes: {createdAt: -1}Note that it does not create an index on createdAt. An index on createdAt is redundant with the index already on _id which encodes the document create time
in the first 4 bytes.
Dependencies
This module does not pull in an other dependencies but does require that your application is using Mongoose 4.4.7 or greater. That was the version that first supported setting the timestamps option directly on the schema with set rather than in the schema constructor.
Testing
To stub the values used to timestamp Mongoose objects, use sinon.useFakeTimers.
Contributing
Please follow our Code of Conduct when contributing to this project.
$ git clone https://github.com/goodeggs/goodeggs-mongoose-timestamps && cd goodeggs-mongoose-timestamps
$ npm install
$ npm testModule scaffold generated by generator-goodeggs-npm.
Releasing
For authorized authors:
git co master && git pull
npm version [major|minor|patch]
git push --follow-tags
npm publish