项目作者: james075

项目描述 :
Mongoose plugin - timestamp handler
高级语言: JavaScript
项目地址: git://github.com/james075/mongoose-createdat-updatedat.git
创建时间: 2015-08-10T12:59:12Z
项目社区:https://github.com/james075/mongoose-createdat-updatedat

开源协议:MIT License

下载


Mongoose-createdAt-updatedAt

A mongoose plugin that adds createdAt and updatedAt fields to subscribed models in order to avoid redundancy.

Now supporting query-based updates like update() or findOneAndUpdate().

Install via npm:

  1. $> npm install mongoose-createdat-updatedat --save

Usage:

  1. var plugin = require('mongoose-createdat-updatedat');
  2. var User = new Schema({ ... });
  3. User.plugin(plugin);

Optionally, you can pass an options object to set the name of the fields, or to disable certain fields.

Another example:

  1. var plugin = require('mongoose-createdat-updatedat');
  2. var User = new Schema({ ... });
  3. var options = {
  4. createdAt: 'created_at',
  5. updatedAt: null
  6. }
  7. User.plugin(plugin, options);

Here, the createdAt field will be named created_at, and the updatedAt field will be disabled.

Options:

createdAt

Type: String
Default: createdAt

Name of the createdAt field. Set to null to disable the field

updatedAt

Type: String
Default: updatedAt

Name of the updatedAt field. Set to null to disable the field

Test:

  1. $> npm test
  2. $>
  3. CreatedAt and UpdatedAt support
  4. should has "James" as firstname
  5. should save user without error (237ms)
  6. should createdAt and updatedAt have equal values
  7. should update user lastname to "Heng" without error
  8. updatedAt should be more recent than createdAt