@sera4/essentia 1.1.63 → 1.1.64

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.1.63",
3
+ "version": "1.1.64",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/package.tar.gz CHANGED
Binary file
@@ -4,7 +4,7 @@ class SequelizePaginator {
4
4
 
5
5
  constructor() {
6
6
  this.paginateIn = {
7
- size: "page_size",
7
+ size: "page_size",
8
8
  index: "page_index"
9
9
  };
10
10
  this.paginateOut = {
@@ -99,11 +99,11 @@ class SequelizePaginator {
99
99
  links[this.paginateOut["first_page"]] = `${resource}?${this.paginateIn["index"]}=${firstPageIndex}&${this.paginateIn["size"]}=${data[this.paginateOut["page_size"]]}`;
100
100
  links[this.paginateOut["last_page"]] = `${resource}?${this.paginateIn["index"]}=${lastPageIndex}&${this.paginateIn["size"]}=${data[this.paginateOut["page_size"]]}`;
101
101
  if (!data[this.paginateOut["first_page"]]) {
102
- links[this.paginateOut["previous_page"]] =
102
+ links[this.paginateOut["previous_page"]] =
103
103
  `${resource}?${this.paginateIn["index"]}=${data[this.paginateOut["previous_page"]]}&${this.paginateIn["size"]}=${data[this.paginateOut["page_size"]]}`;
104
- }
104
+ }
105
105
  if (!data[this.paginateOut["last_page"]]) {
106
- links[this.paginateOut["next_page"]] =
106
+ links[this.paginateOut["next_page"]] =
107
107
  `${resource}?${this.paginateIn["index"]}=${data[this.paginateOut["next_page"]]}&${this.paginateIn["size"]}=${data[this.paginateOut["page_size"]]}`;
108
108
  }
109
109
  data[linksKey] = links;
@@ -114,13 +114,13 @@ class SequelizePaginator {
114
114
  const getPaginationData = (result) => {
115
115
  var currentPage = this.zeroBasedOffset ? pagination.offset : pagination.offset + 1;
116
116
  var outOfBounds = result.rows.length === 0;
117
-
117
+
118
118
  // For queries with aggregation (Group By, Count), sequelize findAndCountAll returns an array of objects instead of just an integer
119
119
  // To avoid implementing a custom findAndCountAll, we just use the array's Length
120
120
  if (Array.isArray(result.count)) {
121
121
  result.count = result.count.length;
122
122
  }
123
-
123
+
124
124
  var totalPages = Math.ceil(result.count / pagination.limit);
125
125
  var data = {};
126
126
  data[this.paginateOut["current_page"]] = currentPage;
@@ -175,4 +175,4 @@ class SequelizePaginator {
175
175
  }
176
176
 
177
177
  const sqlPaginator = new SequelizePaginator();
178
- export default sqlPaginator;
178
+ export default sqlPaginator;
@@ -33,6 +33,7 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
33
33
  ],
34
34
  // additional fields to exclude on a global basis
35
35
  excludeExtras: [],
36
+ excludeVirtualAttributes: true, // by default do not record these
36
37
  censor: ["password", "password_salt", "temp_secret", "mfa_totp_secret"],
37
38
  revisionAttribute: 'revision',
38
39
  revisionModel: 'Revision',
@@ -107,6 +108,7 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
107
108
 
108
109
  let previousVersion = _.cloneDeep(instance._previousDataValues);
109
110
  let currentVersion = _.cloneDeep(instance.dataValues);
111
+
110
112
  if (!destroyOperation && options.enableCompression) {
111
113
  _.forEach(opt.fields, a => {
112
114
  previousVersion[a] = instance._previousDataValues[a];
@@ -116,18 +118,23 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
116
118
  previousVersion = instance._previousDataValues;
117
119
  currentVersion = instance.dataValues;
118
120
  }
119
- // Supported nested models.
121
+
122
+ const excludes = options.exclude.concat([...this.extraRevisionAttributesExclude]);
123
+
124
+ // Supported nested models, allowing JSONB objects.
120
125
  previousVersion = _.omitBy(
121
126
  previousVersion,
122
- i => i != null && typeof i === 'object' && !(i instanceof Date),
127
+ i => i != null && typeof i === 'object' && !(i instanceof Date || _.isPlainObject(i)),
123
128
  );
124
- previousVersion = _.omit(previousVersion, options.exclude);
129
+
130
+ previousVersion = _.omit(previousVersion, excludes);
125
131
 
126
132
  currentVersion = _.omitBy(
127
133
  currentVersion,
128
- i => i != null && typeof i === 'object' && !(i instanceof Date),
134
+ i => i != null && typeof i === 'object' && !(i instanceof Date || _.isPlainObject(i)),
129
135
  );
130
- currentVersion = _.omit(currentVersion, options.exclude);
136
+
137
+ currentVersion = _.omit(currentVersion, excludes);
131
138
 
132
139
  // Disallow change of revision
133
140
  instance.set(
@@ -260,23 +267,26 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
260
267
  currentVersion = instance.dataValues;
261
268
  }
262
269
 
270
+ // we need to support nested Objects in a different way
271
+ // if the type of change is a nested object, we need to lookat the deltas and join the path with periods
272
+ // to get and show the differences.
273
+
263
274
  // Supported nested models.
264
- previousVersion = _.omitBy(
265
- previousVersion,
266
- i =>
267
- i != null &&
268
- typeof i === 'object' &&
269
- !(i instanceof Date),
270
- );
275
+ previousVersion = _.omitBy(previousVersion, value => {
276
+ if (value == null) return false; // keep null/undefined
277
+ if (value instanceof Date) return false; // keep Dates
278
+ if (_.isPlainObject(value)) return false; // keep plain objects (including nested objects)
279
+ return typeof value === 'object'; // omit if it's an object that's not plain or a Date
280
+ });
271
281
  previousVersion = _.omit(previousVersion, options.exclude);
272
282
 
273
- currentVersion = _.omitBy(
274
- currentVersion,
275
- i =>
276
- i != null &&
277
- typeof i === 'object' &&
278
- !(i instanceof Date),
279
- );
283
+ currentVersion = _.omitBy(currentVersion, value => {
284
+ if (value == null) return false;
285
+ if (value instanceof Date) return false;
286
+ if (_.isPlainObject(value)) return false;
287
+ return typeof value === 'object';
288
+ });
289
+
280
290
  currentVersion = _.omit(currentVersion, options.exclude);
281
291
 
282
292
  const store = options.als && options.als.getStore()
@@ -287,6 +297,37 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
287
297
  );
288
298
  }
289
299
 
300
+ let objectsChanged = new Set();
301
+ // if the previous version or new version contain objects; push them onto this list
302
+ _.forEach(previousVersion, (value, key) => {
303
+ if (typeof(value) === 'object' ) {
304
+ objectsChanged.add(key);
305
+ // blank it so we can set it equal to ONLY the values changed in the next segment
306
+ previousVersion[key] = {};
307
+ }
308
+ });
309
+ _.forEach(currentVersion, (value, key) => {
310
+ if (typeof(value) === 'object' ) {
311
+ objectsChanged.add(key);
312
+ currentVersion[key] = {};
313
+ }
314
+ });
315
+
316
+
317
+ // we don't want to see a full object; we want to see only
318
+ // x.y.z -> value
319
+ if (delta && objectsChanged.size > 0 ) {
320
+ // the format of these are A,E,D,N (added, edited, deleted, new) for the kind
321
+ // the array is either the key or key + subkeys ['options','subkey','subkey2'] etc.
322
+ // {kind: 'E', path: Array(2), lhs: false, rhs: true}
323
+ for (const difference of delta) {
324
+ // blank the currentVersion[difference.path[0]]
325
+ // and previousVersion[difference.path[0]]
326
+ _.set(currentVersion, difference.path, difference.rhs);
327
+ _.set(previousVersion, difference.path, difference.lhs);
328
+ };
329
+ }
330
+
290
331
  let document;
291
332
  if (destroyOperation) {
292
333
  document = { oldValues: currentVersion };
@@ -480,6 +521,15 @@ paperTrail.init = (sequelize, sequelizePackage, optionsArg) => {
480
521
  // not sure if we need this
481
522
  this.refreshAttributes();
482
523
 
524
+ this.extraRevisionAttributesExclude = new Set();
525
+ if (options.excludeVirtualAttributes) {
526
+ for (const attribute in this.rawAttributes) {
527
+ if (this.rawAttributes[attribute].type == "VIRTUAL") {
528
+ this.extraRevisionAttributesExclude.add(attribute);
529
+ }
530
+ }
531
+ }
532
+
483
533
  if (options.enableMigration) {
484
534
  const tableName = this.getTableName();
485
535
 
@@ -782,4 +832,4 @@ paperTrail.enableFailHard = () => {
782
832
  failHard = true;
783
833
  };
784
834
 
785
- export default paperTrail;
835
+ export default paperTrail;