Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

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

Notifications You must be signed in to change notification settings

vadimdemedes/mongorito-timestamps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

Code of conduct

Stars

Watchers

Forks

Packages

No packages published