mongorito-timestamps

1.0.1 • Public • Published

mongorito-timestamps Build Status

Plugin to add "created" and "updated" timestamps for Mongorito models.

Installation

$ npm install --save mongorito-timestamps

Usage

const {Database, Model} = require('mongorito');
const timestamps = require('mongorito-timestamps');

class Post extends Model {}

const db = new Database('localhost/blog');
await db.connect();

db.use(timestamps());
db.register(Post);

const post = new Post({title: 'Hello'});
await post.save();

post.get('created_at');
//=> 2017-05-17T18:02:06.612Z

post.get('updated_at');
//=> same as `created_at`

post.set({title: 'World'});
await post.save();

post.get('created_at');
//=> same as previous `created_at`

post.get('updated_at');
//=> 2017-05-17T18:02:06.971Z

API

timestamps([options])

Configures and returns a timestamps plugin. Accepts an optional options object to customize field names and a function, which generates the timestamps.

options

createdAt

Type: string
Default: created_at

Name of the field, which stores the date when the document was created. This field doesn't change after the initial creation.

updatedAt

Type: string
Default: updated_at

Name of the field, which stores the date when the document was updated. This field changes on every save of the document.

getTimestamp

Type: function
Default:

const getTimestamp = () => new Date();

Function, which generates a timestamp.

License

MIT © Vadim Demedes

Package Sidebar

Install

npm i mongorito-timestamps

Weekly Downloads

3

Version

1.0.1

License

MIT

Last publish

Collaborators

  • vdemedes