@mikro-orm/core 6.4.17-dev.9 → 6.4.17-dev.90

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 (54) hide show
  1. package/EntityManager.d.ts +11 -2
  2. package/EntityManager.js +37 -22
  3. package/README.md +1 -2
  4. package/connections/Connection.d.ts +4 -2
  5. package/connections/Connection.js +2 -2
  6. package/decorators/Entity.d.ts +14 -0
  7. package/decorators/Indexed.d.ts +2 -2
  8. package/decorators/Transactional.d.ts +1 -0
  9. package/decorators/Transactional.js +3 -3
  10. package/drivers/IDatabaseDriver.d.ts +4 -0
  11. package/entity/ArrayCollection.d.ts +3 -1
  12. package/entity/ArrayCollection.js +7 -2
  13. package/entity/EntityFactory.d.ts +6 -0
  14. package/entity/EntityFactory.js +17 -6
  15. package/entity/EntityHelper.js +4 -1
  16. package/entity/EntityLoader.js +26 -18
  17. package/entity/Reference.d.ts +5 -0
  18. package/entity/Reference.js +16 -0
  19. package/entity/WrappedEntity.js +1 -1
  20. package/entity/defineEntity.d.ts +528 -0
  21. package/entity/defineEntity.js +684 -0
  22. package/entity/index.d.ts +2 -0
  23. package/entity/index.js +2 -0
  24. package/entity/utils.d.ts +7 -0
  25. package/entity/utils.js +16 -3
  26. package/enums.d.ts +3 -1
  27. package/enums.js +2 -0
  28. package/hydration/ObjectHydrator.js +1 -1
  29. package/index.d.ts +1 -1
  30. package/index.mjs +4 -0
  31. package/metadata/MetadataDiscovery.d.ts +0 -1
  32. package/metadata/MetadataDiscovery.js +16 -13
  33. package/package.json +4 -4
  34. package/platforms/Platform.d.ts +3 -1
  35. package/types/BooleanType.d.ts +1 -1
  36. package/typings.d.ts +18 -9
  37. package/typings.js +21 -4
  38. package/unit-of-work/ChangeSetPersister.d.ts +4 -2
  39. package/unit-of-work/ChangeSetPersister.js +14 -10
  40. package/unit-of-work/UnitOfWork.d.ts +1 -1
  41. package/unit-of-work/UnitOfWork.js +22 -6
  42. package/utils/Configuration.d.ts +7 -1
  43. package/utils/Configuration.js +1 -0
  44. package/utils/ConfigurationLoader.js +2 -2
  45. package/utils/Cursor.js +3 -0
  46. package/utils/EntityComparator.d.ts +6 -2
  47. package/utils/EntityComparator.js +29 -8
  48. package/utils/QueryHelper.d.ts +6 -0
  49. package/utils/QueryHelper.js +47 -4
  50. package/utils/RawQueryFragment.d.ts +34 -0
  51. package/utils/RawQueryFragment.js +35 -0
  52. package/utils/Utils.d.ts +2 -2
  53. package/utils/Utils.js +32 -8
  54. package/utils/upsert-utils.js +9 -1
@@ -77,6 +77,7 @@ export declare class Configuration<D extends IDatabaseDriver = IDatabaseDriver,
77
77
  upsertManaged: true;
78
78
  forceEntityConstructor: false;
79
79
  forceUndefined: false;
80
+ processOnCreateHooksEarly: false;
80
81
  ensureDatabase: true;
81
82
  ensureIndexes: false;
82
83
  batchSize: number;
@@ -345,6 +346,11 @@ export interface MikroORMOptions<D extends IDatabaseDriver = IDatabaseDriver, EM
345
346
  upsertManaged: boolean;
346
347
  forceEntityConstructor: boolean | (Constructor<AnyEntity> | string)[];
347
348
  forceUndefined: boolean;
349
+ /**
350
+ * Property `onCreate` hooks are normally executed during `flush` operation.
351
+ * With this option, they will be processed early inside `em.create()` method.
352
+ */
353
+ processOnCreateHooksEarly: boolean;
348
354
  forceUtcTimezone?: boolean;
349
355
  timezone?: string;
350
356
  ensureDatabase: boolean | EnsureDatabaseOptions;
@@ -353,7 +359,7 @@ export interface MikroORMOptions<D extends IDatabaseDriver = IDatabaseDriver, EM
353
359
  useBatchUpdates?: boolean;
354
360
  batchSize: number;
355
361
  hydrator: HydratorConstructor;
356
- loadStrategy: LoadStrategy | 'select-in' | 'joined';
362
+ loadStrategy: LoadStrategy | `${LoadStrategy}`;
357
363
  dataloader: DataloaderType | boolean;
358
364
  populateWhere?: PopulateHint | `${PopulateHint}`;
359
365
  flushMode: FlushMode | 'commit' | 'auto' | 'always';
@@ -71,6 +71,7 @@ class Configuration {
71
71
  upsertManaged: true,
72
72
  forceEntityConstructor: false,
73
73
  forceUndefined: false,
74
+ processOnCreateHooksEarly: false,
74
75
  ensureDatabase: true,
75
76
  ensureIndexes: false,
76
77
  batchSize: 300,
@@ -213,7 +213,7 @@ class ConfigurationLoader {
213
213
  const baseDir = options instanceof Configuration_1.Configuration ? options.get('baseDir') : options?.baseDir;
214
214
  const path = process.env.MIKRO_ORM_ENV ?? ((baseDir ?? process.cwd()) + '/.env');
215
215
  const env = {};
216
- dotenv_1.default.config({ path, processEnv: env });
216
+ dotenv_1.default.config({ path, processEnv: env, quiet: true });
217
217
  // only propagate known env vars
218
218
  for (const key of Object.keys(env)) {
219
219
  if (key.startsWith('MIKRO_ORM_')) {
@@ -347,7 +347,7 @@ class ConfigurationLoader {
347
347
  // inspired by https://github.com/facebook/docusaurus/pull/3386
348
348
  static checkPackageVersion() {
349
349
  const coreVersion = Utils_1.Utils.getORMVersion();
350
- if (process.env.MIKRO_ORM_ALLOW_VERSION_MISMATCH) {
350
+ if (process.env.MIKRO_ORM_ALLOW_VERSION_MISMATCH || coreVersion === 'N/A') {
351
351
  return coreVersion;
352
352
  }
353
353
  const deps = this.getORMPackages();
package/utils/Cursor.js CHANGED
@@ -110,6 +110,9 @@ class Cursor {
110
110
  if (Utils_1.Utils.isEntity(value, true)) {
111
111
  value = (0, wrap_1.helper)(value).getPrimaryKey();
112
112
  }
113
+ if (Utils_1.Utils.isScalarReference(value)) {
114
+ value = value.unwrap();
115
+ }
113
116
  if (object) {
114
117
  return ({ [prop]: value });
115
118
  }
@@ -1,6 +1,8 @@
1
1
  import type { EntityData, EntityDictionary, EntityMetadata, EntityProperty, IMetadataStorage } from '../typings';
2
2
  import type { Platform } from '../platforms';
3
- type Comparator<T> = (a: T, b: T) => EntityData<T>;
3
+ type Comparator<T> = (a: T, b: T, options?: {
4
+ includeInverseSides?: boolean;
5
+ }) => EntityData<T>;
4
6
  type ResultMapper<T> = (result: EntityData<T>) => EntityData<T> | null;
5
7
  type SnapshotGenerator<T> = (entity: T) => EntityData<T>;
6
8
  type CompositeKeyPart = string | CompositeKeyPart[];
@@ -18,7 +20,9 @@ export declare class EntityComparator {
18
20
  /**
19
21
  * Computes difference between two entities.
20
22
  */
21
- diffEntities<T>(entityName: string, a: EntityData<T>, b: EntityData<T>): EntityData<T>;
23
+ diffEntities<T>(entityName: string, a: EntityData<T>, b: EntityData<T>, options?: {
24
+ includeInverseSides?: boolean;
25
+ }): EntityData<T>;
22
26
  matching<T>(entityName: string, a: EntityData<T>, b: EntityData<T>): boolean;
23
27
  /**
24
28
  * Removes ORM specific code from entities and prepares it for serializing. Used before change set computation.
@@ -23,9 +23,9 @@ class EntityComparator {
23
23
  /**
24
24
  * Computes difference between two entities.
25
25
  */
26
- diffEntities(entityName, a, b) {
26
+ diffEntities(entityName, a, b, options) {
27
27
  const comparator = this.getEntityComparator(entityName);
28
- return Utils_1.Utils.callCompiledFunction(comparator, a, b);
28
+ return Utils_1.Utils.callCompiledFunction(comparator, a, b, options);
29
29
  }
30
30
  matching(entityName, a, b) {
31
31
  const diff = this.diffEntities(entityName, a, b);
@@ -158,6 +158,7 @@ class EntityComparator {
158
158
  const lines = [];
159
159
  const context = new Map();
160
160
  context.set('getCompositeKeyValue', (val) => Utils_1.Utils.flatten(Utils_1.Utils.getCompositeKeyValue(val, meta, 'convertToDatabaseValue', this.platform)));
161
+ context.set('getPrimaryKeyHash', (val) => Utils_1.Utils.getPrimaryKeyHash(Utils_1.Utils.asArray(val)));
161
162
  if (meta.primaryKeys.length > 1) {
162
163
  lines.push(` const pks = entity.__helper.__pk ? getCompositeKeyValue(entity.__helper.__pk) : [`);
163
164
  meta.primaryKeys.forEach(pk => {
@@ -173,14 +174,23 @@ class EntityComparator {
173
174
  }
174
175
  else {
175
176
  const pk = meta.primaryKeys[0];
176
- if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
177
+ const prop = meta.properties[pk];
178
+ if (prop.kind !== enums_1.ReferenceKind.SCALAR) {
177
179
  lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) return entity${this.wrap(pk)}.__helper.getSerializedPrimaryKey();`);
178
180
  }
179
181
  const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
180
182
  if (serializedPrimaryKey) {
181
183
  lines.push(` return '' + entity.${serializedPrimaryKey.name};`);
182
184
  }
183
- lines.push(` return '' + entity.${meta.primaryKeys[0]};`);
185
+ else if (prop.customType) {
186
+ const convertorKey = this.registerCustomType(meta.properties[pk], context);
187
+ const idx = this.tmpIndex++;
188
+ lines.push(` const val_${idx} = convertToDatabaseValue_${convertorKey}(entity${this.wrap(pk)});`);
189
+ lines.push(` return getPrimaryKeyHash(val_${idx});`);
190
+ }
191
+ else {
192
+ lines.push(` return '' + entity${this.wrap(pk)};`);
193
+ }
184
194
  }
185
195
  const code = `// compiled pk serializer for entity ${meta.className}\n`
186
196
  + `return function(entity) {\n${lines.join('\n')}\n}`;
@@ -534,11 +544,22 @@ class EntityComparator {
534
544
  context.set('compareBuffers', Utils_1.compareBuffers);
535
545
  context.set('compareObjects', Utils_1.compareObjects);
536
546
  context.set('equals', Utils_1.equals);
537
- meta.comparableProps.forEach(prop => {
538
- lines.push(this.getPropertyComparator(prop, context));
539
- });
547
+ for (const prop of meta.comparableProps) {
548
+ // skip properties that are not hydrated
549
+ if (prop.hydrate !== false) {
550
+ lines.push(this.getPropertyComparator(prop, context));
551
+ }
552
+ }
553
+ // also compare 1:1 inverse sides, important for `factory.mergeData`
554
+ lines.push(`if (options?.includeInverseSides) {`);
555
+ for (const prop of meta.bidirectionalRelations) {
556
+ if (prop.kind === enums_1.ReferenceKind.ONE_TO_ONE && !prop.owner && prop.hydrate !== false) {
557
+ lines.push(this.getPropertyComparator(prop, context));
558
+ }
559
+ }
560
+ lines.push(`}`);
540
561
  const code = `// compiled comparator for entity ${meta.className}\n`
541
- + `return function(last, current) {\n const diff = {};\n${lines.join('\n')}\n return diff;\n}`;
562
+ + `return function(last, current, options) {\n const diff = {};\n${lines.join('\n')}\n return diff;\n}`;
542
563
  const comparator = Utils_1.Utils.createFunction(context, code);
543
564
  this.comparators.set(entityName, comparator);
544
565
  return comparator;
@@ -1,10 +1,16 @@
1
1
  import type { Dictionary, EntityMetadata, EntityProperty, FilterDef, FilterQuery } from '../typings';
2
2
  import type { Platform } from '../platforms';
3
3
  import type { MetadataStorage } from '../metadata/MetadataStorage';
4
+ /** @internal */
4
5
  export declare class QueryHelper {
5
6
  static readonly SUPPORTED_OPERATORS: string[];
6
7
  static processParams(params: unknown): any;
7
8
  static processObjectParams<T extends object>(params?: T): T;
9
+ /**
10
+ * converts `{ account: { $or: [ [Object], [Object] ] } }`
11
+ * to `{ $or: [ { account: [Object] }, { account: [Object] } ] }`
12
+ */
13
+ static liftGroupOperators<T extends object>(where: Dictionary, meta: EntityMetadata<T>, metadata: MetadataStorage, key?: string): string | undefined;
8
14
  static inlinePrimaryKeyObjects<T extends object>(where: Dictionary, meta: EntityMetadata<T>, metadata: MetadataStorage, key?: string): boolean;
9
15
  static processWhere<T extends object>(options: ProcessWhereOptions<T>): FilterQuery<T>;
10
16
  static getActiveFilters(entityName: string, options: Dictionary<boolean | Dictionary> | string[] | boolean, filters: Dictionary<FilterDef>): FilterDef[];
@@ -7,6 +7,7 @@ const enums_1 = require("../enums");
7
7
  const JsonType_1 = require("../types/JsonType");
8
8
  const wrap_1 = require("../entity/wrap");
9
9
  const RawQueryFragment_1 = require("./RawQueryFragment");
10
+ /** @internal */
10
11
  class QueryHelper {
11
12
  static SUPPORTED_OPERATORS = ['>', '<', '<=', '>=', '!', '!='];
12
13
  static processParams(params) {
@@ -36,11 +37,52 @@ class QueryHelper {
36
37
  });
37
38
  return params;
38
39
  }
40
+ /**
41
+ * converts `{ account: { $or: [ [Object], [Object] ] } }`
42
+ * to `{ $or: [ { account: [Object] }, { account: [Object] } ] }`
43
+ */
44
+ static liftGroupOperators(where, meta, metadata, key) {
45
+ if (!Utils_1.Utils.isPlainObject(where)) {
46
+ return undefined;
47
+ }
48
+ const keys = Object.keys(where);
49
+ const groupOperator = keys.find(k => {
50
+ return Utils_1.Utils.isGroupOperator(k) && Array.isArray(where[k]) && where[k].every(cond => {
51
+ return Utils_1.Utils.isPlainObject(cond) && Object.keys(cond).every(k2 => {
52
+ if (Utils_1.Utils.isOperator(k2, false)) {
53
+ if (k2 === '$not') {
54
+ return Object.keys(cond[k2]).every(k3 => meta.primaryKeys.includes(k3));
55
+ }
56
+ return true;
57
+ }
58
+ return meta.primaryKeys.includes(k2);
59
+ });
60
+ });
61
+ });
62
+ if (groupOperator) {
63
+ return groupOperator;
64
+ }
65
+ for (const k of keys) {
66
+ const value = where[k];
67
+ const prop = meta.properties[k];
68
+ if (!prop || ![enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
69
+ continue;
70
+ }
71
+ const op = this.liftGroupOperators(value, prop.targetMeta, metadata, k);
72
+ if (op) {
73
+ delete where[k];
74
+ where[op] = value[op].map((v) => {
75
+ return { [k]: v };
76
+ });
77
+ }
78
+ }
79
+ return undefined;
80
+ }
39
81
  static inlinePrimaryKeyObjects(where, meta, metadata, key) {
40
82
  if (Array.isArray(where)) {
41
83
  where.forEach((item, i) => {
42
84
  if (this.inlinePrimaryKeyObjects(item, meta, metadata, key)) {
43
- where[i] = Utils_1.Utils.getPrimaryKeyValues(item, meta.primaryKeys, false);
85
+ where[i] = Utils_1.Utils.getPrimaryKeyValues(item, meta, false);
44
86
  }
45
87
  });
46
88
  }
@@ -50,7 +92,7 @@ class QueryHelper {
50
92
  if (meta.primaryKeys.every(pk => pk in where) && Utils_1.Utils.getObjectKeysSize(where) === meta.primaryKeys.length) {
51
93
  return !!key && !enums_1.GroupOperator[key] && key !== '$not' && Object.keys(where).every(k => !Utils_1.Utils.isPlainObject(where[k]) || Object.keys(where[k]).every(v => {
52
94
  if (Utils_1.Utils.isOperator(v, false)) {
53
- return false;
95
+ return true;
54
96
  }
55
97
  if (meta.properties[k].primary && [enums_1.ReferenceKind.ONE_TO_ONE, enums_1.ReferenceKind.MANY_TO_ONE].includes(meta.properties[k].kind)) {
56
98
  return this.inlinePrimaryKeyObjects(where[k], meta.properties[k].targetMeta, metadata, v);
@@ -61,7 +103,7 @@ class QueryHelper {
61
103
  Object.keys(where).forEach(k => {
62
104
  const meta2 = metadata.find(meta.properties[k]?.type) || meta;
63
105
  if (this.inlinePrimaryKeyObjects(where[k], meta2, metadata, k)) {
64
- where[k] = Utils_1.Utils.getPrimaryKeyValues(where[k], meta2.primaryKeys, true);
106
+ where[k] = Utils_1.Utils.getPrimaryKeyValues(where[k], meta2, true);
65
107
  }
66
108
  });
67
109
  return false;
@@ -72,6 +114,7 @@ class QueryHelper {
72
114
  const meta = metadata.find(entityName);
73
115
  // inline PK-only objects in M:N queries, so we don't join the target entity when not needed
74
116
  if (meta && root) {
117
+ QueryHelper.liftGroupOperators(where, meta, metadata);
75
118
  QueryHelper.inlinePrimaryKeyObjects(where, meta, metadata);
76
119
  }
77
120
  if (options.platform.getConfig().get('ignoreUndefinedInQuery') && where && typeof where === 'object') {
@@ -121,7 +164,7 @@ class QueryHelper {
121
164
  value = QueryHelper.processCustomType(prop, value, platform, undefined, true);
122
165
  }
123
166
  const isJsonProperty = prop?.customType instanceof JsonType_1.JsonType && Utils_1.Utils.isPlainObject(value) && !platform.isRaw(value) && Object.keys(value)[0] !== '$eq';
124
- if (isJsonProperty) {
167
+ if (isJsonProperty && prop?.kind !== enums_1.ReferenceKind.EMBEDDED) {
125
168
  return this.processJsonCondition(o, value, [prop.fieldNames[0]], platform, aliased);
126
169
  }
127
170
  if (Array.isArray(value) && !Utils_1.Utils.isOperator(key) && !QueryHelper.isSupportedOperator(key) && !key.includes('?') && options.type !== 'orderBy') {
@@ -70,6 +70,24 @@ export declare const ALIAS_REPLACEMENT_RE = "\\[::alias::\\]";
70
70
  * ```ts
71
71
  * @Filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
72
72
  * ```
73
+ *
74
+ * The `raw` helper can be used within indexes and uniques to write database-agnostic SQL expressions. In that case, you can use `'??'` to tag your database identifiers (table name, column names, index name, ...) inside your expression, and pass those identifiers as a second parameter to the `raw` helper. Internally, those will automatically be quoted according to the database in use:
75
+ *
76
+ * ```ts
77
+ * // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("country")
78
+ * // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`country`)
79
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => raw(`create index ?? on ?? (??)`, ['custom_idx_on_name', table, columns.name]) })
80
+ * @Entity({ schema: 'library' })
81
+ * export class Author { ... }
82
+ * ```
83
+ *
84
+ * You can also use the `quote` tag function to write database-agnostic SQL expressions. The end-result is the same as using the `raw` function regarding database identifiers quoting, only to have a more elegant expression syntax:
85
+ *
86
+ * ```ts
87
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
88
+ * @Entity({ schema: 'library' })
89
+ * export class Author { ... }
90
+ * ```
73
91
  */
74
92
  export declare function raw<T extends object = any, R = any>(sql: EntityKey<T> | EntityKey<T>[] | AnyString | ((alias: string) => string) | RawQueryFragment, params?: readonly unknown[] | Dictionary<unknown>): R;
75
93
  /**
@@ -94,3 +112,19 @@ export declare namespace sql {
94
112
  var upper: <T extends object>(key: string | ((alias: string) => string)) => string;
95
113
  }
96
114
  export declare function createSqlFunction<T extends object, R = string>(func: string, key: string | ((alias: string) => string)): R;
115
+ /**
116
+ * Tag function providing quoting of db identifiers (table name, columns names, index names, ...).
117
+ *
118
+ * Within the template literal on which the tag function is applied, all placeholders are considered to be database identifiers, and will thus be quoted as so according to the database in use.
119
+ *
120
+ * ```ts
121
+ * // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("name")
122
+ * // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`name`)
123
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
124
+ * @Entity({ schema: 'library' })
125
+ * export class Author { ... }
126
+ * ```
127
+ */
128
+ export declare function quote(expParts: readonly string[], ...values: (string | {
129
+ toString(): string;
130
+ })[]): any;
@@ -4,6 +4,7 @@ exports.ALIAS_REPLACEMENT_RE = exports.ALIAS_REPLACEMENT = exports.RawQueryFragm
4
4
  exports.raw = raw;
5
5
  exports.sql = sql;
6
6
  exports.createSqlFunction = createSqlFunction;
7
+ exports.quote = quote;
7
8
  const node_async_hooks_1 = require("node:async_hooks");
8
9
  const node_util_1 = require("node:util");
9
10
  const Utils_1 = require("./Utils");
@@ -150,6 +151,24 @@ exports.ALIAS_REPLACEMENT_RE = '\\[::alias::\\]';
150
151
  * ```ts
151
152
  * @Filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
152
153
  * ```
154
+ *
155
+ * The `raw` helper can be used within indexes and uniques to write database-agnostic SQL expressions. In that case, you can use `'??'` to tag your database identifiers (table name, column names, index name, ...) inside your expression, and pass those identifiers as a second parameter to the `raw` helper. Internally, those will automatically be quoted according to the database in use:
156
+ *
157
+ * ```ts
158
+ * // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("country")
159
+ * // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`country`)
160
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => raw(`create index ?? on ?? (??)`, ['custom_idx_on_name', table, columns.name]) })
161
+ * @Entity({ schema: 'library' })
162
+ * export class Author { ... }
163
+ * ```
164
+ *
165
+ * You can also use the `quote` tag function to write database-agnostic SQL expressions. The end-result is the same as using the `raw` function regarding database identifiers quoting, only to have a more elegant expression syntax:
166
+ *
167
+ * ```ts
168
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
169
+ * @Entity({ schema: 'library' })
170
+ * export class Author { ... }
171
+ * ```
153
172
  */
154
173
  function raw(sql, params) {
155
174
  if (sql instanceof RawQueryFragment) {
@@ -207,3 +226,19 @@ sql.ref = (...keys) => raw('??', [keys.join('.')]);
207
226
  sql.now = (length) => raw('current_timestamp' + (length == null ? '' : `(${length})`));
208
227
  sql.lower = (key) => createSqlFunction('lower', key);
209
228
  sql.upper = (key) => createSqlFunction('upper', key);
229
+ /**
230
+ * Tag function providing quoting of db identifiers (table name, columns names, index names, ...).
231
+ *
232
+ * Within the template literal on which the tag function is applied, all placeholders are considered to be database identifiers, and will thus be quoted as so according to the database in use.
233
+ *
234
+ * ```ts
235
+ * // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("name")
236
+ * // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`name`)
237
+ * @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
238
+ * @Entity({ schema: 'library' })
239
+ * export class Author { ... }
240
+ * ```
241
+ */
242
+ function quote(expParts, ...values) {
243
+ return raw(expParts.join('??'), values);
244
+ }
package/utils/Utils.d.ts CHANGED
@@ -134,14 +134,14 @@ export declare class Utils {
134
134
  static getCompositeKeyHash<T>(data: EntityData<T>, meta: EntityMetadata<T>, convertCustomTypes?: boolean, platform?: Platform, flat?: boolean): string;
135
135
  static getPrimaryKeyHash(pks: (string | Buffer | Date)[]): string;
136
136
  static splitPrimaryKeys<T extends object>(key: string): EntityKey<T>[];
137
- static getPrimaryKeyValues<T>(entity: T, primaryKeys: string[], allowScalar?: boolean, convertCustomTypes?: boolean): any;
137
+ static getPrimaryKeyValues<T>(entity: T, primaryKeys: string[] | EntityMetadata<T>, allowScalar?: boolean, convertCustomTypes?: boolean): any;
138
138
  static getPrimaryKeyCond<T>(entity: T, primaryKeys: EntityKey<T>[]): Record<string, Primary<T>> | null;
139
139
  /**
140
140
  * Maps nested FKs from `[1, 2, 3]` to `[1, [2, 3]]`.
141
141
  */
142
142
  static mapFlatCompositePrimaryKey(fk: Primary<any>[], prop: EntityProperty, fieldNames?: string[], idx?: number): Primary<any> | Primary<any>[];
143
143
  static getPrimaryKeyCondFromArray<T extends object>(pks: Primary<T>[], meta: EntityMetadata<T>): Record<string, Primary<T>>;
144
- static getOrderedPrimaryKeys<T>(id: Primary<T> | Record<string, Primary<T>>, meta: EntityMetadata<T>, platform?: Platform, convertCustomTypes?: boolean): Primary<T>[];
144
+ static getOrderedPrimaryKeys<T>(id: Primary<T> | Record<string, Primary<T>>, meta: EntityMetadata<T>, platform?: Platform, convertCustomTypes?: boolean, allowScalar?: boolean): Primary<T>[];
145
145
  /**
146
146
  * Checks whether given object is an entity instance.
147
147
  */
package/utils/Utils.js CHANGED
@@ -498,6 +498,7 @@ class Utils {
498
498
  static splitPrimaryKeys(key) {
499
499
  return key.split(this.PK_SEPARATOR);
500
500
  }
501
+ // TODO v7: remove support for `primaryKeys: string[]`
501
502
  static getPrimaryKeyValues(entity, primaryKeys, allowScalar = false, convertCustomTypes = false) {
502
503
  /* istanbul ignore next */
503
504
  if (entity == null) {
@@ -509,9 +510,24 @@ class Utils {
509
510
  }
510
511
  return val;
511
512
  }
512
- const pk = Utils.isEntity(entity, true)
513
- ? (0, wrap_1.helper)(entity).getPrimaryKey(convertCustomTypes)
514
- : primaryKeys.reduce((o, pk) => { o[pk] = entity[pk]; return o; }, {});
513
+ const meta = Array.isArray(primaryKeys) ? undefined : primaryKeys;
514
+ primaryKeys = Array.isArray(primaryKeys) ? primaryKeys : meta.primaryKeys;
515
+ let pk;
516
+ if (Utils.isEntity(entity, true)) {
517
+ pk = (0, wrap_1.helper)(entity).getPrimaryKey(convertCustomTypes);
518
+ }
519
+ else {
520
+ pk = primaryKeys.reduce((o, pk) => {
521
+ const targetMeta = meta?.properties[pk].targetMeta;
522
+ if (targetMeta && Utils.isPlainObject(entity[pk])) {
523
+ o[pk] = Utils.getPrimaryKeyValues(entity[pk], targetMeta, allowScalar, convertCustomTypes);
524
+ }
525
+ else {
526
+ o[pk] = entity[pk];
527
+ }
528
+ return o;
529
+ }, {});
530
+ }
515
531
  if (primaryKeys.length > 1) {
516
532
  return toArray(pk);
517
533
  }
@@ -561,7 +577,7 @@ class Utils {
561
577
  return o;
562
578
  }, {});
563
579
  }
564
- static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false) {
580
+ static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false, allowScalar = false) {
565
581
  const data = (Utils.isPrimaryKey(id) ? { [meta.primaryKeys[0]]: id } : id);
566
582
  const pks = meta.primaryKeys.map((pk, idx) => {
567
583
  const prop = meta.properties[pk];
@@ -571,11 +587,14 @@ class Utils {
571
587
  value = prop.customType.convertToJSValue(value, platform);
572
588
  }
573
589
  if (prop.kind !== enums_1.ReferenceKind.SCALAR && prop.targetMeta) {
574
- const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes);
590
+ const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes, allowScalar);
575
591
  value = value2.length > 1 ? value2 : value2[0];
576
592
  }
577
593
  return value;
578
594
  });
595
+ if (allowScalar && pks.length === 1) {
596
+ return pks[0];
597
+ }
579
598
  // we need to flatten the PKs as composite PKs can be build from another composite PKs
580
599
  // and this method is used to get the PK hash in identity map, that expects flat array
581
600
  return Utils.flatten(pks);
@@ -928,9 +947,14 @@ class Utils {
928
947
  return require('../../package.json').version;
929
948
  }
930
949
  catch {
931
- // this works with node in production build (where we do not have the `src` folder)
932
- // eslint-disable-next-line @typescript-eslint/no-var-requires
933
- return require('../package.json').version;
950
+ try {
951
+ // this works with node in production build (where we do not have the `src` folder)
952
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
953
+ return require('../package.json').version;
954
+ }
955
+ catch {
956
+ return 'N/A';
957
+ }
934
958
  }
935
959
  }
936
960
  /* istanbul ignore next */
@@ -75,7 +75,15 @@ function getOnConflictReturningFields(meta, data, uniqueFields, options) {
75
75
  if (!meta) {
76
76
  return '*';
77
77
  }
78
- const keys = meta.comparableProps.filter(p => !p.lazy && !p.embeddable && Array.isArray(uniqueFields) && !uniqueFields.includes(p.name)).map(p => p.name);
78
+ const keys = meta.comparableProps.filter(p => {
79
+ if (p.lazy || p.embeddable) {
80
+ return false;
81
+ }
82
+ if (p.autoincrement) {
83
+ return true;
84
+ }
85
+ return Array.isArray(uniqueFields) && !uniqueFields.includes(p.name);
86
+ }).map(p => p.name);
79
87
  if (meta.versionProperty) {
80
88
  keys.push(meta.versionProperty);
81
89
  }