@mikro-orm/core 7.0.0-dev.33 → 7.0.0-dev.331
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 +70 -75
- package/EntityManager.js +487 -402
- package/MikroORM.d.ts +45 -38
- package/MikroORM.js +123 -156
- package/README.md +7 -4
- package/cache/FileCacheAdapter.d.ts +2 -7
- package/cache/FileCacheAdapter.js +35 -30
- package/cache/GeneratedCacheAdapter.d.ts +1 -2
- package/cache/GeneratedCacheAdapter.js +6 -8
- package/cache/MemoryCacheAdapter.d.ts +1 -2
- package/cache/MemoryCacheAdapter.js +8 -8
- package/cache/index.d.ts +1 -2
- package/cache/index.js +0 -2
- package/connections/Connection.d.ts +12 -5
- package/connections/Connection.js +37 -15
- package/drivers/DatabaseDriver.d.ts +25 -18
- package/drivers/DatabaseDriver.js +144 -45
- package/drivers/IDatabaseDriver.d.ts +118 -23
- package/entity/BaseEntity.d.ts +63 -4
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +95 -31
- package/entity/Collection.js +487 -139
- package/entity/EntityAssigner.js +37 -25
- package/entity/EntityFactory.d.ts +8 -9
- package/entity/EntityFactory.js +152 -100
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +69 -27
- package/entity/EntityLoader.d.ts +12 -13
- package/entity/EntityLoader.js +286 -125
- package/entity/EntityRepository.d.ts +28 -8
- package/entity/EntityRepository.js +8 -2
- package/entity/PolymorphicRef.d.ts +12 -0
- package/entity/PolymorphicRef.js +18 -0
- package/entity/Reference.d.ts +3 -8
- package/entity/Reference.js +62 -29
- package/entity/WrappedEntity.d.ts +7 -10
- package/entity/WrappedEntity.js +6 -7
- package/entity/defineEntity.d.ts +472 -313
- package/entity/defineEntity.js +134 -290
- package/entity/index.d.ts +2 -2
- package/entity/index.js +2 -2
- package/entity/utils.d.ts +6 -1
- package/entity/utils.js +46 -11
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +66 -0
- package/enums.d.ts +8 -6
- package/enums.js +13 -17
- package/errors.d.ts +26 -16
- package/errors.js +63 -31
- package/events/EventManager.d.ts +3 -5
- package/events/EventManager.js +37 -26
- package/events/index.d.ts +1 -1
- package/events/index.js +0 -1
- package/exceptions.js +9 -2
- package/hydration/Hydrator.js +1 -2
- package/hydration/ObjectHydrator.d.ts +5 -6
- package/hydration/ObjectHydrator.js +109 -50
- package/index.d.ts +2 -2
- package/index.js +1 -2
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/DefaultLogger.js +3 -4
- package/logging/SimpleLogger.d.ts +1 -1
- package/logging/colors.d.ts +1 -1
- package/logging/colors.js +4 -6
- package/logging/index.d.ts +2 -1
- package/logging/index.js +1 -1
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +11 -0
- package/metadata/EntitySchema.d.ts +47 -23
- package/metadata/EntitySchema.js +103 -34
- package/metadata/MetadataDiscovery.d.ts +65 -18
- package/metadata/MetadataDiscovery.js +940 -424
- package/metadata/MetadataProvider.d.ts +11 -2
- package/metadata/MetadataProvider.js +71 -2
- package/metadata/MetadataStorage.d.ts +11 -13
- package/metadata/MetadataStorage.js +79 -48
- package/metadata/MetadataValidator.d.ts +32 -9
- package/metadata/MetadataValidator.js +214 -44
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +40 -0
- package/metadata/index.d.ts +1 -1
- package/metadata/index.js +0 -1
- package/metadata/types.d.ts +577 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
- package/naming-strategy/AbstractNamingStrategy.js +26 -5
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
- package/naming-strategy/EntityCaseNamingStrategy.js +7 -6
- package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
- package/naming-strategy/MongoNamingStrategy.js +6 -6
- package/naming-strategy/NamingStrategy.d.ts +28 -4
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
- package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
- package/naming-strategy/index.d.ts +1 -1
- package/naming-strategy/index.js +0 -1
- package/not-supported.d.ts +2 -0
- package/not-supported.js +8 -0
- package/package.json +47 -36
- package/platforms/ExceptionConverter.js +1 -1
- package/platforms/Platform.d.ts +33 -15
- package/platforms/Platform.js +125 -69
- package/serialization/EntitySerializer.d.ts +6 -3
- package/serialization/EntitySerializer.js +54 -30
- package/serialization/EntityTransformer.js +37 -22
- package/serialization/SerializationContext.d.ts +10 -14
- package/serialization/SerializationContext.js +24 -19
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +2 -3
- package/types/BigIntType.js +1 -1
- package/types/BlobType.d.ts +0 -1
- package/types/BlobType.js +0 -3
- package/types/BooleanType.d.ts +1 -0
- package/types/BooleanType.js +3 -0
- package/types/DecimalType.js +2 -2
- package/types/DoubleType.js +1 -1
- package/types/EnumArrayType.js +1 -2
- package/types/JsonType.d.ts +1 -1
- package/types/JsonType.js +7 -2
- package/types/TinyIntType.js +1 -1
- package/types/Type.d.ts +2 -4
- package/types/Type.js +3 -3
- package/types/Uint8ArrayType.d.ts +0 -1
- package/types/Uint8ArrayType.js +1 -4
- package/types/UuidType.d.ts +2 -0
- package/types/UuidType.js +14 -2
- package/types/index.d.ts +3 -2
- package/typings.d.ts +427 -170
- package/typings.js +100 -45
- package/unit-of-work/ChangeSet.d.ts +4 -6
- package/unit-of-work/ChangeSet.js +8 -9
- package/unit-of-work/ChangeSetComputer.d.ts +2 -12
- package/unit-of-work/ChangeSetComputer.js +61 -38
- package/unit-of-work/ChangeSetPersister.d.ts +10 -17
- package/unit-of-work/ChangeSetPersister.js +136 -73
- package/unit-of-work/CommitOrderCalculator.d.ts +13 -14
- package/unit-of-work/CommitOrderCalculator.js +22 -20
- package/unit-of-work/IdentityMap.d.ts +12 -3
- package/unit-of-work/IdentityMap.js +51 -13
- package/unit-of-work/UnitOfWork.d.ts +39 -23
- package/unit-of-work/UnitOfWork.js +441 -246
- package/utils/AbstractMigrator.d.ts +101 -0
- package/utils/AbstractMigrator.js +303 -0
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +30 -18
- package/utils/AsyncContext.d.ts +6 -0
- package/utils/AsyncContext.js +42 -0
- package/utils/Configuration.d.ts +647 -185
- package/utils/Configuration.js +215 -252
- package/utils/ConfigurationLoader.d.ts +1 -52
- package/utils/ConfigurationLoader.js +1 -330
- package/utils/Cursor.d.ts +3 -6
- package/utils/Cursor.js +32 -17
- package/utils/DataloaderUtils.d.ts +10 -5
- package/utils/DataloaderUtils.js +42 -22
- package/utils/EntityComparator.d.ts +21 -21
- package/utils/EntityComparator.js +224 -118
- package/utils/QueryHelper.d.ts +34 -7
- package/utils/QueryHelper.js +183 -72
- package/utils/RawQueryFragment.d.ts +28 -34
- package/utils/RawQueryFragment.js +37 -72
- package/utils/RequestContext.js +2 -2
- package/utils/TransactionContext.js +2 -2
- package/utils/TransactionManager.js +11 -8
- package/utils/Utils.d.ts +16 -127
- package/utils/Utils.js +104 -402
- package/utils/clone.js +13 -23
- package/utils/env-vars.d.ts +7 -0
- package/utils/env-vars.js +98 -0
- package/utils/fs-utils.d.ts +20 -0
- package/utils/fs-utils.js +193 -0
- package/utils/index.d.ts +1 -3
- package/utils/index.js +1 -3
- package/utils/upsert-utils.d.ts +9 -4
- package/utils/upsert-utils.js +51 -5
- package/decorators/Check.d.ts +0 -3
- package/decorators/Check.js +0 -13
- package/decorators/CreateRequestContext.d.ts +0 -3
- package/decorators/CreateRequestContext.js +0 -32
- package/decorators/Embeddable.d.ts +0 -8
- package/decorators/Embeddable.js +0 -11
- package/decorators/Embedded.d.ts +0 -12
- package/decorators/Embedded.js +0 -18
- package/decorators/Entity.d.ts +0 -33
- package/decorators/Entity.js +0 -12
- package/decorators/Enum.d.ts +0 -9
- package/decorators/Enum.js +0 -16
- package/decorators/Filter.d.ts +0 -2
- package/decorators/Filter.js +0 -8
- package/decorators/Formula.d.ts +0 -4
- package/decorators/Formula.js +0 -15
- package/decorators/Indexed.d.ts +0 -19
- package/decorators/Indexed.js +0 -20
- package/decorators/ManyToMany.d.ts +0 -42
- package/decorators/ManyToMany.js +0 -14
- package/decorators/ManyToOne.d.ts +0 -34
- package/decorators/ManyToOne.js +0 -14
- package/decorators/OneToMany.d.ts +0 -28
- package/decorators/OneToMany.js +0 -17
- package/decorators/OneToOne.d.ts +0 -28
- package/decorators/OneToOne.js +0 -7
- package/decorators/PrimaryKey.d.ts +0 -8
- package/decorators/PrimaryKey.js +0 -20
- package/decorators/Property.d.ts +0 -250
- package/decorators/Property.js +0 -32
- package/decorators/Transactional.d.ts +0 -14
- package/decorators/Transactional.js +0 -28
- package/decorators/hooks.d.ts +0 -16
- package/decorators/hooks.js +0 -47
- package/decorators/index.d.ts +0 -17
- package/decorators/index.js +0 -17
- package/entity/ArrayCollection.d.ts +0 -118
- package/entity/ArrayCollection.js +0 -407
- package/entity/EntityValidator.d.ts +0 -19
- package/entity/EntityValidator.js +0 -150
- package/metadata/ReflectMetadataProvider.d.ts +0 -8
- package/metadata/ReflectMetadataProvider.js +0 -44
- package/utils/resolveContextProvider.d.ts +0 -10
- package/utils/resolveContextProvider.js +0 -28
package/events/EventManager.js
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
import { Utils } from '../utils/Utils.js';
|
|
2
2
|
import { EventType, EventTypeMap } from '../enums.js';
|
|
3
3
|
export class EventManager {
|
|
4
|
-
listeners = {};
|
|
5
|
-
entities = new Map();
|
|
6
|
-
cache = new Map();
|
|
7
|
-
subscribers =
|
|
4
|
+
#listeners = {};
|
|
5
|
+
#entities = new Map();
|
|
6
|
+
#cache = new Map();
|
|
7
|
+
#subscribers = new Set();
|
|
8
8
|
constructor(subscribers) {
|
|
9
|
-
|
|
9
|
+
for (const subscriber of subscribers) {
|
|
10
|
+
this.registerSubscriber(subscriber);
|
|
11
|
+
}
|
|
10
12
|
}
|
|
11
13
|
registerSubscriber(subscriber) {
|
|
12
|
-
this
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
if (this.#subscribers.has(subscriber)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
this.#subscribers.add(subscriber);
|
|
18
|
+
this.#entities.set(subscriber, this.getSubscribedEntities(subscriber));
|
|
19
|
+
this.#cache.clear();
|
|
15
20
|
Utils.keys(EventType)
|
|
16
21
|
.filter(event => event in subscriber)
|
|
17
22
|
.forEach(event => {
|
|
18
|
-
this
|
|
19
|
-
this
|
|
23
|
+
this.#listeners[event] ??= new Set();
|
|
24
|
+
this.#listeners[event].add(subscriber);
|
|
20
25
|
});
|
|
21
26
|
}
|
|
27
|
+
getSubscribers() {
|
|
28
|
+
return this.#subscribers;
|
|
29
|
+
}
|
|
22
30
|
dispatchEvent(event, args, meta) {
|
|
23
31
|
const listeners = [];
|
|
24
32
|
const entity = args.entity;
|
|
@@ -27,47 +35,50 @@ export class EventManager {
|
|
|
27
35
|
const hooks = (meta?.hooks[event] || []);
|
|
28
36
|
listeners.push(...hooks.map(hook => {
|
|
29
37
|
const prototypeHook = meta?.prototype[hook];
|
|
30
|
-
const handler = typeof hook === 'function' ? hook : entity[hook] ?? prototypeHook;
|
|
38
|
+
const handler = typeof hook === 'function' ? hook : (entity[hook] ?? prototypeHook);
|
|
31
39
|
return handler.bind(entity);
|
|
32
40
|
}));
|
|
33
|
-
for (const listener of this
|
|
34
|
-
const entities = this
|
|
35
|
-
if (entities.
|
|
41
|
+
for (const listener of this.#listeners[event] ?? new Set()) {
|
|
42
|
+
const entities = this.#entities.get(listener);
|
|
43
|
+
if (entities.size === 0 || !entity || entities.has(entity.constructor.name)) {
|
|
36
44
|
listeners.push(listener[event].bind(listener));
|
|
37
45
|
}
|
|
38
46
|
}
|
|
39
47
|
if (event === EventType.onInit) {
|
|
40
|
-
|
|
48
|
+
for (const listener of listeners) {
|
|
49
|
+
void listener(args);
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
41
52
|
}
|
|
42
53
|
return Utils.runSerial(listeners, listener => listener(args));
|
|
43
54
|
}
|
|
44
55
|
hasListeners(event, meta) {
|
|
45
56
|
const cacheKey = meta._id + EventTypeMap[event];
|
|
46
|
-
if (this
|
|
47
|
-
return this
|
|
57
|
+
if (this.#cache.has(cacheKey)) {
|
|
58
|
+
return this.#cache.get(cacheKey);
|
|
48
59
|
}
|
|
49
60
|
const hasHooks = meta.hooks[event]?.length;
|
|
50
61
|
if (hasHooks) {
|
|
51
|
-
this
|
|
62
|
+
this.#cache.set(cacheKey, true);
|
|
52
63
|
return true;
|
|
53
64
|
}
|
|
54
|
-
for (const listener of this
|
|
55
|
-
const entities = this
|
|
56
|
-
if (entities.
|
|
57
|
-
this
|
|
65
|
+
for (const listener of this.#listeners[event] ?? new Set()) {
|
|
66
|
+
const entities = this.#entities.get(listener);
|
|
67
|
+
if (entities.size === 0 || entities.has(meta.className)) {
|
|
68
|
+
this.#cache.set(cacheKey, true);
|
|
58
69
|
return true;
|
|
59
70
|
}
|
|
60
71
|
}
|
|
61
|
-
this
|
|
72
|
+
this.#cache.set(cacheKey, false);
|
|
62
73
|
return false;
|
|
63
74
|
}
|
|
64
75
|
clone() {
|
|
65
|
-
return new EventManager(this
|
|
76
|
+
return new EventManager(this.#subscribers);
|
|
66
77
|
}
|
|
67
78
|
getSubscribedEntities(listener) {
|
|
68
79
|
if (!listener.getSubscribedEntities) {
|
|
69
|
-
return
|
|
80
|
+
return new Set();
|
|
70
81
|
}
|
|
71
|
-
return listener.getSubscribedEntities().map(name => Utils.className(name));
|
|
82
|
+
return new Set(listener.getSubscribedEntities().map(name => Utils.className(name)));
|
|
72
83
|
}
|
|
73
84
|
}
|
package/events/index.d.ts
CHANGED
package/events/index.js
CHANGED
package/exceptions.js
CHANGED
|
@@ -9,10 +9,17 @@ export class DriverException extends Error {
|
|
|
9
9
|
errmsg;
|
|
10
10
|
constructor(previous) {
|
|
11
11
|
super(previous.message);
|
|
12
|
-
Object.getOwnPropertyNames(previous).forEach(k => this[k] = previous[k]);
|
|
12
|
+
Object.getOwnPropertyNames(previous).forEach(k => (this[k] = previous[k]));
|
|
13
13
|
this.name = this.constructor.name;
|
|
14
14
|
Error.captureStackTrace(this, this.constructor);
|
|
15
|
-
|
|
15
|
+
if (previous.stack) {
|
|
16
|
+
this.stack +=
|
|
17
|
+
'\n\n' +
|
|
18
|
+
previous.stack
|
|
19
|
+
.split('\n')
|
|
20
|
+
.filter(l => l.trim().startsWith('at '))
|
|
21
|
+
.join('\n');
|
|
22
|
+
}
|
|
16
23
|
}
|
|
17
24
|
}
|
|
18
25
|
/**
|
package/hydration/Hydrator.js
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
import type { EntityData, EntityMetadata } from '../typings.js';
|
|
2
2
|
import { Hydrator } from './Hydrator.js';
|
|
3
3
|
import type { EntityFactory } from '../entity/EntityFactory.js';
|
|
4
|
-
type EntityHydrator<T extends object> = (entity: T, data: EntityData<T>, factory: EntityFactory, newEntity: boolean, convertCustomTypes: boolean, schema?: string, parentSchema?: string) => void;
|
|
4
|
+
type EntityHydrator<T extends object> = (entity: T, data: EntityData<T>, factory: EntityFactory, newEntity: boolean, convertCustomTypes: boolean, schema?: string, parentSchema?: string, normalizeAccessors?: boolean) => void;
|
|
5
5
|
export declare class ObjectHydrator extends Hydrator {
|
|
6
|
-
private
|
|
7
|
-
private tmpIndex;
|
|
6
|
+
#private;
|
|
8
7
|
/**
|
|
9
8
|
* @inheritDoc
|
|
10
9
|
*/
|
|
11
|
-
hydrate<T extends object>(entity: T, meta: EntityMetadata<T>, data: EntityData<T>, factory: EntityFactory, type: 'full' | 'reference', newEntity?: boolean, convertCustomTypes?: boolean, schema?: string, parentSchema?: string): void;
|
|
10
|
+
hydrate<T extends object>(entity: T, meta: EntityMetadata<T>, data: EntityData<T>, factory: EntityFactory, type: 'full' | 'reference', newEntity?: boolean, convertCustomTypes?: boolean, schema?: string, parentSchema?: string, normalizeAccessors?: boolean): void;
|
|
12
11
|
/**
|
|
13
12
|
* @inheritDoc
|
|
14
13
|
*/
|
|
15
|
-
hydrateReference<T extends object>(entity: T, meta: EntityMetadata<T>, data: EntityData<T>, factory: EntityFactory, convertCustomTypes?: boolean, schema?: string, parentSchema?: string): void;
|
|
14
|
+
hydrateReference<T extends object>(entity: T, meta: EntityMetadata<T>, data: EntityData<T>, factory: EntityFactory, convertCustomTypes?: boolean, schema?: string, parentSchema?: string, normalizeAccessors?: boolean): void;
|
|
16
15
|
/**
|
|
17
16
|
* @internal Highly performance-sensitive method.
|
|
18
17
|
*/
|
|
19
|
-
getEntityHydrator<T extends object>(meta: EntityMetadata<T>, type: 'full' | 'reference'): EntityHydrator<T>;
|
|
18
|
+
getEntityHydrator<T extends object>(meta: EntityMetadata<T>, type: 'full' | 'reference', normalizeAccessors?: boolean): EntityHydrator<T>;
|
|
20
19
|
private createCollectionItemMapper;
|
|
21
20
|
private wrap;
|
|
22
21
|
private safeKey;
|
|
@@ -1,42 +1,47 @@
|
|
|
1
1
|
import { Hydrator } from './Hydrator.js';
|
|
2
2
|
import { Collection } from '../entity/Collection.js';
|
|
3
3
|
import { Reference, ScalarReference } from '../entity/Reference.js';
|
|
4
|
+
import { PolymorphicRef } from '../entity/PolymorphicRef.js';
|
|
4
5
|
import { parseJsonSafe, Utils } from '../utils/Utils.js';
|
|
5
6
|
import { ReferenceKind } from '../enums.js';
|
|
6
|
-
import {
|
|
7
|
+
import { Raw } from '../utils/RawQueryFragment.js';
|
|
8
|
+
import { ValidationError } from '../errors.js';
|
|
7
9
|
export class ObjectHydrator extends Hydrator {
|
|
8
|
-
hydrators = {
|
|
9
|
-
full: new Map(),
|
|
10
|
-
|
|
10
|
+
#hydrators = {
|
|
11
|
+
'full~true': new Map(),
|
|
12
|
+
'full~false': new Map(),
|
|
13
|
+
'reference~true': new Map(),
|
|
14
|
+
'reference~false': new Map(),
|
|
11
15
|
};
|
|
12
|
-
tmpIndex = 0;
|
|
16
|
+
#tmpIndex = 0;
|
|
13
17
|
/**
|
|
14
18
|
* @inheritDoc
|
|
15
19
|
*/
|
|
16
|
-
hydrate(entity, meta, data, factory, type, newEntity = false, convertCustomTypes = false, schema, parentSchema) {
|
|
17
|
-
const hydrate = this.getEntityHydrator(meta, type);
|
|
20
|
+
hydrate(entity, meta, data, factory, type, newEntity = false, convertCustomTypes = false, schema, parentSchema, normalizeAccessors) {
|
|
21
|
+
const hydrate = this.getEntityHydrator(meta, type, normalizeAccessors);
|
|
18
22
|
const running = this.running;
|
|
19
23
|
// the running state is used to consider propagation as hydration, saving the values directly to the entity data,
|
|
20
24
|
// but we don't want that for new entities, their propagation should result in entity updates when flushing
|
|
21
25
|
this.running = !newEntity;
|
|
22
|
-
Utils.callCompiledFunction(hydrate, entity, data, factory, newEntity, convertCustomTypes, schema, parentSchema);
|
|
26
|
+
Utils.callCompiledFunction(hydrate, entity, data, factory, newEntity, convertCustomTypes, schema, parentSchema, normalizeAccessors);
|
|
23
27
|
this.running = running;
|
|
24
28
|
}
|
|
25
29
|
/**
|
|
26
30
|
* @inheritDoc
|
|
27
31
|
*/
|
|
28
|
-
hydrateReference(entity, meta, data, factory, convertCustomTypes = false, schema, parentSchema) {
|
|
29
|
-
const hydrate = this.getEntityHydrator(meta, 'reference');
|
|
32
|
+
hydrateReference(entity, meta, data, factory, convertCustomTypes = false, schema, parentSchema, normalizeAccessors) {
|
|
33
|
+
const hydrate = this.getEntityHydrator(meta, 'reference', normalizeAccessors);
|
|
30
34
|
const running = this.running;
|
|
31
35
|
this.running = true;
|
|
32
|
-
Utils.callCompiledFunction(hydrate, entity, data, factory, false, convertCustomTypes, schema, parentSchema);
|
|
36
|
+
Utils.callCompiledFunction(hydrate, entity, data, factory, false, convertCustomTypes, schema, parentSchema, normalizeAccessors);
|
|
33
37
|
this.running = running;
|
|
34
38
|
}
|
|
35
39
|
/**
|
|
36
40
|
* @internal Highly performance-sensitive method.
|
|
37
41
|
*/
|
|
38
|
-
getEntityHydrator(meta, type) {
|
|
39
|
-
const
|
|
42
|
+
getEntityHydrator(meta, type, normalizeAccessors = false) {
|
|
43
|
+
const key = `${type}~${normalizeAccessors}`;
|
|
44
|
+
const exists = this.#hydrators[key].get(meta.class);
|
|
40
45
|
if (exists) {
|
|
41
46
|
return exists;
|
|
42
47
|
}
|
|
@@ -46,22 +51,27 @@ export class ObjectHydrator extends Hydrator {
|
|
|
46
51
|
context.set('isPrimaryKey', Utils.isPrimaryKey);
|
|
47
52
|
context.set('Collection', Collection);
|
|
48
53
|
context.set('Reference', Reference);
|
|
54
|
+
context.set('PolymorphicRef', PolymorphicRef);
|
|
55
|
+
context.set('ValidationError', ValidationError);
|
|
49
56
|
const registerCustomType = (prop, convertorKey, method, context) => {
|
|
50
57
|
context.set(`${method}_${convertorKey}`, (val) => {
|
|
51
|
-
/* v8 ignore next
|
|
52
|
-
if (
|
|
58
|
+
/* v8 ignore next */
|
|
59
|
+
if (Raw.isKnownFragment(val)) {
|
|
53
60
|
return val;
|
|
54
61
|
}
|
|
55
62
|
return prop.customType[method](val, this.platform, { mode: 'serialization' });
|
|
56
63
|
});
|
|
57
64
|
return convertorKey;
|
|
58
65
|
};
|
|
59
|
-
const hydrateScalar = (prop,
|
|
66
|
+
const hydrateScalar = (prop, path, dataKey) => {
|
|
60
67
|
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
61
68
|
const tz = this.platform.getTimezone();
|
|
62
|
-
const convertorKey = path
|
|
69
|
+
const convertorKey = path
|
|
70
|
+
.filter(k => !/\[idx_\d+]/.exec(k))
|
|
71
|
+
.map(k => this.safeKey(k))
|
|
72
|
+
.join('_');
|
|
63
73
|
const ret = [];
|
|
64
|
-
const idx = this
|
|
74
|
+
const idx = this.#tmpIndex++;
|
|
65
75
|
const nullVal = this.config.get('forceUndefined') ? 'undefined' : 'null';
|
|
66
76
|
if (prop.getter && !prop.setter && prop.persist === false) {
|
|
67
77
|
return [];
|
|
@@ -131,24 +141,58 @@ export class ObjectHydrator extends Hydrator {
|
|
|
131
141
|
const nullVal = this.config.get('forceUndefined') ? 'undefined' : 'null';
|
|
132
142
|
ret.push(` if (data${dataKey} === null) {\n entity${entityKey} = ${nullVal};`);
|
|
133
143
|
ret.push(` } else if (typeof data${dataKey} !== 'undefined') {`);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
144
|
+
// For polymorphic: instanceof check; for regular: isPrimaryKey() check
|
|
145
|
+
const pkCheck = prop.polymorphic
|
|
146
|
+
? `data${dataKey} instanceof PolymorphicRef`
|
|
147
|
+
: `isPrimaryKey(data${dataKey}, true)`;
|
|
148
|
+
ret.push(` if (${pkCheck}) {`);
|
|
149
|
+
// When targetKey is set, pass the key option to createReference so it uses the alternate key
|
|
150
|
+
const keyOption = prop.targetKey ? `, key: '${prop.targetKey}'` : '';
|
|
151
|
+
if (prop.polymorphic) {
|
|
152
|
+
// For polymorphic: target class from discriminator map, PK from data.id
|
|
153
|
+
const discriminatorMapKey = this.safeKey(`discriminatorMap_${prop.name}_${this.#tmpIndex++}`);
|
|
154
|
+
context.set(discriminatorMapKey, prop.discriminatorMap);
|
|
155
|
+
ret.push(` const targetClass = ${discriminatorMapKey}[data${dataKey}.discriminator];`);
|
|
156
|
+
ret.push(` if (!targetClass) throw new ValidationError(\`Unknown discriminator value '\${data${dataKey}.discriminator}' for polymorphic relation '${prop.name}'. Valid values: \${Object.keys(${discriminatorMapKey}).join(', ')}\`);`);
|
|
157
|
+
if (prop.ref) {
|
|
158
|
+
ret.push(` entity${entityKey} = Reference.create(factory.createReference(targetClass, data${dataKey}.id, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} }));`);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
ret.push(` entity${entityKey} = factory.createReference(targetClass, data${dataKey}.id, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} });`);
|
|
162
|
+
}
|
|
137
163
|
}
|
|
138
164
|
else {
|
|
139
|
-
|
|
165
|
+
// For regular: fixed target class, PK is the data itself
|
|
166
|
+
const targetKey = this.safeKey(`${prop.targetMeta.tableName}_${this.#tmpIndex++}`);
|
|
167
|
+
context.set(targetKey, prop.targetMeta.class);
|
|
168
|
+
if (prop.ref) {
|
|
169
|
+
ret.push(` entity${entityKey} = Reference.create(factory.createReference(${targetKey}, data${dataKey}, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} }));`);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
ret.push(` entity${entityKey} = factory.createReference(${targetKey}, data${dataKey}, { merge: true, convertCustomTypes, normalizeAccessors, schema${keyOption} });`);
|
|
173
|
+
}
|
|
140
174
|
}
|
|
141
175
|
ret.push(` } else if (data${dataKey} && typeof data${dataKey} === 'object') {`);
|
|
176
|
+
// For full entity hydration, polymorphic needs to determine target class from entity itself
|
|
177
|
+
let hydrateTargetExpr;
|
|
178
|
+
if (prop.polymorphic) {
|
|
179
|
+
hydrateTargetExpr = `data${dataKey}.constructor`;
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
const targetKey = this.safeKey(`${prop.targetMeta.tableName}_${this.#tmpIndex++}`);
|
|
183
|
+
context.set(targetKey, prop.targetMeta.class);
|
|
184
|
+
hydrateTargetExpr = targetKey;
|
|
185
|
+
}
|
|
142
186
|
if (prop.ref) {
|
|
143
|
-
ret.push(` entity${entityKey} = Reference.create(factory.${method}(
|
|
187
|
+
ret.push(` entity${entityKey} = Reference.create(factory.${method}(${hydrateTargetExpr}, data${dataKey}, { initialized: true, merge: true, newEntity, convertCustomTypes, normalizeAccessors, schema }));`);
|
|
144
188
|
}
|
|
145
189
|
else {
|
|
146
|
-
ret.push(` entity${entityKey} = factory.${method}(
|
|
190
|
+
ret.push(` entity${entityKey} = factory.${method}(${hydrateTargetExpr}, data${dataKey}, { initialized: true, merge: true, newEntity, convertCustomTypes, normalizeAccessors, schema });`);
|
|
147
191
|
}
|
|
148
192
|
ret.push(` }`);
|
|
149
193
|
ret.push(` }`);
|
|
150
194
|
if (prop.kind === ReferenceKind.ONE_TO_ONE) {
|
|
151
|
-
const meta2 = this.metadata.get(prop.
|
|
195
|
+
const meta2 = this.metadata.get(prop.targetMeta.class);
|
|
152
196
|
const prop2 = meta2.properties[prop.inversedBy || prop.mappedBy];
|
|
153
197
|
if (prop2 && !prop2.mapToPk) {
|
|
154
198
|
ret.push(` if (data${dataKey} && entity${entityKey} && !entity${entityKey}.${this.safeKey(prop2.name)}) {`);
|
|
@@ -166,7 +210,7 @@ export class ObjectHydrator extends Hydrator {
|
|
|
166
210
|
};
|
|
167
211
|
const hydrateToMany = (prop, dataKey, entityKey) => {
|
|
168
212
|
const ret = [];
|
|
169
|
-
ret.push(...this.createCollectionItemMapper(prop));
|
|
213
|
+
ret.push(...this.createCollectionItemMapper(prop, context));
|
|
170
214
|
ret.push(` if (data${dataKey} && !Array.isArray(data${dataKey}) && typeof data${dataKey} === 'object') {`);
|
|
171
215
|
ret.push(` data${dataKey} = [data${dataKey}];`);
|
|
172
216
|
ret.push(` }`);
|
|
@@ -193,7 +237,10 @@ export class ObjectHydrator extends Hydrator {
|
|
|
193
237
|
return ret;
|
|
194
238
|
};
|
|
195
239
|
const registerEmbeddedPrototype = (prop, path) => {
|
|
196
|
-
const convertorKey = path
|
|
240
|
+
const convertorKey = path
|
|
241
|
+
.filter(k => !/\[idx_\d+]/.exec(k))
|
|
242
|
+
.map(k => this.safeKey(k))
|
|
243
|
+
.join('_');
|
|
197
244
|
if (prop.targetMeta?.polymorphs) {
|
|
198
245
|
prop.targetMeta.polymorphs.forEach(meta => {
|
|
199
246
|
context.set(`prototype_${convertorKey}_${meta.className}`, meta.prototype);
|
|
@@ -240,7 +287,7 @@ export class ObjectHydrator extends Hydrator {
|
|
|
240
287
|
else {
|
|
241
288
|
ret.push(` const embeddedData = {`);
|
|
242
289
|
for (const childProp of Object.values(prop.embeddedProps)) {
|
|
243
|
-
const key = childProp.embedded[1]
|
|
290
|
+
const key = /^\w+$/.exec(childProp.embedded[1]) ? childProp.embedded[1] : `'${childProp.embedded[1]}'`;
|
|
244
291
|
ret.push(` ${key}: data${this.wrap(childProp.name)},`);
|
|
245
292
|
}
|
|
246
293
|
ret.push(` };`);
|
|
@@ -249,15 +296,18 @@ export class ObjectHydrator extends Hydrator {
|
|
|
249
296
|
prop.targetMeta.polymorphs.forEach(childMeta => {
|
|
250
297
|
const childProp = prop.embeddedProps[prop.targetMeta.discriminatorColumn];
|
|
251
298
|
const childDataKey = prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
299
|
+
context.set(childMeta.className, childMeta.class);
|
|
252
300
|
// weak comparison as we can have numbers that might have been converted to strings due to being object keys
|
|
253
301
|
ret.push(` if (data${childDataKey} == '${childMeta.discriminatorValue}') {`);
|
|
254
302
|
ret.push(` if (entity${entityKey} == null) {`);
|
|
255
|
-
ret.push(` entity${entityKey} = factory.createEmbeddable(
|
|
303
|
+
ret.push(` entity${entityKey} = factory.createEmbeddable(${childMeta.className}, embeddedData, { newEntity, convertCustomTypes, normalizeAccessors });`);
|
|
256
304
|
ret.push(` }`);
|
|
257
305
|
meta.props
|
|
258
306
|
.filter(p => p.embedded?.[0] === prop.name)
|
|
259
307
|
.forEach(childProp => {
|
|
260
|
-
const childDataKey = prop.object
|
|
308
|
+
const childDataKey = prop.object
|
|
309
|
+
? dataKey + this.wrap(childProp.embedded[1])
|
|
310
|
+
: this.wrap(childProp.name);
|
|
261
311
|
const prop2 = childMeta.properties[childProp.embedded[1]];
|
|
262
312
|
const prop3 = {
|
|
263
313
|
...prop2,
|
|
@@ -265,22 +315,26 @@ export class ObjectHydrator extends Hydrator {
|
|
|
265
315
|
embedded: childProp.embedded,
|
|
266
316
|
embeddedProps: childProp.embeddedProps,
|
|
267
317
|
};
|
|
268
|
-
|
|
269
|
-
|
|
318
|
+
ret.push(
|
|
319
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define, no-use-before-define
|
|
320
|
+
...hydrateProperty(prop3, childProp.object, [...path, childProp.embedded[1]], childDataKey).map(l => ' ' + l));
|
|
270
321
|
});
|
|
271
322
|
ret.push(` }`);
|
|
272
323
|
});
|
|
273
324
|
}
|
|
274
325
|
else {
|
|
326
|
+
const targetKey = this.safeKey(`${prop.targetMeta.tableName}_${this.#tmpIndex++}`);
|
|
327
|
+
context.set(targetKey, prop.targetMeta.class);
|
|
275
328
|
ret.push(` if (entity${entityKey} == null) {`);
|
|
276
|
-
ret.push(` entity${entityKey} = factory.createEmbeddable(
|
|
329
|
+
ret.push(` entity${entityKey} = factory.createEmbeddable(${targetKey}, embeddedData, { newEntity, convertCustomTypes, normalizeAccessors });`);
|
|
277
330
|
ret.push(` }`);
|
|
278
331
|
meta.props
|
|
279
332
|
.filter(p => p.embedded?.[0] === prop.name)
|
|
280
333
|
.forEach(childProp => {
|
|
281
334
|
const childDataKey = prop.object ? dataKey + this.wrap(childProp.embedded[1]) : this.wrap(childProp.name);
|
|
282
|
-
|
|
283
|
-
|
|
335
|
+
ret.push(
|
|
336
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define, no-use-before-define
|
|
337
|
+
...hydrateProperty(childProp, prop.object, [...path, childProp.embedded[1]], childDataKey).map(l => ' ' + l));
|
|
284
338
|
});
|
|
285
339
|
}
|
|
286
340
|
/* v8 ignore next */
|
|
@@ -298,7 +352,7 @@ export class ObjectHydrator extends Hydrator {
|
|
|
298
352
|
const hydrateEmbeddedArray = (prop, path, dataKey) => {
|
|
299
353
|
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
300
354
|
const ret = [];
|
|
301
|
-
const idx = this
|
|
355
|
+
const idx = this.#tmpIndex++;
|
|
302
356
|
registerEmbeddedPrototype(prop, path);
|
|
303
357
|
parseObjectEmbeddable(prop, dataKey, ret);
|
|
304
358
|
ret.push(` if (Array.isArray(data${dataKey})) {`);
|
|
@@ -311,7 +365,8 @@ export class ObjectHydrator extends Hydrator {
|
|
|
311
365
|
};
|
|
312
366
|
const hydrateProperty = (prop, object = prop.object, path = [prop.name], dataKey) => {
|
|
313
367
|
const entityKey = path.map(k => this.wrap(k)).join('');
|
|
314
|
-
dataKey =
|
|
368
|
+
dataKey =
|
|
369
|
+
dataKey ?? (object ? entityKey : this.wrap(normalizeAccessors ? (prop.accessor ?? prop.name) : prop.name));
|
|
315
370
|
const ret = [];
|
|
316
371
|
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && !prop.mapToPk) {
|
|
317
372
|
ret.push(...hydrateToOne(prop, dataKey, entityKey));
|
|
@@ -330,8 +385,9 @@ export class ObjectHydrator extends Hydrator {
|
|
|
330
385
|
}
|
|
331
386
|
}
|
|
332
387
|
}
|
|
333
|
-
else {
|
|
334
|
-
|
|
388
|
+
else {
|
|
389
|
+
// ReferenceKind.SCALAR
|
|
390
|
+
ret.push(...hydrateScalar(prop, path, dataKey));
|
|
335
391
|
}
|
|
336
392
|
if (this.config.get('forceUndefined')) {
|
|
337
393
|
ret.push(` if (data${dataKey} === null) entity${entityKey} = undefined;`);
|
|
@@ -341,15 +397,16 @@ export class ObjectHydrator extends Hydrator {
|
|
|
341
397
|
for (const prop of props) {
|
|
342
398
|
lines.push(...hydrateProperty(prop));
|
|
343
399
|
}
|
|
344
|
-
const code = `// compiled hydrator for entity ${meta.className} (${type})\n`
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
const
|
|
348
|
-
this.
|
|
400
|
+
const code = `// compiled hydrator for entity ${meta.className} (${type + normalizeAccessors ? ' normalized' : ''})\n` +
|
|
401
|
+
`return function(entity, data, factory, newEntity, convertCustomTypes, schema, parentSchema, normalizeAccessors) {\n` +
|
|
402
|
+
`${lines.join('\n')}\n}`;
|
|
403
|
+
const fnKey = `hydrator-${meta.uniqueName}-${type}-${normalizeAccessors}`;
|
|
404
|
+
const hydrator = Utils.createFunction(context, code, this.config.get('compiledFunctions'), fnKey);
|
|
405
|
+
this.#hydrators[key].set(meta.class, hydrator);
|
|
349
406
|
return hydrator;
|
|
350
407
|
}
|
|
351
|
-
createCollectionItemMapper(prop) {
|
|
352
|
-
const meta = this.metadata.get(prop.
|
|
408
|
+
createCollectionItemMapper(prop, context) {
|
|
409
|
+
const meta = this.metadata.get(prop.targetMeta.class);
|
|
353
410
|
const lines = [];
|
|
354
411
|
lines.push(` const createCollectionItem_${this.safeKey(prop.name)} = (value, entity) => {`);
|
|
355
412
|
const prop2 = prop.targetMeta.properties[prop.mappedBy];
|
|
@@ -358,17 +415,19 @@ export class ObjectHydrator extends Hydrator {
|
|
|
358
415
|
lines.push(` value = { ...value, ['${prop2.name}']: Reference.wrapReference(entity, { ref: ${prop2.ref} }) };`);
|
|
359
416
|
lines.push(` }`);
|
|
360
417
|
}
|
|
361
|
-
|
|
418
|
+
const targetKey = this.safeKey(`${prop.targetMeta.tableName}_${this.#tmpIndex++}`);
|
|
419
|
+
context.set(targetKey, prop.targetMeta.class);
|
|
420
|
+
lines.push(` if (isPrimaryKey(value, ${meta.compositePK})) return factory.createReference(${targetKey}, value, { convertCustomTypes, schema, normalizeAccessors, merge: true });`);
|
|
362
421
|
lines.push(` if (value && value.__entity) return value;`);
|
|
363
|
-
lines.push(` return factory.create(
|
|
422
|
+
lines.push(` return factory.create(${targetKey}, value, { newEntity, convertCustomTypes, schema, normalizeAccessors, merge: true });`);
|
|
364
423
|
lines.push(` }`);
|
|
365
424
|
return lines;
|
|
366
425
|
}
|
|
367
426
|
wrap(key) {
|
|
368
|
-
if (
|
|
427
|
+
if (/^\[.*]$/.exec(key)) {
|
|
369
428
|
return key;
|
|
370
429
|
}
|
|
371
|
-
return
|
|
430
|
+
return /^\w+$/.exec(key) ? `.${key}` : `['${key}']`;
|
|
372
431
|
}
|
|
373
432
|
safeKey(key) {
|
|
374
433
|
return key.replace(/\W/g, '_');
|
package/index.d.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* @packageDocumentation
|
|
3
3
|
* @module core
|
|
4
4
|
*/
|
|
5
|
-
export {
|
|
5
|
+
export { EntityMetadata, PrimaryKeyProp, EntityRepositoryType, OptionalProps, EagerProps, HiddenProps, Config, EntityName, } from './typings.js';
|
|
6
|
+
export type { CompiledFunctions, Constructor, ConnectionType, Dictionary, Primary, IPrimaryKey, ObjectQuery, FilterQuery, IWrappedEntity, InferEntityName, EntityData, Highlighter, MaybePromise, AnyEntity, EntityClass, EntityProperty, PopulateOptions, Populate, Loaded, New, LoadedReference, LoadedCollection, IMigrator, IMigrationGenerator, MigratorEvent, GetRepository, MigrationObject, DeepPartial, PrimaryProperty, Cast, IsUnknown, EntityDictionary, EntityDTO, EntityDTOFlat, EntityDTOProp, SerializeDTO, MigrationDiff, GenerateOptions, FilterObject, IMigrationRunner, IEntityGenerator, ISeedManager, SeederObject, IMigratorStorage, RequiredEntityData, CheckCallback, IndexCallback, FormulaCallback, FormulaTable, SchemaTable, SchemaColumns, SimpleColumnMeta, Rel, Ref, ScalarRef, EntityRef, ISchemaGenerator, MigrationInfo, MigrateOptions, MigrationResult, MigrationRow, EntityKey, EntityValue, EntityDataValue, FilterKey, EntityType, FromEntityType, Selected, IsSubset, EntityProps, ExpandProperty, ExpandScalar, FilterItemValue, ExpandQuery, Scalar, ExpandHint, FilterValue, MergeLoaded, MergeSelected, TypeConfig, AnyString, ClearDatabaseOptions, CreateSchemaOptions, EnsureDatabaseOptions, UpdateSchemaOptions, DropSchemaOptions, RefreshDatabaseOptions, AutoPath, UnboxArray, MetadataProcessor, ImportsResolver, RequiredNullable, DefineConfig, Opt, Hidden, EntitySchemaWithMeta, InferEntity, CheckConstraint, GeneratedColumnCallback, FilterDef, EntityCtor, Subquery, PopulateHintOptions, Prefixes, } from './typings.js';
|
|
6
7
|
export * from './enums.js';
|
|
7
8
|
export * from './errors.js';
|
|
8
9
|
export * from './exceptions.js';
|
|
@@ -22,4 +23,3 @@ export * from './types/index.js';
|
|
|
22
23
|
export * from './naming-strategy/index.js';
|
|
23
24
|
export * from './metadata/index.js';
|
|
24
25
|
export * from './cache/index.js';
|
|
25
|
-
export * from './decorators/index.js';
|
package/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @packageDocumentation
|
|
3
3
|
* @module core
|
|
4
4
|
*/
|
|
5
|
-
export {
|
|
5
|
+
export { EntityMetadata, PrimaryKeyProp, EntityRepositoryType, OptionalProps, EagerProps, HiddenProps, Config, EntityName, } from './typings.js';
|
|
6
6
|
export * from './enums.js';
|
|
7
7
|
export * from './errors.js';
|
|
8
8
|
export * from './exceptions.js';
|
|
@@ -22,4 +22,3 @@ export * from './types/index.js';
|
|
|
22
22
|
export * from './naming-strategy/index.js';
|
|
23
23
|
export * from './metadata/index.js';
|
|
24
24
|
export * from './cache/index.js';
|
|
25
|
-
export * from './decorators/index.js';
|
package/logging/DefaultLogger.js
CHANGED
|
@@ -29,9 +29,7 @@ export class DefaultLogger {
|
|
|
29
29
|
if (context?.level === 'warning') {
|
|
30
30
|
message = colors.yellow(message);
|
|
31
31
|
}
|
|
32
|
-
const label = context?.label
|
|
33
|
-
? colors.cyan(`(${context.label}) `)
|
|
34
|
-
: '';
|
|
32
|
+
const label = context?.label ? colors.cyan(`(${context.label}) `) : '';
|
|
35
33
|
this.writer(colors.grey(`[${namespace}] `) + label + message);
|
|
36
34
|
}
|
|
37
35
|
/**
|
|
@@ -60,7 +58,8 @@ export class DefaultLogger {
|
|
|
60
58
|
if (namespace === 'deprecated') {
|
|
61
59
|
const { ignoreDeprecations = false } = this.options;
|
|
62
60
|
return Array.isArray(ignoreDeprecations)
|
|
63
|
-
?
|
|
61
|
+
? /* v8 ignore next */
|
|
62
|
+
!ignoreDeprecations.includes(context?.label ?? '')
|
|
64
63
|
: !ignoreDeprecations;
|
|
65
64
|
}
|
|
66
65
|
return !!debugMode && (!Array.isArray(debugMode) || debugMode.includes(namespace));
|
package/logging/colors.d.ts
CHANGED
package/logging/colors.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
&& boolIfDefined(process.env.FORCE_COLOR)
|
|
6
|
-
&& boolIfDefined(process.env.MIKRO_ORM_COLORS);
|
|
1
|
+
import { getEnv } from '../utils/env-vars.js';
|
|
2
|
+
const bool = (k) => ['true', 't', '1'].includes(getEnv(k)?.toLowerCase() ?? '');
|
|
3
|
+
const boolIfDefined = (k) => (getEnv(k) != null ? bool(k) : true);
|
|
4
|
+
const enabled = () => !bool('NO_COLOR') && !bool('MIKRO_ORM_NO_COLOR') && boolIfDefined('FORCE_COLOR') && boolIfDefined('MIKRO_ORM_COLORS');
|
|
7
5
|
const wrap = (fn) => (text) => enabled() ? fn(text) : text;
|
|
8
6
|
/** @internal */
|
|
9
7
|
export const colors = {
|
package/logging/index.d.ts
CHANGED
package/logging/index.js
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
let nodeInspect;
|
|
2
|
+
/** @internal */
|
|
3
|
+
export function inspect(value, options) {
|
|
4
|
+
nodeInspect ??= globalThis.process?.getBuiltinModule?.('node:util').inspect;
|
|
5
|
+
/* v8 ignore else */
|
|
6
|
+
if (nodeInspect) {
|
|
7
|
+
return nodeInspect(value, options);
|
|
8
|
+
}
|
|
9
|
+
/* v8 ignore next */
|
|
10
|
+
return JSON.stringify(value, null, 2);
|
|
11
|
+
}
|