@yandjin-mikro-orm/core 6.1.4-rc-sti-changes-1
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/EntityManager.d.ts +553 -0
- package/EntityManager.js +1736 -0
- package/LICENSE +21 -0
- package/MikroORM.d.ts +102 -0
- package/MikroORM.js +258 -0
- package/README.md +383 -0
- package/cache/CacheAdapter.d.ts +40 -0
- package/cache/CacheAdapter.js +2 -0
- package/cache/FileCacheAdapter.d.ts +31 -0
- package/cache/FileCacheAdapter.js +90 -0
- package/cache/GeneratedCacheAdapter.d.ts +25 -0
- package/cache/GeneratedCacheAdapter.js +38 -0
- package/cache/MemoryCacheAdapter.d.ts +24 -0
- package/cache/MemoryCacheAdapter.js +44 -0
- package/cache/NullCacheAdapter.d.ts +19 -0
- package/cache/NullCacheAdapter.js +30 -0
- package/cache/index.d.ts +5 -0
- package/cache/index.js +21 -0
- package/connections/Connection.d.ts +85 -0
- package/connections/Connection.js +128 -0
- package/connections/index.d.ts +1 -0
- package/connections/index.js +17 -0
- package/decorators/Check.d.ts +3 -0
- package/decorators/Check.js +17 -0
- package/decorators/CreateRequestContext.d.ts +2 -0
- package/decorators/CreateRequestContext.js +31 -0
- package/decorators/Embeddable.d.ts +8 -0
- package/decorators/Embeddable.js +15 -0
- package/decorators/Embedded.d.ts +13 -0
- package/decorators/Embedded.js +21 -0
- package/decorators/EnsureRequestContext.d.ts +2 -0
- package/decorators/EnsureRequestContext.js +26 -0
- package/decorators/Entity.d.ts +18 -0
- package/decorators/Entity.js +17 -0
- package/decorators/Enum.d.ts +9 -0
- package/decorators/Enum.js +20 -0
- package/decorators/Filter.d.ts +2 -0
- package/decorators/Filter.js +12 -0
- package/decorators/Formula.d.ts +5 -0
- package/decorators/Formula.js +19 -0
- package/decorators/Indexed.d.ts +12 -0
- package/decorators/Indexed.js +25 -0
- package/decorators/ManyToMany.d.ts +21 -0
- package/decorators/ManyToMany.js +17 -0
- package/decorators/ManyToOne.d.ts +15 -0
- package/decorators/ManyToOne.js +17 -0
- package/decorators/OneToMany.d.ts +18 -0
- package/decorators/OneToMany.js +21 -0
- package/decorators/OneToOne.d.ts +12 -0
- package/decorators/OneToOne.js +11 -0
- package/decorators/PrimaryKey.d.ts +8 -0
- package/decorators/PrimaryKey.js +24 -0
- package/decorators/Property.d.ts +218 -0
- package/decorators/Property.js +35 -0
- package/decorators/Subscriber.d.ts +1 -0
- package/decorators/Subscriber.js +2 -0
- package/decorators/hooks.d.ts +16 -0
- package/decorators/hooks.js +60 -0
- package/decorators/index.d.ts +17 -0
- package/decorators/index.js +36 -0
- package/drivers/DatabaseDriver.d.ts +72 -0
- package/drivers/DatabaseDriver.js +358 -0
- package/drivers/IDatabaseDriver.d.ts +193 -0
- package/drivers/IDatabaseDriver.js +4 -0
- package/drivers/index.d.ts +2 -0
- package/drivers/index.js +18 -0
- package/entity/ArrayCollection.d.ts +113 -0
- package/entity/ArrayCollection.js +386 -0
- package/entity/BaseEntity.d.ts +22 -0
- package/entity/BaseEntity.js +47 -0
- package/entity/Collection.d.ts +104 -0
- package/entity/Collection.js +373 -0
- package/entity/EntityAssigner.d.ts +28 -0
- package/entity/EntityAssigner.js +226 -0
- package/entity/EntityFactory.d.ts +41 -0
- package/entity/EntityFactory.js +302 -0
- package/entity/EntityHelper.d.ts +29 -0
- package/entity/EntityHelper.js +250 -0
- package/entity/EntityIdentifier.d.ts +10 -0
- package/entity/EntityIdentifier.js +19 -0
- package/entity/EntityLoader.d.ts +65 -0
- package/entity/EntityLoader.js +579 -0
- package/entity/EntityRepository.d.ts +161 -0
- package/entity/EntityRepository.js +207 -0
- package/entity/EntityValidator.d.ts +19 -0
- package/entity/EntityValidator.js +152 -0
- package/entity/Reference.d.ts +89 -0
- package/entity/Reference.js +242 -0
- package/entity/WrappedEntity.d.ts +67 -0
- package/entity/WrappedEntity.js +146 -0
- package/entity/index.d.ts +13 -0
- package/entity/index.js +29 -0
- package/entity/wrap.d.ts +15 -0
- package/entity/wrap.js +26 -0
- package/enums.d.ts +160 -0
- package/enums.js +169 -0
- package/errors.d.ts +65 -0
- package/errors.js +222 -0
- package/events/EventManager.d.ts +17 -0
- package/events/EventManager.js +77 -0
- package/events/EventSubscriber.d.ts +39 -0
- package/events/EventSubscriber.js +2 -0
- package/events/TransactionEventBroadcaster.d.ts +11 -0
- package/events/TransactionEventBroadcaster.js +17 -0
- package/events/index.d.ts +3 -0
- package/events/index.js +19 -0
- package/exceptions.d.ts +104 -0
- package/exceptions.js +130 -0
- package/hydration/Hydrator.d.ts +23 -0
- package/hydration/Hydrator.js +51 -0
- package/hydration/ObjectHydrator.d.ts +24 -0
- package/hydration/ObjectHydrator.js +332 -0
- package/hydration/index.d.ts +2 -0
- package/hydration/index.js +18 -0
- package/index.d.ts +25 -0
- package/index.js +50 -0
- package/index.mjs +192 -0
- package/logging/DefaultLogger.d.ts +32 -0
- package/logging/DefaultLogger.js +90 -0
- package/logging/Logger.d.ts +56 -0
- package/logging/Logger.js +2 -0
- package/logging/SimpleLogger.d.ts +17 -0
- package/logging/SimpleLogger.js +31 -0
- package/logging/colors.d.ts +9 -0
- package/logging/colors.js +20 -0
- package/logging/index.d.ts +4 -0
- package/logging/index.js +20 -0
- package/metadata/EntitySchema.d.ts +74 -0
- package/metadata/EntitySchema.js +293 -0
- package/metadata/MetadataDiscovery.d.ts +71 -0
- package/metadata/MetadataDiscovery.js +1244 -0
- package/metadata/MetadataProvider.d.ts +11 -0
- package/metadata/MetadataProvider.js +23 -0
- package/metadata/MetadataStorage.d.ts +22 -0
- package/metadata/MetadataStorage.js +87 -0
- package/metadata/MetadataValidator.d.ts +24 -0
- package/metadata/MetadataValidator.js +213 -0
- package/metadata/ReflectMetadataProvider.d.ts +8 -0
- package/metadata/ReflectMetadataProvider.js +48 -0
- package/metadata/index.d.ts +6 -0
- package/metadata/index.js +22 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +18 -0
- package/naming-strategy/AbstractNamingStrategy.js +48 -0
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +12 -0
- package/naming-strategy/EntityCaseNamingStrategy.js +32 -0
- package/naming-strategy/MongoNamingStrategy.d.ts +9 -0
- package/naming-strategy/MongoNamingStrategy.js +25 -0
- package/naming-strategy/NamingStrategy.d.ts +52 -0
- package/naming-strategy/NamingStrategy.js +2 -0
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +10 -0
- package/naming-strategy/UnderscoreNamingStrategy.js +28 -0
- package/naming-strategy/index.d.ts +5 -0
- package/naming-strategy/index.js +21 -0
- package/package.json +70 -0
- package/platforms/ExceptionConverter.d.ts +5 -0
- package/platforms/ExceptionConverter.js +11 -0
- package/platforms/Platform.d.ts +201 -0
- package/platforms/Platform.js +452 -0
- package/platforms/index.d.ts +2 -0
- package/platforms/index.js +18 -0
- package/serialization/EntitySerializer.d.ts +34 -0
- package/serialization/EntitySerializer.js +206 -0
- package/serialization/EntityTransformer.d.ts +8 -0
- package/serialization/EntityTransformer.js +192 -0
- package/serialization/SerializationContext.d.ts +30 -0
- package/serialization/SerializationContext.js +111 -0
- package/serialization/index.d.ts +3 -0
- package/serialization/index.js +19 -0
- package/types/ArrayType.d.ts +13 -0
- package/types/ArrayType.js +47 -0
- package/types/BigIntType.d.ts +16 -0
- package/types/BigIntType.js +45 -0
- package/types/BlobType.d.ts +10 -0
- package/types/BlobType.js +27 -0
- package/types/BooleanType.d.ts +8 -0
- package/types/BooleanType.js +16 -0
- package/types/DateTimeType.d.ts +8 -0
- package/types/DateTimeType.js +16 -0
- package/types/DateType.d.ts +8 -0
- package/types/DateType.js +16 -0
- package/types/DecimalType.d.ts +11 -0
- package/types/DecimalType.js +22 -0
- package/types/DoubleType.d.ts +11 -0
- package/types/DoubleType.js +22 -0
- package/types/EnumArrayType.d.ts +9 -0
- package/types/EnumArrayType.js +32 -0
- package/types/EnumType.d.ts +8 -0
- package/types/EnumType.js +16 -0
- package/types/FloatType.d.ts +8 -0
- package/types/FloatType.js +16 -0
- package/types/IntegerType.d.ts +8 -0
- package/types/IntegerType.js +16 -0
- package/types/IntervalType.d.ts +8 -0
- package/types/IntervalType.js +16 -0
- package/types/JsonType.d.ts +13 -0
- package/types/JsonType.js +34 -0
- package/types/MediumIntType.d.ts +6 -0
- package/types/MediumIntType.js +10 -0
- package/types/SmallIntType.d.ts +8 -0
- package/types/SmallIntType.js +16 -0
- package/types/StringType.d.ts +8 -0
- package/types/StringType.js +16 -0
- package/types/TextType.d.ts +8 -0
- package/types/TextType.js +16 -0
- package/types/TimeType.d.ts +9 -0
- package/types/TimeType.js +23 -0
- package/types/TinyIntType.d.ts +8 -0
- package/types/TinyIntType.js +16 -0
- package/types/Type.d.ts +64 -0
- package/types/Type.js +84 -0
- package/types/Uint8ArrayType.d.ts +11 -0
- package/types/Uint8ArrayType.js +37 -0
- package/types/UnknownType.d.ts +7 -0
- package/types/UnknownType.js +13 -0
- package/types/UuidType.d.ts +7 -0
- package/types/UuidType.js +13 -0
- package/types/index.d.ts +75 -0
- package/types/index.js +77 -0
- package/typings.d.ts +767 -0
- package/typings.js +198 -0
- package/unit-of-work/ChangeSet.d.ts +34 -0
- package/unit-of-work/ChangeSet.js +62 -0
- package/unit-of-work/ChangeSetComputer.d.ts +26 -0
- package/unit-of-work/ChangeSetComputer.js +153 -0
- package/unit-of-work/ChangeSetPersister.d.ts +50 -0
- package/unit-of-work/ChangeSetPersister.js +361 -0
- package/unit-of-work/CommitOrderCalculator.d.ts +62 -0
- package/unit-of-work/CommitOrderCalculator.js +113 -0
- package/unit-of-work/IdentityMap.d.ts +17 -0
- package/unit-of-work/IdentityMap.js +84 -0
- package/unit-of-work/UnitOfWork.d.ts +124 -0
- package/unit-of-work/UnitOfWork.js +1013 -0
- package/unit-of-work/index.d.ts +6 -0
- package/unit-of-work/index.js +22 -0
- package/utils/AbstractSchemaGenerator.d.ts +38 -0
- package/utils/AbstractSchemaGenerator.js +101 -0
- package/utils/Configuration.d.ts +390 -0
- package/utils/Configuration.js +357 -0
- package/utils/ConfigurationLoader.d.ts +29 -0
- package/utils/ConfigurationLoader.js +282 -0
- package/utils/Cursor.d.ts +77 -0
- package/utils/Cursor.js +169 -0
- package/utils/DataloaderUtils.d.ts +43 -0
- package/utils/DataloaderUtils.js +194 -0
- package/utils/EntityComparator.d.ts +73 -0
- package/utils/EntityComparator.js +568 -0
- package/utils/NullHighlighter.d.ts +4 -0
- package/utils/NullHighlighter.js +9 -0
- package/utils/QueryHelper.d.ts +28 -0
- package/utils/QueryHelper.js +228 -0
- package/utils/RawQueryFragment.d.ts +96 -0
- package/utils/RawQueryFragment.js +188 -0
- package/utils/RequestContext.d.ts +34 -0
- package/utils/RequestContext.js +54 -0
- package/utils/TransactionContext.d.ts +19 -0
- package/utils/TransactionContext.js +34 -0
- package/utils/Utils.d.ts +274 -0
- package/utils/Utils.js +1073 -0
- package/utils/clone.d.ts +6 -0
- package/utils/clone.js +127 -0
- package/utils/index.d.ts +13 -0
- package/utils/index.js +29 -0
- package/utils/upsert-utils.d.ts +6 -0
- package/utils/upsert-utils.js +33 -0
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EntityComparator = void 0;
|
|
4
|
+
const clone_1 = require("./clone");
|
|
5
|
+
const enums_1 = require("../enums");
|
|
6
|
+
const Utils_1 = require("./Utils");
|
|
7
|
+
const JsonType_1 = require("../types/JsonType");
|
|
8
|
+
class EntityComparator {
|
|
9
|
+
metadata;
|
|
10
|
+
platform;
|
|
11
|
+
comparators = new Map();
|
|
12
|
+
mappers = new Map();
|
|
13
|
+
snapshotGenerators = new Map();
|
|
14
|
+
pkGetters = new Map();
|
|
15
|
+
pkGettersConverted = new Map();
|
|
16
|
+
pkSerializers = new Map();
|
|
17
|
+
tmpIndex = 0;
|
|
18
|
+
constructor(metadata, platform) {
|
|
19
|
+
this.metadata = metadata;
|
|
20
|
+
this.platform = platform;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Computes difference between two entities.
|
|
24
|
+
*/
|
|
25
|
+
diffEntities(entityName, a, b) {
|
|
26
|
+
const comparator = this.getEntityComparator(entityName);
|
|
27
|
+
return Utils_1.Utils.callCompiledFunction(comparator, a, b);
|
|
28
|
+
}
|
|
29
|
+
matching(entityName, a, b) {
|
|
30
|
+
const diff = this.diffEntities(entityName, a, b);
|
|
31
|
+
return Utils_1.Utils.getObjectKeysSize(diff) === 0;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Removes ORM specific code from entities and prepares it for serializing. Used before change set computation.
|
|
35
|
+
* References will be mapped to primary keys, collections to arrays of primary keys.
|
|
36
|
+
*/
|
|
37
|
+
prepareEntity(entity) {
|
|
38
|
+
const generator = this.getSnapshotGenerator(entity.constructor.name);
|
|
39
|
+
return Utils_1.Utils.callCompiledFunction(generator, entity);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Maps database columns to properties.
|
|
43
|
+
*/
|
|
44
|
+
mapResult(entityName, result) {
|
|
45
|
+
const mapper = this.getResultMapper(entityName);
|
|
46
|
+
return Utils_1.Utils.callCompiledFunction(mapper, result);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @internal Highly performance-sensitive method.
|
|
50
|
+
*/
|
|
51
|
+
getPkGetter(meta) {
|
|
52
|
+
const exists = this.pkGetters.get(meta.className);
|
|
53
|
+
/* istanbul ignore next */
|
|
54
|
+
if (exists) {
|
|
55
|
+
return exists;
|
|
56
|
+
}
|
|
57
|
+
const lines = [];
|
|
58
|
+
const context = new Map();
|
|
59
|
+
if (meta.primaryKeys.length > 1) {
|
|
60
|
+
lines.push(` const cond = {`);
|
|
61
|
+
meta.primaryKeys.forEach(pk => {
|
|
62
|
+
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
63
|
+
lines.push(` ${pk}: (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) ? entity${this.wrap(pk)}.__helper.getPrimaryKey() : entity${this.wrap(pk)},`);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
lines.push(` ${pk}: entity${this.wrap(pk)},`);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
lines.push(` };`);
|
|
70
|
+
lines.push(` if (${meta.primaryKeys.map(pk => `cond.${pk} == null`).join(' || ')}) return null;`);
|
|
71
|
+
lines.push(` return cond;`);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const pk = meta.primaryKeys[0];
|
|
75
|
+
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
76
|
+
lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) return entity${this.wrap(pk)}.__helper.getPrimaryKey();`);
|
|
77
|
+
}
|
|
78
|
+
lines.push(` return entity${this.wrap(pk)};`);
|
|
79
|
+
}
|
|
80
|
+
const code = `// compiled pk serializer for entity ${meta.className}\n`
|
|
81
|
+
+ `return function(entity) {\n${lines.join('\n')}\n}`;
|
|
82
|
+
const pkSerializer = Utils_1.Utils.createFunction(context, code);
|
|
83
|
+
this.pkGetters.set(meta.className, pkSerializer);
|
|
84
|
+
return pkSerializer;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @internal Highly performance-sensitive method.
|
|
88
|
+
*/
|
|
89
|
+
getPkGetterConverted(meta) {
|
|
90
|
+
const exists = this.pkGettersConverted.get(meta.className);
|
|
91
|
+
/* istanbul ignore next */
|
|
92
|
+
if (exists) {
|
|
93
|
+
return exists;
|
|
94
|
+
}
|
|
95
|
+
const lines = [];
|
|
96
|
+
const context = new Map();
|
|
97
|
+
if (meta.primaryKeys.length > 1) {
|
|
98
|
+
lines.push(` const cond = {`);
|
|
99
|
+
meta.primaryKeys.forEach(pk => {
|
|
100
|
+
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
101
|
+
lines.push(` ${pk}: (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) ? entity${this.wrap(pk)}.__helper.getPrimaryKey(true) : entity${this.wrap(pk)},`);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
if (meta.properties[pk].customType) {
|
|
105
|
+
const convertorKey = this.safeKey(pk);
|
|
106
|
+
context.set(`convertToDatabaseValue_${convertorKey}`, (val) => meta.properties[pk].customType.convertToDatabaseValue(val, this.platform, { mode: 'serialization' }));
|
|
107
|
+
lines.push(` ${pk}: convertToDatabaseValue_${convertorKey}(entity${this.wrap(pk)}),`);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
lines.push(` ${pk}: entity${this.wrap(pk)},`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
lines.push(` };`);
|
|
115
|
+
lines.push(` if (${meta.primaryKeys.map(pk => `cond.${pk} == null`).join(' || ')}) return null;`);
|
|
116
|
+
lines.push(` return cond;`);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
const pk = meta.primaryKeys[0];
|
|
120
|
+
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
121
|
+
lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) return entity${this.wrap(pk)}.__helper.getPrimaryKey(true);`);
|
|
122
|
+
}
|
|
123
|
+
if (meta.properties[pk].customType) {
|
|
124
|
+
const convertorKey = this.safeKey(pk);
|
|
125
|
+
context.set(`convertToDatabaseValue_${convertorKey}`, (val) => meta.properties[pk].customType.convertToDatabaseValue(val, this.platform, { mode: 'serialization' }));
|
|
126
|
+
lines.push(` return convertToDatabaseValue_${convertorKey}(entity${this.wrap(pk)});`);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
lines.push(` return entity${this.wrap(pk)};`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const code = `// compiled pk getter (with converted custom types) for entity ${meta.className}\n`
|
|
133
|
+
+ `return function(entity) {\n${lines.join('\n')}\n}`;
|
|
134
|
+
const pkSerializer = Utils_1.Utils.createFunction(context, code);
|
|
135
|
+
this.pkGettersConverted.set(meta.className, pkSerializer);
|
|
136
|
+
return pkSerializer;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* @internal Highly performance-sensitive method.
|
|
140
|
+
*/
|
|
141
|
+
getPkSerializer(meta) {
|
|
142
|
+
const exists = this.pkSerializers.get(meta.className);
|
|
143
|
+
/* istanbul ignore next */
|
|
144
|
+
if (exists) {
|
|
145
|
+
return exists;
|
|
146
|
+
}
|
|
147
|
+
const lines = [];
|
|
148
|
+
const context = new Map();
|
|
149
|
+
context.set('getCompositeKeyValue', (val) => Utils_1.Utils.flatten(Utils_1.Utils.getCompositeKeyValue(val, meta, true, this.platform)));
|
|
150
|
+
if (meta.primaryKeys.length > 1) {
|
|
151
|
+
lines.push(` const pks = entity.__helper.__pk ? getCompositeKeyValue(entity.__helper.__pk) : [`);
|
|
152
|
+
meta.primaryKeys.forEach(pk => {
|
|
153
|
+
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
154
|
+
lines.push(` (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) ? entity${this.wrap(pk)}.__helper.getSerializedPrimaryKey() : entity${this.wrap(pk)},`);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
lines.push(` entity${this.wrap(pk)},`);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
lines.push(` ];`);
|
|
161
|
+
lines.push(` return pks.join('${Utils_1.Utils.PK_SEPARATOR}');`);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const pk = meta.primaryKeys[0];
|
|
165
|
+
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
166
|
+
lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) return entity${this.wrap(pk)}.__helper.getSerializedPrimaryKey();`);
|
|
167
|
+
}
|
|
168
|
+
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
169
|
+
if (serializedPrimaryKey) {
|
|
170
|
+
lines.push(` return '' + entity.${serializedPrimaryKey.name};`);
|
|
171
|
+
}
|
|
172
|
+
lines.push(` return '' + entity.${meta.primaryKeys[0]};`);
|
|
173
|
+
}
|
|
174
|
+
const code = `// compiled pk serializer for entity ${meta.className}\n`
|
|
175
|
+
+ `return function(entity) {\n${lines.join('\n')}\n}`;
|
|
176
|
+
const pkSerializer = Utils_1.Utils.createFunction(context, code);
|
|
177
|
+
this.pkSerializers.set(meta.className, pkSerializer);
|
|
178
|
+
return pkSerializer;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* @internal Highly performance-sensitive method.
|
|
182
|
+
*/
|
|
183
|
+
getSnapshotGenerator(entityName) {
|
|
184
|
+
const exists = this.snapshotGenerators.get(entityName);
|
|
185
|
+
if (exists) {
|
|
186
|
+
return exists;
|
|
187
|
+
}
|
|
188
|
+
const meta = this.metadata.find(entityName);
|
|
189
|
+
const lines = [];
|
|
190
|
+
const context = new Map();
|
|
191
|
+
context.set('clone', clone_1.clone);
|
|
192
|
+
context.set('cloneEmbeddable', (o) => this.platform.cloneEmbeddable(o)); // do not clone prototypes
|
|
193
|
+
if (meta.discriminatorValue) {
|
|
194
|
+
lines.push(` ret${this.wrap(meta.root.discriminatorColumn)} = '${meta.discriminatorValue}'`);
|
|
195
|
+
}
|
|
196
|
+
const getRootProperty = (prop) => prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop;
|
|
197
|
+
// copy all comparable props, ignore collections and references, process custom types
|
|
198
|
+
meta.comparableProps
|
|
199
|
+
.filter(prop => {
|
|
200
|
+
const root = getRootProperty(prop);
|
|
201
|
+
return prop === root || root.kind !== enums_1.ReferenceKind.EMBEDDED;
|
|
202
|
+
})
|
|
203
|
+
.forEach(prop => lines.push(this.getPropertySnapshot(meta, prop, context, this.wrap(prop.name), this.wrap(prop.name), [prop.name])));
|
|
204
|
+
const code = `return function(entity) {\n const ret = {};\n${lines.join('\n')}\n return ret;\n}`;
|
|
205
|
+
const snapshotGenerator = Utils_1.Utils.createFunction(context, code);
|
|
206
|
+
this.snapshotGenerators.set(entityName, snapshotGenerator);
|
|
207
|
+
return snapshotGenerator;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* @internal Highly performance-sensitive method.
|
|
211
|
+
*/
|
|
212
|
+
getResultMapper(entityName) {
|
|
213
|
+
const exists = this.mappers.get(entityName);
|
|
214
|
+
if (exists) {
|
|
215
|
+
return exists;
|
|
216
|
+
}
|
|
217
|
+
const meta = this.metadata.get(entityName);
|
|
218
|
+
const lines = [];
|
|
219
|
+
const context = new Map();
|
|
220
|
+
const propName = (name, parent = 'result') => parent + this.wrap(name);
|
|
221
|
+
// respects nested composite keys, e.g. `[1, [2, 3]]`
|
|
222
|
+
const createCompositeKeyArray = (prop, fieldNames = prop.fieldNames, idx = 0) => {
|
|
223
|
+
if (!prop.targetMeta) {
|
|
224
|
+
return propName(fieldNames[idx++]);
|
|
225
|
+
}
|
|
226
|
+
const parts = [];
|
|
227
|
+
for (const pk of prop.targetMeta.getPrimaryProps()) {
|
|
228
|
+
parts.push(createCompositeKeyArray(pk, fieldNames, idx));
|
|
229
|
+
idx += pk.fieldNames.length;
|
|
230
|
+
}
|
|
231
|
+
if (parts.length < 2) {
|
|
232
|
+
return parts[0];
|
|
233
|
+
}
|
|
234
|
+
return '[' + parts.join(', ') + ']';
|
|
235
|
+
};
|
|
236
|
+
const tz = this.platform.getTimezone();
|
|
237
|
+
const parseDate = (key, value, padding = '') => {
|
|
238
|
+
lines.push(`${padding} if (${value} == null || ${value} instanceof Date) {`);
|
|
239
|
+
lines.push(`${padding} ${key} = ${value};`);
|
|
240
|
+
if (!tz || tz === 'local') {
|
|
241
|
+
lines.push(`${padding} } else {`);
|
|
242
|
+
lines.push(`${padding} ${key} = parseDate(${value});`);
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
lines.push(`${padding} } else if (typeof ${value} === 'number' || ${value}.includes('+')) {`);
|
|
246
|
+
lines.push(`${padding} ${key} = parseDate(${value});`);
|
|
247
|
+
lines.push(`${padding} } else {`);
|
|
248
|
+
lines.push(`${padding} ${key} = parseDate(${value} + '${tz}');`);
|
|
249
|
+
}
|
|
250
|
+
lines.push(`${padding} }`);
|
|
251
|
+
};
|
|
252
|
+
lines.push(` const mapped = {};`);
|
|
253
|
+
const processProps = (meta) => {
|
|
254
|
+
meta.props.forEach(prop => {
|
|
255
|
+
if (!prop.fieldNames) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (prop.targetMeta && prop.fieldNames.length > 1) {
|
|
259
|
+
lines.push(` if (${prop.fieldNames.map(field => `typeof ${propName(field)} === 'undefined'`).join(' && ')}) {`);
|
|
260
|
+
lines.push(` } else if (${prop.fieldNames.map(field => `${propName(field)} != null`).join(' && ')}) {`);
|
|
261
|
+
lines.push(` ret${this.wrap(prop.name)} = ${createCompositeKeyArray(prop)};`);
|
|
262
|
+
lines.push(...prop.fieldNames.map(field => ` ${propName(field, 'mapped')} = true;`));
|
|
263
|
+
lines.push(` } else if (${prop.fieldNames.map(field => `${propName(field)} == null`).join(' && ')}) {\n ret${this.wrap(prop.name)} = null;`);
|
|
264
|
+
lines.push(...prop.fieldNames.map(field => ` ${propName(field, 'mapped')} = true;`), ' }');
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (prop.embedded && (meta.embeddable || meta.properties[prop.embedded[0]].object)) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (prop.runtimeType === 'boolean') {
|
|
271
|
+
lines.push(` if (typeof ${propName(prop.fieldNames[0])} !== 'undefined') {`);
|
|
272
|
+
lines.push(` ret${this.wrap(prop.name)} = ${propName(prop.fieldNames[0])} == null ? ${propName(prop.fieldNames[0])} : !!${propName(prop.fieldNames[0])};`);
|
|
273
|
+
lines.push(` ${propName(prop.fieldNames[0], 'mapped')} = true;`);
|
|
274
|
+
lines.push(` }`);
|
|
275
|
+
}
|
|
276
|
+
else if (prop.runtimeType === 'Date') {
|
|
277
|
+
lines.push(` if (typeof ${propName(prop.fieldNames[0])} !== 'undefined') {`);
|
|
278
|
+
context.set('parseDate', (value) => this.platform.parseDate(value));
|
|
279
|
+
parseDate('ret' + this.wrap(prop.name), propName(prop.fieldNames[0]));
|
|
280
|
+
lines.push(` ${propName(prop.fieldNames[0], 'mapped')} = true;`);
|
|
281
|
+
lines.push(` }`);
|
|
282
|
+
}
|
|
283
|
+
else if (prop.kind === enums_1.ReferenceKind.EMBEDDED && (prop.object || meta.embeddable)) {
|
|
284
|
+
const idx = this.tmpIndex++;
|
|
285
|
+
context.set(`mapEmbeddedResult_${idx}`, (data) => {
|
|
286
|
+
const item = (0, Utils_1.parseJsonSafe)(data);
|
|
287
|
+
if (Array.isArray(item)) {
|
|
288
|
+
return item.map(row => row == null ? row : this.getResultMapper(prop.type)(row));
|
|
289
|
+
}
|
|
290
|
+
return item == null ? item : this.getResultMapper(prop.type)(item);
|
|
291
|
+
});
|
|
292
|
+
lines.push(` if (typeof ${propName(prop.fieldNames[0])} !== 'undefined') {`);
|
|
293
|
+
lines.push(` ret${this.wrap(prop.name)} = ${propName(prop.fieldNames[0])} == null ? ${propName(prop.fieldNames[0])} : mapEmbeddedResult_${idx}(${propName(prop.fieldNames[0])});`);
|
|
294
|
+
lines.push(` ${propName(prop.fieldNames[0], 'mapped')} = true;`);
|
|
295
|
+
lines.push(` }`);
|
|
296
|
+
}
|
|
297
|
+
else if (prop.kind !== enums_1.ReferenceKind.EMBEDDED) {
|
|
298
|
+
lines.push(` if (typeof ${propName(prop.fieldNames[0])} !== 'undefined') {`);
|
|
299
|
+
lines.push(` ret${this.wrap(prop.name)} = ${propName(prop.fieldNames[0])};`);
|
|
300
|
+
lines.push(` ${propName(prop.fieldNames[0], 'mapped')} = true;`);
|
|
301
|
+
lines.push(` }`);
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
};
|
|
305
|
+
if (meta.root.discriminatorColumn && !meta.root.embeddable) {
|
|
306
|
+
for (const [value, className] of Object.entries(meta.root.discriminatorMap)) {
|
|
307
|
+
const meta = this.metadata.get(className);
|
|
308
|
+
lines.push(`if (${propName(meta.root.discriminatorColumn)} === '${value}') {`);
|
|
309
|
+
processProps(meta);
|
|
310
|
+
lines.push(`}`);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
processProps(meta);
|
|
315
|
+
}
|
|
316
|
+
lines.push(` for (let k in result) { if (Object.hasOwn(result, k) && !mapped[k]) ret[k] = result[k]; }`);
|
|
317
|
+
const code = `// compiled mapper for entity ${meta.className}\n`
|
|
318
|
+
+ `return function(result) {\n const ret = {};\n${lines.join('\n')}\n return ret;\n}`;
|
|
319
|
+
const resultMapper = Utils_1.Utils.createFunction(context, code);
|
|
320
|
+
this.mappers.set(entityName, resultMapper);
|
|
321
|
+
return resultMapper;
|
|
322
|
+
}
|
|
323
|
+
getPropertyCondition(path) {
|
|
324
|
+
const parts = path.slice(); // copy first
|
|
325
|
+
if (parts.length > 1) {
|
|
326
|
+
parts.pop();
|
|
327
|
+
}
|
|
328
|
+
let tail = '';
|
|
329
|
+
return parts
|
|
330
|
+
.map(k => {
|
|
331
|
+
if (k.match(/^\[idx_\d+]$/)) {
|
|
332
|
+
tail += k;
|
|
333
|
+
return '';
|
|
334
|
+
}
|
|
335
|
+
const mapped = `typeof entity${tail ? '.' + tail : ''}${this.wrap(k)} !== 'undefined'`;
|
|
336
|
+
tail += tail ? ('.' + k) : k;
|
|
337
|
+
return mapped;
|
|
338
|
+
})
|
|
339
|
+
.filter(k => k)
|
|
340
|
+
.join(' && ');
|
|
341
|
+
}
|
|
342
|
+
getEmbeddedArrayPropertySnapshot(meta, prop, context, level, path, dataKey) {
|
|
343
|
+
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
344
|
+
const ret = [];
|
|
345
|
+
const padding = ' '.repeat(level * 2);
|
|
346
|
+
const idx = this.tmpIndex++;
|
|
347
|
+
ret.push(`${padding}if (Array.isArray(entity${entityKey})) {`);
|
|
348
|
+
ret.push(`${padding} ret${dataKey} = [];`);
|
|
349
|
+
ret.push(`${padding} entity${entityKey}.forEach((_, idx_${idx}) => {`);
|
|
350
|
+
ret.push(this.getEmbeddedPropertySnapshot(meta, prop, context, level + 2, [...path, `[idx_${idx}]`], `${dataKey}[idx_${idx}]`, true));
|
|
351
|
+
ret.push(`${padding} });`);
|
|
352
|
+
if (this.shouldSerialize(prop, dataKey)) {
|
|
353
|
+
ret.push(`${padding} ret${dataKey} = cloneEmbeddable(ret${dataKey});`);
|
|
354
|
+
}
|
|
355
|
+
ret.push(`${padding}}`);
|
|
356
|
+
return ret.join('\n');
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* we need to serialize only object embeddables, and only the top level ones, so root object embeddable
|
|
360
|
+
* properties and first child nested object embeddables with inlined parent
|
|
361
|
+
*/
|
|
362
|
+
shouldSerialize(prop, dataKey) {
|
|
363
|
+
dataKey = dataKey.replace(/^\./, '');
|
|
364
|
+
const contains = (str, re) => (str.match(re) || []).length > 0;
|
|
365
|
+
const a = contains(dataKey, /\./g);
|
|
366
|
+
const b = contains(dataKey, /\[/g);
|
|
367
|
+
return !!prop.object && !(a || b);
|
|
368
|
+
}
|
|
369
|
+
getEmbeddedPropertySnapshot(meta, prop, context, level, path, dataKey, object = prop.object) {
|
|
370
|
+
const padding = ' '.repeat(level * 2);
|
|
371
|
+
let ret = `${level === 1 ? '' : '\n'}`;
|
|
372
|
+
if (object) {
|
|
373
|
+
const nullCond = `entity${path.map(k => this.wrap(k)).join('')} === null`;
|
|
374
|
+
ret += `${padding}if (${nullCond}) ret${dataKey} = null;\n`;
|
|
375
|
+
}
|
|
376
|
+
const cond = `entity${path.map(k => this.wrap(k)).join('')} != null`;
|
|
377
|
+
ret += `${padding}if (${cond}) {\n`;
|
|
378
|
+
if (object) {
|
|
379
|
+
ret += `${padding} ret${dataKey} = {};\n`;
|
|
380
|
+
}
|
|
381
|
+
function shouldProcessCustomType(childProp) {
|
|
382
|
+
if (!childProp.customType) {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
if (childProp.customType instanceof JsonType_1.JsonType) {
|
|
386
|
+
return !prop.object;
|
|
387
|
+
}
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
ret += meta.props.filter(p => p.embedded?.[0] === prop.name).map(childProp => {
|
|
391
|
+
const childDataKey = meta.embeddable || prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
392
|
+
const childEntityKey = [...path, childProp.embedded[1]].map(k => this.wrap(k)).join('');
|
|
393
|
+
const childCond = `typeof entity${childEntityKey} !== 'undefined'`;
|
|
394
|
+
if (childProp.kind === enums_1.ReferenceKind.EMBEDDED) {
|
|
395
|
+
return this.getPropertySnapshot(meta, childProp, context, childDataKey, childEntityKey, [...path, childProp.embedded[1]], level + 1, prop.object);
|
|
396
|
+
}
|
|
397
|
+
if (childProp.kind !== enums_1.ReferenceKind.SCALAR) {
|
|
398
|
+
return this.getPropertySnapshot(meta, childProp, context, childDataKey, childEntityKey, [...path, childProp.embedded[1]], level, prop.object)
|
|
399
|
+
.split('\n').map(l => padding + l).join('\n');
|
|
400
|
+
}
|
|
401
|
+
if (shouldProcessCustomType(childProp)) {
|
|
402
|
+
const convertorKey = this.safeKey(childProp.name);
|
|
403
|
+
context.set(`convertToDatabaseValue_${convertorKey}`, (val) => childProp.customType.convertToDatabaseValue(val, this.platform, { mode: 'serialization' }));
|
|
404
|
+
if (['number', 'string', 'boolean', 'bigint'].includes(childProp.customType.compareAsType().toLowerCase())) {
|
|
405
|
+
return `${padding} if (${childCond}) ret${childDataKey} = convertToDatabaseValue_${convertorKey}(entity${childEntityKey});`;
|
|
406
|
+
}
|
|
407
|
+
return `${padding} if (${childCond}) ret${childDataKey} = clone(convertToDatabaseValue_${convertorKey}(entity${childEntityKey}));`;
|
|
408
|
+
}
|
|
409
|
+
return `${padding} if (${childCond}) ret${childDataKey} = clone(entity${childEntityKey});`;
|
|
410
|
+
}).join('\n') + `\n`;
|
|
411
|
+
if (this.shouldSerialize(prop, dataKey)) {
|
|
412
|
+
return `${ret + padding} ret${dataKey} = cloneEmbeddable(ret${dataKey});\n${padding}}`;
|
|
413
|
+
}
|
|
414
|
+
return `${ret}${padding}}`;
|
|
415
|
+
}
|
|
416
|
+
getPropertySnapshot(meta, prop, context, dataKey, entityKey, path, level = 1, object) {
|
|
417
|
+
const convertorKey = this.safeKey(prop.name);
|
|
418
|
+
const unwrap = prop.ref ? '?.unwrap()' : '';
|
|
419
|
+
let ret = ` if (${this.getPropertyCondition(path)}) {\n`;
|
|
420
|
+
if (['number', 'string', 'boolean'].includes(prop.type.toLowerCase())) {
|
|
421
|
+
return ret + ` ret${dataKey} = entity${entityKey}${unwrap};\n }\n`;
|
|
422
|
+
}
|
|
423
|
+
if (prop.kind === enums_1.ReferenceKind.EMBEDDED) {
|
|
424
|
+
if (prop.array) {
|
|
425
|
+
return this.getEmbeddedArrayPropertySnapshot(meta, prop, context, level, path, dataKey) + '\n';
|
|
426
|
+
}
|
|
427
|
+
return this.getEmbeddedPropertySnapshot(meta, prop, context, level, path, dataKey, object) + '\n';
|
|
428
|
+
}
|
|
429
|
+
if (prop.kind === enums_1.ReferenceKind.ONE_TO_ONE || prop.kind === enums_1.ReferenceKind.MANY_TO_ONE) {
|
|
430
|
+
if (prop.mapToPk) {
|
|
431
|
+
if (prop.customType) {
|
|
432
|
+
context.set(`convertToDatabaseValue_${convertorKey}`, (val) => prop.customType.convertToDatabaseValue(val, this.platform, { mode: 'serialization' }));
|
|
433
|
+
ret += ` ret${dataKey} = convertToDatabaseValue_${convertorKey}(entity${entityKey});\n`;
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
ret += ` ret${dataKey} = entity${entityKey};\n`;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
const toArray = (val) => {
|
|
441
|
+
if (Utils_1.Utils.isPlainObject(val)) {
|
|
442
|
+
return Object.values(val).map(v => toArray(v));
|
|
443
|
+
}
|
|
444
|
+
return val;
|
|
445
|
+
};
|
|
446
|
+
context.set('toArray', toArray);
|
|
447
|
+
ret += ` if (entity${entityKey} === null) {\n`;
|
|
448
|
+
ret += ` ret${dataKey} = null;\n`;
|
|
449
|
+
ret += ` } else if (typeof entity${entityKey} !== 'undefined') {\n`;
|
|
450
|
+
ret += ` ret${dataKey} = toArray(entity${entityKey}.__helper.getPrimaryKey(true));\n`;
|
|
451
|
+
ret += ` }\n`;
|
|
452
|
+
}
|
|
453
|
+
return ret + ' }\n';
|
|
454
|
+
}
|
|
455
|
+
if (prop.customType) {
|
|
456
|
+
context.set(`convertToDatabaseValue_${convertorKey}`, (val) => prop.customType.convertToDatabaseValue(val, this.platform, { mode: 'serialization' }));
|
|
457
|
+
if (['number', 'string', 'boolean', 'bigint'].includes(prop.customType.compareAsType().toLowerCase())) {
|
|
458
|
+
return ret + ` ret${dataKey} = convertToDatabaseValue_${convertorKey}(entity${entityKey}${unwrap});\n }\n`;
|
|
459
|
+
}
|
|
460
|
+
return ret + ` ret${dataKey} = clone(convertToDatabaseValue_${convertorKey}(entity${entityKey}${unwrap}));\n }\n`;
|
|
461
|
+
}
|
|
462
|
+
if (prop.runtimeType === 'Date') {
|
|
463
|
+
context.set('processDateProperty', this.platform.processDateProperty.bind(this.platform));
|
|
464
|
+
return ret + ` ret${dataKey} = clone(processDateProperty(entity${entityKey}${unwrap}));\n }\n`;
|
|
465
|
+
}
|
|
466
|
+
return ret + ` ret${dataKey} = clone(entity${entityKey}${unwrap});\n }\n`;
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* @internal Highly performance-sensitive method.
|
|
470
|
+
*/
|
|
471
|
+
getEntityComparator(entityName) {
|
|
472
|
+
const exists = this.comparators.get(entityName);
|
|
473
|
+
if (exists) {
|
|
474
|
+
return exists;
|
|
475
|
+
}
|
|
476
|
+
const meta = this.metadata.find(entityName);
|
|
477
|
+
const lines = [];
|
|
478
|
+
const context = new Map();
|
|
479
|
+
context.set('compareArrays', Utils_1.compareArrays);
|
|
480
|
+
context.set('compareBooleans', Utils_1.compareBooleans);
|
|
481
|
+
context.set('compareBuffers', Utils_1.compareBuffers);
|
|
482
|
+
context.set('compareObjects', Utils_1.compareObjects);
|
|
483
|
+
context.set('equals', Utils_1.equals);
|
|
484
|
+
meta.comparableProps.forEach(prop => {
|
|
485
|
+
lines.push(this.getPropertyComparator(prop, context));
|
|
486
|
+
});
|
|
487
|
+
const code = `// compiled comparator for entity ${meta.className}\n`
|
|
488
|
+
+ `return function(last, current) {\n const diff = {};\n${lines.join('\n')}\n return diff;\n}`;
|
|
489
|
+
const comparator = Utils_1.Utils.createFunction(context, code);
|
|
490
|
+
this.comparators.set(entityName, comparator);
|
|
491
|
+
return comparator;
|
|
492
|
+
}
|
|
493
|
+
getGenericComparator(prop, cond) {
|
|
494
|
+
return ` if (current${prop} == null && last${prop} == null) {\n\n` +
|
|
495
|
+
` } else if ((current${prop} != null && last${prop} == null) || (current${prop} == null && last${prop} != null)) {\n` +
|
|
496
|
+
` diff${prop} = current${prop};\n` +
|
|
497
|
+
` } else if (${cond}) {\n` +
|
|
498
|
+
` diff${prop} = current${prop};\n` +
|
|
499
|
+
` }\n`;
|
|
500
|
+
}
|
|
501
|
+
getPropertyComparator(prop, context) {
|
|
502
|
+
let type = prop.type.toLowerCase();
|
|
503
|
+
if (prop.kind !== enums_1.ReferenceKind.SCALAR && prop.kind !== enums_1.ReferenceKind.EMBEDDED) {
|
|
504
|
+
const meta2 = this.metadata.find(prop.type);
|
|
505
|
+
if (meta2.primaryKeys.length > 1) {
|
|
506
|
+
type = 'array';
|
|
507
|
+
}
|
|
508
|
+
else {
|
|
509
|
+
type = meta2.properties[meta2.primaryKeys[0]].type.toLowerCase();
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
if (prop.customType) {
|
|
513
|
+
if (prop.customType.compareValues) {
|
|
514
|
+
const idx = this.tmpIndex++;
|
|
515
|
+
context.set(`compareValues_${idx}`, (a, b) => prop.customType.compareValues(a, b));
|
|
516
|
+
return this.getGenericComparator(this.wrap(prop.name), `!compareValues_${idx}(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
517
|
+
}
|
|
518
|
+
type = prop.customType.compareAsType().toLowerCase();
|
|
519
|
+
}
|
|
520
|
+
if (type.endsWith('[]')) {
|
|
521
|
+
type = 'array';
|
|
522
|
+
}
|
|
523
|
+
if (['string', 'number', 'bigint'].includes(type)) {
|
|
524
|
+
return this.getGenericComparator(this.wrap(prop.name), `last${this.wrap(prop.name)} !== current${this.wrap(prop.name)}`);
|
|
525
|
+
}
|
|
526
|
+
if (type === 'boolean') {
|
|
527
|
+
return this.getGenericComparator(this.wrap(prop.name), `!compareBooleans(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
528
|
+
}
|
|
529
|
+
if (['array'].includes(type) || type.endsWith('[]')) {
|
|
530
|
+
return this.getGenericComparator(this.wrap(prop.name), `!compareArrays(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
531
|
+
}
|
|
532
|
+
if (['buffer', 'uint8array'].includes(type)) {
|
|
533
|
+
return this.getGenericComparator(this.wrap(prop.name), `!compareBuffers(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
534
|
+
}
|
|
535
|
+
if (['date'].includes(type)) {
|
|
536
|
+
return this.getGenericComparator(this.wrap(prop.name), `last${this.wrap(prop.name)}.valueOf() !== current${this.wrap(prop.name)}.valueOf()`);
|
|
537
|
+
}
|
|
538
|
+
if (['objectid'].includes(type)) {
|
|
539
|
+
// We might be comparing PK to object, in case we compare with cached data of populated entity
|
|
540
|
+
// in such case we just ignore the comparison and fallback to `equals()` (which will still mark
|
|
541
|
+
// it as not equal as we compare PK to plain object).
|
|
542
|
+
const cond = `last${this.wrap(prop.name)}.toHexString?.() !== current${this.wrap(prop.name)}.toHexString?.()`;
|
|
543
|
+
return this.getGenericComparator(this.wrap(prop.name), cond);
|
|
544
|
+
}
|
|
545
|
+
return this.getGenericComparator(this.wrap(prop.name), `!equals(last${this.wrap(prop.name)}, current${this.wrap(prop.name)})`);
|
|
546
|
+
}
|
|
547
|
+
wrap(key) {
|
|
548
|
+
if (key.match(/^\[.*]$/)) {
|
|
549
|
+
return key;
|
|
550
|
+
}
|
|
551
|
+
return key.match(/^\w+$/) ? `.${key}` : `['${key}']`;
|
|
552
|
+
}
|
|
553
|
+
safeKey(key) {
|
|
554
|
+
return key.replace(/\W/g, '_');
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* perf: used to generate list of comparable properties during discovery, so we speed up the runtime comparison
|
|
558
|
+
*/
|
|
559
|
+
static isComparable(prop, root) {
|
|
560
|
+
const virtual = prop.persist === false;
|
|
561
|
+
const inverse = prop.kind === enums_1.ReferenceKind.ONE_TO_ONE && !prop.owner;
|
|
562
|
+
// const discriminator = prop.name === root.discriminatorColumn;
|
|
563
|
+
const collection = prop.kind === enums_1.ReferenceKind.ONE_TO_MANY || prop.kind === enums_1.ReferenceKind.MANY_TO_MANY;
|
|
564
|
+
// return !virtual && !collection && !inverse && !discriminator && !prop.version;
|
|
565
|
+
return !virtual && !collection && !inverse && !prop.version;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
exports.EntityComparator = EntityComparator;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Dictionary, EntityMetadata, EntityProperty, FilterDef, FilterQuery } from '../typings';
|
|
2
|
+
import type { Platform } from '../platforms';
|
|
3
|
+
import type { MetadataStorage } from '../metadata/MetadataStorage';
|
|
4
|
+
export declare class QueryHelper {
|
|
5
|
+
static readonly SUPPORTED_OPERATORS: string[];
|
|
6
|
+
static processParams(params: unknown): any;
|
|
7
|
+
static processObjectParams<T extends object>(params?: T): T;
|
|
8
|
+
static inlinePrimaryKeyObjects<T extends object>(where: Dictionary, meta: EntityMetadata<T>, metadata: MetadataStorage, key?: string): boolean;
|
|
9
|
+
static processWhere<T extends object>(options: ProcessWhereOptions<T>): FilterQuery<T>;
|
|
10
|
+
static getActiveFilters(entityName: string, options: Dictionary<boolean | Dictionary> | string[] | boolean, filters: Dictionary<FilterDef>): FilterDef[];
|
|
11
|
+
static isFilterActive(entityName: string, filterName: string, filter: FilterDef, options: Dictionary<boolean | Dictionary>): boolean;
|
|
12
|
+
static processCustomType<T extends object>(prop: EntityProperty<T>, cond: FilterQuery<T>, platform: Platform, key?: string, fromQuery?: boolean): FilterQuery<T>;
|
|
13
|
+
private static isSupportedOperator;
|
|
14
|
+
private static processJsonCondition;
|
|
15
|
+
static findProperty<T>(fieldName: string, options: ProcessWhereOptions<T>): EntityProperty<T> | undefined;
|
|
16
|
+
}
|
|
17
|
+
interface ProcessWhereOptions<T> {
|
|
18
|
+
where: FilterQuery<T>;
|
|
19
|
+
entityName: string;
|
|
20
|
+
metadata: MetadataStorage;
|
|
21
|
+
platform: Platform;
|
|
22
|
+
aliased?: boolean;
|
|
23
|
+
aliasMap?: Dictionary<string>;
|
|
24
|
+
convertCustomTypes?: boolean;
|
|
25
|
+
root?: boolean;
|
|
26
|
+
type?: 'where' | 'orderBy';
|
|
27
|
+
}
|
|
28
|
+
export {};
|