项目作者: stennettm

项目描述 :
A Mongoose plugin that archives document diffs and manages document history.
高级语言: JavaScript
项目地址: git://github.com/stennettm/historical.git
创建时间: 2014-05-13T04:35:44Z
项目社区:https://github.com/stennettm/historical

开源协议:MIT License

下载


Build Status

Historical

A Mongoose plugin that archives document diffs and manages document history.

Historical requires a primary key (typically _id) to be present in your schema.

Installation

npm install historical@^2.0.0

Getting Started

Attach the plugin to your schema with any of these optional configuration parameters:

  • name: Provide a collection name. Defaults to <collection>_historicals.
  • connection: Provide a mongoose connection for the historical collection. Defaults to your schema’s connection.
  • primaryKeyName: Provide your schema’s primary key name. Defaults to _id.
  • primaryKeyType: Provide your schema’s primary key type. Defaults to your schema’s primary key field configuration.
  • ignore: An array of field names to ignore. Fields included in this list will not be stored in history.
  1. var mongoose = require('mongoose'),
  2. ExampleSchema = new mongoose.Schema({
  3. myField: String,
  4. ignoredField: String,
  5. anotherIgnoredField: String
  6. });
  7. ExampleSchema.plugin(require('historical'), {
  8. connection: mongoose.createConnection('mongodb://localhost/example'),
  9. name: null,
  10. primaryKeyName: null,
  11. primaryKeyType: null,
  12. ignore: ['ignoredField', 'anotherIgnoredField']
  13. });

Document #historicalDetails()

List historical documents up to the given date for a document.

  1. myDocument.historicalDetails(new Date('2010-08-17T12:09:36'), function(e, objs){
  2. //the list of historical changes for my document
  3. console.log(objs);
  4. });

Document #historicalRestore()

Returns a document as it was at the given date.

  1. myDocument.historicalRestore(new Date('2010-08-17T12:09:36'), function(e, obj){
  2. //my document as it was in 2010
  3. //or null, if it either had not yet been created or was deleted before this time
  4. if(obj) {
  5. obj.save();
  6. }
  7. });

Document #historicalTrim()

Creates a new historical document by flattening history up to the given date, and deletes the touched historical documents.
Useful for pruning or establishing a maximum history retention period.

  1. myDocument.historicalTrim(new Date('2010-08-17T12:09:36'), function(e, obj){
  2. //any history before this time has been flattened into one historical document
  3. //my document as it was provided
  4. console.log(obj);
  5. });

Document #historicalSnapshot()

Take a complete and current snapshot of my document and store it in history.
Unmodified documents only.

  1. myDocument.historicalSnapshot(function(e, obj){
  2. //my document as it was provided
  3. console.log(obj);
  4. });

Document #historicalClear()

Clear all history for my document and take a snapshot.
Unmodified documents only.

  1. myDocument.historicalClear(function(e, obj){
  2. //my document as it was provided
  3. console.log(obj);
  4. });