# all files / keystone/lib/schemaPlugins/ history.js

11.9% Statements 5/42
0% Branches 0/25
0% Functions 0/5
11.9% Lines 5/42

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    1141×

    1×

    1×

    1×

    1×

     var keystone = require('../../');

    var historyModelSuffix = '_revisions';

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

    function getHistoryModel (list, userModel) {

    	var 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 },
    	}, {
    		id: true,
    		versionKey: false,
    	});

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

    	return keystone.mongoose.model(collection, schema, collection);

    }

    /**
     * List history option
     *
     * When enabled, it tracks changes to each document on save or remove.
     */

    module.exports = function history () {

    	var 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);
    	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');

    	var 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;

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

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

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

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

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

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

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

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

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

    };