Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/james075/mongoose-createdat-updatedat
Mongoose plugin - timestamp handler
https://github.com/james075/mongoose-createdat-updatedat
mongoose mongoose-createdat-updatedat mongoose-plugin mongoose-schema
Last synced: 2 months ago
JSON representation
Mongoose plugin - timestamp handler
- Host: GitHub
- URL: https://github.com/james075/mongoose-createdat-updatedat
- Owner: james075
- License: mit
- Created: 2015-08-10T12:59:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T04:05:51.000Z (almost 2 years ago)
- Last Synced: 2024-09-28T20:43:00.542Z (3 months ago)
- Topics: mongoose, mongoose-createdat-updatedat, mongoose-plugin, mongoose-schema
- Language: JavaScript
- Homepage:
- Size: 194 KB
- Stars: 12
- Watchers: 2
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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:
```bash
$> npm install mongoose-createdat-updatedat --save
```### Usage:
```js
var plugin = require('mongoose-createdat-updatedat');
var User = new Schema({ ... });
User.plugin(plugin);
```Optionally, you can pass an options object to set the name of the fields, or to disable certain fields.
Another example:
```js
var plugin = require('mongoose-createdat-updatedat');
var User = new Schema({ ... });
var options = {
createdAt: 'created_at',
updatedAt: null
}
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:
```bash
$> npm test
$>
CreatedAt and UpdatedAt support
✓ should has "James" as firstname
✓ should save user without error (237ms)
✓ should createdAt and updatedAt have equal values
✓ should update user lastname to "Heng" without error
✓ updatedAt should be more recent than createdAt
```