-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
356 lines (308 loc) · 10.7 KB
/
index.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
'use strict';
const cloneDeep = require('lodash/cloneDeep');
const merge = require('lodash/merge');
/**
* @class
* SequelizeHistory
*
* @classdesc
* Creates a revision history for instances of a given Sequelize model
*
* @constructor
* @description
* The constructor parses passed options, attaches hooks
*
* @param {object} model - Sequelize model to track
* @param {object} sequelize - Sequelize object (enforces installation above this module)
* @param {object} options - Object instantiation options
* @param {string} options.authorFieldName - String to indicate a field name to store author of the revisions, or null to disable
* @param {string} options.modelSuffix - String to append to tracked model's name when creating name of tracking model
* @param {array} options.excludedAttributes - Array of attributes to be ignored and excluded when recording a change to the target model
* @param {array} options.excludedAttributeProperties - Array of attribute properties to ignore when duplicating the target model's attributes
* @return {null}
*/
class SequelizeHistory {
constructor(model, sequelize, options) {
this.options = Object.assign({},
SequelizeHistory.DEFAULTS,
options || {});
this.model = model;
// Create name of tracking model by appending
// suffice option to the tracked model name
this.modelName = [
this.model.name,
this.options.modelSuffix
].join('');
// Create the tracking model's schema
this.fields = this.createSchema(
sequelize.Sequelize);
// Register the tracking model with Sequelize
sequelize.define(
this.modelName,
this.setAttributes(),
{});
// Store reference to the newly created tracking model
this.modelHistory = sequelize.models[this.modelName];
// Add static author tracking method to original model if enabled
if (typeof this.options.authorFieldName === 'string') {
this.addModelAuthorSetter(sequelize);
// Add relationship with the original model to ensure
// table constraints are not applied if added manually
this.model.hasMany(this.modelHistory, {
foreignKey: 'modelId',
contraints: false,
as: 'revisions'
});
this.modelHistory.belongsTo(this.model, {
foreignKey: 'modelId',
contraints: false,
as: 'model'
});
}
// Setup the necessary hooks for revision tracking
this.hookup();
}
/**
* Adds a static `setRevisingAuthor` method to the tracked model if author tracking is enabled.
* @private
* @param {Sequelize} sequelize - The passed Sequelize instance
*/
addModelAuthorSetter(sequelize) {
const modelName = this.model.name;
sequelize.models[modelName].setRevisingAuthor = function (value) {
sequelize.models[modelName]._sequelizeHistoryProps = {
_authorId: value
};
};
}
/**
* Sets attributes of history model by parsing out target model attributes
* @private
* @return {object}
*/
setAttributes() {
const cloned = cloneDeep(this.model.rawAttributes);
const attributes = [];
Object.keys(cloned).forEach(field => {
const f = cloned[field];
// If attribute should be excluded, skip...
if (this.options.excludedAttributes.indexOf(f.fieldName) > -1) {
return;
}
// Skip the id attribute...
if (f.fieldName === 'id') {
return;
}
// Remove any attribute properties that should be excluded...
this.options.excludedAttributeProperties.forEach(prop => {
if (typeof f[prop] !== 'undefined') {
delete f[prop];
}
});
// Remove the default behavior of auto-updating the timestamps...
if (f.fieldName === 'createdAt' ||
f.fieldName === 'updatedAt') {
delete f.defaultValue;
}
// Allow all fields to be NULL...
f.allowNull = true;
// And store the modified attribute
attributes[field] = f;
});
return merge({}, this.fields, attributes);
}
/**
* Creates fields to be added in addition to the tracked model's fields
* @private
* @return {object} - Model instance field options
*/
createSchema(sequelize) {
const schema = {
id: {
type: sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
unique: true
},
modelId: {
type: sequelize.INTEGER,
allowNull: true
},
archivedAt: {
type: sequelize.DATE,
defaultValue: sequelize.NOW,
allowNull: false
}
};
// Add our author tracking field if set
if (typeof this.options.authorFieldName === 'string') {
schema[this.options.authorFieldName] = {
type: sequelize.INTEGER,
allowNull: true
};
}
return schema;
}
/**
* Attaches hooks to target model and history model
* @private
* @return {null}
*/
hookup() {
this.model.addHook('beforeUpdate', this.insertHook.bind(this));
this.model.addHook('beforeDestroy', this.insertHook.bind(this));
this.model.addHook('beforeBulkUpdate', this.insertBulkHook.bind(this));
this.model.addHook('beforeBulkDestroy', this.insertBulkHook.bind(this));
this.modelHistory.addHook('beforeUpdate', this.readOnlyHook.bind(this));
this.modelHistory.addHook('beforeDestroy', this.readOnlyHook.bind(this));
}
/**
* Enforces read-only nature of history model instances
* @private
* @return {null}
*/
readOnlyHook() {
throw new Error('This is a read-only history database. You cannot modify it.');
}
/**
* Hook to trigger recording of revision
* @private
* @param {Sequelize.Model} doc - instance to track
* @param {object} options - instance options
* @return {Sequelize.Model} - Instance representing the revision
*/
insertHook(doc, options) {
const dataValues = doc._previousDataValues || doc.dataValues;
dataValues.modelId = dataValues.id;
// Grab the static revision author property from the tracked class
// and null it out after its first use when called via an instance
if (typeof this.options.authorFieldName === 'string' &&
typeof this.model._sequelizeHistoryProps !== 'undefined') {
dataValues[this.options.authorFieldName] = this.model._sequelizeHistoryProps._authorId;
this.model._sequelizeHistoryProps._authorId = null;
}
delete dataValues.id;
const historyRecord = this.modelHistory.create(dataValues, {
transaction: options.transaction
});
return historyRecord;
}
/**
* Hook to trigger recording of multiple revision
* @param {object} options - options
* @return {Promise} = resolves
*/
insertBulkHook(options) {
if (!options.individualHooks) {
const queryAll = this.model.findAll({
where: options.where,
transaction: options.transaction
}).then(hits => {
if (hits !== null) {
const docs = hits.map(hit => {
const dataSet = cloneDeep(hit.dataValues);
// Grab the static revision author property from the tracked class
if (typeof this.options.authorFieldName === 'string' &&
typeof this.model._sequelizeHistoryProps !== 'undefined') {
dataSet[this.options.authorFieldName] = this.model._sequelizeHistoryProps._authorId;
}
dataSet.modelId = hit.id;
delete dataSet.id;
return dataSet;
});
// ...and null it out after all bulk updates are complete
if (typeof this.options.authorFieldName === 'string' &&
typeof this.model._sequelizeHistoryProps !== 'undefined') {
this.model._sequelizeHistoryProps._authorId = null;
}
return this.modelHistory.bulkCreate(docs, {
transaction: options.transaction
});
}
});
return queryAll;
}
}
}
SequelizeHistory.DEFAULTS = {
// String to indicate a field name to use to store the
// author of the revisions to the model, or null if you
// don't want to track revision authors
authorFieldName: null,
// String to append to tracked model's name in creating
// name of model's history model
modelSuffix: 'History',
// Array of attributes to be ignored and excluded when
// recording a change to the target model
excludedAttributes: [],
// Array of attribute properties to ignore when duplicating
// the target model's attributes - this is mostly to prevent
// the use of constraints that may be in place on the target
excludedAttributeProperties: [
'Model',
'unique',
'primaryKey',
'references',
'onUpdate',
'onDelete',
'autoIncrement',
'set',
'get',
'_modelAttribute'
]
};
/**
* Factory method for creation without requiring the constructor
*
* @module
* TrackFactory
*
* @description
* Factory method to avoid having to deal with the constructor directly
* since you're likely applying this to more than one model. All constructor
* options are passed transparently upon instantiation.
*
* @param {object} model - Sequelize model to track
* @param {object} sequelize - Sequelize object (enforces installation above this module)
* @param {object} options - Object instantiation options
* @param {string} options.authorFieldName - String to indicate a field name to store author of the revisions, or null to disable
* @param {string} options.modelSuffix - String to append to tracked model's name when creating name of tracking model
* @param {array} options.excludedAttributes - Array of attributes to be ignored and excluded when recording a change to the target model
* @param {array} options.excludedAttributeProperties - Array of attribute properties to ignore when duplicating the target model's attributes
* @return {object} - returns the tracked model and generated tracking model
*/
module.exports = (model, sequelize, options) => {
const instance = new SequelizeHistory(
model, sequelize, options);
return instance.modelHistory;
};
/**
* Factory method to track changes for all sequelize models
*
* @module
* TrackAllFactory
*
* @description
* Convenience factory method to track changes for all models found
* within the passed sequelize instance. All constructor options
* are passed transparently upon instantiation.
*
* @param {object} sequelize - Sequelize object (enforces installation above this module)
* @param {object} options - Object instantiation options
* @param {string} options.authorFieldName - String to indicate a field name to store author of the revisions, or null to disable
* @param {string} options.modelSuffix - String to append to tracked model's name when creating name of tracking model
* @param {array} options.excludedAttributes - Array of attributes to be ignored and excluded when recording a change to the target model
* @param {array} options.excludedAttributeProperties - Array of attribute properties to ignore when duplicating the target model's attributes
* @return {null}
*/
module.exports.all = (sequelize, options) => {
const instances = {};
const names = Object.keys(sequelize.models);
names.forEach(key => {
const instance = new SequelizeHistory(
sequelize.models[key], sequelize, options);
instances[instance.modelName] = instance;
});
return instances;
};
module.exports.SequelizeHistory = SequelizeHistory;