@mikro-orm/core 7.0.0-dev.23 → 7.0.0-dev.230

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.
Files changed (209) hide show
  1. package/EntityManager.d.ts +91 -59
  2. package/EntityManager.js +303 -251
  3. package/MikroORM.d.ts +44 -35
  4. package/MikroORM.js +109 -143
  5. package/README.md +2 -0
  6. package/cache/FileCacheAdapter.d.ts +1 -1
  7. package/cache/FileCacheAdapter.js +17 -8
  8. package/cache/GeneratedCacheAdapter.d.ts +0 -1
  9. package/cache/GeneratedCacheAdapter.js +0 -2
  10. package/cache/index.d.ts +0 -1
  11. package/cache/index.js +0 -1
  12. package/connections/Connection.d.ts +12 -5
  13. package/connections/Connection.js +21 -12
  14. package/drivers/DatabaseDriver.d.ts +25 -16
  15. package/drivers/DatabaseDriver.js +118 -35
  16. package/drivers/IDatabaseDriver.d.ts +42 -19
  17. package/entity/BaseEntity.d.ts +61 -2
  18. package/entity/BaseEntity.js +0 -3
  19. package/entity/Collection.d.ts +101 -29
  20. package/entity/Collection.js +436 -104
  21. package/entity/EntityAssigner.d.ts +1 -1
  22. package/entity/EntityAssigner.js +26 -18
  23. package/entity/EntityFactory.d.ts +7 -1
  24. package/entity/EntityFactory.js +83 -54
  25. package/entity/EntityHelper.d.ts +2 -2
  26. package/entity/EntityHelper.js +48 -15
  27. package/entity/EntityLoader.d.ts +7 -6
  28. package/entity/EntityLoader.js +215 -89
  29. package/entity/EntityRepository.d.ts +27 -7
  30. package/entity/EntityRepository.js +8 -2
  31. package/entity/PolymorphicRef.d.ts +12 -0
  32. package/entity/PolymorphicRef.js +18 -0
  33. package/entity/Reference.d.ts +1 -5
  34. package/entity/Reference.js +21 -12
  35. package/entity/WrappedEntity.d.ts +0 -5
  36. package/entity/WrappedEntity.js +2 -7
  37. package/entity/defineEntity.d.ts +380 -310
  38. package/entity/defineEntity.js +124 -273
  39. package/entity/index.d.ts +2 -2
  40. package/entity/index.js +2 -2
  41. package/entity/utils.js +1 -1
  42. package/entity/validators.d.ts +11 -0
  43. package/entity/validators.js +65 -0
  44. package/enums.d.ts +8 -6
  45. package/enums.js +2 -1
  46. package/errors.d.ts +20 -10
  47. package/errors.js +55 -23
  48. package/events/EventManager.d.ts +2 -1
  49. package/events/EventManager.js +19 -11
  50. package/hydration/Hydrator.js +1 -2
  51. package/hydration/ObjectHydrator.d.ts +4 -4
  52. package/hydration/ObjectHydrator.js +87 -35
  53. package/index.d.ts +2 -2
  54. package/index.js +1 -2
  55. package/logging/DefaultLogger.d.ts +1 -1
  56. package/logging/DefaultLogger.js +1 -0
  57. package/logging/SimpleLogger.d.ts +1 -1
  58. package/logging/colors.d.ts +1 -1
  59. package/logging/colors.js +7 -6
  60. package/logging/index.d.ts +1 -0
  61. package/logging/index.js +1 -0
  62. package/logging/inspect.d.ts +2 -0
  63. package/logging/inspect.js +11 -0
  64. package/metadata/EntitySchema.d.ts +47 -23
  65. package/metadata/EntitySchema.js +92 -33
  66. package/metadata/MetadataDiscovery.d.ts +64 -9
  67. package/metadata/MetadataDiscovery.js +778 -325
  68. package/metadata/MetadataProvider.d.ts +11 -2
  69. package/metadata/MetadataProvider.js +46 -2
  70. package/metadata/MetadataStorage.d.ts +13 -11
  71. package/metadata/MetadataStorage.js +70 -37
  72. package/metadata/MetadataValidator.d.ts +32 -9
  73. package/metadata/MetadataValidator.js +196 -41
  74. package/metadata/discover-entities.d.ts +5 -0
  75. package/metadata/discover-entities.js +40 -0
  76. package/metadata/index.d.ts +1 -1
  77. package/metadata/index.js +1 -1
  78. package/metadata/types.d.ts +526 -0
  79. package/metadata/types.js +1 -0
  80. package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
  81. package/naming-strategy/AbstractNamingStrategy.js +20 -2
  82. package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
  83. package/naming-strategy/EntityCaseNamingStrategy.js +6 -5
  84. package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
  85. package/naming-strategy/MongoNamingStrategy.js +6 -6
  86. package/naming-strategy/NamingStrategy.d.ts +28 -4
  87. package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
  88. package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
  89. package/not-supported.d.ts +2 -0
  90. package/not-supported.js +4 -0
  91. package/package.json +19 -11
  92. package/platforms/ExceptionConverter.js +1 -1
  93. package/platforms/Platform.d.ts +7 -14
  94. package/platforms/Platform.js +20 -43
  95. package/serialization/EntitySerializer.d.ts +5 -0
  96. package/serialization/EntitySerializer.js +47 -27
  97. package/serialization/EntityTransformer.js +28 -18
  98. package/serialization/SerializationContext.d.ts +6 -6
  99. package/serialization/SerializationContext.js +3 -3
  100. package/types/ArrayType.d.ts +1 -1
  101. package/types/ArrayType.js +2 -3
  102. package/types/BigIntType.d.ts +8 -6
  103. package/types/BigIntType.js +1 -1
  104. package/types/BlobType.d.ts +0 -1
  105. package/types/BlobType.js +0 -3
  106. package/types/BooleanType.d.ts +1 -0
  107. package/types/BooleanType.js +3 -0
  108. package/types/DecimalType.d.ts +6 -4
  109. package/types/DecimalType.js +2 -2
  110. package/types/DoubleType.js +1 -1
  111. package/types/EnumArrayType.js +1 -2
  112. package/types/JsonType.d.ts +1 -1
  113. package/types/JsonType.js +7 -2
  114. package/types/TinyIntType.js +1 -1
  115. package/types/Type.d.ts +2 -4
  116. package/types/Type.js +3 -3
  117. package/types/Uint8ArrayType.d.ts +0 -1
  118. package/types/Uint8ArrayType.js +1 -4
  119. package/types/index.d.ts +1 -1
  120. package/typings.d.ts +381 -171
  121. package/typings.js +97 -44
  122. package/unit-of-work/ChangeSet.d.ts +4 -6
  123. package/unit-of-work/ChangeSet.js +4 -5
  124. package/unit-of-work/ChangeSetComputer.d.ts +1 -3
  125. package/unit-of-work/ChangeSetComputer.js +35 -14
  126. package/unit-of-work/ChangeSetPersister.d.ts +7 -3
  127. package/unit-of-work/ChangeSetPersister.js +83 -25
  128. package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
  129. package/unit-of-work/CommitOrderCalculator.js +13 -13
  130. package/unit-of-work/IdentityMap.d.ts +12 -0
  131. package/unit-of-work/IdentityMap.js +39 -1
  132. package/unit-of-work/UnitOfWork.d.ts +27 -3
  133. package/unit-of-work/UnitOfWork.js +258 -92
  134. package/utils/AbstractSchemaGenerator.d.ts +5 -5
  135. package/utils/AbstractSchemaGenerator.js +28 -17
  136. package/utils/AsyncContext.d.ts +6 -0
  137. package/utils/AsyncContext.js +42 -0
  138. package/utils/Configuration.d.ts +795 -209
  139. package/utils/Configuration.js +150 -192
  140. package/utils/ConfigurationLoader.d.ts +1 -54
  141. package/utils/ConfigurationLoader.js +1 -352
  142. package/utils/Cursor.d.ts +0 -3
  143. package/utils/Cursor.js +24 -11
  144. package/utils/DataloaderUtils.d.ts +10 -5
  145. package/utils/DataloaderUtils.js +29 -12
  146. package/utils/EntityComparator.d.ts +16 -9
  147. package/utils/EntityComparator.js +158 -58
  148. package/utils/QueryHelper.d.ts +18 -6
  149. package/utils/QueryHelper.js +76 -23
  150. package/utils/RawQueryFragment.d.ts +28 -34
  151. package/utils/RawQueryFragment.js +35 -71
  152. package/utils/RequestContext.js +2 -2
  153. package/utils/TransactionContext.js +2 -2
  154. package/utils/TransactionManager.js +28 -4
  155. package/utils/Utils.d.ts +14 -127
  156. package/utils/Utils.js +85 -397
  157. package/utils/clone.js +8 -23
  158. package/utils/env-vars.d.ts +7 -0
  159. package/utils/env-vars.js +97 -0
  160. package/utils/fs-utils.d.ts +33 -0
  161. package/utils/fs-utils.js +192 -0
  162. package/utils/index.d.ts +1 -1
  163. package/utils/index.js +1 -1
  164. package/utils/upsert-utils.d.ts +9 -4
  165. package/utils/upsert-utils.js +46 -3
  166. package/decorators/Check.d.ts +0 -3
  167. package/decorators/Check.js +0 -13
  168. package/decorators/CreateRequestContext.d.ts +0 -3
  169. package/decorators/CreateRequestContext.js +0 -32
  170. package/decorators/Embeddable.d.ts +0 -8
  171. package/decorators/Embeddable.js +0 -11
  172. package/decorators/Embedded.d.ts +0 -12
  173. package/decorators/Embedded.js +0 -18
  174. package/decorators/Entity.d.ts +0 -33
  175. package/decorators/Entity.js +0 -12
  176. package/decorators/Enum.d.ts +0 -9
  177. package/decorators/Enum.js +0 -16
  178. package/decorators/Filter.d.ts +0 -2
  179. package/decorators/Filter.js +0 -8
  180. package/decorators/Formula.d.ts +0 -4
  181. package/decorators/Formula.js +0 -15
  182. package/decorators/Indexed.d.ts +0 -19
  183. package/decorators/Indexed.js +0 -20
  184. package/decorators/ManyToMany.d.ts +0 -42
  185. package/decorators/ManyToMany.js +0 -14
  186. package/decorators/ManyToOne.d.ts +0 -34
  187. package/decorators/ManyToOne.js +0 -14
  188. package/decorators/OneToMany.d.ts +0 -28
  189. package/decorators/OneToMany.js +0 -17
  190. package/decorators/OneToOne.d.ts +0 -28
  191. package/decorators/OneToOne.js +0 -7
  192. package/decorators/PrimaryKey.d.ts +0 -8
  193. package/decorators/PrimaryKey.js +0 -20
  194. package/decorators/Property.d.ts +0 -250
  195. package/decorators/Property.js +0 -32
  196. package/decorators/Transactional.d.ts +0 -14
  197. package/decorators/Transactional.js +0 -28
  198. package/decorators/hooks.d.ts +0 -16
  199. package/decorators/hooks.js +0 -47
  200. package/decorators/index.d.ts +0 -17
  201. package/decorators/index.js +0 -17
  202. package/entity/ArrayCollection.d.ts +0 -118
  203. package/entity/ArrayCollection.js +0 -407
  204. package/entity/EntityValidator.d.ts +0 -19
  205. package/entity/EntityValidator.js +0 -150
  206. package/metadata/ReflectMetadataProvider.d.ts +0 -8
  207. package/metadata/ReflectMetadataProvider.js +0 -44
  208. package/utils/resolveContextProvider.d.ts +0 -10
  209. package/utils/resolveContextProvider.js +0 -28
@@ -15,8 +15,8 @@ function isVisible(meta, propName, options) {
15
15
  if (options.exclude?.find(item => item === propName)) {
16
16
  return false;
17
17
  }
18
- const visible = prop && !prop.hidden;
19
- const prefixed = prop && !prop.primary && propName.startsWith('_'); // ignore prefixed properties, if it's not a PK
18
+ const visible = prop && !(prop.hidden && !options.includeHidden);
19
+ const prefixed = prop && !prop.primary && !prop.accessor && propName.startsWith('_'); // ignore prefixed properties, if it's not a PK
20
20
  return visible && !prefixed;
21
21
  }
22
22
  function isPopulated(propName, options) {
@@ -41,23 +41,41 @@ export class EntitySerializer {
41
41
  }
42
42
  const root = wrapped.__serializationContext.root;
43
43
  const ret = {};
44
- const keys = new Set(meta.primaryKeys);
45
- Utils.keys(entity).forEach(prop => keys.add(prop));
44
+ const props = new Set();
45
+ if (meta.serializedPrimaryKey && !meta.compositePK) {
46
+ props.add(meta.serializedPrimaryKey);
47
+ }
48
+ else {
49
+ meta.primaryKeys.forEach(pk => props.add(pk));
50
+ }
51
+ if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
52
+ const entityKeys = new Set(Object.keys(entity));
53
+ for (const prop of meta.props) {
54
+ if (entityKeys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
55
+ props.add(prop.name);
56
+ }
57
+ }
58
+ for (const key of entityKeys) {
59
+ if (!meta.properties[key]) {
60
+ props.add(key);
61
+ }
62
+ }
63
+ }
46
64
  const visited = root.visited.has(entity);
47
65
  if (!visited) {
48
66
  root.visited.add(entity);
49
67
  }
50
- for (const prop of keys) {
68
+ for (const prop of props) {
51
69
  if (!isVisible(meta, prop, options)) {
52
70
  continue;
53
71
  }
54
- const cycle = root.visit(meta.className, prop);
72
+ const cycle = root.visit(meta.class, prop);
55
73
  if (cycle && visited) {
56
74
  continue;
57
75
  }
58
76
  const val = this.processProperty(prop, entity, options);
59
77
  if (!cycle) {
60
- root.leave(meta.className, prop);
78
+ root.leave(meta.class, prop);
61
79
  }
62
80
  if (options.skipNull && Utils.isPlainObject(val)) {
63
81
  Utils.dropUndefinedProperties(val, null);
@@ -67,7 +85,7 @@ export class EntitySerializer {
67
85
  }
68
86
  const visible = typeof val !== 'undefined' && !(val === null && options.skipNull);
69
87
  if (visible) {
70
- ret[this.propertyName(meta, prop, wrapped.__platform)] = val;
88
+ ret[this.propertyName(meta, prop)] = val;
71
89
  }
72
90
  }
73
91
  if (contextCreated) {
@@ -81,26 +99,26 @@ export class EntitySerializer {
81
99
  if (prop.getterName != null) {
82
100
  const visible = entity[prop.getterName] instanceof Function && isVisible(meta, prop.name, options);
83
101
  if (visible) {
84
- ret[this.propertyName(meta, prop.name, wrapped.__platform)] = this.processProperty(prop.getterName, entity, options);
102
+ ret[this.propertyName(meta, prop.name)] = this.processProperty(prop.getterName, entity, options);
85
103
  }
86
104
  }
87
105
  else {
88
106
  // decorated getters
89
107
  const visible = typeof entity[prop.name] !== 'undefined' && isVisible(meta, prop.name, options);
90
108
  if (visible) {
91
- ret[this.propertyName(meta, prop.name, wrapped.__platform)] = this.processProperty(prop.name, entity, options);
109
+ ret[this.propertyName(meta, prop.name)] = this.processProperty(prop.name, entity, options);
92
110
  }
93
111
  }
94
112
  }
95
113
  return ret;
96
114
  }
97
- static propertyName(meta, prop, platform) {
98
- /* v8 ignore next 3 */
115
+ static propertyName(meta, prop) {
116
+ /* v8 ignore next */
99
117
  if (meta.properties[prop]?.serializedName) {
100
118
  return meta.properties[prop].serializedName;
101
119
  }
102
- if (meta.properties[prop]?.primary && platform) {
103
- return platform.getSerializedPrimaryKeyField(prop);
120
+ if (meta.properties[prop]?.primary && meta.serializedPrimaryKey) {
121
+ return meta.serializedPrimaryKey;
104
122
  }
105
123
  return prop;
106
124
  }
@@ -119,7 +137,7 @@ export class EntitySerializer {
119
137
  }
120
138
  return returnValue;
121
139
  }
122
- /* v8 ignore next 3 */
140
+ /* v8 ignore next */
123
141
  if (!options.ignoreSerializers && serializer) {
124
142
  return serializer(value);
125
143
  }
@@ -132,7 +150,7 @@ export class EntitySerializer {
132
150
  if (Utils.isScalarReference(value)) {
133
151
  return value.unwrap();
134
152
  }
135
- /* v8 ignore next 9 */
153
+ /* v8 ignore next */
136
154
  if (property?.kind === ReferenceKind.EMBEDDED) {
137
155
  if (Array.isArray(value)) {
138
156
  return value.map(item => helper(item).toJSON());
@@ -141,12 +159,20 @@ export class EntitySerializer {
141
159
  return helper(value).toJSON();
142
160
  }
143
161
  }
144
- const customType = property?.customType;
145
- if (customType) {
146
- return customType.toJSON(value, wrapped.__platform);
162
+ if (property.customType) {
163
+ return this.processCustomType(value, property, wrapped.__platform, options.convertCustomTypes);
147
164
  }
148
165
  return wrapped.__platform.normalizePrimaryKey(value);
149
166
  }
167
+ static processCustomType(value, prop, platform, convertCustomTypes) {
168
+ if (!prop.customType) {
169
+ return value;
170
+ }
171
+ if (convertCustomTypes) {
172
+ return prop.customType.convertToDatabaseValue(value, platform, { mode: 'serialization' });
173
+ }
174
+ return prop.customType.toJSON(value, platform);
175
+ }
150
176
  static extractChildOptions(options, prop) {
151
177
  return {
152
178
  ...options,
@@ -165,10 +191,7 @@ export class EntitySerializer {
165
191
  if (expand) {
166
192
  return this.serialize(child, childOptions);
167
193
  }
168
- let pk = wrapped.getPrimaryKey();
169
- if (prop.customType) {
170
- pk = prop.customType.toJSON(pk, wrapped.__platform);
171
- }
194
+ const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
172
195
  if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
173
196
  return Utils.primaryKeyToObject(meta, pk, visible);
174
197
  }
@@ -192,10 +215,7 @@ export class EntitySerializer {
192
215
  if (populated || !wrapped.__managed) {
193
216
  return this.serialize(item, this.extractChildOptions(options, prop.name));
194
217
  }
195
- let pk = wrapped.getPrimaryKey();
196
- if (prop.customType) {
197
- pk = prop.customType.toJSON(pk, wrapped.__platform);
198
- }
218
+ const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
199
219
  if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
200
220
  return Utils.primaryKeyToObject(wrapped.__meta, pk);
201
221
  }
@@ -6,7 +6,7 @@ import { isRaw } from '../utils/RawQueryFragment.js';
6
6
  function isVisible(meta, propName, ignoreFields = []) {
7
7
  const prop = meta.properties[propName];
8
8
  const visible = prop && !prop.hidden;
9
- const prefixed = prop && !prop.primary && propName.startsWith('_'); // ignore prefixed properties, if it's not a PK
9
+ const prefixed = prop && !prop.primary && !prop.accessor && propName.startsWith('_'); // ignore prefixed properties, if it's not a PK
10
10
  return visible && !prefixed && !ignoreFields.includes(propName);
11
11
  }
12
12
  export class EntityTransformer {
@@ -27,39 +27,49 @@ export class EntityTransformer {
27
27
  const root = wrapped.__serializationContext.root;
28
28
  const meta = wrapped.__meta;
29
29
  const ret = {};
30
- const keys = new Set();
30
+ const props = new Set();
31
31
  if (meta.serializedPrimaryKey && !meta.compositePK) {
32
- keys.add(meta.serializedPrimaryKey);
32
+ props.add(meta.serializedPrimaryKey);
33
33
  }
34
34
  else {
35
- meta.primaryKeys.forEach(pk => keys.add(pk));
35
+ meta.primaryKeys.forEach(pk => props.add(pk));
36
36
  }
37
37
  if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
38
- Utils.keys(entity).forEach(prop => keys.add(prop));
38
+ const entityKeys = new Set(Object.keys(entity));
39
+ for (const prop of meta.props) {
40
+ if (entityKeys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
41
+ props.add(prop.name);
42
+ }
43
+ }
44
+ for (const key of entityKeys) {
45
+ if (!meta.properties[key]) {
46
+ props.add(key);
47
+ }
48
+ }
39
49
  }
40
50
  const visited = root.visited.has(entity);
41
51
  const includePrimaryKeys = wrapped.__config.get('serialization').includePrimaryKeys;
42
52
  if (!visited) {
43
53
  root.visited.add(entity);
44
54
  }
45
- for (const prop of keys) {
55
+ for (const prop of props) {
46
56
  const visible = raw ? meta.properties[prop] : isVisible(meta, prop, ignoreFields);
47
57
  if (!visible) {
48
58
  continue;
49
59
  }
50
- const populated = root.isMarkedAsPopulated(meta.className, prop);
51
- const partiallyLoaded = root.isPartiallyLoaded(meta.className, prop);
60
+ const populated = root.isMarkedAsPopulated(meta.class, prop);
61
+ const partiallyLoaded = root.isPartiallyLoaded(meta.class, prop);
52
62
  const isPrimary = includePrimaryKeys && meta.properties[prop].primary;
53
63
  if (!partiallyLoaded && !populated && !isPrimary) {
54
64
  continue;
55
65
  }
56
- const cycle = root.visit(meta.className, prop);
66
+ const cycle = root.visit(meta.class, prop);
57
67
  if (cycle && visited) {
58
68
  continue;
59
69
  }
60
70
  const val = EntityTransformer.processProperty(prop, entity, raw, populated);
61
71
  if (!cycle) {
62
- root.leave(meta.className, prop);
72
+ root.leave(meta.class, prop);
63
73
  }
64
74
  if (isRaw(val)) {
65
75
  throw new Error(`Trying to serialize raw SQL fragment: '${val.sql}'`);
@@ -67,7 +77,7 @@ export class EntityTransformer {
67
77
  if (typeof val === 'undefined') {
68
78
  continue;
69
79
  }
70
- ret[this.propertyName(meta, prop, wrapped.__platform, raw)] = val;
80
+ ret[this.propertyName(meta, prop, raw)] = val;
71
81
  }
72
82
  if (!wrapped.isInitialized() && wrapped.hasPrimaryKey()) {
73
83
  return ret;
@@ -76,17 +86,17 @@ export class EntityTransformer {
76
86
  // decorated get methods
77
87
  if (prop.getterName != null) {
78
88
  const visible = !prop.hidden && entity[prop.getterName] instanceof Function;
79
- const populated = root.isMarkedAsPopulated(meta.className, prop.name);
89
+ const populated = root.isMarkedAsPopulated(meta.class, prop.name);
80
90
  if (visible) {
81
- ret[this.propertyName(meta, prop.name, wrapped.__platform, raw)] = this.processProperty(prop.getterName, entity, raw, populated);
91
+ ret[this.propertyName(meta, prop.name, raw)] = this.processProperty(prop.getterName, entity, raw, populated);
82
92
  }
83
93
  }
84
94
  else {
85
95
  // decorated getters
86
96
  const visible = !prop.hidden && typeof entity[prop.name] !== 'undefined';
87
- const populated = root.isMarkedAsPopulated(meta.className, prop.name);
97
+ const populated = root.isMarkedAsPopulated(meta.class, prop.name);
88
98
  if (visible) {
89
- ret[this.propertyName(meta, prop.name, wrapped.__platform, raw)] = this.processProperty(prop.name, entity, raw, populated);
99
+ ret[this.propertyName(meta, prop.name, raw)] = this.processProperty(prop.name, entity, raw, populated);
90
100
  }
91
101
  }
92
102
  }
@@ -95,15 +105,15 @@ export class EntityTransformer {
95
105
  }
96
106
  return ret;
97
107
  }
98
- static propertyName(meta, prop, platform, raw) {
108
+ static propertyName(meta, prop, raw) {
99
109
  if (raw) {
100
110
  return prop;
101
111
  }
102
112
  if (meta.properties[prop].serializedName) {
103
113
  return meta.properties[prop].serializedName;
104
114
  }
105
- if (meta.properties[prop].primary && platform) {
106
- return platform.getSerializedPrimaryKeyField(prop);
115
+ if (meta.properties[prop].primary && meta.serializedPrimaryKey) {
116
+ return meta.serializedPrimaryKey;
107
117
  }
108
118
  return prop;
109
119
  }
@@ -1,4 +1,4 @@
1
- import type { AnyEntity, EntityMetadata, PopulateOptions } from '../typings.js';
1
+ import type { AnyEntity, EntityMetadata, EntityName, PopulateOptions } from '../typings.js';
2
2
  import type { Configuration } from '../utils/Configuration.js';
3
3
  /**
4
4
  * Helper that allows to keep track of where we are currently at when serializing complex entity graph with cycles.
@@ -10,21 +10,21 @@ export declare class SerializationContext<T extends object> {
10
10
  private readonly populate;
11
11
  private readonly fields?;
12
12
  private readonly exclude?;
13
- readonly path: [string, string][];
13
+ readonly path: [EntityName, string][];
14
14
  readonly visited: Set<Partial<any>>;
15
15
  private entities;
16
16
  constructor(config: Configuration, populate?: PopulateOptions<T>[], fields?: Set<string> | undefined, exclude?: string[] | undefined);
17
17
  /**
18
18
  * Returns true when there is a cycle detected.
19
19
  */
20
- visit(entityName: string, prop: string): boolean;
21
- leave<U>(entityName: string, prop: string): void;
20
+ visit(entityName: EntityName, prop: string): boolean;
21
+ leave(entityName: EntityName, prop: string): void;
22
22
  close(): void;
23
23
  /**
24
24
  * When initializing new context, we need to propagate it to the whole entity graph recursively.
25
25
  */
26
26
  static propagate(root: SerializationContext<any>, entity: AnyEntity, isVisible: (meta: EntityMetadata, prop: string) => boolean): void;
27
- isMarkedAsPopulated(entityName: string, prop: string): boolean;
28
- isPartiallyLoaded(entityName: string, prop: string): boolean;
27
+ isMarkedAsPopulated(entityName: EntityName, prop: string): boolean;
28
+ isPartiallyLoaded(entityName: EntityName, prop: string): boolean;
29
29
  private register;
30
30
  }
@@ -36,8 +36,8 @@ export class SerializationContext {
36
36
  }
37
37
  leave(entityName, prop) {
38
38
  const last = this.path.pop();
39
- /* v8 ignore next 3 */
40
- if (!last || last[0] !== entityName || last[1] !== prop) {
39
+ /* v8 ignore next */
40
+ if (last?.[0] !== entityName || last[1] !== prop) {
41
41
  throw new Error(`Trying to leave wrong property: ${entityName}.${prop} instead of ${last?.join('.')}`);
42
42
  }
43
43
  }
@@ -98,7 +98,7 @@ export class SerializationContext {
98
98
  }
99
99
  let fields = [...this.fields];
100
100
  for (const segment of this.path) {
101
- /* v8 ignore next 3 */
101
+ /* v8 ignore next */
102
102
  if (fields.length === 0) {
103
103
  return true;
104
104
  }
@@ -1,4 +1,4 @@
1
- import { Type, type TransformContext } from './Type.js';
1
+ import { type TransformContext, Type } from './Type.js';
2
2
  import type { EntityProperty } from '../typings.js';
3
3
  import type { Platform } from '../platforms/Platform.js';
4
4
  export declare class ArrayType<T = string> extends Type<T[] | null, string | null> {
@@ -1,5 +1,4 @@
1
1
  import { Type } from './Type.js';
2
- import { Utils } from '../utils/Utils.js';
3
2
  import { ValidationError } from '../errors.js';
4
3
  export class ArrayType extends Type {
5
4
  toJsValue;
@@ -16,7 +15,7 @@ export class ArrayType extends Type {
16
15
  if (Array.isArray(value)) {
17
16
  return platform.marshallArray(value.map(i => this.toDbValue(i)));
18
17
  }
19
- /* v8 ignore next 3 */
18
+ /* v8 ignore next */
20
19
  if (context?.fromQuery) {
21
20
  return value;
22
21
  }
@@ -26,7 +25,7 @@ export class ArrayType extends Type {
26
25
  if (value == null) {
27
26
  return value;
28
27
  }
29
- if (Utils.isString(value)) {
28
+ if (typeof value === 'string') {
30
29
  value = platform.unmarshallArray(value);
31
30
  }
32
31
  return value.map(i => this.toJsValue(i));
@@ -5,13 +5,15 @@ import type { EntityProperty } from '../typings.js';
5
5
  * This type will automatically convert string values returned from the database to native JS bigints (default)
6
6
  * or numbers (safe only for values up to `Number.MAX_SAFE_INTEGER`), or strings, depending on the `mode`.
7
7
  */
8
- export declare class BigIntType extends Type<string | bigint | number | null | undefined, string | null | undefined> {
9
- mode?: "bigint" | "number" | "string" | undefined;
10
- constructor(mode?: "bigint" | "number" | "string" | 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;
8
+ export declare class BigIntType<Mode extends 'bigint' | 'number' | 'string' = 'bigint'> extends Type<JSTypeByMode<Mode> | null | undefined, string | null | undefined> {
9
+ mode?: Mode | undefined;
10
+ constructor(mode?: Mode | undefined);
11
+ convertToDatabaseValue(value: JSTypeByMode<Mode> | null | undefined): string | null | undefined;
12
+ convertToJSValue(value: string | bigint | null | undefined): JSTypeByMode<Mode> | null | undefined;
13
+ toJSON(value: JSTypeByMode<Mode> | null | undefined): JSTypeByMode<Mode> | null | undefined;
14
14
  getColumnType(prop: EntityProperty, platform: Platform): string;
15
15
  compareAsType(): string;
16
16
  compareValues(a: string, b: string): boolean;
17
17
  }
18
+ type JSTypeByMode<Mode extends 'bigint' | 'number' | 'string'> = Mode extends 'bigint' ? bigint : Mode extends 'number' ? number : string;
19
+ export {};
@@ -16,7 +16,7 @@ export class BigIntType extends Type {
16
16
  return '' + value;
17
17
  }
18
18
  convertToJSValue(value) {
19
- /* v8 ignore next 3 */
19
+ /* v8 ignore next */
20
20
  if (value == null) {
21
21
  return value;
22
22
  }
@@ -4,6 +4,5 @@ import type { EntityProperty } from '../typings.js';
4
4
  export declare class BlobType extends Uint8ArrayType {
5
5
  convertToJSValue(value: Buffer): Buffer | null;
6
6
  compareAsType(): string;
7
- ensureComparable(): boolean;
8
7
  getColumnType(prop: EntityProperty, platform: Platform): string;
9
8
  }
package/types/BlobType.js CHANGED
@@ -12,9 +12,6 @@ export class BlobType extends Uint8ArrayType {
12
12
  compareAsType() {
13
13
  return 'Buffer';
14
14
  }
15
- ensureComparable() {
16
- return false;
17
- }
18
15
  getColumnType(prop, platform) {
19
16
  return platform.getBlobDeclarationSQL();
20
17
  }
@@ -4,5 +4,6 @@ import type { EntityProperty } from '../typings.js';
4
4
  export declare class BooleanType extends Type<boolean | null | undefined, boolean | null | undefined> {
5
5
  getColumnType(prop: EntityProperty, platform: Platform): string;
6
6
  compareAsType(): string;
7
+ convertToJSValue(value: boolean | null | undefined): boolean | null | undefined;
7
8
  ensureComparable(): boolean;
8
9
  }
@@ -6,6 +6,9 @@ export class BooleanType extends Type {
6
6
  compareAsType() {
7
7
  return 'boolean';
8
8
  }
9
+ convertToJSValue(value) {
10
+ return Boolean(value);
11
+ }
9
12
  ensureComparable() {
10
13
  return false;
11
14
  }
@@ -4,12 +4,14 @@ import type { EntityProperty } from '../typings.js';
4
4
  /**
5
5
  * Type that maps an SQL DECIMAL to a JS string or number.
6
6
  */
7
- export declare class DecimalType extends Type<string | number, string> {
8
- mode?: "number" | "string" | undefined;
9
- constructor(mode?: "number" | "string" | undefined);
10
- convertToJSValue(value: string): number | string;
7
+ export declare class DecimalType<Mode extends 'number' | 'string' = 'string'> extends Type<JSTypeByMode<Mode>, string> {
8
+ mode?: Mode | undefined;
9
+ constructor(mode?: Mode | undefined);
10
+ convertToJSValue(value: string): JSTypeByMode<Mode>;
11
11
  compareValues(a: string, b: string): boolean;
12
12
  private format;
13
13
  getColumnType(prop: EntityProperty, platform: Platform): string;
14
14
  compareAsType(): string;
15
15
  }
16
+ type JSTypeByMode<Mode extends 'number' | 'string'> = Mode extends 'number' ? number : string;
17
+ export {};
@@ -8,7 +8,7 @@ export class DecimalType extends Type {
8
8
  super();
9
9
  this.mode = mode;
10
10
  }
11
- /* v8 ignore next 7 */
11
+ /* v8 ignore next */
12
12
  convertToJSValue(value) {
13
13
  if ((this.mode ?? this.prop?.runtimeType) === 'number') {
14
14
  return +value;
@@ -19,7 +19,7 @@ export class DecimalType extends Type {
19
19
  return this.format(a) === this.format(b);
20
20
  }
21
21
  format(val) {
22
- /* v8 ignore next 3 */
22
+ /* v8 ignore next */
23
23
  if (this.prop?.scale == null) {
24
24
  return +val;
25
25
  }
@@ -3,7 +3,7 @@ import { Type } from './Type.js';
3
3
  * Type that maps an SQL DOUBLE to a JS string or number.
4
4
  */
5
5
  export class DoubleType extends Type {
6
- /* v8 ignore next 7 */
6
+ /* v8 ignore next */
7
7
  convertToJSValue(value) {
8
8
  if (this.prop?.runtimeType === 'number') {
9
9
  return +value;
@@ -1,4 +1,3 @@
1
- import { inspect } from 'node:util';
2
1
  import { ArrayType } from './ArrayType.js';
3
2
  import { ValidationError } from '../errors.js';
4
3
  function mapHydrator(items, hydrate) {
@@ -19,7 +18,7 @@ export class EnumArrayType extends ArrayType {
19
18
  if (Array.isArray(value) && Array.isArray(this.items)) {
20
19
  const invalid = value.filter(v => !this.items.includes(v));
21
20
  if (invalid.length > 0) {
22
- throw new ValidationError(`Invalid enum array items provided in ${this.owner}: ${inspect(invalid)}`);
21
+ throw ValidationError.invalidEnumArrayItems(this.owner, invalid);
23
22
  }
24
23
  }
25
24
  return super.convertToDatabaseValue(value, platform, context);
@@ -5,7 +5,7 @@ export declare class JsonType extends Type<unknown, string | null> {
5
5
  convertToDatabaseValue(value: unknown, platform: Platform, context?: TransformContext): string | null;
6
6
  convertToJSValueSQL(key: string, platform: Platform): string;
7
7
  convertToDatabaseValueSQL(key: string, platform: Platform): string;
8
- convertToJSValue(value: string | unknown, platform: Platform): unknown;
8
+ convertToJSValue(value: string | unknown, platform: Platform, context?: TransformContext): unknown;
9
9
  getColumnType(prop: EntityProperty, platform: Platform): string;
10
10
  ensureComparable<T extends object>(meta: EntityMetadata<T>, prop: EntityProperty<T>): boolean;
11
11
  compareAsType(): string;
package/types/JsonType.js CHANGED
@@ -12,8 +12,13 @@ export class JsonType extends Type {
12
12
  convertToDatabaseValueSQL(key, platform) {
13
13
  return key + platform.castColumn(this.prop);
14
14
  }
15
- convertToJSValue(value, platform) {
16
- return platform.convertJsonToJSValue(value, this.prop);
15
+ convertToJSValue(value, platform, context) {
16
+ const isJsonColumn = ['json', 'jsonb', platform.getJsonDeclarationSQL()].includes(this.prop.columnTypes[0]);
17
+ const isObjectEmbedded = this.prop.embedded && this.prop.object;
18
+ if ((platform.convertsJsonAutomatically() || isObjectEmbedded) && isJsonColumn && !context?.force) {
19
+ return value;
20
+ }
21
+ return platform.convertJsonToJSValue(value, context);
17
22
  }
18
23
  getColumnType(prop, platform) {
19
24
  return platform.getJsonDeclarationSQL();
@@ -6,7 +6,7 @@ export class TinyIntType extends Type {
6
6
  compareAsType() {
7
7
  return 'number';
8
8
  }
9
- /* v8 ignore next 3 */
9
+ /* v8 ignore next */
10
10
  ensureComparable() {
11
11
  return false;
12
12
  }
package/types/Type.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { inspect } from 'node:util';
2
1
  import type { Platform } from '../platforms/Platform.js';
3
2
  import type { Constructor, EntityMetadata, EntityProperty } from '../typings.js';
4
3
  export interface TransformContext {
5
4
  fromQuery?: boolean;
5
+ force?: boolean;
6
6
  key?: string;
7
7
  mode?: 'hydration' | 'query' | 'query-data' | 'discovery' | 'serialization';
8
8
  }
@@ -23,7 +23,7 @@ export declare abstract class Type<JSType = string, DBType = JSType> {
23
23
  /**
24
24
  * Converts a value from its database representation to its JS representation of this type.
25
25
  */
26
- convertToJSValue(value: DBType, platform: Platform): JSType;
26
+ convertToJSValue(value: DBType, platform: Platform, context?: TransformContext): JSType;
27
27
  /**
28
28
  * Converts a value from its JS representation to its database representation of this type.
29
29
  */
@@ -78,6 +78,4 @@ export declare abstract class Type<JSType = string, DBType = JSType> {
78
78
  * Checks whether the argument is instance of `Type`.
79
79
  */
80
80
  static isMappedType(data: any): data is Type<any>;
81
- /** @ignore */
82
- [inspect.custom](depth?: number): string;
83
81
  }
package/types/Type.js CHANGED
@@ -1,4 +1,4 @@
1
- import { inspect } from 'node:util';
1
+ import { inspect } from '../logging/inspect.js';
2
2
  export class Type {
3
3
  static types = new Map();
4
4
  platform;
@@ -13,7 +13,7 @@ export class Type {
13
13
  /**
14
14
  * Converts a value from its database representation to its JS representation of this type.
15
15
  */
16
- convertToJSValue(value, platform) {
16
+ convertToJSValue(value, platform, context) {
17
17
  return value;
18
18
  }
19
19
  /**
@@ -65,7 +65,7 @@ export class Type {
65
65
  return !!data?.__mappedType;
66
66
  }
67
67
  /** @ignore */
68
- [inspect.custom](depth = 2) {
68
+ [Symbol.for('nodejs.util.inspect.custom')](depth = 2) {
69
69
  const object = { ...this };
70
70
  const hidden = ['prop', 'platform', 'meta'];
71
71
  hidden.forEach(k => delete object[k]);
@@ -5,6 +5,5 @@ export declare class Uint8ArrayType extends Type<Uint8Array | null> {
5
5
  convertToDatabaseValue(value: Uint8Array): Buffer;
6
6
  convertToJSValue(value: Buffer): Uint8Array | null;
7
7
  compareAsType(): string;
8
- ensureComparable(): boolean;
9
8
  getColumnType(prop: EntityProperty, platform: Platform): string;
10
9
  }
@@ -7,7 +7,7 @@ export class Uint8ArrayType extends Type {
7
7
  return Buffer.from(value);
8
8
  }
9
9
  convertToJSValue(value) {
10
- /* v8 ignore next 3 */
10
+ /* v8 ignore next */
11
11
  if (!value) {
12
12
  return value;
13
13
  }
@@ -22,9 +22,6 @@ export class Uint8ArrayType extends Type {
22
22
  compareAsType() {
23
23
  return 'Buffer';
24
24
  }
25
- ensureComparable() {
26
- return false;
27
- }
28
25
  getColumnType(prop, platform) {
29
26
  return platform.getBlobDeclarationSQL();
30
27
  }
package/types/index.d.ts CHANGED
@@ -19,7 +19,7 @@ import { StringType } from './StringType.js';
19
19
  import { TextType } from './TextType.js';
20
20
  import { TimeType } from './TimeType.js';
21
21
  import { TinyIntType } from './TinyIntType.js';
22
- import { IType, TransformContext, Type } from './Type.js';
22
+ import { type IType, type TransformContext, Type } from './Type.js';
23
23
  import { Uint8ArrayType } from './Uint8ArrayType.js';
24
24
  import { UnknownType } from './UnknownType.js';
25
25
  import { UuidType } from './UuidType.js';