goodeggs-mongoose-timestamps

2.1.0 • Public • Published

goodeggs-mongoose-timestamps

Adds createdAt and updatedAt fields to any schema using Mongoose's built-in timestamps schema option.

NPM version Build Status MIT License

Usage

npm install goodeggs-mongoose-timestamps
timestamps = require 'goodeggs-mongoose-timestamps'

schema = new mongoose.Schema {}
schema.plugin timestamps

Indexes

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 test

Module 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

Dependents (1)

Package Sidebar

Install

npm i goodeggs-mongoose-timestamps

Weekly Downloads

32

Version

2.1.0

License

MIT

Unpacked Size

14.7 kB

Total Files

11

Last publish

Collaborators

  • goodeggs-admin
  • asalant