@sera4/essentia 1.1.28 → 1.1.30

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/index.js CHANGED
@@ -9,5 +9,6 @@ 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
11
  export { default as paperTrail } from "./paper-trail/index.js";
12
+ export { default as serializer } from "./serializer/index.js";
12
13
  export * from "./utils/index.js";
13
14
  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.28",
3
+ "version": "1.1.30",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -20,7 +20,7 @@
20
20
  "devDependencies": {
21
21
  "chai": "^4.3.4",
22
22
  "express": "^4.17.1",
23
- "mocha": "^8.3.2",
23
+ "mocha": "^9.2.2",
24
24
  "mocha-junit-reporter": "^1.23.3",
25
25
  "node-mocks-http": "^1.10.1",
26
26
  "nodemon": "^2.0.7",
@@ -29,14 +29,14 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "amqplib": "^0.8.0",
32
- "async": "^2.6.3",
32
+ "async": "^3.2.2",
33
33
  "axios": "^0.21.4",
34
34
  "deep-diff": "^1.0.2",
35
35
  "diff": "^5.0.0",
36
36
  "fast-safe-stringify": "^2.0.7",
37
37
  "git-rev": "^0.2.1",
38
38
  "lodash": "^4.17.21",
39
- "url-parse": "^1.5.3",
39
+ "url-parse": "^1.5.10",
40
40
  "uuid": "^3.4.0",
41
41
  "winston": "^3.3.3",
42
42
  "winston-logstash": "^0.4.0"
package/package.tar.gz CHANGED
Binary file
@@ -0,0 +1,60 @@
1
+ import _ from "lodash";
2
+
3
+ const customSerializers = {};
4
+
5
+ export default {
6
+ /**
7
+ * Seralizes a Sequelize Model.
8
+ * @param {Object} Model the squelize model to add this plugin to
9
+ * @param {Object} options various options for serialization. These can be:
10
+ * • hide - an array of fields to be omitted
11
+ * • decorators - an array of special decorators to run during serialization
12
+ */
13
+ serialize : (Model, options) => {
14
+ Model.prototype.toJSON = function(eachOptions={}) {
15
+ const hide = options && options.hide ? options.hide : [];
16
+ const values = this.get();
17
+
18
+ if (options && options.decorators) {
19
+ options.decorators.forEach(dec => {
20
+ const k = dec.name;
21
+ const v = values[k];
22
+ values[k] = dec.handler(v);
23
+ });
24
+ }
25
+
26
+ let omitted = _.omitBy(values, (v, k) => {
27
+ return hide.includes(k) || v === null;
28
+ });
29
+
30
+ const s = eachOptions["serializer"];
31
+ if (s && customSerializers[s]) {
32
+ omitted = customSerializers[s](omitted);
33
+ }
34
+
35
+ if (Array.isArray(eachOptions.includes)) {
36
+ // Attach any included options that are
37
+ // not null or undefined
38
+ eachOptions.includes.forEach(include => {
39
+ if (include) {
40
+ const key = Object.keys(include);
41
+ const value = include[key];
42
+ if (value) {
43
+ omitted[key] = value;
44
+ }
45
+ }
46
+ });
47
+ }
48
+ return omitted;
49
+ }
50
+ },
51
+
52
+ /**
53
+ * Registers a custom serializer.
54
+ * @param {String} name the name of the custom serializer
55
+ * @param {Function} serializer a custom serializer function associated with the name
56
+ */
57
+ register : (name, serializer) => {
58
+ customSerializers[name] = serializer;
59
+ }
60
+ }