@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,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serialize = exports.EntitySerializer = void 0;
|
|
4
|
+
const wrap_1 = require("../entity/wrap");
|
|
5
|
+
const Utils_1 = require("../utils/Utils");
|
|
6
|
+
const enums_1 = require("../enums");
|
|
7
|
+
const Reference_1 = require("../entity/Reference");
|
|
8
|
+
const SerializationContext_1 = require("./SerializationContext");
|
|
9
|
+
function isVisible(meta, propName, options) {
|
|
10
|
+
if (Array.isArray(options.populate) && options.populate?.find(item => item === propName || item.startsWith(propName + '.') || item === '*')) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
if (options.exclude?.find(item => item === propName)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const prop = meta.properties[propName];
|
|
17
|
+
const visible = prop && !prop.hidden;
|
|
18
|
+
const prefixed = prop && !prop.primary && propName.startsWith('_'); // ignore prefixed properties, if it's not a PK
|
|
19
|
+
return visible && !prefixed;
|
|
20
|
+
}
|
|
21
|
+
function isPopulated(propName, options) {
|
|
22
|
+
if (typeof options.populate !== 'boolean' && options.populate?.find(item => item === propName || item.startsWith(propName + '.') || item === '*')) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
if (typeof options.populate === 'boolean') {
|
|
26
|
+
return options.populate;
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
class EntitySerializer {
|
|
31
|
+
static serialize(entity, options = {}) {
|
|
32
|
+
const wrapped = (0, wrap_1.helper)(entity);
|
|
33
|
+
const meta = wrapped.__meta;
|
|
34
|
+
let contextCreated = false;
|
|
35
|
+
if (!wrapped.__serializationContext.root) {
|
|
36
|
+
const root = new SerializationContext_1.SerializationContext(wrapped.__config);
|
|
37
|
+
SerializationContext_1.SerializationContext.propagate(root, entity, (meta, prop) => meta.properties[prop]?.kind !== enums_1.ReferenceKind.SCALAR);
|
|
38
|
+
options.populate = (options.populate ? Utils_1.Utils.asArray(options.populate) : options.populate);
|
|
39
|
+
contextCreated = true;
|
|
40
|
+
}
|
|
41
|
+
const root = wrapped.__serializationContext.root;
|
|
42
|
+
const ret = {};
|
|
43
|
+
const keys = new Set(meta.primaryKeys);
|
|
44
|
+
Utils_1.Utils.keys(entity).forEach(prop => keys.add(prop));
|
|
45
|
+
const visited = root.visited.has(entity);
|
|
46
|
+
if (!visited) {
|
|
47
|
+
root.visited.add(entity);
|
|
48
|
+
}
|
|
49
|
+
[...keys]
|
|
50
|
+
.filter(prop => isVisible(meta, prop, options))
|
|
51
|
+
.map(prop => {
|
|
52
|
+
const cycle = root.visit(meta.className, prop);
|
|
53
|
+
if (cycle && visited) {
|
|
54
|
+
return [prop, undefined];
|
|
55
|
+
}
|
|
56
|
+
const val = this.processProperty(prop, entity, options);
|
|
57
|
+
if (!cycle) {
|
|
58
|
+
root.leave(meta.className, prop);
|
|
59
|
+
}
|
|
60
|
+
if (options.skipNull && Utils_1.Utils.isPlainObject(val)) {
|
|
61
|
+
Utils_1.Utils.dropUndefinedProperties(val, null);
|
|
62
|
+
}
|
|
63
|
+
return [prop, val];
|
|
64
|
+
})
|
|
65
|
+
.filter(([, value]) => typeof value !== 'undefined' && !(value === null && options.skipNull))
|
|
66
|
+
.forEach(([prop, value]) => ret[this.propertyName(meta, prop, wrapped.__platform)] = value);
|
|
67
|
+
if (contextCreated) {
|
|
68
|
+
root.close();
|
|
69
|
+
}
|
|
70
|
+
if (!wrapped.isInitialized()) {
|
|
71
|
+
return ret;
|
|
72
|
+
}
|
|
73
|
+
// decorated getters
|
|
74
|
+
meta.props
|
|
75
|
+
.filter(prop => prop.getter && prop.getterName === undefined && typeof entity[prop.name] !== 'undefined' && isVisible(meta, prop.name, options))
|
|
76
|
+
// @ts-ignore
|
|
77
|
+
.forEach(prop => ret[this.propertyName(meta, prop.name, wrapped.__platform)] = this.processProperty(prop.name, entity, options));
|
|
78
|
+
// decorated get methods
|
|
79
|
+
meta.props
|
|
80
|
+
.filter(prop => prop.getterName && entity[prop.getterName] instanceof Function && isVisible(meta, prop.name, options))
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
.forEach(prop => ret[this.propertyName(meta, prop.name, wrapped.__platform)] = this.processProperty(prop.getterName, entity, options));
|
|
83
|
+
return ret;
|
|
84
|
+
}
|
|
85
|
+
static propertyName(meta, prop, platform) {
|
|
86
|
+
/* istanbul ignore next */
|
|
87
|
+
if (meta.properties[prop]?.serializedName) {
|
|
88
|
+
return meta.properties[prop].serializedName;
|
|
89
|
+
}
|
|
90
|
+
if (meta.properties[prop]?.primary && platform) {
|
|
91
|
+
return platform.getSerializedPrimaryKeyField(prop);
|
|
92
|
+
}
|
|
93
|
+
return prop;
|
|
94
|
+
}
|
|
95
|
+
static processProperty(prop, entity, options) {
|
|
96
|
+
const parts = prop.split('.');
|
|
97
|
+
prop = parts[0];
|
|
98
|
+
const wrapped = (0, wrap_1.helper)(entity);
|
|
99
|
+
const property = wrapped.__meta.properties[prop];
|
|
100
|
+
const serializer = property?.serializer;
|
|
101
|
+
const value = entity[prop];
|
|
102
|
+
// getter method
|
|
103
|
+
if (entity[prop] instanceof Function) {
|
|
104
|
+
const returnValue = entity[prop]();
|
|
105
|
+
if (!options.ignoreSerializers && serializer) {
|
|
106
|
+
return serializer(returnValue);
|
|
107
|
+
}
|
|
108
|
+
return returnValue;
|
|
109
|
+
}
|
|
110
|
+
/* istanbul ignore next */
|
|
111
|
+
if (!options.ignoreSerializers && serializer) {
|
|
112
|
+
return serializer(value);
|
|
113
|
+
}
|
|
114
|
+
if (Utils_1.Utils.isCollection(value)) {
|
|
115
|
+
return this.processCollection(prop, entity, options);
|
|
116
|
+
}
|
|
117
|
+
if (Utils_1.Utils.isEntity(value, true)) {
|
|
118
|
+
return this.processEntity(prop, entity, wrapped.__platform, options);
|
|
119
|
+
}
|
|
120
|
+
if (Utils_1.Utils.isScalarReference(value)) {
|
|
121
|
+
return value.unwrap();
|
|
122
|
+
}
|
|
123
|
+
/* istanbul ignore next */
|
|
124
|
+
if (property?.kind === enums_1.ReferenceKind.EMBEDDED) {
|
|
125
|
+
if (Array.isArray(value)) {
|
|
126
|
+
return value.map(item => (0, wrap_1.helper)(item).toJSON());
|
|
127
|
+
}
|
|
128
|
+
if (Utils_1.Utils.isObject(value)) {
|
|
129
|
+
return (0, wrap_1.helper)(value).toJSON();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const customType = property?.customType;
|
|
133
|
+
if (customType) {
|
|
134
|
+
return customType.toJSON(value, wrapped.__platform);
|
|
135
|
+
}
|
|
136
|
+
return wrapped.__platform.normalizePrimaryKey(value);
|
|
137
|
+
}
|
|
138
|
+
static extractChildOptions(options, prop) {
|
|
139
|
+
return {
|
|
140
|
+
...options,
|
|
141
|
+
populate: Array.isArray(options.populate) ? Utils_1.Utils.extractChildElements(options.populate, prop, '*') : options.populate,
|
|
142
|
+
exclude: Array.isArray(options.exclude) ? Utils_1.Utils.extractChildElements(options.exclude, prop) : options.exclude,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
static processEntity(prop, entity, platform, options) {
|
|
146
|
+
const child = Reference_1.Reference.unwrapReference(entity[prop]);
|
|
147
|
+
const wrapped = (0, wrap_1.helper)(child);
|
|
148
|
+
const populated = isPopulated(prop, options) && wrapped.isInitialized();
|
|
149
|
+
const expand = populated || !wrapped.__managed;
|
|
150
|
+
const meta = wrapped.__meta;
|
|
151
|
+
const childOptions = this.extractChildOptions(options, prop);
|
|
152
|
+
const visible = meta.primaryKeys.filter(prop => isVisible(meta, prop, childOptions));
|
|
153
|
+
if (expand) {
|
|
154
|
+
return this.serialize(child, childOptions);
|
|
155
|
+
}
|
|
156
|
+
const pk = wrapped.getPrimaryKey(true);
|
|
157
|
+
if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
|
|
158
|
+
return Utils_1.Utils.primaryKeyToObject(meta, pk, visible);
|
|
159
|
+
}
|
|
160
|
+
if (Utils_1.Utils.isPlainObject(pk)) {
|
|
161
|
+
const pruned = Utils_1.Utils.primaryKeyToObject(meta, pk, visible);
|
|
162
|
+
if (visible.length === 1) {
|
|
163
|
+
return platform.normalizePrimaryKey(pruned[visible[0]]);
|
|
164
|
+
}
|
|
165
|
+
return pruned;
|
|
166
|
+
}
|
|
167
|
+
return platform.normalizePrimaryKey(pk);
|
|
168
|
+
}
|
|
169
|
+
static processCollection(prop, entity, options) {
|
|
170
|
+
const col = entity[prop];
|
|
171
|
+
if (!col.isInitialized()) {
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
return col.getItems(false).map(item => {
|
|
175
|
+
const populated = isPopulated(prop, options);
|
|
176
|
+
const wrapped = (0, wrap_1.helper)(item);
|
|
177
|
+
if (populated || !wrapped.__managed) {
|
|
178
|
+
return this.serialize(item, this.extractChildOptions(options, prop));
|
|
179
|
+
}
|
|
180
|
+
if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
|
|
181
|
+
return Utils_1.Utils.primaryKeyToObject(wrapped.__meta, wrapped.getPrimaryKey(true));
|
|
182
|
+
}
|
|
183
|
+
return (0, wrap_1.helper)(item).getPrimaryKey();
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.EntitySerializer = EntitySerializer;
|
|
188
|
+
/**
|
|
189
|
+
* Converts entity instance to POJO, converting the `Collection`s to arrays and unwrapping the `Reference` wrapper, while respecting the serialization options.
|
|
190
|
+
* This method accepts either a single entity or an array of entities, and returns the corresponding POJO or an array of POJO.
|
|
191
|
+
* To serialize a single entity, you can also use `wrap(entity).serialize()` which handles a single entity only.
|
|
192
|
+
*
|
|
193
|
+
* ```ts
|
|
194
|
+
* const dtos = serialize([user1, user, ...], { exclude: ['id', 'email'], forceObject: true });
|
|
195
|
+
* const [dto2, dto3] = serialize([user2, user3], { exclude: ['id', 'email'], forceObject: true });
|
|
196
|
+
* const dto1 = serialize(user, { exclude: ['id', 'email'], forceObject: true });
|
|
197
|
+
* const dto2 = wrap(user).serialize({ exclude: ['id', 'email'], forceObject: true });
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
function serialize(entities, options) {
|
|
201
|
+
if (Array.isArray(entities)) {
|
|
202
|
+
return entities.map(e => EntitySerializer.serialize(e, options));
|
|
203
|
+
}
|
|
204
|
+
return EntitySerializer.serialize(entities, options);
|
|
205
|
+
}
|
|
206
|
+
exports.serialize = serialize;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { EntityDTO, EntityKey } from '../typings';
|
|
2
|
+
export declare class EntityTransformer {
|
|
3
|
+
static toObject<Entity extends object, Ignored extends EntityKey<Entity> = never>(entity: Entity, ignoreFields?: Ignored[], raw?: boolean): Omit<EntityDTO<Entity>, Ignored>;
|
|
4
|
+
private static propertyName;
|
|
5
|
+
private static processProperty;
|
|
6
|
+
private static processEntity;
|
|
7
|
+
private static processCollection;
|
|
8
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EntityTransformer = void 0;
|
|
4
|
+
const wrap_1 = require("../entity/wrap");
|
|
5
|
+
const Utils_1 = require("../utils/Utils");
|
|
6
|
+
const enums_1 = require("../enums");
|
|
7
|
+
const SerializationContext_1 = require("./SerializationContext");
|
|
8
|
+
function isVisible(meta, propName, ignoreFields = []) {
|
|
9
|
+
const prop = meta.properties[propName];
|
|
10
|
+
const visible = prop && !prop.hidden;
|
|
11
|
+
const prefixed = prop && !prop.primary && propName.startsWith('_'); // ignore prefixed properties, if it's not a PK
|
|
12
|
+
return visible && !prefixed && !ignoreFields.includes(propName);
|
|
13
|
+
}
|
|
14
|
+
class EntityTransformer {
|
|
15
|
+
static toObject(entity, ignoreFields = [], raw = false) {
|
|
16
|
+
if (!Array.isArray(ignoreFields)) {
|
|
17
|
+
ignoreFields = [];
|
|
18
|
+
}
|
|
19
|
+
const wrapped = (0, wrap_1.helper)(entity);
|
|
20
|
+
let contextCreated = false;
|
|
21
|
+
if (!wrapped.__serializationContext.root) {
|
|
22
|
+
const root = new SerializationContext_1.SerializationContext(wrapped.__config, wrapped.__serializationContext.populate, wrapped.__serializationContext.fields, wrapped.__serializationContext.exclude);
|
|
23
|
+
SerializationContext_1.SerializationContext.propagate(root, entity, isVisible);
|
|
24
|
+
contextCreated = true;
|
|
25
|
+
}
|
|
26
|
+
const root = wrapped.__serializationContext.root;
|
|
27
|
+
const meta = wrapped.__meta;
|
|
28
|
+
const ret = {};
|
|
29
|
+
const keys = new Set();
|
|
30
|
+
if (meta.serializedPrimaryKey && !meta.compositePK) {
|
|
31
|
+
keys.add(meta.serializedPrimaryKey);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
meta.primaryKeys.forEach(pk => keys.add(pk));
|
|
35
|
+
}
|
|
36
|
+
if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
|
|
37
|
+
Utils_1.Utils.keys(entity).forEach(prop => keys.add(prop));
|
|
38
|
+
}
|
|
39
|
+
const visited = root.visited.has(entity);
|
|
40
|
+
if (!visited) {
|
|
41
|
+
root.visited.add(entity);
|
|
42
|
+
}
|
|
43
|
+
[...keys]
|
|
44
|
+
.filter(prop => raw ? meta.properties[prop] : isVisible(meta, prop, ignoreFields))
|
|
45
|
+
.map(prop => {
|
|
46
|
+
const populated = root.isMarkedAsPopulated(meta.className, prop);
|
|
47
|
+
const partiallyLoaded = root.isPartiallyLoaded(meta.className, prop);
|
|
48
|
+
const isPrimary = wrapped.__config.get('serialization').includePrimaryKeys && meta.properties[prop].primary;
|
|
49
|
+
if (!partiallyLoaded && !populated && !isPrimary) {
|
|
50
|
+
return [prop, undefined];
|
|
51
|
+
}
|
|
52
|
+
const cycle = root.visit(meta.className, prop);
|
|
53
|
+
if (cycle && visited) {
|
|
54
|
+
return [prop, undefined];
|
|
55
|
+
}
|
|
56
|
+
const val = EntityTransformer.processProperty(prop, entity, raw, populated);
|
|
57
|
+
if (!cycle) {
|
|
58
|
+
root.leave(meta.className, prop);
|
|
59
|
+
}
|
|
60
|
+
return [prop, val];
|
|
61
|
+
})
|
|
62
|
+
.filter(([, value]) => typeof value !== 'undefined')
|
|
63
|
+
.forEach(([prop, value]) => ret[this.propertyName(meta, prop, wrapped.__platform)] = value);
|
|
64
|
+
if (!visited) {
|
|
65
|
+
root.visited.delete(entity);
|
|
66
|
+
}
|
|
67
|
+
if (!wrapped.isInitialized() && wrapped.hasPrimaryKey()) {
|
|
68
|
+
return ret;
|
|
69
|
+
}
|
|
70
|
+
// decorated getters
|
|
71
|
+
meta.props
|
|
72
|
+
.filter(prop => prop.getter && prop.getterName === undefined && !prop.hidden && typeof entity[prop.name] !== 'undefined')
|
|
73
|
+
// @ts-ignore
|
|
74
|
+
.forEach(prop => ret[this.propertyName(meta, prop.name, wrapped.__platform)] = this.processProperty(prop.name, entity, raw));
|
|
75
|
+
// decorated get methods
|
|
76
|
+
meta.props
|
|
77
|
+
.filter(prop => prop.getterName && !prop.hidden && entity[prop.getterName] instanceof Function)
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
.forEach(prop => ret[this.propertyName(meta, prop.name, wrapped.__platform)] = this.processProperty(prop.getterName, entity, raw));
|
|
80
|
+
if (contextCreated) {
|
|
81
|
+
root.close();
|
|
82
|
+
}
|
|
83
|
+
return ret;
|
|
84
|
+
}
|
|
85
|
+
static propertyName(meta, prop, platform) {
|
|
86
|
+
if (meta.properties[prop].serializedName) {
|
|
87
|
+
return meta.properties[prop].serializedName;
|
|
88
|
+
}
|
|
89
|
+
if (meta.properties[prop].primary && platform) {
|
|
90
|
+
return platform.getSerializedPrimaryKeyField(prop);
|
|
91
|
+
}
|
|
92
|
+
return prop;
|
|
93
|
+
}
|
|
94
|
+
static processProperty(prop, entity, raw, populated) {
|
|
95
|
+
const wrapped = (0, wrap_1.helper)(entity);
|
|
96
|
+
const property = wrapped.__meta.properties[prop];
|
|
97
|
+
const serializer = property?.serializer;
|
|
98
|
+
const value = entity[prop];
|
|
99
|
+
// getter method
|
|
100
|
+
if (entity[prop] instanceof Function) {
|
|
101
|
+
const returnValue = entity[prop]();
|
|
102
|
+
if (serializer) {
|
|
103
|
+
return serializer(returnValue);
|
|
104
|
+
}
|
|
105
|
+
return returnValue;
|
|
106
|
+
}
|
|
107
|
+
if (serializer) {
|
|
108
|
+
return serializer(value);
|
|
109
|
+
}
|
|
110
|
+
if (Utils_1.Utils.isCollection(value)) {
|
|
111
|
+
return EntityTransformer.processCollection(prop, entity, raw, populated);
|
|
112
|
+
}
|
|
113
|
+
if (Utils_1.Utils.isEntity(value, true)) {
|
|
114
|
+
return EntityTransformer.processEntity(prop, entity, wrapped.__platform, raw, populated);
|
|
115
|
+
}
|
|
116
|
+
if (Utils_1.Utils.isScalarReference(value)) {
|
|
117
|
+
return value.unwrap();
|
|
118
|
+
}
|
|
119
|
+
if (property.kind === enums_1.ReferenceKind.EMBEDDED) {
|
|
120
|
+
if (Array.isArray(value)) {
|
|
121
|
+
return value.map(item => {
|
|
122
|
+
const wrapped = item && (0, wrap_1.helper)(item);
|
|
123
|
+
return wrapped ? wrapped.toJSON() : item;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
const wrapped = value && (0, wrap_1.helper)(value);
|
|
127
|
+
return wrapped ? wrapped.toJSON() : value;
|
|
128
|
+
}
|
|
129
|
+
const customType = property?.customType;
|
|
130
|
+
if (customType) {
|
|
131
|
+
return customType.toJSON(value, wrapped.__platform);
|
|
132
|
+
}
|
|
133
|
+
if (property?.primary) {
|
|
134
|
+
return wrapped.__platform.normalizePrimaryKey(value);
|
|
135
|
+
}
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
static processEntity(prop, entity, platform, raw, populated) {
|
|
139
|
+
const child = entity[prop];
|
|
140
|
+
const wrapped = (0, wrap_1.helper)(child);
|
|
141
|
+
const meta = wrapped.__meta;
|
|
142
|
+
const visible = meta.primaryKeys.filter(prop => isVisible(meta, prop));
|
|
143
|
+
if (raw && wrapped.isInitialized() && child !== entity) {
|
|
144
|
+
return wrapped.toPOJO();
|
|
145
|
+
}
|
|
146
|
+
function isPopulated() {
|
|
147
|
+
if (wrapped.__populated != null) {
|
|
148
|
+
return wrapped.__populated;
|
|
149
|
+
}
|
|
150
|
+
if (populated) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
return !wrapped.__managed;
|
|
154
|
+
}
|
|
155
|
+
if (wrapped.isInitialized() && isPopulated() && child !== entity) {
|
|
156
|
+
return (0, wrap_1.wrap)(child).toJSON();
|
|
157
|
+
}
|
|
158
|
+
const pk = wrapped.getPrimaryKey(true);
|
|
159
|
+
if (wrapped.__config.get('serialization').forceObject) {
|
|
160
|
+
return Utils_1.Utils.primaryKeyToObject(meta, pk, visible);
|
|
161
|
+
}
|
|
162
|
+
if (Utils_1.Utils.isPlainObject(pk)) {
|
|
163
|
+
const pruned = Utils_1.Utils.primaryKeyToObject(meta, pk, visible);
|
|
164
|
+
if (visible.length === 1) {
|
|
165
|
+
return platform.normalizePrimaryKey(pruned[visible[0]]);
|
|
166
|
+
}
|
|
167
|
+
return pruned;
|
|
168
|
+
}
|
|
169
|
+
return platform.normalizePrimaryKey(pk);
|
|
170
|
+
}
|
|
171
|
+
static processCollection(prop, entity, raw, populated) {
|
|
172
|
+
const col = entity[prop];
|
|
173
|
+
if (raw && col.isInitialized(true)) {
|
|
174
|
+
return col.map(item => (0, wrap_1.helper)(item).toPOJO());
|
|
175
|
+
}
|
|
176
|
+
if (col.shouldPopulate(populated)) {
|
|
177
|
+
return col.toArray();
|
|
178
|
+
}
|
|
179
|
+
if (col.isInitialized()) {
|
|
180
|
+
const wrapped = (0, wrap_1.helper)(entity);
|
|
181
|
+
if (wrapped.__config.get('serialization').forceObject) {
|
|
182
|
+
return col.map(item => {
|
|
183
|
+
const wrapped = (0, wrap_1.helper)(item);
|
|
184
|
+
return Utils_1.Utils.primaryKeyToObject(wrapped.__meta, wrapped.getPrimaryKey(true));
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return col.map(i => (0, wrap_1.helper)(i).getPrimaryKey(true));
|
|
188
|
+
}
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
exports.EntityTransformer = EntityTransformer;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { AnyEntity, EntityMetadata, PopulateOptions } from '../typings';
|
|
2
|
+
import type { Configuration } from '../utils/Configuration';
|
|
3
|
+
/**
|
|
4
|
+
* Helper that allows to keep track of where we are currently at when serializing complex entity graph with cycles.
|
|
5
|
+
* Before we process a property, we call `visit` that checks if it is not a cycle path (but allows to pass cycles that
|
|
6
|
+
* are defined in populate hint). If not, we proceed and call `leave` afterwards.
|
|
7
|
+
*/
|
|
8
|
+
export declare class SerializationContext<T> {
|
|
9
|
+
private readonly config;
|
|
10
|
+
private readonly populate;
|
|
11
|
+
private readonly fields?;
|
|
12
|
+
private readonly exclude?;
|
|
13
|
+
readonly path: [string, string][];
|
|
14
|
+
readonly visited: Set<Partial<any>>;
|
|
15
|
+
private entities;
|
|
16
|
+
constructor(config: Configuration, populate?: PopulateOptions<T>[], fields?: Set<string> | undefined, exclude?: string[] | undefined);
|
|
17
|
+
/**
|
|
18
|
+
* Returns true when there is a cycle detected.
|
|
19
|
+
*/
|
|
20
|
+
visit(entityName: string, prop: string): boolean;
|
|
21
|
+
leave<U>(entityName: string, prop: string): void;
|
|
22
|
+
close(): void;
|
|
23
|
+
/**
|
|
24
|
+
* When initializing new context, we need to propagate it to the whole entity graph recursively.
|
|
25
|
+
*/
|
|
26
|
+
static propagate(root: SerializationContext<AnyEntity>, entity: AnyEntity, isVisible: (meta: EntityMetadata, prop: string) => boolean): void;
|
|
27
|
+
isMarkedAsPopulated(entityName: string, prop: string): boolean;
|
|
28
|
+
isPartiallyLoaded(entityName: string, prop: string): boolean;
|
|
29
|
+
private register;
|
|
30
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SerializationContext = void 0;
|
|
4
|
+
const Utils_1 = require("../utils/Utils");
|
|
5
|
+
const wrap_1 = require("../entity/wrap");
|
|
6
|
+
/**
|
|
7
|
+
* Helper that allows to keep track of where we are currently at when serializing complex entity graph with cycles.
|
|
8
|
+
* Before we process a property, we call `visit` that checks if it is not a cycle path (but allows to pass cycles that
|
|
9
|
+
* are defined in populate hint). If not, we proceed and call `leave` afterwards.
|
|
10
|
+
*/
|
|
11
|
+
class SerializationContext {
|
|
12
|
+
config;
|
|
13
|
+
populate;
|
|
14
|
+
fields;
|
|
15
|
+
exclude;
|
|
16
|
+
path = [];
|
|
17
|
+
visited = new Set();
|
|
18
|
+
entities = new Set();
|
|
19
|
+
constructor(config, populate = [], fields, exclude) {
|
|
20
|
+
this.config = config;
|
|
21
|
+
this.populate = populate;
|
|
22
|
+
this.fields = fields;
|
|
23
|
+
this.exclude = exclude;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns true when there is a cycle detected.
|
|
27
|
+
*/
|
|
28
|
+
visit(entityName, prop) {
|
|
29
|
+
if (!this.path.find(([cls, item]) => entityName === cls && prop === item)) {
|
|
30
|
+
this.path.push([entityName, prop]);
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
// check if the path is explicitly populated
|
|
34
|
+
if (!this.isMarkedAsPopulated(entityName, prop)) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
this.path.push([entityName, prop]);
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
leave(entityName, prop) {
|
|
41
|
+
const last = this.path.pop();
|
|
42
|
+
/* istanbul ignore next */
|
|
43
|
+
if (!last || last[0] !== entityName || last[1] !== prop) {
|
|
44
|
+
throw new Error(`Trying to leave wrong property: ${entityName}.${prop} instead of ${last?.join('.')}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
close() {
|
|
48
|
+
this.entities.forEach(entity => {
|
|
49
|
+
delete (0, wrap_1.helper)(entity).__serializationContext.root;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* When initializing new context, we need to propagate it to the whole entity graph recursively.
|
|
54
|
+
*/
|
|
55
|
+
static propagate(root, entity, isVisible) {
|
|
56
|
+
root.register(entity);
|
|
57
|
+
const meta = (0, wrap_1.helper)(entity).__meta;
|
|
58
|
+
const items = [];
|
|
59
|
+
Object.keys(entity)
|
|
60
|
+
.filter(key => isVisible(meta, key))
|
|
61
|
+
.forEach(key => {
|
|
62
|
+
if (Utils_1.Utils.isEntity(entity[key], true)) {
|
|
63
|
+
items.push(entity[key]);
|
|
64
|
+
}
|
|
65
|
+
else if (Utils_1.Utils.isCollection(entity[key])) {
|
|
66
|
+
items.push(...entity[key].getItems(false));
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
items
|
|
70
|
+
.filter(item => !item.__helper.__serializationContext.root)
|
|
71
|
+
.forEach(item => this.propagate(root, item, isVisible));
|
|
72
|
+
}
|
|
73
|
+
isMarkedAsPopulated(entityName, prop) {
|
|
74
|
+
let populate = this.populate;
|
|
75
|
+
for (const segment of this.path) {
|
|
76
|
+
if (!populate) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const exists = populate.find(p => p.field === segment[1]);
|
|
80
|
+
if (exists) {
|
|
81
|
+
// we need to check for cycles here too, as we could fall into endless loops for bidirectional relations
|
|
82
|
+
if (exists.all) {
|
|
83
|
+
return !this.path.find(([cls, item]) => entityName === cls && prop === item);
|
|
84
|
+
}
|
|
85
|
+
populate = exists.children;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return !!populate?.some(p => p.field === prop);
|
|
89
|
+
}
|
|
90
|
+
isPartiallyLoaded(entityName, prop) {
|
|
91
|
+
if (!this.fields) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
let fields = [...this.fields];
|
|
95
|
+
for (const segment of this.path) {
|
|
96
|
+
/* istanbul ignore next */
|
|
97
|
+
if (fields.length === 0) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
fields = fields
|
|
101
|
+
.filter(field => field.startsWith(`${segment[1]}.`) || field === '*')
|
|
102
|
+
.map(field => field === '*' ? field : field.substring(segment[1].length + 1));
|
|
103
|
+
}
|
|
104
|
+
return fields.some(p => p === prop || p === '*');
|
|
105
|
+
}
|
|
106
|
+
register(entity) {
|
|
107
|
+
(0, wrap_1.helper)(entity).__serializationContext.root = this;
|
|
108
|
+
this.entities.add(entity);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.SerializationContext = SerializationContext;
|
|
@@ -0,0 +1,19 @@
|
|
|
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("./EntityTransformer"), exports);
|
|
18
|
+
__exportStar(require("./EntitySerializer"), exports);
|
|
19
|
+
__exportStar(require("./SerializationContext"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Type, type TransformContext } from './Type';
|
|
2
|
+
import type { EntityProperty } from '../typings';
|
|
3
|
+
import type { Platform } from '../platforms';
|
|
4
|
+
export declare class ArrayType<T = string> extends Type<T[] | null, string | null> {
|
|
5
|
+
private readonly toJsValue;
|
|
6
|
+
private readonly toDbValue;
|
|
7
|
+
constructor(toJsValue?: (i: string) => T, toDbValue?: (i: T) => string);
|
|
8
|
+
convertToDatabaseValue(value: T[] | null, platform: Platform, context?: TransformContext): string | null;
|
|
9
|
+
convertToJSValue(value: T[] | string | null, platform: Platform): T[] | null;
|
|
10
|
+
compareAsType(): string;
|
|
11
|
+
toJSON(value: T[]): T[];
|
|
12
|
+
getColumnType(prop: EntityProperty, platform: Platform): string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArrayType = void 0;
|
|
4
|
+
const Type_1 = require("./Type");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
|
+
class ArrayType extends Type_1.Type {
|
|
8
|
+
toJsValue;
|
|
9
|
+
toDbValue;
|
|
10
|
+
constructor(toJsValue = i => i, toDbValue = i => i) {
|
|
11
|
+
super();
|
|
12
|
+
this.toJsValue = toJsValue;
|
|
13
|
+
this.toDbValue = toDbValue;
|
|
14
|
+
}
|
|
15
|
+
convertToDatabaseValue(value, platform, context) {
|
|
16
|
+
if (!value) {
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
return platform.marshallArray(value.map(i => this.toDbValue(i)));
|
|
21
|
+
}
|
|
22
|
+
/* istanbul ignore next */
|
|
23
|
+
if (context?.fromQuery) {
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
throw errors_1.ValidationError.invalidType(ArrayType, value, 'JS');
|
|
27
|
+
}
|
|
28
|
+
convertToJSValue(value, platform) {
|
|
29
|
+
if (value == null) {
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
if (utils_1.Utils.isString(value)) {
|
|
33
|
+
value = platform.unmarshallArray(value);
|
|
34
|
+
}
|
|
35
|
+
return value.map(i => this.toJsValue(i));
|
|
36
|
+
}
|
|
37
|
+
compareAsType() {
|
|
38
|
+
return 'string[]';
|
|
39
|
+
}
|
|
40
|
+
toJSON(value) {
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
getColumnType(prop, platform) {
|
|
44
|
+
return platform.getArrayDeclarationSQL();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ArrayType = ArrayType;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Type } from './Type';
|
|
2
|
+
import type { Platform } from '../platforms';
|
|
3
|
+
import type { EntityProperty } from '../typings';
|
|
4
|
+
/**
|
|
5
|
+
* This type will automatically convert string values returned from the database to native JS bigints (default)
|
|
6
|
+
* or numbers (safe only for values up to `Number.MAX_SAFE_INTEGER`), or strings, depending on the `mode`.
|
|
7
|
+
*/
|
|
8
|
+
export declare class BigIntType extends Type<string | bigint | number | null | undefined, string | null | undefined> {
|
|
9
|
+
mode?: "string" | "number" | "bigint" | undefined;
|
|
10
|
+
constructor(mode?: "string" | "number" | "bigint" | undefined);
|
|
11
|
+
convertToDatabaseValue(value: string | bigint | null | undefined): string | null | undefined;
|
|
12
|
+
convertToJSValue(value: string | bigint | null | undefined): bigint | number | string | null | undefined;
|
|
13
|
+
toJSON(value: string | bigint | null | undefined): string | bigint | null | undefined;
|
|
14
|
+
getColumnType(prop: EntityProperty, platform: Platform): string;
|
|
15
|
+
compareAsType(): string;
|
|
16
|
+
}
|