@mikro-orm/core 6.6.10-dev.0 → 6.6.10-dev.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/entity/ArrayCollection.d.ts +1 -0
- package/entity/ArrayCollection.js +5 -3
- package/entity/EntityFactory.js +1 -1
- package/entity/EntityHelper.d.ts +1 -0
- package/entity/EntityHelper.js +5 -1
- package/entity/Reference.d.ts +1 -0
- package/entity/Reference.js +16 -9
- package/hydration/ObjectHydrator.js +3 -1
- package/package.json +2 -2
- package/platforms/Platform.d.ts +1 -1
- package/platforms/Platform.js +4 -3
- package/typings.d.ts +2 -1
- package/typings.js +2 -1
- package/utils/EntityComparator.js +9 -6
- package/utils/RawQueryFragment.d.ts +4 -0
- package/utils/RawQueryFragment.js +10 -3
- package/utils/Utils.d.ts +2 -2
- package/utils/Utils.js +13 -6
|
@@ -110,6 +110,7 @@ export declare class ArrayCollection<T extends object, O extends object> {
|
|
|
110
110
|
protected propagateToOwningSide(item: T, method: 'add' | 'remove' | 'takeSnapshot'): void;
|
|
111
111
|
protected shouldPropagateToCollection(collection: ArrayCollection<O, T>, method: 'add' | 'remove' | 'takeSnapshot'): boolean;
|
|
112
112
|
protected incrementCount(value: number): void;
|
|
113
|
+
static isCollection<T extends object, O extends object>(item: any): item is ArrayCollection<T, O>;
|
|
113
114
|
/** @ignore */
|
|
114
115
|
[inspect.custom](depth?: number): string;
|
|
115
116
|
}
|
|
@@ -7,6 +7,7 @@ const wrap_1 = require("./wrap");
|
|
|
7
7
|
const errors_1 = require("../errors");
|
|
8
8
|
const enums_1 = require("../enums");
|
|
9
9
|
const Utils_1 = require("../utils/Utils");
|
|
10
|
+
const collectionSymbol = Symbol('Collection');
|
|
10
11
|
class ArrayCollection {
|
|
11
12
|
owner;
|
|
12
13
|
items = new Set();
|
|
@@ -18,6 +19,7 @@ class ArrayCollection {
|
|
|
18
19
|
_property;
|
|
19
20
|
constructor(owner, items) {
|
|
20
21
|
this.owner = owner;
|
|
22
|
+
Object.defineProperty(this, collectionSymbol, { value: true, enumerable: false });
|
|
21
23
|
/* istanbul ignore next */
|
|
22
24
|
if (items) {
|
|
23
25
|
let i = 0;
|
|
@@ -395,6 +397,9 @@ class ArrayCollection {
|
|
|
395
397
|
this._count += value;
|
|
396
398
|
}
|
|
397
399
|
}
|
|
400
|
+
static isCollection(item) {
|
|
401
|
+
return item != null && Object.hasOwn(item, collectionSymbol);
|
|
402
|
+
}
|
|
398
403
|
/** @ignore */
|
|
399
404
|
[node_util_1.inspect.custom](depth = 2) {
|
|
400
405
|
const object = { ...this };
|
|
@@ -406,6 +411,3 @@ class ArrayCollection {
|
|
|
406
411
|
}
|
|
407
412
|
}
|
|
408
413
|
exports.ArrayCollection = ArrayCollection;
|
|
409
|
-
Object.defineProperties(ArrayCollection.prototype, {
|
|
410
|
-
__collection: { value: true, enumerable: false, writable: false },
|
|
411
|
-
});
|
package/entity/EntityFactory.js
CHANGED
|
@@ -30,7 +30,7 @@ class EntityFactory {
|
|
|
30
30
|
create(entityName, data, options = {}) {
|
|
31
31
|
data = Reference_1.Reference.unwrapReference(data);
|
|
32
32
|
options.initialized ??= true;
|
|
33
|
-
if (data
|
|
33
|
+
if (EntityHelper_1.EntityHelper.isEntity(data)) {
|
|
34
34
|
return data;
|
|
35
35
|
}
|
|
36
36
|
entityName = Utils_1.Utils.className(entityName);
|
package/entity/EntityHelper.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { type EntityMetadata, type EntityProperty, type IHydrator } from '../typ
|
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
6
|
export declare class EntityHelper {
|
|
7
|
+
static isEntity(data: any): boolean;
|
|
7
8
|
static decorate<T extends object>(meta: EntityMetadata<T>, em: EntityManager): void;
|
|
8
9
|
/**
|
|
9
10
|
* As a performance optimization, we create entity state methods in a lazy manner. We first add
|
package/entity/EntityHelper.js
CHANGED
|
@@ -9,10 +9,14 @@ const Utils_1 = require("../utils/Utils");
|
|
|
9
9
|
const WrappedEntity_1 = require("./WrappedEntity");
|
|
10
10
|
const enums_1 = require("../enums");
|
|
11
11
|
const wrap_1 = require("./wrap");
|
|
12
|
+
const entitySymbol = Symbol('Entity');
|
|
12
13
|
/**
|
|
13
14
|
* @internal
|
|
14
15
|
*/
|
|
15
16
|
class EntityHelper {
|
|
17
|
+
static isEntity(data) {
|
|
18
|
+
return data != null && typeof data === 'object' && !!data[entitySymbol];
|
|
19
|
+
}
|
|
16
20
|
static decorate(meta, em) {
|
|
17
21
|
const fork = em.fork(); // use fork so we can access `EntityFactory`
|
|
18
22
|
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
@@ -58,7 +62,7 @@ class EntityHelper {
|
|
|
58
62
|
static defineBaseProperties(meta, prototype, em) {
|
|
59
63
|
const helperParams = meta.embeddable || meta.virtual ? [] : [em.getComparator().getPkGetter(meta), em.getComparator().getPkSerializer(meta), em.getComparator().getPkGetterConverted(meta)];
|
|
60
64
|
Object.defineProperties(prototype, {
|
|
61
|
-
|
|
65
|
+
[entitySymbol]: { value: !meta.embeddable, enumerable: false, configurable: true },
|
|
62
66
|
__meta: { value: meta, configurable: true },
|
|
63
67
|
__config: { value: em.config, configurable: true },
|
|
64
68
|
__platform: { value: em.getPlatform(), configurable: true },
|
package/entity/Reference.d.ts
CHANGED
|
@@ -66,6 +66,7 @@ export declare class ScalarReference<Value> {
|
|
|
66
66
|
bind<Entity extends object>(entity: Entity, property: EntityKey<Entity>): void;
|
|
67
67
|
unwrap(): Value | undefined;
|
|
68
68
|
isInitialized(): boolean;
|
|
69
|
+
static isScalarReference(data: any): data is ScalarReference<any>;
|
|
69
70
|
/** @ignore */
|
|
70
71
|
[inspect.custom](): string;
|
|
71
72
|
}
|
package/entity/Reference.js
CHANGED
|
@@ -6,13 +6,18 @@ exports.rel = rel;
|
|
|
6
6
|
const node_util_1 = require("node:util");
|
|
7
7
|
const enums_1 = require("../enums");
|
|
8
8
|
const wrap_1 = require("./wrap");
|
|
9
|
-
const
|
|
9
|
+
const DataloaderUtils_1 = require("../utils/DataloaderUtils");
|
|
10
|
+
const QueryHelper_1 = require("../utils/QueryHelper");
|
|
11
|
+
const Utils_1 = require("../utils/Utils");
|
|
10
12
|
const errors_1 = require("../errors");
|
|
13
|
+
const referenceSymbol = Symbol('Reference');
|
|
14
|
+
const scalarReferenceSymbol = Symbol('ScalarReference');
|
|
11
15
|
class Reference {
|
|
12
16
|
entity;
|
|
13
17
|
property;
|
|
14
18
|
constructor(entity) {
|
|
15
19
|
this.entity = entity;
|
|
20
|
+
Object.defineProperty(this, referenceSymbol, { value: true, enumerable: false });
|
|
16
21
|
this.set(entity);
|
|
17
22
|
const meta = (0, wrap_1.helper)(this.entity).__meta;
|
|
18
23
|
meta.primaryKeys.forEach(primaryKey => {
|
|
@@ -63,7 +68,7 @@ class Reference {
|
|
|
63
68
|
* Checks whether the argument is instance of `Reference` wrapper.
|
|
64
69
|
*/
|
|
65
70
|
static isReference(data) {
|
|
66
|
-
return data &&
|
|
71
|
+
return data != null && Object.hasOwn(data, referenceSymbol);
|
|
67
72
|
}
|
|
68
73
|
/**
|
|
69
74
|
* Wraps the entity in a `Reference` wrapper if the property is defined as `ref`.
|
|
@@ -92,12 +97,12 @@ class Reference {
|
|
|
92
97
|
if (!wrapped.__em) {
|
|
93
98
|
return this.entity;
|
|
94
99
|
}
|
|
95
|
-
options = { ...options, filters:
|
|
100
|
+
options = { ...options, filters: QueryHelper_1.QueryHelper.mergePropertyFilters(this.property?.filters, options.filters) };
|
|
96
101
|
if (this.isInitialized() && !options.refresh && options.populate) {
|
|
97
102
|
await wrapped.__em.populate(this.entity, options.populate, options);
|
|
98
103
|
}
|
|
99
104
|
if (!this.isInitialized() || options.refresh) {
|
|
100
|
-
if (options.dataloader ?? [enums_1.DataloaderType.ALL, enums_1.DataloaderType.REFERENCE].includes(
|
|
105
|
+
if (options.dataloader ?? [enums_1.DataloaderType.ALL, enums_1.DataloaderType.REFERENCE].includes(DataloaderUtils_1.DataloaderUtils.getDataloaderType(wrapped.__em.config.get('dataloader')))) {
|
|
101
106
|
// eslint-disable-next-line dot-notation
|
|
102
107
|
return wrapped.__em['refLoader'].load([this, options]);
|
|
103
108
|
}
|
|
@@ -173,6 +178,7 @@ class ScalarReference {
|
|
|
173
178
|
constructor(value, initialized = value != null) {
|
|
174
179
|
this.value = value;
|
|
175
180
|
this.initialized = initialized;
|
|
181
|
+
Object.defineProperty(this, scalarReferenceSymbol, { value: true, enumerable: false });
|
|
176
182
|
}
|
|
177
183
|
/**
|
|
178
184
|
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
@@ -219,6 +225,9 @@ class ScalarReference {
|
|
|
219
225
|
isInitialized() {
|
|
220
226
|
return this.initialized;
|
|
221
227
|
}
|
|
228
|
+
static isScalarReference(data) {
|
|
229
|
+
return data != null && typeof data === 'object' && Object.hasOwn(data, scalarReferenceSymbol);
|
|
230
|
+
}
|
|
222
231
|
/* istanbul ignore next */
|
|
223
232
|
/** @ignore */
|
|
224
233
|
[node_util_1.inspect.custom]() {
|
|
@@ -227,7 +236,6 @@ class ScalarReference {
|
|
|
227
236
|
}
|
|
228
237
|
exports.ScalarReference = ScalarReference;
|
|
229
238
|
Object.defineProperties(Reference.prototype, {
|
|
230
|
-
__reference: { value: true, enumerable: false },
|
|
231
239
|
__meta: { get() { return this.entity.__meta; } },
|
|
232
240
|
__platform: { get() { return this.entity.__platform; } },
|
|
233
241
|
__helper: { get() { return this.entity.__helper; } },
|
|
@@ -235,7 +243,6 @@ Object.defineProperties(Reference.prototype, {
|
|
|
235
243
|
get: { get() { return () => this.entity; } },
|
|
236
244
|
});
|
|
237
245
|
Object.defineProperties(ScalarReference.prototype, {
|
|
238
|
-
__scalarReference: { value: true, enumerable: false },
|
|
239
246
|
$: { get() { return this.value; } },
|
|
240
247
|
get: { get() { return () => this.value; } },
|
|
241
248
|
});
|
|
@@ -246,10 +253,10 @@ function ref(entityOrType, pk) {
|
|
|
246
253
|
if (entityOrType == null) {
|
|
247
254
|
return entityOrType;
|
|
248
255
|
}
|
|
249
|
-
if (
|
|
256
|
+
if (Utils_1.Utils.isEntity(entityOrType, true)) {
|
|
250
257
|
return (0, wrap_1.helper)(entityOrType).toReference();
|
|
251
258
|
}
|
|
252
|
-
if (
|
|
259
|
+
if (Utils_1.Utils.isEntity(pk, true)) {
|
|
253
260
|
return (0, wrap_1.helper)(pk).toReference();
|
|
254
261
|
}
|
|
255
262
|
if (arguments.length === 1) {
|
|
@@ -264,7 +271,7 @@ function ref(entityOrType, pk) {
|
|
|
264
271
|
* shortcut for `Reference.createNakedFromPK(entityType, pk)`
|
|
265
272
|
*/
|
|
266
273
|
function rel(entityType, pk) {
|
|
267
|
-
if (pk == null ||
|
|
274
|
+
if (pk == null || Utils_1.Utils.isEntity(pk)) {
|
|
268
275
|
return pk;
|
|
269
276
|
}
|
|
270
277
|
return Reference.createNakedFromPK(entityType, pk);
|
|
@@ -4,6 +4,7 @@ exports.ObjectHydrator = void 0;
|
|
|
4
4
|
const Hydrator_1 = require("./Hydrator");
|
|
5
5
|
const Collection_1 = require("../entity/Collection");
|
|
6
6
|
const Reference_1 = require("../entity/Reference");
|
|
7
|
+
const EntityHelper_1 = require("../entity/EntityHelper");
|
|
7
8
|
const Utils_1 = require("../utils/Utils");
|
|
8
9
|
const enums_1 = require("../enums");
|
|
9
10
|
const RawQueryFragment_1 = require("../utils/RawQueryFragment");
|
|
@@ -50,6 +51,7 @@ class ObjectHydrator extends Hydrator_1.Hydrator {
|
|
|
50
51
|
const context = new Map();
|
|
51
52
|
const props = this.getProperties(meta, type);
|
|
52
53
|
context.set('isPrimaryKey', Utils_1.Utils.isPrimaryKey);
|
|
54
|
+
context.set('isEntity', EntityHelper_1.EntityHelper.isEntity);
|
|
53
55
|
context.set('Collection', Collection_1.Collection);
|
|
54
56
|
context.set('Reference', Reference_1.Reference);
|
|
55
57
|
const registerCustomType = (prop, convertorKey, method, context) => {
|
|
@@ -364,7 +366,7 @@ class ObjectHydrator extends Hydrator_1.Hydrator {
|
|
|
364
366
|
lines.push(` }`);
|
|
365
367
|
}
|
|
366
368
|
lines.push(` if (isPrimaryKey(value, ${meta.compositePK})) return factory.createReference('${prop.type}', value, { convertCustomTypes, schema, normalizeAccessors, merge: true });`);
|
|
367
|
-
lines.push(` if (value && value
|
|
369
|
+
lines.push(` if (value && isEntity(value)) return value;`);
|
|
368
370
|
lines.push(` return factory.create('${prop.type}', value, { newEntity, convertCustomTypes, schema, normalizeAccessors, merge: true });`);
|
|
369
371
|
lines.push(` }`);
|
|
370
372
|
return lines;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "6.6.10-dev.
|
|
3
|
+
"version": "6.6.10-dev.1",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"esprima": "4.0.1",
|
|
65
65
|
"fs-extra": "11.3.3",
|
|
66
66
|
"globby": "11.1.0",
|
|
67
|
-
"mikro-orm": "6.6.10-dev.
|
|
67
|
+
"mikro-orm": "6.6.10-dev.1",
|
|
68
68
|
"reflect-metadata": "0.2.2"
|
|
69
69
|
}
|
|
70
70
|
}
|
package/platforms/Platform.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EntityRepository } from '../entity';
|
|
1
|
+
import { EntityRepository } from '../entity/EntityRepository';
|
|
2
2
|
import { type NamingStrategy } from '../naming-strategy';
|
|
3
3
|
import type { Constructor, EntityProperty, IPrimaryKey, ISchemaGenerator, PopulateOptions, Primary, EntityMetadata, SimpleColumnMeta } from '../typings';
|
|
4
4
|
import { ExceptionConverter } from './ExceptionConverter';
|
package/platforms/Platform.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Platform = exports.JsonProperty = void 0;
|
|
4
4
|
const clone_1 = require("../utils/clone");
|
|
5
|
-
const
|
|
5
|
+
const RawQueryFragment_1 = require("../utils/RawQueryFragment");
|
|
6
|
+
const EntityRepository_1 = require("../entity/EntityRepository");
|
|
6
7
|
const naming_strategy_1 = require("../naming-strategy");
|
|
7
8
|
const ExceptionConverter_1 = require("./ExceptionConverter");
|
|
8
9
|
const types_1 = require("../types");
|
|
@@ -133,7 +134,7 @@ class Platform {
|
|
|
133
134
|
return prop.columnTypes?.[0] === 'bigint';
|
|
134
135
|
}
|
|
135
136
|
isRaw(value) {
|
|
136
|
-
return
|
|
137
|
+
return RawQueryFragment_1.RawQueryFragment.isRaw(value);
|
|
137
138
|
}
|
|
138
139
|
getDefaultSchemaName() {
|
|
139
140
|
return undefined;
|
|
@@ -309,7 +310,7 @@ class Platform {
|
|
|
309
310
|
return date;
|
|
310
311
|
}
|
|
311
312
|
getRepositoryClass() {
|
|
312
|
-
return
|
|
313
|
+
return EntityRepository_1.EntityRepository;
|
|
313
314
|
}
|
|
314
315
|
getDefaultCharset() {
|
|
315
316
|
return 'utf8';
|
package/typings.d.ts
CHANGED
|
@@ -6,7 +6,8 @@ import type { SerializationContext, SerializeOptions } from './serialization';
|
|
|
6
6
|
import type { EntitySchema, MetadataStorage } from './metadata';
|
|
7
7
|
import type { Type, types } from './types';
|
|
8
8
|
import type { Platform } from './platforms';
|
|
9
|
-
import type { Configuration
|
|
9
|
+
import type { Configuration } from './utils/Configuration';
|
|
10
|
+
import { RawQueryFragment } from './utils/RawQueryFragment';
|
|
10
11
|
import type { EntityManager } from './EntityManager';
|
|
11
12
|
import type { EmbeddedPrefixMode } from './decorators/Embedded';
|
|
12
13
|
import type { EventSubscriber } from './events';
|
package/typings.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.EntityMetadata = exports.Config = exports.HiddenProps = exports.EagerProps = exports.OptionalProps = exports.PrimaryKeyProp = exports.EntityRepositoryType = void 0;
|
|
4
4
|
const enums_1 = require("./enums");
|
|
5
5
|
const entity_1 = require("./entity");
|
|
6
|
+
const RawQueryFragment_1 = require("./utils/RawQueryFragment");
|
|
6
7
|
const Utils_1 = require("./utils/Utils");
|
|
7
8
|
const EntityComparator_1 = require("./utils/EntityComparator");
|
|
8
9
|
exports.EntityRepositoryType = Symbol('EntityRepositoryType');
|
|
@@ -151,7 +152,7 @@ class EntityMetadata {
|
|
|
151
152
|
return this.__helper.__data[prop.name];
|
|
152
153
|
},
|
|
153
154
|
set(val) {
|
|
154
|
-
if (
|
|
155
|
+
if (RawQueryFragment_1.RawQueryFragment.isRaw(val)) {
|
|
155
156
|
val.assign();
|
|
156
157
|
}
|
|
157
158
|
this.__helper.__data[prop.name] = val;
|
|
@@ -58,11 +58,12 @@ class EntityComparator {
|
|
|
58
58
|
}
|
|
59
59
|
const lines = [];
|
|
60
60
|
const context = new Map();
|
|
61
|
+
context.set('isEntityOrRef', (val) => Utils_1.Utils.isEntity(val, true));
|
|
61
62
|
if (meta.primaryKeys.length > 1) {
|
|
62
63
|
lines.push(` const cond = {`);
|
|
63
64
|
meta.primaryKeys.forEach(pk => {
|
|
64
65
|
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
65
|
-
lines.push(` ${pk}: (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}
|
|
66
|
+
lines.push(` ${pk}: (entity${this.wrap(pk)} != null && isEntityOrRef(entity${this.wrap(pk)})) ? entity${this.wrap(pk)}.__helper.getPrimaryKey() : entity${this.wrap(pk)},`);
|
|
66
67
|
}
|
|
67
68
|
else {
|
|
68
69
|
lines.push(` ${pk}: entity${this.wrap(pk)},`);
|
|
@@ -75,7 +76,7 @@ class EntityComparator {
|
|
|
75
76
|
else {
|
|
76
77
|
const pk = meta.primaryKeys[0];
|
|
77
78
|
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
78
|
-
lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}
|
|
79
|
+
lines.push(` if (entity${this.wrap(pk)} != null && isEntityOrRef(entity${this.wrap(pk)})) {`);
|
|
79
80
|
lines.push(` const pk = entity${this.wrap(pk)}.__helper.getPrimaryKey();`);
|
|
80
81
|
if (meta.properties[pk].targetMeta.compositePK) {
|
|
81
82
|
lines.push(` if (typeof pk === 'object' && pk != null) {`);
|
|
@@ -108,11 +109,12 @@ class EntityComparator {
|
|
|
108
109
|
}
|
|
109
110
|
const lines = [];
|
|
110
111
|
const context = new Map();
|
|
112
|
+
context.set('isEntityOrRef', (val) => Utils_1.Utils.isEntity(val, true));
|
|
111
113
|
if (meta.primaryKeys.length > 1) {
|
|
112
114
|
lines.push(` const cond = {`);
|
|
113
115
|
meta.primaryKeys.forEach(pk => {
|
|
114
116
|
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
115
|
-
lines.push(` ${pk}: (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}
|
|
117
|
+
lines.push(` ${pk}: (entity${this.wrap(pk)} != null && isEntityOrRef(entity${this.wrap(pk)})) ? entity${this.wrap(pk)}.__helper.getPrimaryKey(true) : entity${this.wrap(pk)},`);
|
|
116
118
|
}
|
|
117
119
|
else {
|
|
118
120
|
if (meta.properties[pk].customType) {
|
|
@@ -131,7 +133,7 @@ class EntityComparator {
|
|
|
131
133
|
else {
|
|
132
134
|
const pk = meta.primaryKeys[0];
|
|
133
135
|
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
134
|
-
lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}
|
|
136
|
+
lines.push(` if (entity${this.wrap(pk)} != null && isEntityOrRef(entity${this.wrap(pk)})) return entity${this.wrap(pk)}.__helper.getPrimaryKey(true);`);
|
|
135
137
|
}
|
|
136
138
|
if (meta.properties[pk].customType) {
|
|
137
139
|
const convertorKey = this.registerCustomType(meta.properties[pk], context);
|
|
@@ -158,13 +160,14 @@ class EntityComparator {
|
|
|
158
160
|
}
|
|
159
161
|
const lines = [];
|
|
160
162
|
const context = new Map();
|
|
163
|
+
context.set('isEntityOrRef', (val) => Utils_1.Utils.isEntity(val, true));
|
|
161
164
|
context.set('getCompositeKeyValue', (val) => Utils_1.Utils.flatten(Utils_1.Utils.getCompositeKeyValue(val, meta, 'convertToDatabaseValue', this.platform)));
|
|
162
165
|
context.set('getPrimaryKeyHash', (val) => Utils_1.Utils.getPrimaryKeyHash(Utils_1.Utils.asArray(val)));
|
|
163
166
|
if (meta.primaryKeys.length > 1) {
|
|
164
167
|
lines.push(` const pks = entity.__helper.__pk ? getCompositeKeyValue(entity.__helper.__pk) : [`);
|
|
165
168
|
meta.primaryKeys.forEach(pk => {
|
|
166
169
|
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
167
|
-
lines.push(` (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}
|
|
170
|
+
lines.push(` (entity${this.wrap(pk)} != null && isEntityOrRef(entity${this.wrap(pk)})) ? entity${this.wrap(pk)}.__helper.getSerializedPrimaryKey() : entity${this.wrap(pk)},`);
|
|
168
171
|
}
|
|
169
172
|
else {
|
|
170
173
|
lines.push(` entity${this.wrap(pk)},`);
|
|
@@ -177,7 +180,7 @@ class EntityComparator {
|
|
|
177
180
|
const pk = meta.primaryKeys[0];
|
|
178
181
|
const prop = meta.properties[pk];
|
|
179
182
|
if (prop.kind !== enums_1.ReferenceKind.SCALAR) {
|
|
180
|
-
lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}
|
|
183
|
+
lines.push(` if (entity${this.wrap(pk)} != null && isEntityOrRef(entity${this.wrap(pk)})) return entity${this.wrap(pk)}.__helper.getSerializedPrimaryKey();`);
|
|
181
184
|
}
|
|
182
185
|
const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
|
|
183
186
|
if (serializedPrimaryKey) {
|
|
@@ -18,6 +18,10 @@ export declare class RawQueryFragment {
|
|
|
18
18
|
* @internal allows testing we don't leak memory, as the raw fragments cache needs to be cleared automatically
|
|
19
19
|
*/
|
|
20
20
|
static checkCacheSize(): number;
|
|
21
|
+
/** @internal */
|
|
22
|
+
static isRaw(value: unknown): boolean;
|
|
23
|
+
/** @internal */
|
|
24
|
+
static markRaw(obj: object): void;
|
|
21
25
|
static isKnownFragment(key: string | RawQueryFragment): boolean;
|
|
22
26
|
static getKnownFragment(key: string | RawQueryFragment, cleanup?: boolean): RawQueryFragment | undefined;
|
|
23
27
|
static remove(key: string): void;
|
|
@@ -8,6 +8,7 @@ exports.quote = quote;
|
|
|
8
8
|
const node_async_hooks_1 = require("node:async_hooks");
|
|
9
9
|
const node_util_1 = require("node:util");
|
|
10
10
|
const Utils_1 = require("./Utils");
|
|
11
|
+
const rawSymbol = Symbol('RawQueryFragment');
|
|
11
12
|
class RawQueryFragment {
|
|
12
13
|
sql;
|
|
13
14
|
params;
|
|
@@ -22,6 +23,7 @@ class RawQueryFragment {
|
|
|
22
23
|
this.sql = sql;
|
|
23
24
|
this.params = params;
|
|
24
25
|
this.#key = `[raw]: ${this.sql} (#${RawQueryFragment.#index++})`;
|
|
26
|
+
Object.defineProperty(this, rawSymbol, { value: true, enumerable: false });
|
|
25
27
|
}
|
|
26
28
|
as(alias) {
|
|
27
29
|
// TODO: to be removed in v7
|
|
@@ -66,6 +68,14 @@ class RawQueryFragment {
|
|
|
66
68
|
static checkCacheSize() {
|
|
67
69
|
return this.#rawQueryCache.size;
|
|
68
70
|
}
|
|
71
|
+
/** @internal */
|
|
72
|
+
static isRaw(value) {
|
|
73
|
+
return typeof value === 'object' && !!value && Object.hasOwn(value, rawSymbol);
|
|
74
|
+
}
|
|
75
|
+
/** @internal */
|
|
76
|
+
static markRaw(obj) {
|
|
77
|
+
Object.defineProperty(obj, rawSymbol, { value: true, enumerable: false });
|
|
78
|
+
}
|
|
69
79
|
static isKnownFragment(key) {
|
|
70
80
|
if (key instanceof RawQueryFragment) {
|
|
71
81
|
return true;
|
|
@@ -108,9 +118,6 @@ class RawQueryFragment {
|
|
|
108
118
|
}
|
|
109
119
|
}
|
|
110
120
|
exports.RawQueryFragment = RawQueryFragment;
|
|
111
|
-
Object.defineProperties(RawQueryFragment.prototype, {
|
|
112
|
-
__raw: { value: true, enumerable: false },
|
|
113
|
-
});
|
|
114
121
|
/** @internal */
|
|
115
122
|
exports.ALIAS_REPLACEMENT = '[::alias::]';
|
|
116
123
|
/** @internal */
|
package/utils/Utils.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { type GlobbyOptions } from 'globby';
|
|
|
2
2
|
import type { Dictionary, EntityData, EntityDictionary, EntityKey, EntityMetadata, EntityName, EntityProperty, IMetadataStorage, Primary } from '../typings';
|
|
3
3
|
import type { Collection } from '../entity/Collection';
|
|
4
4
|
import type { Platform } from '../platforms';
|
|
5
|
-
import
|
|
5
|
+
import { ScalarReference } from '../entity/Reference';
|
|
6
6
|
export declare const ObjectBindingPattern: unique symbol;
|
|
7
7
|
export declare function compareObjects(a: any, b: any): boolean;
|
|
8
8
|
export declare function compareArrays(a: any[] | string, b: any[] | string): boolean;
|
|
@@ -149,7 +149,7 @@ export declare class Utils {
|
|
|
149
149
|
/**
|
|
150
150
|
* Checks whether given object is a scalar reference.
|
|
151
151
|
*/
|
|
152
|
-
static isScalarReference<T = unknown>(data: any
|
|
152
|
+
static isScalarReference<T = unknown>(data: any): data is ScalarReference<any> & {};
|
|
153
153
|
/**
|
|
154
154
|
* Checks whether the argument is ObjectId instance
|
|
155
155
|
*/
|
package/utils/Utils.js
CHANGED
|
@@ -19,8 +19,12 @@ const fs_extra_1 = require("fs-extra");
|
|
|
19
19
|
const node_crypto_1 = require("node:crypto");
|
|
20
20
|
const esprima_1 = require("esprima");
|
|
21
21
|
const clone_1 = require("./clone");
|
|
22
|
+
const RawQueryFragment_1 = require("./RawQueryFragment");
|
|
22
23
|
const enums_1 = require("../enums");
|
|
23
24
|
const wrap_1 = require("../entity/wrap");
|
|
25
|
+
const Reference_1 = require("../entity/Reference");
|
|
26
|
+
const ArrayCollection_1 = require("../entity/ArrayCollection");
|
|
27
|
+
const EntityHelper_1 = require("../entity/EntityHelper");
|
|
24
28
|
exports.ObjectBindingPattern = Symbol('ObjectBindingPattern');
|
|
25
29
|
function compareConstructors(a, b) {
|
|
26
30
|
if (a.constructor === b.constructor) {
|
|
@@ -35,7 +39,7 @@ function compareConstructors(a, b) {
|
|
|
35
39
|
return false;
|
|
36
40
|
}
|
|
37
41
|
function isRawSql(value) {
|
|
38
|
-
return
|
|
42
|
+
return RawQueryFragment_1.RawQueryFragment.isRaw(value);
|
|
39
43
|
}
|
|
40
44
|
function compareObjects(a, b) {
|
|
41
45
|
if (a === b || (a == null && b == null)) {
|
|
@@ -289,6 +293,9 @@ class Utils {
|
|
|
289
293
|
const source = sources.shift();
|
|
290
294
|
if (Utils.isObject(target) && Utils.isPlainObject(source)) {
|
|
291
295
|
for (const [key, value] of Object.entries(source)) {
|
|
296
|
+
if (['__proto__', 'constructor', 'prototype'].includes(key)) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
292
299
|
if (ignoreUndefined && typeof value === 'undefined') {
|
|
293
300
|
continue;
|
|
294
301
|
}
|
|
@@ -610,16 +617,16 @@ class Utils {
|
|
|
610
617
|
if (!Utils.isObject(data)) {
|
|
611
618
|
return false;
|
|
612
619
|
}
|
|
613
|
-
if (allowReference &&
|
|
620
|
+
if (allowReference && Reference_1.Reference.isReference(data)) {
|
|
614
621
|
return true;
|
|
615
622
|
}
|
|
616
|
-
return
|
|
623
|
+
return EntityHelper_1.EntityHelper.isEntity(data);
|
|
617
624
|
}
|
|
618
625
|
/**
|
|
619
626
|
* Checks whether given object is a scalar reference.
|
|
620
627
|
*/
|
|
621
|
-
static isScalarReference(data
|
|
622
|
-
return
|
|
628
|
+
static isScalarReference(data) {
|
|
629
|
+
return Reference_1.ScalarReference.isScalarReference(data);
|
|
623
630
|
}
|
|
624
631
|
/**
|
|
625
632
|
* Checks whether the argument is ObjectId instance
|
|
@@ -745,7 +752,7 @@ class Utils {
|
|
|
745
752
|
return ret;
|
|
746
753
|
}
|
|
747
754
|
static isCollection(item) {
|
|
748
|
-
return item
|
|
755
|
+
return ArrayCollection_1.ArrayCollection.isCollection(item);
|
|
749
756
|
}
|
|
750
757
|
static fileURLToPath(url) {
|
|
751
758
|
// expose `fileURLToPath` on Utils so that it can be properly mocked in tests
|