@sera4/essentia 1.1.18 → 1.1.19
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/package.json +1 -1
- package/package.tar.gz +0 -0
- package/paper-trail/index.js +77 -12
package/package.json
CHANGED
package/package.tar.gz
CHANGED
|
Binary file
|
package/paper-trail/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import * as jsdiff from 'diff';
|
|
|
2
2
|
import _ from 'lodash';
|
|
3
3
|
import helpers from './helpers.js';
|
|
4
4
|
import paginator from "../paginator/sql-pagination.js";
|
|
5
|
+
import { utils } from "../utils/index.js"
|
|
5
6
|
|
|
6
7
|
let failHard = false;
|
|
7
8
|
let als;
|
|
@@ -35,6 +36,8 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
35
36
|
revisionChangeModel: 'RevisionChange',
|
|
36
37
|
enableRevisionChangeModel: false,
|
|
37
38
|
UUID: false,
|
|
39
|
+
// does this project use int's and UUIDs for some references (postgres)
|
|
40
|
+
mixedDocumentReference: false,
|
|
38
41
|
underscored: false,
|
|
39
42
|
underscoredAttributes: false,
|
|
40
43
|
defaultAttributes: {
|
|
@@ -54,6 +57,9 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
54
57
|
mysql: false,
|
|
55
58
|
};
|
|
56
59
|
|
|
60
|
+
if (optsArg.mixedDocumentReference)
|
|
61
|
+
defaultOptions.defaultAttributes.documentUuid = "documentUuid";
|
|
62
|
+
|
|
57
63
|
if (optsArg.underscoredAttributes) {
|
|
58
64
|
helpers.toUnderscored(defaultOptions.defaultAttributes);
|
|
59
65
|
}
|
|
@@ -316,7 +322,11 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
316
322
|
query[options.userModelAttribute] =
|
|
317
323
|
(store && store.get(options.continuationKey)) || opt.userId;
|
|
318
324
|
|
|
319
|
-
|
|
325
|
+
if (options.mixedDocumentReference && utils.isValidUuidV4(instance.id)) {
|
|
326
|
+
query[options.defaultAttributes.documentUuid] = instance.id;
|
|
327
|
+
} else {
|
|
328
|
+
query[options.defaultAttributes.documentId] = instance.id;
|
|
329
|
+
}
|
|
320
330
|
|
|
321
331
|
const revision = Revision.build(query);
|
|
322
332
|
|
|
@@ -472,13 +482,29 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
472
482
|
this.addHook('afterUpdate', createAfterHook('update'));
|
|
473
483
|
|
|
474
484
|
// create association
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
485
|
+
if (options.mixedDocumentReference) {
|
|
486
|
+
this.hasMany(sequelize.models[options.revisionModel], {
|
|
487
|
+
constraints: false,
|
|
488
|
+
foreignKey: options.defaultAttributes.documentUuid,
|
|
489
|
+
});
|
|
490
|
+
this.hasMany(sequelize.models[options.revisionModel], {
|
|
491
|
+
foreignKey: options.defaultAttributes.documentId,
|
|
492
|
+
constraints: false,
|
|
493
|
+
scope: {
|
|
494
|
+
model: this.name,
|
|
495
|
+
},
|
|
496
|
+
});
|
|
497
|
+
} else {
|
|
498
|
+
return this.hasMany(sequelize.models[options.revisionModel], {
|
|
499
|
+
foreignKey: options.defaultAttributes.documentId,
|
|
500
|
+
constraints: false,
|
|
501
|
+
scope: {
|
|
502
|
+
model: this.name,
|
|
503
|
+
},
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
|
|
482
508
|
},
|
|
483
509
|
});
|
|
484
510
|
|
|
@@ -532,16 +558,39 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
532
558
|
defaultValue: Sequelize.UUIDV4,
|
|
533
559
|
};
|
|
534
560
|
|
|
535
|
-
|
|
536
|
-
Sequelize.UUID;
|
|
561
|
+
if (!options.mixedDocumentReference)
|
|
562
|
+
attributes[options.defaultAttributes.documentId].type = Sequelize.UUID;
|
|
537
563
|
|
|
538
564
|
attributes[options.userModelAttribute].type = Sequelize.UUID;
|
|
539
565
|
}
|
|
540
566
|
|
|
567
|
+
if (options.mixedDocumentReference) {
|
|
568
|
+
attributes[options.defaultAttributes.documentId].allowNull = true;
|
|
569
|
+
attributes[options.defaultAttributes.documentUuid] = {
|
|
570
|
+
type: Sequelize.UUID,
|
|
571
|
+
allowNull: true
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
541
575
|
if (options.debug) {
|
|
542
576
|
log('attributes', attributes);
|
|
543
577
|
}
|
|
544
578
|
|
|
579
|
+
let indexes = [{
|
|
580
|
+
name: "document_reference_id",
|
|
581
|
+
fields: [options.defaultAttributes.documentId]
|
|
582
|
+
}];
|
|
583
|
+
|
|
584
|
+
if (options.userModel)
|
|
585
|
+
indexes.push({ fields: [options.userModelAttribute]});
|
|
586
|
+
|
|
587
|
+
if (options.mixedDocumentReference) {
|
|
588
|
+
indexes.push({
|
|
589
|
+
name: "document_reference_uuid",
|
|
590
|
+
fields: [options.defaultAttributes.documentUuid]
|
|
591
|
+
})
|
|
592
|
+
}
|
|
593
|
+
|
|
545
594
|
// Revision model
|
|
546
595
|
const Revision = sequelize.define(
|
|
547
596
|
options.revisionModel,
|
|
@@ -551,9 +600,25 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
551
600
|
tableName: options.tableName,
|
|
552
601
|
timestamps: true,
|
|
553
602
|
createdAt: options.underscored ? "created_at" : "createdAt",
|
|
554
|
-
updatedAt: options.underscored ? "updated_at" : "updatedAt"
|
|
603
|
+
updatedAt: options.underscored ? "updated_at" : "updatedAt",
|
|
604
|
+
indexes
|
|
555
605
|
},
|
|
556
606
|
);
|
|
607
|
+
|
|
608
|
+
// safely prune down values
|
|
609
|
+
Revision.prototype.toJSON = function() {
|
|
610
|
+
let values = this.get();
|
|
611
|
+
const excludedFields = ["updated_at"];
|
|
612
|
+
|
|
613
|
+
// ensure that we always reference the id as document_id
|
|
614
|
+
values.document_id = values.document_id || values.document_uuid;
|
|
615
|
+
delete values.document_uuid;
|
|
616
|
+
|
|
617
|
+
return _.omitBy(values, function(value, key) {
|
|
618
|
+
return (excludedFields.includes(key) || (value === null));
|
|
619
|
+
});
|
|
620
|
+
};
|
|
621
|
+
|
|
557
622
|
Revision.associate = function associate(models) {
|
|
558
623
|
if (options.debug) {
|
|
559
624
|
log('models', models);
|
|
@@ -650,4 +715,4 @@ paperTrail.enableFailHard = () => {
|
|
|
650
715
|
failHard = true;
|
|
651
716
|
};
|
|
652
717
|
|
|
653
|
-
export default paperTrail;
|
|
718
|
+
export default paperTrail;
|