Skip to content

Commit

Permalink
refactor history to display full name of props, add history to options
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Dinh committed Sep 26, 2017
1 parent ac53acc commit 0ddcdff
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
1 change: 1 addition & 0 deletions lib/list/getOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function getOptions () {
defaultSort: this.options.defaultSort,
fields: {},
hidden: this.options.hidden,
history: this.options.history,
initialFields: _.map(this.initialFields, 'path'),
key: this.key,
label: this.label,
Expand Down
92 changes: 48 additions & 44 deletions lib/schemaPlugins/history.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
var keystone = require('../../');

var historyModelSuffix = '_revisions';
const historyModelSuffix = '_revisions';

function getHistoryModelName (list) {
return list.options.schema.collection + historyModelSuffix;
}

function getHistoryModel (list, userModel) {

var collection = getHistoryModelName(list);
const collection = getHistoryModelName(list);

var schema = new keystone.mongoose.Schema({
i: { type: keystone.mongoose.Schema.Types.ObjectId, ref: collection },
t: { type: Date, index: true, required: true },
o: { type: String, index: true, required: true },
c: { type: [String], index: true },
d: { type: keystone.mongoose.Schema.Types.Mixed, required: true },
const schema = new keystone.mongoose.Schema({
id: { type: keystone.mongoose.Schema.Types.ObjectId, ref: collection },
time: { type: Date, index: true, required: true },
operation: { type: String, index: true, required: true },
changes: { type: [String], index: true },
data: { type: keystone.mongoose.Schema.Types.Mixed, required: true },
}, {
id: true,
versionKey: false,
});

if (userModel) {
schema.add({
u: { type: keystone.mongoose.Schema.Types.ObjectId, ref: userModel },
user: { type: keystone.mongoose.Schema.Types.ObjectId, ref: userModel },
});
}

Expand All @@ -39,72 +39,76 @@ function getHistoryModel (list, userModel) {

module.exports = function history () {

var list = this;
const list = this;

// If model already exists for a '_revisions' in an inherited model, log a warning but skip creating the new model (inherited _revisions model will be used).
var collectionName = getHistoryModelName(list);
const collectionName = getHistoryModelName(list);
if (list.get('inherits')
&& collectionName.indexOf(historyModelSuffix, collectionName.length - historyModelSuffix.length) !== -1
&& keystone.mongoose.models[collectionName]) {
console.log('List/model already exists for ' + collectionName + '.\nWon\'t re-create, keystone continuing.');
return;
}

var userModel = keystone.get('user model');
const userModel = keystone.get('user model');

var HistoryModel = list.HistoryModel = getHistoryModel(this, userModel);
const HistoryModel = list.HistoryModel = getHistoryModel(this, userModel);

list.schema.add({
__rev: Number,
});

list.schema.pre('save', function (next) {
this.__rev = (typeof this.__rev === 'number') ? this.__rev + 1 : 1;

var data = this.toObject();
delete data._id;
delete data.__v;
delete data.__rev;
list.schema.pre('save', function (next) {
if (this.isModified()) {
this.__rev = (typeof this.__rev === 'number') ? this.__rev + 1 : 1;

var doc = {
i: this.id,
t: Date.now(),
o: this.isNew ? 'c' : 'u',
c: [],
d: data,
};
const data = this.toObject();
delete data._id;
delete data.__v;
delete data.__rev;

const doc = {
id: this.id,
time: Date.now(),
operation: this.isNew ? 'create' : 'update',
changes: [],
data: data,
};

for (var path in list.fields) {
if (this.isModified(path)) {
doc.c.push(path);
for (const path in list.fields) {
if (this.isModified(path)) {
doc.changes.push(path);
}
}
}

if (list.autokey) {
if (this.isModified(list.autokey.path)) {
doc.c.push(list.autokey.path);
if (list.autokey) {
if (this.isModified(list.autokey.path)) {
doc.changes.push(list.autokey.path);
}
}
}

if (userModel && this._req_user) {
doc.u = this._req_user;
}
if (userModel && this._req_user) {
doc.user = this._req_user;
}

new HistoryModel(doc).save(next);
new HistoryModel(doc).save(next);
}
next();
});

list.schema.pre('remove', function (next) {
var data = this.toObject();
const data = this.toObject();
data.__v = undefined;

var doc = {
t: Date.now(),
o: 'd',
d: data,
const doc = {
time: Date.now(),
operation: 'delete',
data: data,
};

if (userModel && this._req_user) {
doc.u = this._req_user;
doc.user = this._req_user;
}

new HistoryModel(doc).save(next);
Expand Down

0 comments on commit 0ddcdff

Please sign in to comment.