Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/weisjohn/mongoose-history-log

add a collection of events to a schema
https://github.com/weisjohn/mongoose-history-log

Last synced: about 1 month ago
JSON representation

add a collection of events to a schema

Awesome Lists containing this project

README

        

# mongoose-history-log

add a collection of events to a schema

### usage

schema setup:

```javascript
var mongoose = require('mongoose');
var mhl = require('mongoose-history-log');

var schema = new mongoose.Schema({ name: String });

mhl(schema);
```

This adds a `history` property to the schema which is an array of objects:

```javascript
[{
time: { type: Date, default: Date.now },
status: String,
meta: {}
}]
```

The `meta` property allows you to attach an arbitrartily complex object onto the `history` element. When saving the document for the first time, it will automatically add an `{ status : "created" }` record with the current time.

Adding other events:

```javascript
var user = mongoosel.model('user');

user.findOne({ email : '[email protected]' }, function(err, doc) {
doc.history.push({ status: "foo" });
doc.save();
})
```