@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,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Ref = exports.rel = exports.ref = exports.ScalarReference = exports.Reference = void 0;
|
|
4
|
+
const util_1 = require("util");
|
|
5
|
+
const enums_1 = require("../enums");
|
|
6
|
+
const wrap_1 = require("./wrap");
|
|
7
|
+
const utils_1 = require("../utils");
|
|
8
|
+
class Reference {
|
|
9
|
+
entity;
|
|
10
|
+
constructor(entity) {
|
|
11
|
+
this.entity = entity;
|
|
12
|
+
this.set(entity);
|
|
13
|
+
const meta = (0, wrap_1.helper)(this.entity).__meta;
|
|
14
|
+
meta.primaryKeys.forEach(primaryKey => {
|
|
15
|
+
Object.defineProperty(this, primaryKey, {
|
|
16
|
+
get() {
|
|
17
|
+
return this.entity[primaryKey];
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
if (meta.serializedPrimaryKey && meta.primaryKeys[0] !== meta.serializedPrimaryKey) {
|
|
22
|
+
Object.defineProperty(this, meta.serializedPrimaryKey, {
|
|
23
|
+
get() {
|
|
24
|
+
return (0, wrap_1.helper)(this.entity).getSerializedPrimaryKey();
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
static create(entity) {
|
|
30
|
+
const unwrapped = Reference.unwrapReference(entity);
|
|
31
|
+
const ref = (0, wrap_1.helper)(entity).toReference();
|
|
32
|
+
if (unwrapped !== ref.unwrap()) {
|
|
33
|
+
ref.set(unwrapped);
|
|
34
|
+
}
|
|
35
|
+
return ref;
|
|
36
|
+
}
|
|
37
|
+
static createFromPK(entityType, pk, options) {
|
|
38
|
+
const ref = this.createNakedFromPK(entityType, pk, options);
|
|
39
|
+
return (0, wrap_1.helper)(ref).toReference();
|
|
40
|
+
}
|
|
41
|
+
static createNakedFromPK(entityType, pk, options) {
|
|
42
|
+
const factory = entityType.prototype.__factory;
|
|
43
|
+
const entity = factory.createReference(entityType, pk, {
|
|
44
|
+
merge: false,
|
|
45
|
+
convertCustomTypes: false,
|
|
46
|
+
...options,
|
|
47
|
+
});
|
|
48
|
+
const wrapped = (0, wrap_1.helper)(entity);
|
|
49
|
+
wrapped.__meta.primaryKeys.forEach(key => wrapped.__loadedProperties.add(key));
|
|
50
|
+
wrapped.__originalEntityData = factory.getComparator().prepareEntity(entity);
|
|
51
|
+
return entity;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Checks whether the argument is instance of `Reference` wrapper.
|
|
55
|
+
*/
|
|
56
|
+
static isReference(data) {
|
|
57
|
+
return data && !!data.__reference;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Wraps the entity in a `Reference` wrapper if the property is defined as `ref`.
|
|
61
|
+
*/
|
|
62
|
+
static wrapReference(entity, prop) {
|
|
63
|
+
if (entity && prop.ref && !Reference.isReference(entity)) {
|
|
64
|
+
return Reference.create(entity);
|
|
65
|
+
}
|
|
66
|
+
return entity;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Returns wrapped entity.
|
|
70
|
+
*/
|
|
71
|
+
static unwrapReference(ref) {
|
|
72
|
+
return Reference.isReference(ref) ? ref.unwrap() : ref;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded). Returns the entity.
|
|
76
|
+
* If the entity is not found in the database (e.g. it was deleted in the meantime, or currently active filters disallow loading of it)
|
|
77
|
+
* the method returns `null`. Use `loadOrFail()` if you want an error to be thrown in such a case.
|
|
78
|
+
*/
|
|
79
|
+
async load(options = {}) {
|
|
80
|
+
if (!this.isInitialized() || options.refresh) {
|
|
81
|
+
const wrapped = (0, wrap_1.helper)(this.entity);
|
|
82
|
+
if (!wrapped.__em) {
|
|
83
|
+
return this.entity;
|
|
84
|
+
}
|
|
85
|
+
if (options.dataloader ?? [enums_1.DataloaderType.ALL, enums_1.DataloaderType.REFERENCE].includes(utils_1.DataloaderUtils.getDataloaderType(wrapped.__em.config.get('dataloader')))) {
|
|
86
|
+
// eslint-disable-next-line dot-notation
|
|
87
|
+
return wrapped.__em['refLoader'].load([this, options]);
|
|
88
|
+
}
|
|
89
|
+
return wrapped.init(options);
|
|
90
|
+
}
|
|
91
|
+
return this.entity;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
95
|
+
* Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).
|
|
96
|
+
*/
|
|
97
|
+
async loadOrFail(options = {}) {
|
|
98
|
+
const ret = await this.load(options);
|
|
99
|
+
if (!ret) {
|
|
100
|
+
const wrapped = (0, wrap_1.helper)(this.entity);
|
|
101
|
+
options.failHandler ??= wrapped.__em.config.get('findOneOrFailHandler');
|
|
102
|
+
const entityName = this.entity.constructor.name;
|
|
103
|
+
const where = wrapped.getPrimaryKey();
|
|
104
|
+
throw options.failHandler(entityName, where);
|
|
105
|
+
}
|
|
106
|
+
return ret;
|
|
107
|
+
}
|
|
108
|
+
set(entity) {
|
|
109
|
+
this.entity = Reference.unwrapReference(entity);
|
|
110
|
+
delete (0, wrap_1.helper)(this.entity).__reference;
|
|
111
|
+
}
|
|
112
|
+
unwrap() {
|
|
113
|
+
return this.entity;
|
|
114
|
+
}
|
|
115
|
+
getEntity() {
|
|
116
|
+
if (!this.isInitialized()) {
|
|
117
|
+
throw new Error(`Reference<${(0, wrap_1.helper)(this.entity).__meta.name}> ${(0, wrap_1.helper)(this.entity).getPrimaryKey()} not initialized`);
|
|
118
|
+
}
|
|
119
|
+
return this.entity;
|
|
120
|
+
}
|
|
121
|
+
getProperty(prop) {
|
|
122
|
+
return this.getEntity()[prop];
|
|
123
|
+
}
|
|
124
|
+
async loadProperty(prop, options) {
|
|
125
|
+
await this.loadOrFail(options);
|
|
126
|
+
return this.getEntity()[prop];
|
|
127
|
+
}
|
|
128
|
+
isInitialized() {
|
|
129
|
+
return (0, wrap_1.helper)(this.entity).__initialized;
|
|
130
|
+
}
|
|
131
|
+
populated(populated) {
|
|
132
|
+
(0, wrap_1.helper)(this.entity).populated(populated);
|
|
133
|
+
}
|
|
134
|
+
toJSON(...args) {
|
|
135
|
+
return (0, wrap_1.wrap)(this.entity).toJSON(...args);
|
|
136
|
+
}
|
|
137
|
+
/* istanbul ignore next */
|
|
138
|
+
/** @ignore */
|
|
139
|
+
[util_1.inspect.custom](depth) {
|
|
140
|
+
const object = { ...this };
|
|
141
|
+
const hidden = ['meta'];
|
|
142
|
+
hidden.forEach(k => delete object[k]);
|
|
143
|
+
const ret = (0, util_1.inspect)(object, { depth });
|
|
144
|
+
const wrapped = (0, wrap_1.helper)(this.entity);
|
|
145
|
+
const meta = wrapped.__meta;
|
|
146
|
+
const pk = wrapped.hasPrimaryKey() ? '<' + wrapped.getSerializedPrimaryKey() + '>' : '';
|
|
147
|
+
const name = `Ref<${meta.className}${pk}>`;
|
|
148
|
+
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.Reference = Reference;
|
|
152
|
+
exports.Ref = Reference;
|
|
153
|
+
class ScalarReference {
|
|
154
|
+
value;
|
|
155
|
+
initialized;
|
|
156
|
+
entity;
|
|
157
|
+
property;
|
|
158
|
+
constructor(value, initialized = value != null) {
|
|
159
|
+
this.value = value;
|
|
160
|
+
this.initialized = initialized;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
164
|
+
* Returns either the whole entity, or the requested property.
|
|
165
|
+
*/
|
|
166
|
+
async load(options) {
|
|
167
|
+
const opts = typeof options === 'object' ? options : { prop: options };
|
|
168
|
+
if (!this.initialized || opts.refresh) {
|
|
169
|
+
if (this.entity == null || this.property == null) {
|
|
170
|
+
throw new Error('Cannot load scalar reference that is not bound to an entity property.');
|
|
171
|
+
}
|
|
172
|
+
await (0, wrap_1.helper)(this.entity).populate([this.property], opts);
|
|
173
|
+
}
|
|
174
|
+
return this.value;
|
|
175
|
+
}
|
|
176
|
+
set(value) {
|
|
177
|
+
this.value = value;
|
|
178
|
+
this.initialized = true;
|
|
179
|
+
}
|
|
180
|
+
bind(entity, property) {
|
|
181
|
+
this.entity = entity;
|
|
182
|
+
this.property = property;
|
|
183
|
+
Object.defineProperty(this, 'entity', { enumerable: false, value: entity });
|
|
184
|
+
}
|
|
185
|
+
unwrap() {
|
|
186
|
+
return this.value;
|
|
187
|
+
}
|
|
188
|
+
isInitialized() {
|
|
189
|
+
return this.initialized;
|
|
190
|
+
}
|
|
191
|
+
/* istanbul ignore next */
|
|
192
|
+
/** @ignore */
|
|
193
|
+
[util_1.inspect.custom]() {
|
|
194
|
+
return this.initialized ? `Ref<${(0, util_1.inspect)(this.value)}>` : `Ref<?>`;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
exports.ScalarReference = ScalarReference;
|
|
198
|
+
Object.defineProperties(Reference.prototype, {
|
|
199
|
+
__reference: { value: true, enumerable: false },
|
|
200
|
+
__meta: { get() { return this.entity.__meta; } },
|
|
201
|
+
__platform: { get() { return this.entity.__platform; } },
|
|
202
|
+
__helper: { get() { return this.entity.__helper; } },
|
|
203
|
+
$: { get() { return this.entity; } },
|
|
204
|
+
get: { get() { return () => this.entity; } },
|
|
205
|
+
});
|
|
206
|
+
Object.defineProperties(ScalarReference.prototype, {
|
|
207
|
+
__scalarReference: { value: true, enumerable: false },
|
|
208
|
+
$: { get() { return this.value; } },
|
|
209
|
+
get: { get() { return () => this.value; } },
|
|
210
|
+
});
|
|
211
|
+
/**
|
|
212
|
+
* shortcut for `wrap(entity).toReference()`
|
|
213
|
+
*/
|
|
214
|
+
function ref(entityOrType, pk) {
|
|
215
|
+
if (entityOrType == null) {
|
|
216
|
+
return entityOrType;
|
|
217
|
+
}
|
|
218
|
+
if (utils_1.Utils.isEntity(entityOrType, true)) {
|
|
219
|
+
return (0, wrap_1.helper)(entityOrType).toReference();
|
|
220
|
+
}
|
|
221
|
+
if (utils_1.Utils.isEntity(pk, true)) {
|
|
222
|
+
return (0, wrap_1.helper)(pk).toReference();
|
|
223
|
+
}
|
|
224
|
+
if (arguments.length === 1) {
|
|
225
|
+
return new ScalarReference(entityOrType, true);
|
|
226
|
+
}
|
|
227
|
+
if (pk == null) {
|
|
228
|
+
return pk;
|
|
229
|
+
}
|
|
230
|
+
return Reference.createFromPK(entityOrType, pk);
|
|
231
|
+
}
|
|
232
|
+
exports.ref = ref;
|
|
233
|
+
/**
|
|
234
|
+
* shortcut for `Reference.createNakedFromPK(entityType, pk)`
|
|
235
|
+
*/
|
|
236
|
+
function rel(entityType, pk) {
|
|
237
|
+
if (pk == null || utils_1.Utils.isEntity(pk)) {
|
|
238
|
+
return pk;
|
|
239
|
+
}
|
|
240
|
+
return Reference.createNakedFromPK(entityType, pk);
|
|
241
|
+
}
|
|
242
|
+
exports.rel = rel;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { inspect } from 'util';
|
|
3
|
+
import type { EntityManager } from '../EntityManager';
|
|
4
|
+
import type { Dictionary, EntityData, EntityDictionary, EntityMetadata, IHydrator, EntityKey, PopulateOptions, Primary, AutoPath, Ref, AddEager, LoadedReference, EntityDTO, Loaded, FromEntityType, IsSubset, MergeSelected } from '../typings';
|
|
5
|
+
import { Reference } from './Reference';
|
|
6
|
+
import { type AssignOptions } from './EntityAssigner';
|
|
7
|
+
import type { EntityLoaderOptions } from './EntityLoader';
|
|
8
|
+
import type { EntityIdentifier } from './EntityIdentifier';
|
|
9
|
+
import type { SerializationContext } from '../serialization/SerializationContext';
|
|
10
|
+
import { type SerializeOptions } from '../serialization/EntitySerializer';
|
|
11
|
+
import type { FindOneOptions } from '../drivers/IDatabaseDriver';
|
|
12
|
+
export declare class WrappedEntity<Entity extends object> {
|
|
13
|
+
__initialized: boolean;
|
|
14
|
+
__touched: boolean;
|
|
15
|
+
__populated?: boolean;
|
|
16
|
+
__managed?: boolean;
|
|
17
|
+
__onLoadFired?: boolean;
|
|
18
|
+
__schema?: string;
|
|
19
|
+
__em?: EntityManager;
|
|
20
|
+
__serializationContext: {
|
|
21
|
+
root?: SerializationContext<Entity>;
|
|
22
|
+
populate?: PopulateOptions<Entity>[];
|
|
23
|
+
fields?: string[];
|
|
24
|
+
};
|
|
25
|
+
__loadedProperties: Set<string>;
|
|
26
|
+
__data: Dictionary;
|
|
27
|
+
__processing: boolean;
|
|
28
|
+
/** stores last known primary key, as its current state might be broken due to propagation/orphan removal, but we need to know the PK to be able t remove the entity */
|
|
29
|
+
__pk?: Primary<Entity>;
|
|
30
|
+
/** holds the reference wrapper instance (if created), so we can maintain the identity on reference wrappers too */
|
|
31
|
+
__reference?: Reference<Entity>;
|
|
32
|
+
/** holds last entity data snapshot, so we can compute changes when persisting managed entities */
|
|
33
|
+
__originalEntityData?: EntityData<Entity>;
|
|
34
|
+
/** holds wrapped primary key, so we can compute change set without eager commit */
|
|
35
|
+
__identifier?: EntityIdentifier;
|
|
36
|
+
private readonly entity;
|
|
37
|
+
private readonly hydrator;
|
|
38
|
+
private readonly pkGetter?;
|
|
39
|
+
private readonly pkSerializer?;
|
|
40
|
+
private readonly pkGetterConverted?;
|
|
41
|
+
constructor(entity: Entity, hydrator: IHydrator, pkGetter?: (e: Entity) => Primary<Entity>, pkSerializer?: (e: Entity) => string, pkGetterConverted?: (e: Entity) => Primary<Entity>);
|
|
42
|
+
isInitialized(): boolean;
|
|
43
|
+
isTouched(): boolean;
|
|
44
|
+
isManaged(): boolean;
|
|
45
|
+
populated(populated?: boolean | undefined): void;
|
|
46
|
+
toReference(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
|
|
47
|
+
toObject<Ignored extends EntityKey<Entity> = never>(ignoreFields?: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
|
|
48
|
+
serialize<Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Entity, Hint, Exclude>): EntityDTO<Loaded<Entity, Hint>>;
|
|
49
|
+
toPOJO(): EntityDTO<Entity>;
|
|
50
|
+
toJSON(...args: any[]): EntityDictionary<Entity>;
|
|
51
|
+
assign<Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Data extends EntityData<Naked> | Partial<EntityDTO<Naked>> = EntityData<Naked> | Partial<EntityDTO<Naked>>>(data: Data & IsSubset<EntityData<Naked>, Data>, options?: AssignOptions): MergeSelected<Entity, Naked, keyof Data & string>;
|
|
52
|
+
init<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(options?: FindOneOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>;
|
|
53
|
+
populate<Hint extends string = never>(populate: AutoPath<Entity, Hint, '*'>[] | false, options?: EntityLoaderOptions<Entity>): Promise<Loaded<Entity, Hint>>;
|
|
54
|
+
hasPrimaryKey(): boolean;
|
|
55
|
+
getPrimaryKey(convertCustomTypes?: boolean): Primary<Entity> | null;
|
|
56
|
+
getPrimaryKeys(convertCustomTypes?: boolean): Primary<Entity>[] | null;
|
|
57
|
+
getSchema(): string | undefined;
|
|
58
|
+
setSchema(schema?: string): void;
|
|
59
|
+
setPrimaryKey(id: Primary<Entity> | null): void;
|
|
60
|
+
getSerializedPrimaryKey(): string;
|
|
61
|
+
get __meta(): EntityMetadata<Entity>;
|
|
62
|
+
get __platform(): import("..").Platform;
|
|
63
|
+
get __config(): import("..").Configuration<import("../drivers/IDatabaseDriver").IDatabaseDriver<import("..").Connection>, EntityManager<import("../drivers/IDatabaseDriver").IDatabaseDriver<import("..").Connection>>>;
|
|
64
|
+
get __primaryKeys(): Primary<Entity>[];
|
|
65
|
+
/** @ignore */
|
|
66
|
+
[inspect.custom](): string;
|
|
67
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WrappedEntity = void 0;
|
|
4
|
+
const util_1 = require("util");
|
|
5
|
+
const Reference_1 = require("./Reference");
|
|
6
|
+
const EntityTransformer_1 = require("../serialization/EntityTransformer");
|
|
7
|
+
const EntityAssigner_1 = require("./EntityAssigner");
|
|
8
|
+
const Utils_1 = require("../utils/Utils");
|
|
9
|
+
const errors_1 = require("../errors");
|
|
10
|
+
const wrap_1 = require("./wrap");
|
|
11
|
+
const EntitySerializer_1 = require("../serialization/EntitySerializer");
|
|
12
|
+
class WrappedEntity {
|
|
13
|
+
constructor(entity, hydrator, pkGetter, pkSerializer, pkGetterConverted) {
|
|
14
|
+
this.entity = entity;
|
|
15
|
+
this.hydrator = hydrator;
|
|
16
|
+
this.pkGetter = pkGetter;
|
|
17
|
+
this.pkSerializer = pkSerializer;
|
|
18
|
+
this.pkGetterConverted = pkGetterConverted;
|
|
19
|
+
this.__initialized = true;
|
|
20
|
+
this.__touched = false;
|
|
21
|
+
this.__serializationContext = {};
|
|
22
|
+
this.__loadedProperties = new Set();
|
|
23
|
+
this.__data = {};
|
|
24
|
+
this.__processing = false;
|
|
25
|
+
}
|
|
26
|
+
isInitialized() {
|
|
27
|
+
return this.__initialized;
|
|
28
|
+
}
|
|
29
|
+
isTouched() {
|
|
30
|
+
return this.__touched;
|
|
31
|
+
}
|
|
32
|
+
isManaged() {
|
|
33
|
+
return !!this.__managed;
|
|
34
|
+
}
|
|
35
|
+
populated(populated = true) {
|
|
36
|
+
this.__populated = populated;
|
|
37
|
+
}
|
|
38
|
+
toReference() {
|
|
39
|
+
this.__reference ??= new Reference_1.Reference(this.entity);
|
|
40
|
+
return this.__reference;
|
|
41
|
+
}
|
|
42
|
+
toObject(ignoreFields) {
|
|
43
|
+
return EntityTransformer_1.EntityTransformer.toObject(this.entity, ignoreFields);
|
|
44
|
+
}
|
|
45
|
+
serialize(options) {
|
|
46
|
+
return EntitySerializer_1.EntitySerializer.serialize(this.entity, options);
|
|
47
|
+
}
|
|
48
|
+
toPOJO() {
|
|
49
|
+
return EntityTransformer_1.EntityTransformer.toObject(this.entity, [], true);
|
|
50
|
+
}
|
|
51
|
+
toJSON(...args) {
|
|
52
|
+
// toJSON methods is added to the prototype during discovery to support automatic serialization via JSON.stringify()
|
|
53
|
+
return this.entity.toJSON(...args);
|
|
54
|
+
}
|
|
55
|
+
assign(data, options) {
|
|
56
|
+
if ('assign' in this.entity) {
|
|
57
|
+
return this.entity.assign(data, options);
|
|
58
|
+
}
|
|
59
|
+
return EntityAssigner_1.EntityAssigner.assign(this.entity, data, options);
|
|
60
|
+
}
|
|
61
|
+
async init(options) {
|
|
62
|
+
if (!this.__em) {
|
|
63
|
+
throw errors_1.ValidationError.entityNotManaged(this.entity);
|
|
64
|
+
}
|
|
65
|
+
return this.__em.findOne(this.entity.constructor.name, this.entity, { ...options, refresh: true, schema: this.__schema });
|
|
66
|
+
}
|
|
67
|
+
async populate(populate, options = {}) {
|
|
68
|
+
if (!this.__em) {
|
|
69
|
+
throw errors_1.ValidationError.entityNotManaged(this.entity);
|
|
70
|
+
}
|
|
71
|
+
// @ts-ignore hard to type
|
|
72
|
+
await this.__em.populate(this.entity, populate, options);
|
|
73
|
+
return this.entity;
|
|
74
|
+
}
|
|
75
|
+
hasPrimaryKey() {
|
|
76
|
+
const pk = this.getPrimaryKey();
|
|
77
|
+
return pk != null;
|
|
78
|
+
}
|
|
79
|
+
getPrimaryKey(convertCustomTypes = false) {
|
|
80
|
+
const prop = this.__meta.getPrimaryProps()[0];
|
|
81
|
+
if (!prop) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
if (this.__pk != null && this.__meta.compositePK) {
|
|
85
|
+
return Utils_1.Utils.getCompositeKeyValue(this.__pk, this.__meta, convertCustomTypes ? 'convertToDatabaseValue' : false, this.__platform);
|
|
86
|
+
}
|
|
87
|
+
if (convertCustomTypes && this.__pk != null && prop.customType) {
|
|
88
|
+
return prop.customType.convertToDatabaseValue(this.__pk, this.__platform);
|
|
89
|
+
}
|
|
90
|
+
if (convertCustomTypes) {
|
|
91
|
+
return this.__pk ?? this.pkGetterConverted(this.entity);
|
|
92
|
+
}
|
|
93
|
+
return this.__pk ?? this.pkGetter(this.entity);
|
|
94
|
+
}
|
|
95
|
+
// this method is currently used only in `Driver.syncCollection` and can be probably removed
|
|
96
|
+
getPrimaryKeys(convertCustomTypes = false) {
|
|
97
|
+
const pk = this.getPrimaryKey(convertCustomTypes);
|
|
98
|
+
if (pk == null) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
if (this.__meta.compositePK) {
|
|
102
|
+
return this.__meta.primaryKeys.reduce((ret, pk) => {
|
|
103
|
+
const child = this.entity[pk];
|
|
104
|
+
if (Utils_1.Utils.isEntity(child, true)) {
|
|
105
|
+
const childPk = (0, wrap_1.helper)(child).getPrimaryKeys(convertCustomTypes);
|
|
106
|
+
ret.push(...childPk);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
ret.push(child);
|
|
110
|
+
}
|
|
111
|
+
return ret;
|
|
112
|
+
}, []);
|
|
113
|
+
}
|
|
114
|
+
return [pk];
|
|
115
|
+
}
|
|
116
|
+
getSchema() {
|
|
117
|
+
return this.__schema;
|
|
118
|
+
}
|
|
119
|
+
setSchema(schema) {
|
|
120
|
+
this.__schema = schema;
|
|
121
|
+
}
|
|
122
|
+
setPrimaryKey(id) {
|
|
123
|
+
this.entity[this.__meta.primaryKeys[0]] = id;
|
|
124
|
+
this.__pk = id;
|
|
125
|
+
}
|
|
126
|
+
getSerializedPrimaryKey() {
|
|
127
|
+
return this.pkSerializer(this.entity);
|
|
128
|
+
}
|
|
129
|
+
get __meta() {
|
|
130
|
+
return this.entity.__meta;
|
|
131
|
+
}
|
|
132
|
+
get __platform() {
|
|
133
|
+
return this.entity.__platform;
|
|
134
|
+
}
|
|
135
|
+
get __config() {
|
|
136
|
+
return this.__em?.config ?? this.entity.__config;
|
|
137
|
+
}
|
|
138
|
+
get __primaryKeys() {
|
|
139
|
+
return Utils_1.Utils.getPrimaryKeyValues(this.entity, this.__meta.primaryKeys);
|
|
140
|
+
}
|
|
141
|
+
/** @ignore */
|
|
142
|
+
[util_1.inspect.custom]() {
|
|
143
|
+
return `[WrappedEntity<${this.__meta.className}>]`;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.WrappedEntity = WrappedEntity;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './EntityRepository';
|
|
2
|
+
export * from './EntityIdentifier';
|
|
3
|
+
export * from './EntityValidator';
|
|
4
|
+
export * from './EntityAssigner';
|
|
5
|
+
export * from './EntityHelper';
|
|
6
|
+
export * from './EntityFactory';
|
|
7
|
+
export * from './ArrayCollection';
|
|
8
|
+
export * from './Collection';
|
|
9
|
+
export * from './EntityLoader';
|
|
10
|
+
export * from './Reference';
|
|
11
|
+
export * from './BaseEntity';
|
|
12
|
+
export * from './WrappedEntity';
|
|
13
|
+
export * from './wrap';
|
package/entity/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./EntityRepository"), exports);
|
|
18
|
+
__exportStar(require("./EntityIdentifier"), exports);
|
|
19
|
+
__exportStar(require("./EntityValidator"), exports);
|
|
20
|
+
__exportStar(require("./EntityAssigner"), exports);
|
|
21
|
+
__exportStar(require("./EntityHelper"), exports);
|
|
22
|
+
__exportStar(require("./EntityFactory"), exports);
|
|
23
|
+
__exportStar(require("./ArrayCollection"), exports);
|
|
24
|
+
__exportStar(require("./Collection"), exports);
|
|
25
|
+
__exportStar(require("./EntityLoader"), exports);
|
|
26
|
+
__exportStar(require("./Reference"), exports);
|
|
27
|
+
__exportStar(require("./BaseEntity"), exports);
|
|
28
|
+
__exportStar(require("./WrappedEntity"), exports);
|
|
29
|
+
__exportStar(require("./wrap"), exports);
|
package/entity/wrap.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { IWrappedEntity, IWrappedEntityInternal } from '../typings';
|
|
2
|
+
/**
|
|
3
|
+
* returns WrappedEntity instance associated with this entity. This includes all the internal properties like `__meta` or `__em`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function wrap<T extends object>(entity: T, preferHelper: true): IWrappedEntityInternal<T>;
|
|
6
|
+
/**
|
|
7
|
+
* wraps entity type with WrappedEntity internal properties and helpers like init/isInitialized/populated/toJSON
|
|
8
|
+
*/
|
|
9
|
+
export declare function wrap<T extends object>(entity: T, preferHelper?: false): IWrappedEntity<T>;
|
|
10
|
+
/**
|
|
11
|
+
* wraps entity type with WrappedEntity internal properties and helpers like init/isInitialized/populated/toJSON
|
|
12
|
+
* use `preferHelper = true` to have access to the internal `__` properties like `__meta` or `__em`
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export declare function helper<T extends object>(entity: T): IWrappedEntityInternal<T>;
|
package/entity/wrap.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.helper = exports.wrap = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* wraps entity type with WrappedEntity internal properties and helpers like init/isInitialized/populated/toJSON
|
|
6
|
+
* use `preferHelper = true` to have access to the internal `__` properties like `__meta` or `__em`
|
|
7
|
+
*/
|
|
8
|
+
function wrap(entity, preferHelper = false) {
|
|
9
|
+
if (!entity) {
|
|
10
|
+
return entity;
|
|
11
|
+
}
|
|
12
|
+
if (entity.__baseEntity && !preferHelper) {
|
|
13
|
+
return entity;
|
|
14
|
+
}
|
|
15
|
+
return entity.__helper ?? entity;
|
|
16
|
+
}
|
|
17
|
+
exports.wrap = wrap;
|
|
18
|
+
/**
|
|
19
|
+
* wraps entity type with WrappedEntity internal properties and helpers like init/isInitialized/populated/toJSON
|
|
20
|
+
* use `preferHelper = true` to have access to the internal `__` properties like `__meta` or `__em`
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
function helper(entity) {
|
|
24
|
+
return entity.__helper;
|
|
25
|
+
}
|
|
26
|
+
exports.helper = helper;
|