@mikro-orm/core 7.0.0-dev.21 → 7.0.0-dev.211

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 (211) hide show
  1. package/EntityManager.d.ts +99 -57
  2. package/EntityManager.js +302 -276
  3. package/MikroORM.d.ts +44 -35
  4. package/MikroORM.js +103 -143
  5. package/README.md +3 -2
  6. package/cache/FileCacheAdapter.d.ts +1 -1
  7. package/cache/FileCacheAdapter.js +8 -7
  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 +16 -7
  13. package/connections/Connection.js +23 -14
  14. package/drivers/DatabaseDriver.d.ts +25 -16
  15. package/drivers/DatabaseDriver.js +80 -35
  16. package/drivers/IDatabaseDriver.d.ts +44 -17
  17. package/entity/BaseEntity.d.ts +2 -2
  18. package/entity/BaseEntity.js +0 -3
  19. package/entity/Collection.d.ts +94 -29
  20. package/entity/Collection.js +434 -97
  21. package/entity/EntityAssigner.d.ts +1 -1
  22. package/entity/EntityAssigner.js +26 -18
  23. package/entity/EntityFactory.d.ts +13 -1
  24. package/entity/EntityFactory.js +84 -53
  25. package/entity/EntityHelper.d.ts +2 -2
  26. package/entity/EntityHelper.js +40 -15
  27. package/entity/EntityLoader.d.ts +6 -6
  28. package/entity/EntityLoader.js +119 -82
  29. package/entity/EntityRepository.d.ts +24 -4
  30. package/entity/EntityRepository.js +8 -2
  31. package/entity/Reference.d.ts +6 -5
  32. package/entity/Reference.js +34 -9
  33. package/entity/WrappedEntity.d.ts +2 -7
  34. package/entity/WrappedEntity.js +3 -8
  35. package/entity/defineEntity.d.ts +594 -0
  36. package/entity/defineEntity.js +533 -0
  37. package/entity/index.d.ts +3 -2
  38. package/entity/index.js +3 -2
  39. package/entity/utils.d.ts +7 -0
  40. package/entity/utils.js +16 -4
  41. package/entity/validators.d.ts +11 -0
  42. package/entity/validators.js +65 -0
  43. package/enums.d.ts +21 -5
  44. package/enums.js +15 -1
  45. package/errors.d.ts +23 -9
  46. package/errors.js +59 -21
  47. package/events/EventManager.d.ts +2 -1
  48. package/events/EventManager.js +19 -11
  49. package/hydration/Hydrator.js +1 -2
  50. package/hydration/ObjectHydrator.d.ts +4 -4
  51. package/hydration/ObjectHydrator.js +52 -33
  52. package/index.d.ts +2 -2
  53. package/index.js +1 -2
  54. package/logging/DefaultLogger.d.ts +1 -1
  55. package/logging/DefaultLogger.js +1 -0
  56. package/logging/SimpleLogger.d.ts +1 -1
  57. package/logging/colors.d.ts +1 -1
  58. package/logging/colors.js +7 -6
  59. package/logging/index.d.ts +1 -0
  60. package/logging/index.js +1 -0
  61. package/logging/inspect.d.ts +2 -0
  62. package/logging/inspect.js +11 -0
  63. package/metadata/EntitySchema.d.ts +40 -23
  64. package/metadata/EntitySchema.js +81 -34
  65. package/metadata/MetadataDiscovery.d.ts +7 -10
  66. package/metadata/MetadataDiscovery.js +391 -331
  67. package/metadata/MetadataProvider.d.ts +11 -2
  68. package/metadata/MetadataProvider.js +46 -2
  69. package/metadata/MetadataStorage.d.ts +13 -11
  70. package/metadata/MetadataStorage.js +70 -37
  71. package/metadata/MetadataValidator.d.ts +17 -9
  72. package/metadata/MetadataValidator.js +97 -40
  73. package/metadata/discover-entities.d.ts +5 -0
  74. package/metadata/discover-entities.js +40 -0
  75. package/metadata/index.d.ts +1 -1
  76. package/metadata/index.js +1 -1
  77. package/metadata/types.d.ts +502 -0
  78. package/metadata/types.js +1 -0
  79. package/naming-strategy/AbstractNamingStrategy.d.ts +12 -4
  80. package/naming-strategy/AbstractNamingStrategy.js +14 -2
  81. package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
  82. package/naming-strategy/EntityCaseNamingStrategy.js +6 -5
  83. package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
  84. package/naming-strategy/MongoNamingStrategy.js +6 -6
  85. package/naming-strategy/NamingStrategy.d.ts +24 -4
  86. package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
  87. package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
  88. package/not-supported.d.ts +2 -0
  89. package/not-supported.js +4 -0
  90. package/package.json +18 -11
  91. package/platforms/ExceptionConverter.js +1 -1
  92. package/platforms/Platform.d.ts +7 -13
  93. package/platforms/Platform.js +20 -43
  94. package/serialization/EntitySerializer.d.ts +5 -0
  95. package/serialization/EntitySerializer.js +47 -27
  96. package/serialization/EntityTransformer.js +28 -18
  97. package/serialization/SerializationContext.d.ts +6 -6
  98. package/serialization/SerializationContext.js +16 -13
  99. package/types/ArrayType.d.ts +1 -1
  100. package/types/ArrayType.js +2 -3
  101. package/types/BigIntType.d.ts +8 -6
  102. package/types/BigIntType.js +1 -1
  103. package/types/BlobType.d.ts +0 -1
  104. package/types/BlobType.js +0 -3
  105. package/types/BooleanType.d.ts +2 -1
  106. package/types/BooleanType.js +3 -0
  107. package/types/DecimalType.d.ts +6 -4
  108. package/types/DecimalType.js +3 -3
  109. package/types/DoubleType.js +2 -2
  110. package/types/EnumArrayType.js +1 -2
  111. package/types/JsonType.d.ts +1 -1
  112. package/types/JsonType.js +7 -2
  113. package/types/TinyIntType.js +1 -1
  114. package/types/Type.d.ts +2 -4
  115. package/types/Type.js +3 -3
  116. package/types/Uint8ArrayType.d.ts +0 -1
  117. package/types/Uint8ArrayType.js +1 -4
  118. package/types/index.d.ts +1 -1
  119. package/typings.d.ts +290 -137
  120. package/typings.js +59 -44
  121. package/unit-of-work/ChangeSet.d.ts +2 -6
  122. package/unit-of-work/ChangeSet.js +4 -5
  123. package/unit-of-work/ChangeSetComputer.d.ts +1 -3
  124. package/unit-of-work/ChangeSetComputer.js +26 -13
  125. package/unit-of-work/ChangeSetPersister.d.ts +5 -4
  126. package/unit-of-work/ChangeSetPersister.js +70 -34
  127. package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
  128. package/unit-of-work/CommitOrderCalculator.js +13 -13
  129. package/unit-of-work/IdentityMap.d.ts +12 -0
  130. package/unit-of-work/IdentityMap.js +39 -1
  131. package/unit-of-work/UnitOfWork.d.ts +23 -3
  132. package/unit-of-work/UnitOfWork.js +175 -98
  133. package/utils/AbstractSchemaGenerator.d.ts +5 -5
  134. package/utils/AbstractSchemaGenerator.js +18 -16
  135. package/utils/AsyncContext.d.ts +6 -0
  136. package/utils/AsyncContext.js +42 -0
  137. package/utils/Configuration.d.ts +785 -207
  138. package/utils/Configuration.js +147 -190
  139. package/utils/ConfigurationLoader.d.ts +1 -54
  140. package/utils/ConfigurationLoader.js +1 -352
  141. package/utils/Cursor.d.ts +0 -3
  142. package/utils/Cursor.js +27 -11
  143. package/utils/DataloaderUtils.d.ts +15 -5
  144. package/utils/DataloaderUtils.js +64 -30
  145. package/utils/EntityComparator.d.ts +13 -9
  146. package/utils/EntityComparator.js +101 -42
  147. package/utils/QueryHelper.d.ts +14 -6
  148. package/utils/QueryHelper.js +87 -25
  149. package/utils/RawQueryFragment.d.ts +48 -25
  150. package/utils/RawQueryFragment.js +66 -70
  151. package/utils/RequestContext.js +2 -2
  152. package/utils/TransactionContext.js +2 -2
  153. package/utils/TransactionManager.d.ts +65 -0
  154. package/utils/TransactionManager.js +223 -0
  155. package/utils/Utils.d.ts +13 -126
  156. package/utils/Utils.js +100 -391
  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 +32 -0
  161. package/utils/fs-utils.js +178 -0
  162. package/utils/index.d.ts +2 -1
  163. package/utils/index.js +2 -1
  164. package/utils/upsert-utils.d.ts +9 -4
  165. package/utils/upsert-utils.js +55 -4
  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 -18
  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 -40
  185. package/decorators/ManyToMany.js +0 -14
  186. package/decorators/ManyToOne.d.ts +0 -32
  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 -26
  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 -13
  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 -116
  203. package/entity/ArrayCollection.js +0 -402
  204. package/entity/EntityValidator.d.ts +0 -19
  205. package/entity/EntityValidator.js +0 -150
  206. package/exports.d.ts +0 -24
  207. package/exports.js +0 -23
  208. package/metadata/ReflectMetadataProvider.d.ts +0 -8
  209. package/metadata/ReflectMetadataProvider.js +0 -44
  210. package/utils/resolveContextProvider.d.ts +0 -10
  211. package/utils/resolveContextProvider.js +0 -28
@@ -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
  }
@@ -73,18 +73,21 @@ export class SerializationContext {
73
73
  }
74
74
  }
75
75
  isMarkedAsPopulated(entityName, prop) {
76
- let populate = this.populate;
76
+ let populate = this.populate ?? [];
77
77
  for (const segment of this.path) {
78
- if (!populate) {
79
- return false;
80
- }
81
- const exists = populate.find(p => p.field === segment[1]);
82
- if (exists) {
83
- // we need to check for cycles here too, as we could fall into endless loops for bidirectional relations
84
- if (exists.all) {
85
- return !this.path.find(([cls, item]) => entityName === cls && prop === item);
78
+ const hints = populate.filter(p => p.field === segment[1]);
79
+ if (hints.length > 0) {
80
+ const childHints = [];
81
+ for (const hint of hints) {
82
+ // we need to check for cycles here too, as we could fall into endless loops for bidirectional relations
83
+ if (hint.all) {
84
+ return !this.path.find(([cls, item]) => entityName === cls && prop === item);
85
+ }
86
+ if (hint.children) {
87
+ childHints.push(...hint.children);
88
+ }
86
89
  }
87
- populate = exists.children;
90
+ populate = childHints;
88
91
  }
89
92
  }
90
93
  return !!populate?.some(p => p.field === prop);
@@ -95,7 +98,7 @@ export class SerializationContext {
95
98
  }
96
99
  let fields = [...this.fields];
97
100
  for (const segment of this.path) {
98
- /* v8 ignore next 3 */
101
+ /* v8 ignore next */
99
102
  if (fields.length === 0) {
100
103
  return true;
101
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
  }
@@ -1,8 +1,9 @@
1
1
  import { Type } from './Type.js';
2
2
  import type { Platform } from '../platforms/Platform.js';
3
3
  import type { EntityProperty } from '../typings.js';
4
- export declare class BooleanType extends Type<number | null | undefined, number | null | undefined> {
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,18 +8,18 @@ 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;
15
15
  }
16
- return value;
16
+ return String(value);
17
17
  }
18
18
  compareValues(a, b) {
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,12 +3,12 @@ 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;
10
10
  }
11
- return value;
11
+ return String(value);
12
12
  }
13
13
  getColumnType(prop, platform) {
14
14
  return platform.getDoubleDeclarationSQL();
@@ -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';