pwix:collection-timestampable

v2.0.0Published 4 months ago

pwix:collection-timestampable

Forked from zimme:collection-timestampable

Gitter Code Climate License

Timestamps for collections

Add timestamps to collections.

Install

meteor add zimme:collection-timestampable

Usage

Basic usage examples.

Attach

1Posts = new Mongo.Collection('posts');
2
3// Attach behaviour with the default options
4Posts.attachBehaviour('timestampable');
5
6// Attach behaviour with custom options
7Posts.attachBehaviour('timestampable', {
8  createdAt: 'insertedAt',
9  createdBy: 'insertedBy',
10  updatedAt: 'modifiedAt',
11  updatedBy: false
12});

Using CollectionBehaviours.attach you can also attach a behaviour to multiple collections. You can also add multiple behaviours to a collection or add multiple behaviours to multiple collections.

Please see zimme:collection-behaviours for more info on attaching behaviours to collections.

Insert

Examples are using default options.

1Posts.insert({
2  title: 'Awesome post title',
3  body: 'A really informative post.'
4});
5
6// Inserted document
7{
8  "_id": "J9frYKmxaowznW3yM",
9  "createdAt": "2015-04-28T19:31:28.065Z",
10  "createdBy": "0",
11  "body": "A really informative post.",
12  "title": "Awesome post title",
13}

Update

Examples are using default options.

1Posts.update({_id: 'J9frYKmxaowznW3yM'}, {
2  $set: {
3    title: 'More awesome post title'
4  }
5});
6
7// Updated document
8{
9  "_id": "J9frYKmxaowznW3yM",
10  "createdAt": "2015-04-28T19:31:28.065Z",
11  "createdBy": "0",
12  "body": "A really informative post.",
13  "updatedAt": "2015-04-28T19:51:20.047Z",
14  "updatedBy": "0",
15  "title": "Awesome post title",
16}

Options

The following options can be used:

  • createdAt: Optional. Set to 'string' to change the fields name. Set to false to omit field.

  • createdBy: Optional. Set to 'string' to change the fields name. Set to false to omit field.

  • updatedAt: Optional. Set to 'string' to change the fields name. Set to false to omit field.

  • updatedBy: Optional. Set to 'string' to change the fields name. Set to false to omit field.

  • systemId: Optional. Set to 'string' to change the id representing the system.

Global configuration

The global configuration for this package should be in shared code, preferably in a lib folder.

1// Configure behaviour globally
2// All collection using this behaviour will use these settings as defaults
3// The settings below are the package default settings
4CollectionBehaviours.configure('timestampable',{
5  createdAt: 'createdAt',
6  createdBy: 'createdBy',
7  updatedAt: 'updatedAt',
8  updatedBy: 'updatedBy',
9  systemId: '0'
10});

Notes

  • This package attaches a schema to the collection(s) if aldeed:collection2 is

used by the application. If aldeed:autoform is available too, it adds aldeed:autoform specific schema definitions