@sera4/essentia 1.1.18 → 1.1.20
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 +113 -20
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: {
|
|
@@ -44,16 +47,22 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
44
47
|
als: null,
|
|
45
48
|
userModel: false,
|
|
46
49
|
userModelAttribute: 'userId',
|
|
50
|
+
tenantModel: false,
|
|
51
|
+
tenantModelAttribute: 'tenantId',
|
|
47
52
|
enableCompression: false,
|
|
48
53
|
enableMigration: false,
|
|
49
54
|
enableStrictDiff: true,
|
|
50
55
|
continuationNamespace: null,
|
|
51
56
|
continuationKey: 'userId',
|
|
57
|
+
continuationTenantKey: 'tenantId',
|
|
52
58
|
metaDataFields: null,
|
|
53
59
|
metaDataContinuationKey: 'metaData',
|
|
54
60
|
mysql: false,
|
|
55
61
|
};
|
|
56
62
|
|
|
63
|
+
if (optsArg.mixedDocumentReference)
|
|
64
|
+
defaultOptions.defaultAttributes.documentUuid = "documentUuid";
|
|
65
|
+
|
|
57
66
|
if (optsArg.underscoredAttributes) {
|
|
58
67
|
helpers.toUnderscored(defaultOptions.defaultAttributes);
|
|
59
68
|
}
|
|
@@ -316,7 +325,14 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
316
325
|
query[options.userModelAttribute] =
|
|
317
326
|
(store && store.get(options.continuationKey)) || opt.userId;
|
|
318
327
|
|
|
319
|
-
query[options.
|
|
328
|
+
query[options.tenantModelAttribute] =
|
|
329
|
+
(store && store.get(options.continuationTenantKey)) || opt.tenantId;
|
|
330
|
+
|
|
331
|
+
if (options.mixedDocumentReference && utils.isValidUuidV4(instance.id)) {
|
|
332
|
+
query[options.defaultAttributes.documentUuid] = instance.id;
|
|
333
|
+
} else {
|
|
334
|
+
query[options.defaultAttributes.documentId] = instance.id;
|
|
335
|
+
}
|
|
320
336
|
|
|
321
337
|
const revision = Revision.build(query);
|
|
322
338
|
|
|
@@ -472,13 +488,29 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
472
488
|
this.addHook('afterUpdate', createAfterHook('update'));
|
|
473
489
|
|
|
474
490
|
// create association
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
491
|
+
if (options.mixedDocumentReference) {
|
|
492
|
+
this.hasMany(sequelize.models[options.revisionModel], {
|
|
493
|
+
constraints: false,
|
|
494
|
+
foreignKey: options.defaultAttributes.documentUuid,
|
|
495
|
+
});
|
|
496
|
+
this.hasMany(sequelize.models[options.revisionModel], {
|
|
497
|
+
foreignKey: options.defaultAttributes.documentId,
|
|
498
|
+
constraints: false,
|
|
499
|
+
scope: {
|
|
500
|
+
model: this.name,
|
|
501
|
+
},
|
|
502
|
+
});
|
|
503
|
+
} else {
|
|
504
|
+
return this.hasMany(sequelize.models[options.revisionModel], {
|
|
505
|
+
foreignKey: options.defaultAttributes.documentId,
|
|
506
|
+
constraints: false,
|
|
507
|
+
scope: {
|
|
508
|
+
model: this.name,
|
|
509
|
+
},
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
|
|
482
514
|
},
|
|
483
515
|
});
|
|
484
516
|
|
|
@@ -520,10 +552,20 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
520
552
|
allowNull: false,
|
|
521
553
|
};
|
|
522
554
|
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
555
|
+
if (options.userModel) {
|
|
556
|
+
attributes[options.userModelAttribute] = {
|
|
557
|
+
type: options.UUID ? Sequelize.UUID : Sequelize.INTEGER,
|
|
558
|
+
allowNull: true
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (options.tenantModel) {
|
|
563
|
+
attributes[options.tenantModelAttribute] = {
|
|
564
|
+
type: options.UUID ? Sequelize.UUID : Sequelize.INTEGER,
|
|
565
|
+
allowNull: true
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
|
|
527
569
|
|
|
528
570
|
if (options.UUID) {
|
|
529
571
|
attributes.id = {
|
|
@@ -532,16 +574,42 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
532
574
|
defaultValue: Sequelize.UUIDV4,
|
|
533
575
|
};
|
|
534
576
|
|
|
535
|
-
|
|
536
|
-
Sequelize.UUID;
|
|
577
|
+
if (!options.mixedDocumentReference)
|
|
578
|
+
attributes[options.defaultAttributes.documentId].type = Sequelize.UUID;
|
|
537
579
|
|
|
538
580
|
attributes[options.userModelAttribute].type = Sequelize.UUID;
|
|
539
581
|
}
|
|
540
582
|
|
|
583
|
+
if (options.mixedDocumentReference) {
|
|
584
|
+
attributes[options.defaultAttributes.documentId].allowNull = true;
|
|
585
|
+
attributes[options.defaultAttributes.documentUuid] = {
|
|
586
|
+
type: Sequelize.UUID,
|
|
587
|
+
allowNull: true
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
541
591
|
if (options.debug) {
|
|
542
592
|
log('attributes', attributes);
|
|
543
593
|
}
|
|
544
594
|
|
|
595
|
+
let indexes = [{
|
|
596
|
+
name: "document_reference_id",
|
|
597
|
+
fields: [options.defaultAttributes.documentId]
|
|
598
|
+
}];
|
|
599
|
+
|
|
600
|
+
if (options.userModel)
|
|
601
|
+
indexes.push({ fields: [options.userModelAttribute]});
|
|
602
|
+
|
|
603
|
+
if (options.tenantModel)
|
|
604
|
+
indexes.push({ fields: [options.tenantModelAttribute]});
|
|
605
|
+
|
|
606
|
+
if (options.mixedDocumentReference) {
|
|
607
|
+
indexes.push({
|
|
608
|
+
name: "document_reference_uuid",
|
|
609
|
+
fields: [options.defaultAttributes.documentUuid]
|
|
610
|
+
})
|
|
611
|
+
}
|
|
612
|
+
|
|
545
613
|
// Revision model
|
|
546
614
|
const Revision = sequelize.define(
|
|
547
615
|
options.revisionModel,
|
|
@@ -551,18 +619,43 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
|
|
|
551
619
|
tableName: options.tableName,
|
|
552
620
|
timestamps: true,
|
|
553
621
|
createdAt: options.underscored ? "created_at" : "createdAt",
|
|
554
|
-
updatedAt: options.underscored ? "updated_at" : "updatedAt"
|
|
622
|
+
updatedAt: options.underscored ? "updated_at" : "updatedAt",
|
|
623
|
+
indexes
|
|
555
624
|
},
|
|
556
625
|
);
|
|
626
|
+
|
|
627
|
+
// safely prune down values
|
|
628
|
+
Revision.prototype.toJSON = function() {
|
|
629
|
+
let values = this.get();
|
|
630
|
+
const excludedFields = ["updated_at"];
|
|
631
|
+
|
|
632
|
+
// ensure that we always reference the id as document_id
|
|
633
|
+
values.document_id = values.document_id || values.document_uuid;
|
|
634
|
+
delete values.document_uuid;
|
|
635
|
+
|
|
636
|
+
return _.omitBy(values, function(value, key) {
|
|
637
|
+
return (excludedFields.includes(key) || (value === null));
|
|
638
|
+
});
|
|
639
|
+
};
|
|
640
|
+
|
|
557
641
|
Revision.associate = function associate(models) {
|
|
558
642
|
if (options.debug) {
|
|
559
643
|
log('models', models);
|
|
560
644
|
}
|
|
561
645
|
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
646
|
+
if (options.userModel) {
|
|
647
|
+
Revision.belongsTo(
|
|
648
|
+
sequelize.model(options.userModel),
|
|
649
|
+
options.belongsToUserOptions,
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
if (options.tenantModel) {
|
|
654
|
+
Revision.belongsTo(
|
|
655
|
+
sequelize.model(options.tenantModel),
|
|
656
|
+
options.belongsToTenantOptions,
|
|
657
|
+
);
|
|
658
|
+
}
|
|
566
659
|
};
|
|
567
660
|
|
|
568
661
|
if (options.enableRevisionChangeModel) {
|
|
@@ -650,4 +743,4 @@ paperTrail.enableFailHard = () => {
|
|
|
650
743
|
failHard = true;
|
|
651
744
|
};
|
|
652
745
|
|
|
653
|
-
export default paperTrail;
|
|
746
|
+
export default paperTrail;
|