@etohq/utils 1.5.1-alpha.7 → 1.5.1-alpha.8
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/dist/common/container.d.ts +7 -98
- package/dist/common/container.d.ts.map +1 -1
- package/dist/common/container.js +7 -94
- package/dist/common/container.js.map +1 -1
- package/dist/common/create-container-like.d.ts.map +1 -1
- package/dist/common/create-container-like.js +9 -1
- package/dist/common/create-container-like.js.map +1 -1
- package/dist/common/create-psql-index-helper.d.ts.map +1 -1
- package/dist/common/create-psql-index-helper.js +3 -1
- package/dist/common/create-psql-index-helper.js.map +1 -1
- package/dist/common/deep-copy.js +3 -3
- package/dist/common/deep-copy.js.map +1 -1
- package/dist/common/define-file-config.d.ts +1 -2
- package/dist/common/define-file-config.d.ts.map +1 -1
- package/dist/common/define-file-config.js +9 -12
- package/dist/common/define-file-config.js.map +1 -1
- package/dist/common/dynamic-import.d.ts.map +1 -1
- package/dist/common/dynamic-import.js +6 -24
- package/dist/common/dynamic-import.js.map +1 -1
- package/dist/common/errors.d.ts +8 -10
- package/dist/common/errors.d.ts.map +1 -1
- package/dist/common/errors.js +12 -33
- package/dist/common/errors.js.map +1 -1
- package/dist/common/execute-with-concurrency.d.ts +3 -1
- package/dist/common/execute-with-concurrency.d.ts.map +1 -1
- package/dist/common/execute-with-concurrency.js +3 -1
- package/dist/common/execute-with-concurrency.js.map +1 -1
- package/dist/common/get-resolved-plugins.d.ts +1 -1
- package/dist/common/get-resolved-plugins.d.ts.map +1 -1
- package/dist/common/get-resolved-plugins.js +2 -2
- package/dist/common/get-resolved-plugins.js.map +1 -1
- package/dist/common/index.d.ts +4 -11
- package/dist/common/index.d.ts.map +1 -1
- package/dist/common/index.js +4 -11
- package/dist/common/index.js.map +1 -1
- package/dist/common/merge-plugin-modules.d.ts.map +1 -1
- package/dist/common/merge-plugin-modules.js +23 -8
- package/dist/common/merge-plugin-modules.js.map +1 -1
- package/dist/common/normalize-import-path-with-source.d.ts +1 -1
- package/dist/common/normalize-import-path-with-source.d.ts.map +1 -1
- package/dist/common/normalize-import-path-with-source.js +2 -2
- package/dist/common/normalize-import-path-with-source.js.map +1 -1
- package/dist/common/normalize-locale.d.ts +19 -0
- package/dist/common/normalize-locale.d.ts.map +1 -0
- package/dist/common/normalize-locale.js +37 -0
- package/dist/common/normalize-locale.js.map +1 -0
- package/dist/common/parse-stringify-if-necessary.d.ts +7 -1
- package/dist/common/parse-stringify-if-necessary.d.ts.map +1 -1
- package/dist/common/parse-stringify-if-necessary.js +8 -2
- package/dist/common/parse-stringify-if-necessary.js.map +1 -1
- package/dist/common/read-dir-recursive.d.ts +1 -0
- package/dist/common/read-dir-recursive.d.ts.map +1 -1
- package/dist/common/read-dir-recursive.js +4 -3
- package/dist/common/read-dir-recursive.js.map +1 -1
- package/dist/common/wrap-handler.d.ts +17 -0
- package/dist/common/wrap-handler.d.ts.map +1 -1
- package/dist/common/wrap-handler.js +33 -37
- package/dist/common/wrap-handler.js.map +1 -1
- package/dist/dal/index.d.ts +4 -4
- package/dist/dal/index.d.ts.map +1 -1
- package/dist/dal/index.js +4 -4
- package/dist/dal/index.js.map +1 -1
- package/dist/dal/mikro-orm/big-number-field.d.ts.map +1 -1
- package/dist/dal/mikro-orm/big-number-field.js +37 -1
- package/dist/dal/mikro-orm/big-number-field.js.map +1 -1
- package/dist/dal/mikro-orm/custom-db-migrator.d.ts +8 -0
- package/dist/dal/mikro-orm/custom-db-migrator.d.ts.map +1 -0
- package/dist/dal/mikro-orm/custom-db-migrator.js +60 -0
- package/dist/dal/mikro-orm/custom-db-migrator.js.map +1 -0
- package/dist/dal/mikro-orm/mikro-orm-create-connection.d.ts +1 -1
- package/dist/dal/mikro-orm/mikro-orm-create-connection.d.ts.map +1 -1
- package/dist/dal/mikro-orm/mikro-orm-create-connection.js +16 -1
- package/dist/dal/mikro-orm/mikro-orm-create-connection.js.map +1 -1
- package/dist/dal/mikro-orm/mikro-orm-free-text-search-filter.d.ts.map +1 -1
- package/dist/dal/mikro-orm/mikro-orm-free-text-search-filter.js +3 -3
- package/dist/dal/mikro-orm/mikro-orm-free-text-search-filter.js.map +1 -1
- package/dist/dal/mikro-orm/mikro-orm-repository.d.ts +1 -0
- package/dist/dal/mikro-orm/mikro-orm-repository.d.ts.map +1 -1
- package/dist/dal/mikro-orm/mikro-orm-repository.js +276 -68
- package/dist/dal/mikro-orm/mikro-orm-repository.js.map +1 -1
- package/dist/dal/mikro-orm/mikro-orm-serializer-old.d.ts +29 -0
- package/dist/dal/mikro-orm/mikro-orm-serializer-old.d.ts.map +1 -0
- package/dist/dal/mikro-orm/mikro-orm-serializer-old.js +443 -0
- package/dist/dal/mikro-orm/mikro-orm-serializer-old.js.map +1 -0
- package/dist/dal/mikro-orm/mikro-orm-serializer.d.ts +25 -10
- package/dist/dal/mikro-orm/mikro-orm-serializer.d.ts.map +1 -1
- package/dist/dal/mikro-orm/mikro-orm-serializer.js +383 -174
- package/dist/dal/mikro-orm/mikro-orm-serializer.js.map +1 -1
- package/dist/dml/helpers/entity-builder/create-big-number-properties.d.ts.map +1 -1
- package/dist/dml/helpers/entity-builder/create-big-number-properties.js +16 -4
- package/dist/dml/helpers/entity-builder/create-big-number-properties.js.map +1 -1
- package/dist/dml/helpers/graphql-builder/get-attribute.d.ts.map +1 -1
- package/dist/dml/helpers/graphql-builder/get-attribute.js +1 -0
- package/dist/dml/helpers/graphql-builder/get-attribute.js.map +1 -1
- package/dist/dml/integration-tests/utils.d.ts.map +1 -1
- package/dist/dml/integration-tests/utils.js +2 -1
- package/dist/dml/integration-tests/utils.js.map +1 -1
- package/dist/dml/properties/text.d.ts +6 -0
- package/dist/dml/properties/text.d.ts.map +1 -1
- package/dist/dml/properties/text.js +8 -0
- package/dist/dml/properties/text.js.map +1 -1
- package/dist/event-bus/index.d.ts +19 -0
- package/dist/event-bus/index.d.ts.map +1 -1
- package/dist/event-bus/index.js +20 -3
- package/dist/event-bus/index.js.map +1 -1
- package/dist/event-bus/message-aggregator.d.ts +1 -1
- package/dist/event-bus/message-aggregator.d.ts.map +1 -1
- package/dist/event-bus/message-aggregator.js +29 -7
- package/dist/event-bus/message-aggregator.js.map +1 -1
- package/dist/event-bus/utils.d.ts +19 -8
- package/dist/event-bus/utils.d.ts.map +1 -1
- package/dist/event-bus/utils.js +12 -0
- package/dist/event-bus/utils.js.map +1 -1
- package/dist/feature-flags/index.d.ts +5 -3
- package/dist/feature-flags/index.d.ts.map +1 -1
- package/dist/feature-flags/index.js +5 -4
- package/dist/feature-flags/index.js.map +1 -1
- package/dist/modules-sdk/create-eto-mikro-orm-event-subscriber.d.ts +11 -0
- package/dist/modules-sdk/create-eto-mikro-orm-event-subscriber.d.ts.map +1 -0
- package/dist/modules-sdk/create-eto-mikro-orm-event-subscriber.js +45 -0
- package/dist/modules-sdk/create-eto-mikro-orm-event-subscriber.js.map +1 -0
- package/dist/modules-sdk/create-pg-connection.d.ts.map +1 -1
- package/dist/modules-sdk/create-pg-connection.js +12 -1
- package/dist/modules-sdk/create-pg-connection.js.map +1 -1
- package/dist/modules-sdk/decorators/inject-manager.js +3 -3
- package/dist/modules-sdk/decorators/inject-manager.js.map +1 -1
- package/dist/modules-sdk/decorators/inject-transaction-manager.d.ts.map +1 -1
- package/dist/modules-sdk/decorators/inject-transaction-manager.js +2 -4
- package/dist/modules-sdk/decorators/inject-transaction-manager.js.map +1 -1
- package/dist/modules-sdk/define-link.d.ts.map +1 -1
- package/dist/modules-sdk/define-link.js +37 -15
- package/dist/modules-sdk/define-link.js.map +1 -1
- package/dist/modules-sdk/define-policies.d.ts +29 -0
- package/dist/modules-sdk/define-policies.d.ts.map +1 -0
- package/dist/modules-sdk/define-policies.js +47 -0
- package/dist/modules-sdk/define-policies.js.map +1 -0
- package/dist/modules-sdk/eto-internal-service.d.ts +14 -3
- package/dist/modules-sdk/eto-internal-service.d.ts.map +1 -1
- package/dist/modules-sdk/eto-internal-service.js +116 -1
- package/dist/modules-sdk/eto-internal-service.js.map +1 -1
- package/dist/modules-sdk/eto-service.d.ts.map +1 -1
- package/dist/modules-sdk/eto-service.js +57 -1
- package/dist/modules-sdk/eto-service.js.map +1 -1
- package/dist/modules-sdk/index.d.ts +3 -0
- package/dist/modules-sdk/index.d.ts.map +1 -1
- package/dist/modules-sdk/index.js +3 -0
- package/dist/modules-sdk/index.js.map +1 -1
- package/dist/modules-sdk/joiner-config-builder.d.ts +7 -1
- package/dist/modules-sdk/joiner-config-builder.d.ts.map +1 -1
- package/dist/modules-sdk/joiner-config-builder.js +32 -1
- package/dist/modules-sdk/joiner-config-builder.js.map +1 -1
- package/dist/modules-sdk/load-module-database-config.d.ts.map +1 -1
- package/dist/modules-sdk/load-module-database-config.js +15 -1
- package/dist/modules-sdk/load-module-database-config.js.map +1 -1
- package/dist/modules-sdk/migration-scripts/migration-down.d.ts +3 -1
- package/dist/modules-sdk/migration-scripts/migration-down.d.ts.map +1 -1
- package/dist/modules-sdk/migration-scripts/migration-down.js +7 -2
- package/dist/modules-sdk/migration-scripts/migration-down.js.map +1 -1
- package/dist/modules-sdk/migration-scripts/migration-up.d.ts +1 -1
- package/dist/modules-sdk/migration-scripts/migration-up.d.ts.map +1 -1
- package/dist/modules-sdk/migration-scripts/migration-up.js +3 -0
- package/dist/modules-sdk/migration-scripts/migration-up.js.map +1 -1
- package/dist/modules-sdk/migration-scripts/seed.d.ts.map +1 -1
- package/dist/modules-sdk/migration-scripts/seed.js +3 -0
- package/dist/modules-sdk/migration-scripts/seed.js.map +1 -1
- package/dist/modules-sdk/mikro-orm-cli-config-builder.d.ts.map +1 -1
- package/dist/modules-sdk/mikro-orm-cli-config-builder.js +1 -0
- package/dist/modules-sdk/mikro-orm-cli-config-builder.js.map +1 -1
- package/dist/modules-sdk/module.d.ts.map +1 -1
- package/dist/modules-sdk/module.js +2 -0
- package/dist/modules-sdk/module.js.map +1 -1
- package/dist/modules-sdk/policy-to-types.d.ts +4 -0
- package/dist/modules-sdk/policy-to-types.d.ts.map +1 -0
- package/dist/modules-sdk/policy-to-types.js +62 -0
- package/dist/modules-sdk/policy-to-types.js.map +1 -0
- package/dist/notification/events.d.ts +6 -6
- package/dist/totals/big-number.d.ts +3 -0
- package/dist/totals/big-number.d.ts.map +1 -1
- package/dist/totals/big-number.js +26 -6
- package/dist/totals/big-number.js.map +1 -1
- package/dist/totals/index.d.ts +0 -1
- package/dist/totals/index.d.ts.map +1 -1
- package/dist/totals/index.js +0 -1
- package/dist/totals/index.js.map +1 -1
- package/dist/totals/math.d.ts +3 -1
- package/dist/totals/math.d.ts.map +1 -1
- package/dist/totals/math.js +8 -7
- package/dist/totals/math.js.map +1 -1
- package/dist/translations/apply-translations.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/user/events.d.ts +12 -12
- package/package.json +3 -2
|
@@ -2,242 +2,451 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.mikroOrmSerializer = exports.EntitySerializer = void 0;
|
|
4
4
|
const core_1 = require("@mikro-orm/core");
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const STATIC_OPTIONS_SHAPE = {
|
|
6
|
+
populate: true,
|
|
7
|
+
exclude: undefined,
|
|
8
|
+
preventCircularRef: true,
|
|
9
|
+
skipNull: undefined,
|
|
10
|
+
ignoreSerializers: undefined,
|
|
11
|
+
forceObject: true,
|
|
12
|
+
};
|
|
13
|
+
const EMPTY_ARRAY = Object.freeze([]);
|
|
14
|
+
const WILDCARD = "*";
|
|
15
|
+
const DOT = ".";
|
|
16
|
+
const UNDERSCORE = "_";
|
|
17
|
+
function isVisible(propName, populate, exclude, meta) {
|
|
18
|
+
if (populate === true)
|
|
7
19
|
return true;
|
|
8
|
-
|
|
9
|
-
if (Array.isArray(options.populate) &&
|
|
10
|
-
options.exclude?.find((item) => item === propName)) {
|
|
20
|
+
if (exclude && exclude.includes(propName))
|
|
11
21
|
return false;
|
|
22
|
+
if (Array.isArray(populate)) {
|
|
23
|
+
const populateLength = populate.length;
|
|
24
|
+
const propNameLength = propName.length;
|
|
25
|
+
for (let i = 0; i < populateLength; i++) {
|
|
26
|
+
const item = populate[i];
|
|
27
|
+
if (item === WILDCARD)
|
|
28
|
+
return true;
|
|
29
|
+
if (item === propName)
|
|
30
|
+
return true;
|
|
31
|
+
if (item.length > propNameLength && item[propNameLength] === DOT) {
|
|
32
|
+
if (item.slice(0, propNameLength) === propName)
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
12
36
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
const prop = meta.properties[propName];
|
|
19
|
-
const visible = (prop && !prop.hidden) || prop === undefined; // allow unknown properties
|
|
20
|
-
const prefixed = prop && !prop.primary && propName.startsWith("_"); // ignore prefixed properties, if it's not a PK
|
|
37
|
+
const prop = meta[propName];
|
|
38
|
+
const visible = (prop && !prop.hidden) || prop === undefined;
|
|
39
|
+
const prefixed = prop && !prop.primary && propName.charAt(0) === UNDERSCORE;
|
|
21
40
|
return visible && !prefixed;
|
|
22
41
|
}
|
|
23
|
-
function isPopulated(
|
|
24
|
-
if (
|
|
25
|
-
(options.populate?.find((item) => item === propName || item.startsWith(propName + ".")) ||
|
|
26
|
-
options.populate.includes("*"))) {
|
|
42
|
+
function isPopulated(propName, populate) {
|
|
43
|
+
if (populate === true)
|
|
27
44
|
return true;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
45
|
+
if (populate === false || !Array.isArray(populate))
|
|
46
|
+
return false;
|
|
47
|
+
const propNameLength = propName.length;
|
|
48
|
+
const populateLength = populate.length;
|
|
49
|
+
for (let i = 0; i < populateLength; i++) {
|
|
50
|
+
const item = populate[i];
|
|
51
|
+
if (item === WILDCARD || item === propName)
|
|
52
|
+
return true;
|
|
53
|
+
if (item.length > propNameLength && item[propNameLength] === DOT) {
|
|
54
|
+
if (item.slice(0, propNameLength) === propName)
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
31
57
|
}
|
|
32
58
|
return false;
|
|
33
59
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
60
|
+
class RequestScopedSerializationContext {
|
|
61
|
+
constructor() {
|
|
62
|
+
this.propertyNameCache = new Map();
|
|
63
|
+
this.visitedEntities = new WeakSet();
|
|
64
|
+
// The buffer essentially replaces what would otherwise be a Set → Array conversion with a more
|
|
65
|
+
// efficient pre-allocated array approach, while maintaining the
|
|
66
|
+
// deduplication logic via the separate seenKeys Set.
|
|
67
|
+
this.keyCollectionBuffer = new Array(100); // Pre-allocated buffer for key collection
|
|
68
|
+
this.keyBufferIndex = 0;
|
|
69
|
+
this.propertyNameCache.set("id", "id");
|
|
70
|
+
this.propertyNameCache.set("created_at", "created_at");
|
|
71
|
+
this.propertyNameCache.set("updated_at", "updated_at");
|
|
72
|
+
this.propertyNameCache.set("deleted_at", "deleted_at");
|
|
73
|
+
}
|
|
74
|
+
resetKeyBuffer() {
|
|
75
|
+
this.keyBufferIndex = 0;
|
|
76
|
+
}
|
|
77
|
+
addKey(key) {
|
|
78
|
+
if (this.keyBufferIndex < this.keyCollectionBuffer.length) {
|
|
79
|
+
this.keyCollectionBuffer[this.keyBufferIndex++] = key;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.keyCollectionBuffer.push(key);
|
|
83
|
+
this.keyBufferIndex++;
|
|
53
84
|
}
|
|
54
|
-
return !parents.some((parent) => parent === prop.type);
|
|
55
85
|
}
|
|
56
|
-
|
|
86
|
+
getKeys() {
|
|
87
|
+
// Avoid slice allocation if buffer is exactly full
|
|
88
|
+
if (this.keyBufferIndex === this.keyCollectionBuffer.length) {
|
|
89
|
+
return this.keyCollectionBuffer;
|
|
90
|
+
}
|
|
91
|
+
return this.keyCollectionBuffer.slice(0, this.keyBufferIndex);
|
|
92
|
+
}
|
|
57
93
|
}
|
|
58
94
|
class EntitySerializer {
|
|
59
|
-
static serialize(entity, options =
|
|
60
|
-
const
|
|
95
|
+
static serialize(entity, options = STATIC_OPTIONS_SHAPE, parents = EMPTY_ARRAY, requestCtx) {
|
|
96
|
+
const ctx = requestCtx ?? new RequestScopedSerializationContext();
|
|
61
97
|
const wrapped = (0, core_1.helper)(entity);
|
|
62
98
|
const meta = wrapped.__meta;
|
|
63
99
|
let contextCreated = false;
|
|
64
|
-
|
|
100
|
+
const populate = options.populate ?? STATIC_OPTIONS_SHAPE.populate;
|
|
101
|
+
const exclude = options.exclude;
|
|
102
|
+
const skipNull = options.skipNull ?? STATIC_OPTIONS_SHAPE.skipNull;
|
|
103
|
+
const preventCircularRef = options.preventCircularRef ?? STATIC_OPTIONS_SHAPE.preventCircularRef;
|
|
104
|
+
const ignoreSerializers = options.ignoreSerializers ?? STATIC_OPTIONS_SHAPE.ignoreSerializers;
|
|
105
|
+
const forceObject = options.forceObject ?? STATIC_OPTIONS_SHAPE.forceObject;
|
|
106
|
+
const serializationContext = wrapped.__serializationContext;
|
|
107
|
+
if (!serializationContext.root) {
|
|
65
108
|
const root = new core_1.SerializationContext({});
|
|
66
109
|
core_1.SerializationContext.propagate(root, entity, (meta, prop) => meta.properties[prop]?.kind !== core_1.ReferenceKind.SCALAR);
|
|
67
110
|
contextCreated = true;
|
|
68
111
|
}
|
|
69
|
-
const root =
|
|
70
|
-
.root;
|
|
112
|
+
const root = serializationContext.root;
|
|
71
113
|
const ret = {};
|
|
72
|
-
|
|
73
|
-
|
|
114
|
+
ctx.resetKeyBuffer();
|
|
115
|
+
const seenKeys = new Set();
|
|
116
|
+
const primaryKeys = meta.primaryKeys;
|
|
117
|
+
const primaryKeysLength = primaryKeys.length;
|
|
118
|
+
const entityKeys = Object.keys(entity);
|
|
119
|
+
const entityKeysLength = entityKeys.length;
|
|
120
|
+
const metaPropertyKeys = Object.keys(meta.properties);
|
|
121
|
+
const metaPropertyKeysLength = metaPropertyKeys.length;
|
|
122
|
+
for (let i = 0; i < primaryKeysLength; i++) {
|
|
123
|
+
const key = primaryKeys[i];
|
|
124
|
+
if (!seenKeys.has(key)) {
|
|
125
|
+
seenKeys.add(key);
|
|
126
|
+
ctx.addKey(key);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
for (let i = 0; i < entityKeysLength; i++) {
|
|
130
|
+
const key = entityKeys[i];
|
|
131
|
+
if (!seenKeys.has(key)) {
|
|
132
|
+
seenKeys.add(key);
|
|
133
|
+
ctx.addKey(key);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
for (let i = 0; i < metaPropertyKeysLength; i++) {
|
|
137
|
+
const key = metaPropertyKeys[i];
|
|
138
|
+
if (!seenKeys.has(key)) {
|
|
139
|
+
seenKeys.add(key);
|
|
140
|
+
ctx.addKey(key);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const allKeys = ctx.getKeys();
|
|
144
|
+
const allKeysLength = allKeys.length;
|
|
145
|
+
let hasComplexProperties = false;
|
|
146
|
+
const metaProperties = meta.properties;
|
|
147
|
+
for (let i = 0; i < allKeysLength; i++) {
|
|
148
|
+
const prop = allKeys[i];
|
|
149
|
+
const propMeta = metaProperties[prop];
|
|
150
|
+
if (propMeta && propMeta.kind !== core_1.ReferenceKind.SCALAR) {
|
|
151
|
+
hasComplexProperties = true;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (!hasComplexProperties && populate === true) {
|
|
156
|
+
for (let i = 0; i < allKeysLength; i++) {
|
|
157
|
+
const prop = allKeys[i];
|
|
158
|
+
const propValue = entity[prop];
|
|
159
|
+
if (propValue !== undefined && !(propValue === null && skipNull)) {
|
|
160
|
+
ret[prop] = propValue;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (contextCreated)
|
|
164
|
+
root.close();
|
|
165
|
+
return ret;
|
|
166
|
+
}
|
|
74
167
|
const visited = root.visited.has(entity);
|
|
75
|
-
if (!visited)
|
|
168
|
+
if (!visited)
|
|
76
169
|
root.visited.add(entity);
|
|
170
|
+
const className = meta.className;
|
|
171
|
+
const platform = wrapped.__platform;
|
|
172
|
+
for (let i = 0; i < allKeysLength; i++) {
|
|
173
|
+
const prop = allKeys[i];
|
|
174
|
+
const isPropertyVisible = isVisible(prop, populate, exclude, metaProperties);
|
|
175
|
+
if (!isPropertyVisible)
|
|
176
|
+
continue;
|
|
177
|
+
const propMeta = metaProperties[prop];
|
|
178
|
+
let shouldSerialize = true;
|
|
179
|
+
if (propMeta &&
|
|
180
|
+
preventCircularRef &&
|
|
181
|
+
propMeta.kind !== core_1.ReferenceKind.SCALAR) {
|
|
182
|
+
if (!propMeta.mapToPk) {
|
|
183
|
+
const propType = propMeta.type;
|
|
184
|
+
const parentsLength = parents.length;
|
|
185
|
+
for (let j = 0; j < parentsLength; j++) {
|
|
186
|
+
if (parents[j] === propType) {
|
|
187
|
+
shouldSerialize = false;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (!shouldSerialize)
|
|
194
|
+
continue;
|
|
195
|
+
const cycle = root.visit(className, prop);
|
|
196
|
+
if (cycle && visited)
|
|
197
|
+
continue;
|
|
198
|
+
const val = this.processProperty(prop, entity, populate, exclude, skipNull, preventCircularRef, ignoreSerializers, forceObject, parents, ctx);
|
|
199
|
+
if (!cycle)
|
|
200
|
+
root.leave(className, prop);
|
|
201
|
+
if (val !== undefined && !(val === null && skipNull)) {
|
|
202
|
+
let propName;
|
|
203
|
+
if (propMeta?.serializedName) {
|
|
204
|
+
propName = propMeta.serializedName;
|
|
205
|
+
}
|
|
206
|
+
else if (propMeta?.primary && platform) {
|
|
207
|
+
propName = platform.getSerializedPrimaryKeyField(prop);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
propName = prop;
|
|
211
|
+
}
|
|
212
|
+
ret[propName] = val;
|
|
213
|
+
}
|
|
77
214
|
}
|
|
78
|
-
|
|
79
|
-
[...keys]
|
|
80
|
-
/** Eto Custom properties filtering **/
|
|
81
|
-
.filter((prop) => filterEntityPropToSerialize({
|
|
82
|
-
propName: prop,
|
|
83
|
-
meta,
|
|
84
|
-
options,
|
|
85
|
-
parents: parents_,
|
|
86
|
-
}))
|
|
87
|
-
.map((prop) => {
|
|
88
|
-
const cycle = root.visit(meta.className, prop);
|
|
89
|
-
if (cycle && visited) {
|
|
90
|
-
return [prop, undefined];
|
|
91
|
-
}
|
|
92
|
-
const val = this.processProperty(prop, entity, options, parents_);
|
|
93
|
-
if (!cycle) {
|
|
94
|
-
root.leave(meta.className, prop);
|
|
95
|
-
}
|
|
96
|
-
if (options.skipNull && core_1.Utils.isPlainObject(val)) {
|
|
97
|
-
core_1.Utils.dropUndefinedProperties(val, null);
|
|
98
|
-
}
|
|
99
|
-
return [prop, val];
|
|
100
|
-
})
|
|
101
|
-
.filter(([, value]) => typeof value !== "undefined" && !(value === null && options.skipNull))
|
|
102
|
-
.forEach(([prop, value]) => (ret[this.propertyName(meta, prop, wrapped.__platform)] = value));
|
|
103
|
-
if (contextCreated) {
|
|
215
|
+
if (contextCreated)
|
|
104
216
|
root.close();
|
|
105
|
-
|
|
106
|
-
if (!wrapped.isInitialized()) {
|
|
217
|
+
if (!wrapped.isInitialized())
|
|
107
218
|
return ret;
|
|
219
|
+
const metaProps = meta.props;
|
|
220
|
+
const metaPropsLength = metaProps.length;
|
|
221
|
+
for (let i = 0; i < metaPropsLength; i++) {
|
|
222
|
+
const prop = metaProps[i];
|
|
223
|
+
const propName = prop.name;
|
|
224
|
+
if (!isVisible(propName, populate, exclude, meta.properties))
|
|
225
|
+
continue;
|
|
226
|
+
let propertyKey;
|
|
227
|
+
let shouldProcess = false;
|
|
228
|
+
if (prop.getter && !prop.getterName && entity[propName] !== undefined) {
|
|
229
|
+
propertyKey = propName;
|
|
230
|
+
shouldProcess = true;
|
|
231
|
+
}
|
|
232
|
+
else if (prop.getterName &&
|
|
233
|
+
typeof entity[prop.getterName] === "function") {
|
|
234
|
+
propertyKey = prop.getterName;
|
|
235
|
+
shouldProcess = true;
|
|
236
|
+
}
|
|
237
|
+
if (shouldProcess) {
|
|
238
|
+
ret[this.propertyName(meta, propName, platform, ctx)] =
|
|
239
|
+
this.processProperty(propertyKey, entity, populate, exclude, skipNull, preventCircularRef, ignoreSerializers, forceObject, parents, ctx);
|
|
240
|
+
}
|
|
108
241
|
}
|
|
109
|
-
// decorated getters
|
|
110
|
-
meta.props
|
|
111
|
-
.filter((prop) => prop.getter &&
|
|
112
|
-
prop.getterName === undefined &&
|
|
113
|
-
typeof entity[prop.name] !== "undefined" &&
|
|
114
|
-
isVisible(meta, prop.name, options))
|
|
115
|
-
.forEach((prop) => (ret[this.propertyName(meta, prop.name, wrapped.__platform)] =
|
|
116
|
-
this.processProperty(prop.name, entity, options, parents_)));
|
|
117
|
-
// decorated get methods
|
|
118
|
-
meta.props
|
|
119
|
-
.filter((prop) => prop.getterName &&
|
|
120
|
-
entity[prop.getterName] instanceof Function &&
|
|
121
|
-
isVisible(meta, prop.name, options))
|
|
122
|
-
.forEach((prop) => (ret[this.propertyName(meta, prop.name, wrapped.__platform)] =
|
|
123
|
-
this.processProperty(prop.getterName, entity, options, parents_)));
|
|
124
242
|
return ret;
|
|
125
243
|
}
|
|
126
|
-
static propertyName(meta, prop, platform) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
244
|
+
static propertyName(meta, prop, platform, ctx) {
|
|
245
|
+
const cacheKey = `${meta.className}:${prop}:${platform?.constructor.name || "none"}`;
|
|
246
|
+
const cached = ctx.propertyNameCache.get(cacheKey);
|
|
247
|
+
if (cached !== undefined)
|
|
248
|
+
return cached;
|
|
249
|
+
const property = meta.properties[prop];
|
|
250
|
+
let result;
|
|
251
|
+
if (property?.serializedName) {
|
|
252
|
+
result = property.serializedName;
|
|
253
|
+
}
|
|
254
|
+
else if (property?.primary && platform) {
|
|
255
|
+
result = platform.getSerializedPrimaryKeyField(prop);
|
|
130
256
|
}
|
|
131
|
-
|
|
132
|
-
|
|
257
|
+
else {
|
|
258
|
+
result = prop;
|
|
133
259
|
}
|
|
134
|
-
|
|
260
|
+
ctx.propertyNameCache.set(cacheKey, result);
|
|
261
|
+
return result;
|
|
135
262
|
}
|
|
136
|
-
static processProperty(prop, entity,
|
|
137
|
-
const
|
|
138
|
-
const
|
|
139
|
-
|
|
263
|
+
static processProperty(prop, entity, populate, exclude, skipNull, preventCircularRef, ignoreSerializers, forceObject, parents, ctx) {
|
|
264
|
+
const entityConstructorName = entity.constructor.name;
|
|
265
|
+
const newParents = parents.length > 0
|
|
266
|
+
? [...parents, entityConstructorName]
|
|
267
|
+
: [entityConstructorName];
|
|
268
|
+
const dotIndex = prop.indexOf(DOT);
|
|
269
|
+
if (dotIndex > 0) {
|
|
270
|
+
prop = prop.substring(0, dotIndex);
|
|
271
|
+
}
|
|
140
272
|
const wrapped = (0, core_1.helper)(entity);
|
|
141
273
|
const property = wrapped.__meta.properties[prop];
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return serializer(returnValue);
|
|
274
|
+
const propValue = entity[prop];
|
|
275
|
+
if (typeof propValue === "function") {
|
|
276
|
+
const returnValue = propValue();
|
|
277
|
+
if (!ignoreSerializers && property?.serializer) {
|
|
278
|
+
return property.serializer(returnValue);
|
|
148
279
|
}
|
|
149
280
|
return returnValue;
|
|
150
281
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
return serializer(entity[prop]);
|
|
282
|
+
if (!ignoreSerializers && property?.serializer) {
|
|
283
|
+
return property.serializer(propValue);
|
|
154
284
|
}
|
|
155
|
-
if (core_1.Utils.isCollection(
|
|
156
|
-
return this.processCollection(prop, entity,
|
|
285
|
+
if (core_1.Utils.isCollection(propValue)) {
|
|
286
|
+
return this.processCollection(prop, entity, populate, exclude, skipNull, preventCircularRef, ignoreSerializers, forceObject, newParents, ctx);
|
|
157
287
|
}
|
|
158
|
-
if (core_1.Utils.isEntity(
|
|
159
|
-
return this.processEntity(prop, entity, wrapped.__platform,
|
|
288
|
+
if (core_1.Utils.isEntity(propValue, true)) {
|
|
289
|
+
return this.processEntity(prop, entity, wrapped.__platform, populate, exclude, skipNull, preventCircularRef, ignoreSerializers, forceObject, newParents, ctx);
|
|
160
290
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
291
|
+
if (property?.kind === core_1.ReferenceKind.EMBEDDED) {
|
|
292
|
+
if (Array.isArray(propValue)) {
|
|
293
|
+
const result = new Array(propValue.length);
|
|
294
|
+
for (let i = 0; i < propValue.length; i++) {
|
|
295
|
+
result[i] = (0, core_1.helper)(propValue[i]).toJSON();
|
|
296
|
+
}
|
|
297
|
+
return result;
|
|
165
298
|
}
|
|
166
|
-
if (core_1.Utils.isObject(
|
|
167
|
-
return (0, core_1.helper)(
|
|
299
|
+
if (core_1.Utils.isObject(propValue)) {
|
|
300
|
+
return (0, core_1.helper)(propValue).toJSON();
|
|
168
301
|
}
|
|
169
302
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return customType.toJSON(entity[prop], wrapped.__platform);
|
|
303
|
+
if (property?.customType) {
|
|
304
|
+
return property.customType.toJSON(propValue, wrapped.__platform);
|
|
173
305
|
}
|
|
174
|
-
return wrapped.__platform.normalizePrimaryKey(
|
|
306
|
+
return wrapped.__platform.normalizePrimaryKey(propValue);
|
|
175
307
|
}
|
|
176
|
-
static
|
|
177
|
-
|
|
178
|
-
return
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
308
|
+
static extractChildPopulate(populate, prop) {
|
|
309
|
+
if (!Array.isArray(populate) || populate.includes(WILDCARD)) {
|
|
310
|
+
return populate;
|
|
311
|
+
}
|
|
312
|
+
const propPrefix = prop + DOT;
|
|
313
|
+
const propPrefixLength = propPrefix.length;
|
|
314
|
+
const childPopulate = [];
|
|
315
|
+
const populateLength = populate.length;
|
|
316
|
+
for (let i = 0; i < populateLength; i++) {
|
|
317
|
+
const field = populate[i];
|
|
318
|
+
if (field.length > propPrefixLength &&
|
|
319
|
+
field.slice(0, propPrefixLength) === propPrefix) {
|
|
320
|
+
childPopulate.push(field.substring(propPrefixLength));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return childPopulate.length > 0 ? childPopulate : false;
|
|
324
|
+
}
|
|
325
|
+
static createChildOptions(populate, exclude, skipNull, preventCircularRef, ignoreSerializers, forceObject, prop) {
|
|
326
|
+
const childPopulate = this.extractChildPopulate(populate, prop);
|
|
182
327
|
return {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
: options.exclude,
|
|
328
|
+
populate: childPopulate,
|
|
329
|
+
exclude,
|
|
330
|
+
preventCircularRef,
|
|
331
|
+
skipNull,
|
|
332
|
+
ignoreSerializers,
|
|
333
|
+
forceObject,
|
|
190
334
|
};
|
|
191
335
|
}
|
|
192
|
-
static processEntity(prop, entity, platform,
|
|
193
|
-
const parents_ = [...parents, entity.constructor.name];
|
|
336
|
+
static processEntity(prop, entity, platform, populate, exclude, skipNull, preventCircularRef, ignoreSerializers, forceObject, parents, ctx) {
|
|
194
337
|
const child = core_1.Reference.unwrapReference(entity[prop]);
|
|
195
338
|
const wrapped = (0, core_1.helper)(child);
|
|
196
|
-
const populated = isPopulated(
|
|
197
|
-
const expand = populated ||
|
|
339
|
+
const populated = isPopulated(prop, populate) && wrapped.isInitialized();
|
|
340
|
+
const expand = populated || forceObject || !wrapped.__managed;
|
|
198
341
|
if (expand) {
|
|
199
|
-
|
|
342
|
+
const childOptions = this.createChildOptions(populate, exclude, skipNull, preventCircularRef, ignoreSerializers, forceObject, prop);
|
|
343
|
+
return this.serialize(child, childOptions, parents, ctx);
|
|
200
344
|
}
|
|
201
345
|
return platform.normalizePrimaryKey(wrapped.getPrimaryKey());
|
|
202
346
|
}
|
|
203
|
-
static processCollection(prop, entity,
|
|
204
|
-
const parents_ = [...parents, entity.constructor.name];
|
|
347
|
+
static processCollection(prop, entity, populate, exclude, skipNull, preventCircularRef, ignoreSerializers, forceObject, parents, ctx) {
|
|
205
348
|
const col = entity[prop];
|
|
206
|
-
if (!col.isInitialized())
|
|
349
|
+
if (!col.isInitialized())
|
|
207
350
|
return undefined;
|
|
351
|
+
const items = col.getItems(false);
|
|
352
|
+
const itemsLength = items.length;
|
|
353
|
+
if (itemsLength === 0)
|
|
354
|
+
return [];
|
|
355
|
+
const result = new Array(itemsLength);
|
|
356
|
+
let shouldPopulateCollection = false;
|
|
357
|
+
if (populate === true) {
|
|
358
|
+
shouldPopulateCollection = true;
|
|
208
359
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
360
|
+
else if (Array.isArray(populate)) {
|
|
361
|
+
const propLength = prop.length;
|
|
362
|
+
const populateLength = populate.length;
|
|
363
|
+
for (let j = 0; j < populateLength; j++) {
|
|
364
|
+
const item = populate[j];
|
|
365
|
+
if (item === WILDCARD || item === prop) {
|
|
366
|
+
shouldPopulateCollection = true;
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
if (item.length > propLength && item[propLength] === DOT) {
|
|
370
|
+
if (item.slice(0, propLength) === prop) {
|
|
371
|
+
shouldPopulateCollection = true;
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
212
375
|
}
|
|
213
|
-
|
|
214
|
-
|
|
376
|
+
}
|
|
377
|
+
if (!shouldPopulateCollection) {
|
|
378
|
+
for (let i = 0; i < itemsLength; i++) {
|
|
379
|
+
const item = items[i];
|
|
380
|
+
const wrapped = (0, core_1.helper)(item);
|
|
381
|
+
result[i] = wrapped.getPrimaryKey();
|
|
382
|
+
}
|
|
383
|
+
return result;
|
|
384
|
+
}
|
|
385
|
+
let childPopulate = populate;
|
|
386
|
+
if (Array.isArray(populate) && !populate.includes(WILDCARD)) {
|
|
387
|
+
const propPrefix = prop + DOT;
|
|
388
|
+
const propPrefixLength = propPrefix.length;
|
|
389
|
+
const childPopulateArray = [];
|
|
390
|
+
for (let j = 0; j < populate.length; j++) {
|
|
391
|
+
const field = populate[j];
|
|
392
|
+
if (field.length > propPrefixLength &&
|
|
393
|
+
field.slice(0, propPrefixLength) === propPrefix) {
|
|
394
|
+
childPopulateArray.push(field.substring(propPrefixLength));
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
childPopulate = childPopulateArray.length > 0 ? childPopulateArray : false;
|
|
398
|
+
}
|
|
399
|
+
const childOptions = {
|
|
400
|
+
populate: childPopulate,
|
|
401
|
+
exclude,
|
|
402
|
+
preventCircularRef,
|
|
403
|
+
skipNull,
|
|
404
|
+
ignoreSerializers,
|
|
405
|
+
forceObject,
|
|
406
|
+
};
|
|
407
|
+
for (let i = 0; i < itemsLength; i++) {
|
|
408
|
+
result[i] = this.serialize(items[i], childOptions, parents, ctx);
|
|
409
|
+
}
|
|
410
|
+
return result;
|
|
215
411
|
}
|
|
216
412
|
}
|
|
217
413
|
exports.EntitySerializer = EntitySerializer;
|
|
218
414
|
const mikroOrmSerializer = (data, options) => {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
415
|
+
const ctx = new RequestScopedSerializationContext();
|
|
416
|
+
const finalOptions = options
|
|
417
|
+
? {
|
|
418
|
+
populate: Array.isArray(options.populate) &&
|
|
419
|
+
options.populate?.includes(WILDCARD)
|
|
420
|
+
? true
|
|
421
|
+
: options.populate ?? STATIC_OPTIONS_SHAPE.populate,
|
|
422
|
+
exclude: options.exclude,
|
|
423
|
+
preventCircularRef: options.preventCircularRef ?? STATIC_OPTIONS_SHAPE.preventCircularRef,
|
|
424
|
+
skipNull: options.skipNull ?? STATIC_OPTIONS_SHAPE.skipNull,
|
|
425
|
+
ignoreSerializers: options.ignoreSerializers ?? STATIC_OPTIONS_SHAPE.ignoreSerializers,
|
|
426
|
+
forceObject: options.forceObject ?? STATIC_OPTIONS_SHAPE.forceObject,
|
|
427
|
+
}
|
|
428
|
+
: STATIC_OPTIONS_SHAPE;
|
|
429
|
+
if (!Array.isArray(data)) {
|
|
430
|
+
if (data?.__meta) {
|
|
431
|
+
return EntitySerializer.serialize(data, finalOptions, EMPTY_ARRAY, ctx);
|
|
432
|
+
}
|
|
433
|
+
return data;
|
|
434
|
+
}
|
|
435
|
+
const dataLength = data.length;
|
|
436
|
+
if (dataLength === 0) {
|
|
437
|
+
return [];
|
|
438
|
+
}
|
|
439
|
+
const result = new Array(dataLength);
|
|
440
|
+
for (let i = 0; i < dataLength; i++) {
|
|
441
|
+
const item = data[i];
|
|
442
|
+
if (item?.__meta) {
|
|
443
|
+
result[i] = EntitySerializer.serialize(item, finalOptions, EMPTY_ARRAY, ctx);
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
result[i] = item;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return result;
|
|
241
450
|
};
|
|
242
451
|
exports.mikroOrmSerializer = mikroOrmSerializer;
|
|
243
452
|
//# sourceMappingURL=mikro-orm-serializer.js.map
|