@sera4/essentia 1.1.13 → 1.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,3 +7,4 @@ A collection of small modules providing essential utilities for TWS services:
7
7
  * A logging tool
8
8
  * An HTTP response tool for consistenty formatting error responses
9
9
  * A small utility to dump the last commit of the project
10
+ * [Paper Trail](paper-trail/README.md)
package/index.js CHANGED
@@ -8,5 +8,6 @@ export { default as halDecorator } from "./hal/index.js";
8
8
  export { default as formatter } from "./formatter/index.js";
9
9
  export { HealthCheck as healthCheck } from "./health/index.js";
10
10
  export { default as queue } from "./queue/index.js";
11
+ export { default as paperTrail } from "./paper-trail/index.js";
11
12
  export * from "./utils/index.js";
12
13
  export * from "./safe_proxy/index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.1.13",
3
+ "version": "1.1.15",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -30,7 +30,8 @@
30
30
  "dependencies": {
31
31
  "amqplib": "^0.8.0",
32
32
  "async": "^2.6.3",
33
- "axios": "^0.21.1",
33
+ "axios": "^0.21.4",
34
+ "deep-diff": "^1.0.2",
34
35
  "fast-safe-stringify": "^2.0.7",
35
36
  "git-rev": "^0.2.1",
36
37
  "lodash": "^4.17.21",
package/package.tar.gz CHANGED
Binary file
@@ -0,0 +1,67 @@
1
+ # Sequelize Paper Trail
2
+
3
+ A paper trail/sequelize hook plugin. This plugin has been modified to use the node14 CLS API called AsyncLocalStorage as well as fix some of the postgres related bugs. We have kept our own implemnetation and various changes were needed to support the express framework and pushing this to the public would hinder the general usability of this library for all.
4
+
5
+ ## Usage
6
+
7
+ In your database setup, setup your models and then attach the paperTrail to them. Many default options need to be changed to support underscore notation and the use of Account instead of User as the default table, thus suggestions options for TWS projects are shown below.
8
+
9
+ ```
10
+ import { paperTrail } from "@sera4/essentia"
11
+ import Sequelize from 'sequelize';
12
+
13
+ ...
14
+
15
+ const sequelize = new Sequelize(...);
16
+
17
+ PaperTrail.init(sequelize, Sequelize, { UUID: true,
18
+ underscored: true, enableMigration: true, underscoredAttributes: true,
19
+ debug: false, userModel: 'Account', enableCompression: true });
20
+ };
21
+
22
+ const db = {};
23
+ // for any model you want enabled
24
+ db["Tenant"] = TenantModel(sequelize, Sequelize);
25
+
26
+ db.PaperTrail = PaperTrail.init(sequelize, Sequelize, { UUID: true, underscored: true,
27
+ enableMigration: true, underscoredAttributes: true, continuationKey: "accountId",
28
+ debug: false, userModel: 'Account', userModelAttribute: 'account_id',
29
+ enableCompression: true });
30
+
31
+ db.PaperTrail.defineModels();
32
+
33
+ .. and for any model that needs a papertrail, call hasPaperTrail() on it ..
34
+ db["Tenant"].hasPaperTrail();
35
+ db["TenantLicense"].hasPaperTrail();
36
+ ```
37
+
38
+ and similarly, to use the automatic ALS (asyncLocalStorage) for automatic setting of the account/userID - add this into your middleware
39
+
40
+ ```
41
+ import db from "../database/models/index.js";
42
+
43
+ let alsEnabled = db.PaperTrail.alsEnabled();
44
+
45
+ const localStorageMiddleware = async (req, res, next) => {
46
+ await asyncLocalStorage.run(new Map(), () => {
47
+ if (!alsEnabled) {
48
+ alsEnabled = true;
49
+ db.PaperTrail.setupALS(asyncLocalStorage);
50
+ }
51
+
52
+ next();
53
+ });
54
+ };
55
+ ```
56
+ as this will ensure that the ALS context is initialized AFTER the first `run()` invocation.
57
+
58
+ `Please note`: For automatic accountId fetching, it is imperitive that you add
59
+
60
+ ```
61
+ asyncLocalStorage.getStore().set("accountId", accountId);
62
+ ```
63
+ somewhere into your express middleware so the database hooks can pickup the accountId when changes are made.
64
+
65
+ ## History
66
+
67
+ This is a private fork/implementation of https://github.com/nielsgl/sequelize-paper-trail which has been released under MIT license. This was privitized due to the number of commented out items/untested in their project as well as the change from CLS (continuation-local-storage) which is experimental to the supported AsyncLocalStorage which was stabilized in Node 14 as the CLS API 101.
@@ -0,0 +1,99 @@
1
+ import pkg from 'deep-diff';
2
+ import _ from 'lodash';
3
+
4
+ const { diff } = pkg;
5
+ const capitalizeFirstLetter = string =>
6
+ string.charAt(0).toUpperCase() + string.slice(1);
7
+
8
+ const toUnderscored = obj => {
9
+ _.forEach(obj, (k, v) => {
10
+ obj[k] = v
11
+ .replace(/(?:^|\.?)([A-Z])/g, (x, y) => `_${y.toLowerCase()}`)
12
+ .replace(/^_/, '');
13
+ });
14
+ return obj;
15
+ };
16
+
17
+ const calcDelta = (current, next, exclude, strict, DEBUG = false) => {
18
+ if (DEBUG) {
19
+ console.log('current', current);
20
+ console.log('next', next);
21
+ console.log('exclude', exclude);
22
+ }
23
+
24
+ const di = diff(current, next);
25
+
26
+ if (DEBUG) {
27
+ console.log('di', di);
28
+
29
+ let diffs2 = [];
30
+ if (di) {
31
+ diffs2 = di.map(i =>
32
+ JSON.parse(JSON.stringify(i).replace('"__data",', '')),
33
+ );
34
+ }
35
+
36
+ console.log('diffs2', diffs2);
37
+
38
+ console.log(
39
+ 'filter1',
40
+ diffs2.filter(i => i.path.join(',').indexOf('_') === -1),
41
+ );
42
+
43
+ console.log(
44
+ 'filter2',
45
+ diffs2.filter(i => exclude.every(x => i.path.indexOf(x) === -1)),
46
+ );
47
+ }
48
+
49
+ let diffs = [];
50
+ if (di) {
51
+ diffs = di
52
+ .map(i => JSON.parse(JSON.stringify(i).replace('"__data",', '')))
53
+ .filter(i => {
54
+ if (!strict && i.kind === 'E') {
55
+ // eslint-disable-next-line eqeqeq
56
+ if (i.lhs != i.rhs) return i;
57
+ } else return i;
58
+ return false;
59
+ })
60
+ .filter(i => exclude.every(x => i.path.indexOf(x) === -1));
61
+ }
62
+
63
+ if (diffs.length > 0) {
64
+ return diffs;
65
+ }
66
+ return null;
67
+ };
68
+
69
+ const diffToString = val => {
70
+ if (typeof val === 'undefined' || val === null)
71
+ return '';
72
+
73
+ if (val === true)
74
+ return '1';
75
+
76
+ if (val === false)
77
+ return '0';
78
+
79
+ if (typeof val === 'string')
80
+ return val;
81
+
82
+ if (!Number.isNaN(Number(val)))
83
+ return `${String(val)}`;
84
+
85
+ if ((typeof val === 'undefined' ? 'undefined' : typeof val) === 'object')
86
+ return `${JSON.stringify(val)}`;
87
+
88
+ if (Array.isArray(val))
89
+ return `${JSON.stringify(val)}`;
90
+
91
+ return '';
92
+ };
93
+
94
+ export default {
95
+ capitalizeFirstLetter,
96
+ toUnderscored,
97
+ calcDelta,
98
+ diffToString,
99
+ };
@@ -0,0 +1,636 @@
1
+ import * as jsdiff from 'diff';
2
+ import _ from 'lodash';
3
+ import helpers from './helpers.js';
4
+ import paginator from "../paginator/s4-pagination.js";
5
+
6
+ let failHard = false;
7
+ let als;
8
+ let Sequelize = null
9
+
10
+ const paperTrail = {};
11
+
12
+ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
13
+ Sequelize = sequelizePackage;
14
+ // In case that options is being parsed as a readonly attribute.
15
+ // Or it is not passed at all
16
+ const optsArg = _.cloneDeep(optionsArg || {});
17
+
18
+ const defaultOptions = {
19
+ debug: false,
20
+ log: null,
21
+ exclude: [
22
+ 'id',
23
+ 'createdAt',
24
+ 'updatedAt',
25
+ 'deletedAt',
26
+ 'created_at',
27
+ 'updated_at',
28
+ 'deleted_at',
29
+ 'revision',
30
+ ],
31
+ revisionAttribute: 'revision',
32
+ revisionModel: 'Revision',
33
+ revisionChangeModel: 'RevisionChange',
34
+ enableRevisionChangeModel: false,
35
+ UUID: false,
36
+ underscored: false,
37
+ underscoredAttributes: false,
38
+ defaultAttributes: {
39
+ documentId: 'documentId',
40
+ revisionId: 'revisionId',
41
+ },
42
+ als: null,
43
+ userModel: false,
44
+ userModelAttribute: 'userId',
45
+ enableCompression: false,
46
+ enableMigration: false,
47
+ enableStrictDiff: true,
48
+ continuationNamespace: null,
49
+ continuationKey: 'userId',
50
+ metaDataFields: null,
51
+ metaDataContinuationKey: 'metaData',
52
+ mysql: false,
53
+ };
54
+
55
+ if (optsArg.underscoredAttributes) {
56
+ helpers.toUnderscored(defaultOptions.defaultAttributes);
57
+ }
58
+
59
+ const options = _.defaults(optsArg, defaultOptions);
60
+
61
+ const log = options.log || console.log;
62
+
63
+ function createBeforeHook(operation) {
64
+
65
+ const beforeHook = function beforeHook(instance, opt) {
66
+ if (options.debug) {
67
+ log('beforeHook called');
68
+ log('instance:', instance);
69
+ log('opt:', opt);
70
+ }
71
+
72
+ if (opt.noPaperTrail) {
73
+ if (options.debug) {
74
+ log('noPaperTrail opt: is true, not logging');
75
+ }
76
+ return;
77
+ }
78
+
79
+ const destroyOperation = operation === 'destroy';
80
+
81
+ let previousVersion = {};
82
+ let currentVersion = {};
83
+ if (!destroyOperation && options.enableCompression) {
84
+ _.forEach(opt.defaultFields, a => {
85
+ previousVersion[a] = instance._previousDataValues[a];
86
+ currentVersion[a] = instance.dataValues[a];
87
+ });
88
+ } else {
89
+ previousVersion = instance._previousDataValues;
90
+ currentVersion = instance.dataValues;
91
+ }
92
+ // Supported nested models.
93
+ previousVersion = _.omitBy(
94
+ previousVersion,
95
+ i => i != null && typeof i === 'object' && !(i instanceof Date),
96
+ );
97
+ previousVersion = _.omit(previousVersion, options.exclude);
98
+
99
+ currentVersion = _.omitBy(
100
+ currentVersion,
101
+ i => i != null && typeof i === 'object' && !(i instanceof Date),
102
+ );
103
+ currentVersion = _.omit(currentVersion, options.exclude);
104
+
105
+ // Disallow change of revision
106
+ instance.set(
107
+ options.revisionAttribute,
108
+ instance._previousDataValues[options.revisionAttribute],
109
+ );
110
+
111
+ // Get diffs
112
+ const delta = helpers.calcDelta(
113
+ previousVersion,
114
+ currentVersion,
115
+ options.exclude,
116
+ options.enableStrictDiff,
117
+ );
118
+
119
+ const currentRevisionId = instance.get(options.revisionAttribute);
120
+
121
+ if (failHard && !currentRevisionId && opt.type === 'UPDATE') {
122
+ throw new Error('Revision Id was undefined');
123
+ }
124
+
125
+ if (options.debug) {
126
+ log('delta:', delta);
127
+ log('revisionId', currentRevisionId);
128
+ }
129
+ // Check if all required fields have been provided to the opts / ALS
130
+ if (options.metaDataFields) {
131
+ // get all required field keys as an array
132
+ const requiredFields = _.keys(
133
+ _.pickBy(
134
+ options.metaDataFields,
135
+ function isMetaDataFieldRequired(required) {
136
+ return required;
137
+ },
138
+ ),
139
+ );
140
+ if (requiredFields && requiredFields.length) {
141
+ const store = options.als && options.als.getStore();
142
+ const metaData = (store && store.get(metaDataContinuationKey)) || opt.metaData
143
+ const requiredFieldsProvided = _.filter(
144
+ requiredFields,
145
+ function isMetaDataFieldNonUndefined(field) {
146
+ return metaData[field] !== undefined;
147
+ },
148
+ );
149
+ if (
150
+ requiredFieldsProvided.length !== requiredFields.length
151
+ ) {
152
+ log(
153
+ 'Required fields: ',
154
+ options.metaDataFields,
155
+ requiredFields,
156
+ );
157
+ log(
158
+ 'Required fields provided: ',
159
+ metaData,
160
+ requiredFieldsProvided,
161
+ );
162
+ throw new Error(
163
+ 'Not all required fields are provided to paper trail!',
164
+ );
165
+ }
166
+ }
167
+ }
168
+
169
+ if (destroyOperation || (delta && delta.length > 0)) {
170
+ const revisionId = (currentRevisionId || 0) + 1;
171
+ instance.set(options.revisionAttribute, revisionId);
172
+
173
+ if (!instance.context) {
174
+ instance.context = {};
175
+ }
176
+ instance.context.delta = delta;
177
+ }
178
+
179
+ if (options.debug) {
180
+ log('end of beforeHook');
181
+ }
182
+ };
183
+ return beforeHook;
184
+ }
185
+
186
+ function createAfterHook(operation) {
187
+ const afterHook = function afterHook(instance, opt) {
188
+ const store = options.als && options.als.getStore()
189
+ if (options.debug) {
190
+ log('afterHook called');
191
+ log('instance:', instance);
192
+ log('opt:', opt);
193
+
194
+ if (store) {
195
+ log(
196
+ `ALS ${options.continuationKey}:`,
197
+ store.get(options.continuationKey),
198
+ );
199
+ }
200
+ }
201
+
202
+ const destroyOperation = operation === 'destroy';
203
+
204
+ if (
205
+ instance.context &&
206
+ ((instance.context.delta &&
207
+ instance.context.delta.length > 0) ||
208
+ destroyOperation)
209
+ ) {
210
+ const Revision = sequelize.model(options.revisionModel);
211
+ let RevisionChange;
212
+
213
+ if (options.enableRevisionChangeModel) {
214
+ RevisionChange = sequelize.model(
215
+ options.revisionChangeModel,
216
+ );
217
+ }
218
+
219
+ const { delta } = instance.context;
220
+
221
+ let previousVersion = {};
222
+ let currentVersion = {};
223
+ if (!destroyOperation && options.enableCompression) {
224
+ _.forEach(opt.defaultFields, a => {
225
+ previousVersion[a] = instance._previousDataValues[a];
226
+ currentVersion[a] = instance.dataValues[a];
227
+ });
228
+ } else {
229
+ previousVersion = instance._previousDataValues;
230
+ currentVersion = instance.dataValues;
231
+ }
232
+
233
+ // Supported nested models.
234
+ previousVersion = _.omitBy(
235
+ previousVersion,
236
+ i =>
237
+ i != null &&
238
+ typeof i === 'object' &&
239
+ !(i instanceof Date),
240
+ );
241
+ previousVersion = _.omit(previousVersion, options.exclude);
242
+
243
+ currentVersion = _.omitBy(
244
+ currentVersion,
245
+ i =>
246
+ i != null &&
247
+ typeof i === 'object' &&
248
+ !(i instanceof Date),
249
+ );
250
+ currentVersion = _.omit(currentVersion, options.exclude);
251
+
252
+ if (!options.als)
253
+ options.als = asyncLocalStorage;
254
+
255
+ const store = options.als && options.als.getStore()
256
+ if (store && !store.has(options.continuationKey)) {
257
+ if (failHard)
258
+ throw new Error(
259
+ `The ALS continuationKey ${options.continuationKey} was not defined.`,
260
+ );
261
+ }
262
+
263
+ let document = currentVersion;
264
+
265
+ if (options.mysql) {
266
+ document = JSON.stringify(document);
267
+ }
268
+
269
+ // Build revision
270
+ const query = {
271
+ model: this.name,
272
+ document,
273
+ operation,
274
+ };
275
+
276
+ // Add all extra data fields to the query object
277
+ if (options.metaDataFields) {
278
+ const metaData =
279
+ (store && store.get(options.metaDataContinuationKey)) ||
280
+ opt.metaData;
281
+ if (metaData) {
282
+ _.forEach(
283
+ options.metaDataFields,
284
+ function getMetaDataValues(required, field) {
285
+ const value = metaData[field];
286
+ if (options.debug) {
287
+ log(
288
+ `Adding metaData field to Revision - ${field} => ${value}`,
289
+ );
290
+ }
291
+ if (!(field in query)) {
292
+ query[field] = value;
293
+ } else if (options.debug) {
294
+ log(
295
+ `Revision object already has a value at ${field} => ${query[field]}`,
296
+ );
297
+ log('Not overwriting the original value');
298
+ }
299
+ },
300
+ );
301
+ }
302
+ }
303
+
304
+ // in case of custom user models that are not 'userId'
305
+ query[options.userModelAttribute] =
306
+ (store && store.get(options.continuationKey)) || opt.userId;
307
+
308
+ query[options.defaultAttributes.documentId] = instance.id;
309
+
310
+ const revision = Revision.build(query);
311
+
312
+ revision[options.revisionAttribute] = instance.get(
313
+ options.revisionAttribute,
314
+ );
315
+
316
+ // Save revision(
317
+ return revision
318
+ .save({ transaction: opt.transaction })
319
+ .then(objectRevision => {
320
+ // Loop diffs and create a revision-diff for each
321
+ if (options.enableRevisionChangeModel) {
322
+ _.forEach(delta, difference => {
323
+ const o = helpers.diffToString(
324
+ difference.item
325
+ ? difference.item.lhs
326
+ : difference.lhs,
327
+ );
328
+ const n = helpers.diffToString(
329
+ difference.item
330
+ ? difference.item.rhs
331
+ : difference.rhs,
332
+ );
333
+
334
+ // let document = difference;
335
+ document = difference;
336
+ let diff = o || n ? jsdiff.diffChars(o, n) : [];
337
+
338
+ if (options.mysql) {
339
+ document = JSON.stringify(document);
340
+ diff = JSON.stringify(diff);
341
+ }
342
+
343
+ const d = RevisionChange.build({
344
+ path: difference.path[0],
345
+ document,
346
+ diff,
347
+ revisionId: objectRevision.id,
348
+ });
349
+
350
+ d.save({ transaction: opt.transaction })
351
+ .then(savedD => {
352
+ // Add diff to revision
353
+ objectRevision[
354
+ `add${helpers.capitalizeFirstLetter(
355
+ options.revisionChangeModel,
356
+ )}`
357
+ ](savedD);
358
+
359
+ return null;
360
+ })
361
+ .catch(err => {
362
+ log('RevisionChange save error', err);
363
+ throw err;
364
+ });
365
+ });
366
+ }
367
+ return null;
368
+ })
369
+ .catch(err => {
370
+ log('Revision save error', err);
371
+ throw err;
372
+ });
373
+ }
374
+
375
+ if (options.debug) {
376
+ log('end of afterHook');
377
+ }
378
+
379
+ return null;
380
+ };
381
+ return afterHook;
382
+ }
383
+
384
+ // order in which sequelize processes the hooks
385
+ // (1)
386
+ // beforeBulkCreate(instances, options, fn)
387
+ // beforeBulkDestroy(instances, options, fn)
388
+ // beforeBulkUpdate(instances, options, fn)
389
+ // (2)
390
+ // beforeValidate(instance, options, fn)
391
+ // (-)
392
+ // validate
393
+ // (3)
394
+ // afterValidate(instance, options, fn)
395
+ // - or -
396
+ // validationFailed(instance, options, error, fn)
397
+ // (4)
398
+ // beforeCreate(instance, options, fn)
399
+ // beforeDestroy(instance, options, fn)
400
+ // beforeUpdate(instance, options, fn)
401
+ // (-)
402
+ // create
403
+ // destroy
404
+ // update
405
+ // (5)
406
+ // afterCreate(instance, options, fn)
407
+ // afterDestroy(instance, options, fn)
408
+ // afterUpdate(instance, options, fn)
409
+ // (6)
410
+ // afterBulkCreate(instances, options, fn)
411
+ // afterBulkDestroy(instances, options, fn)
412
+ // afterBulkUpdate(instances, options, fn)
413
+
414
+ // Extend model prototype with "hasPaperTrail" function
415
+ // Call model.hasPaperTrail() to enable revisions for model
416
+ _.assignIn(Sequelize.Model, {
417
+ hasPaperTrail: function hasPaperTrail() {
418
+ if (options.debug) {
419
+ log('Enabling paper trail on', this.name);
420
+ }
421
+
422
+ this.rawAttributes[options.revisionAttribute] = {
423
+ type: Sequelize.INTEGER,
424
+ defaultValue: 0,
425
+ };
426
+ this.revisionable = true;
427
+
428
+ // not sure if we need this
429
+ this.refreshAttributes();
430
+
431
+ if (options.enableMigration) {
432
+ const tableName = this.getTableName();
433
+
434
+ const queryInterface = sequelize.getQueryInterface();
435
+
436
+ queryInterface.describeTable(tableName).then(attributes => {
437
+ if (!attributes[options.revisionAttribute]) {
438
+ if (options.debug) {
439
+ log('adding revision attribute to the database');
440
+ }
441
+
442
+ queryInterface
443
+ .addColumn(tableName, options.revisionAttribute, {
444
+ type: Sequelize.INTEGER,
445
+ defaultValue: 0,
446
+ })
447
+ .then(() => null)
448
+ .catch(err => {
449
+ log('something went really wrong..', err);
450
+ return null;
451
+ });
452
+ }
453
+ return null;
454
+ });
455
+ }
456
+
457
+ this.addHook('beforeCreate', createBeforeHook('create'));
458
+ this.addHook('beforeDestroy', createBeforeHook('destroy'));
459
+ this.addHook('beforeUpdate', createBeforeHook('update'));
460
+ this.addHook('afterCreate', createAfterHook('create'));
461
+ this.addHook('afterDestroy', createAfterHook('destroy'));
462
+ this.addHook('afterUpdate', createAfterHook('update'));
463
+
464
+ // create association
465
+ return this.hasMany(sequelize.models[options.revisionModel], {
466
+ foreignKey: options.defaultAttributes.documentId,
467
+ constraints: false,
468
+ scope: {
469
+ model: this.name,
470
+ },
471
+ });
472
+ },
473
+ });
474
+
475
+ return {
476
+ // TWS-1032 since ALS must be set after a .run()
477
+ // invocation this CANNOT be set on init(). If you would
478
+ // like to use ALS, set it after the first invocation of run()
479
+ setupALS: function(asyncLocalStorage) {
480
+ options.als = asyncLocalStorage;
481
+ },
482
+ alsEnabled: function() {
483
+ return (options.als === true)
484
+ },
485
+ defineModels: function defineModels(db) {
486
+ // Attributes for RevisionModel
487
+ let attributes = {
488
+ model: {
489
+ type: Sequelize.TEXT,
490
+ allowNull: false,
491
+ },
492
+ document: {
493
+ type: Sequelize.JSONB,
494
+ allowNull: false,
495
+ },
496
+ operation: Sequelize.STRING(7),
497
+ };
498
+
499
+ if (options.mysql) {
500
+ attributes.document.type = Sequelize.TEXT('MEDIUMTEXT');
501
+ }
502
+
503
+ attributes[options.defaultAttributes.documentId] = {
504
+ type: Sequelize.INTEGER,
505
+ allowNull: false,
506
+ };
507
+
508
+ attributes[options.revisionAttribute] = {
509
+ type: Sequelize.INTEGER,
510
+ allowNull: false,
511
+ };
512
+
513
+ attributes[options.userModelAttribute] = {
514
+ type: Sequelize.INTEGER,
515
+ allowNull: true
516
+ };
517
+
518
+ if (options.UUID) {
519
+ attributes.id = {
520
+ primaryKey: true,
521
+ type: Sequelize.UUID,
522
+ defaultValue: Sequelize.UUIDV4,
523
+ };
524
+ attributes[options.defaultAttributes.documentId].type =
525
+ Sequelize.UUID;
526
+
527
+ console.log("userModelatribute", options.userModelAttribute)
528
+ attributes[options.userModelAttribute].type = Sequelize.UUID;
529
+ }
530
+
531
+ if (options.debug) {
532
+ log('attributes', attributes);
533
+ }
534
+
535
+ // Revision model
536
+ const Revision = sequelize.define(
537
+ options.revisionModel,
538
+ attributes,
539
+ {
540
+ underscored: options.underscored,
541
+ tableName: options.tableName,
542
+ },
543
+ );
544
+ Revision.associate = function associate(models) {
545
+ if (options.debug) {
546
+ log('models', models);
547
+ }
548
+
549
+ Revision.belongsTo(
550
+ sequelize.model(options.userModel),
551
+ options.belongsToUserOptions,
552
+ );
553
+ };
554
+
555
+ if (options.enableRevisionChangeModel) {
556
+ // Attributes for RevisionChangeModel
557
+ attributes = {
558
+ path: {
559
+ type: Sequelize.TEXT,
560
+ allowNull: false,
561
+ },
562
+ document: {
563
+ type: Sequelize.JSONB,
564
+ allowNull: false,
565
+ },
566
+ diff: {
567
+ type: Sequelize.JSONB,
568
+ allowNull: false,
569
+ },
570
+ };
571
+
572
+ if (options.mysql) {
573
+ attributes.document.type = Sequelize.TEXT('MEDIUMTEXT');
574
+ attributes.diff.type = Sequelize.TEXT('MEDIUMTEXT');
575
+ }
576
+
577
+ if (options.UUID) {
578
+ attributes.id = {
579
+ primaryKey: true,
580
+ type: Sequelize.UUID,
581
+ defaultValue: Sequelize.UUIDV4,
582
+ };
583
+ }
584
+ // RevisionChange model
585
+ const RevisionChange = sequelize.define(
586
+ options.revisionChangeModel,
587
+ attributes,
588
+ {
589
+ underscored: options.underscored,
590
+ },
591
+ );
592
+
593
+ // Set associations
594
+ Revision.hasMany(RevisionChange, {
595
+ foreignKey: options.defaultAttributes.revisionId,
596
+ constraints: false,
597
+ });
598
+ paginator.paginate(Revision);
599
+
600
+ RevisionChange.belongsTo(Revision);
601
+
602
+ if (db)
603
+ db[RevisionChange.name] = RevisionChange;
604
+ }
605
+
606
+ if (db)
607
+ db[Revision.name] = Revision;
608
+
609
+ /*
610
+ * We could extract this to a separate function so that having a
611
+ * user model doesn't require different loading
612
+ *
613
+ * or perhaps we could omit this because we are creating the
614
+ * association through the associate call above.
615
+ */
616
+ if (options.userModel) {
617
+ Revision.belongsTo(
618
+ sequelize.model(options.userModel),
619
+ options.belongsToUserOptions,
620
+ );
621
+ }
622
+
623
+ return Revision;
624
+ },
625
+ };
626
+ };
627
+
628
+ /**
629
+ * Throw exceptions when the user identifier from ALS is not set or if the
630
+ * revisionAttribute was not loaded on the model.
631
+ */
632
+ paperTrail.enableFailHard = () => {
633
+ failHard = true;
634
+ };
635
+
636
+ export default paperTrail;