@mikro-orm/core 7.0.0-dev.111 → 7.0.0-dev.113

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.
@@ -154,21 +154,21 @@ export declare class EntityManager<Driver extends IDatabaseDriver = IDatabaseDri
154
154
  * - POJO/entity instance
155
155
  *
156
156
  * ```ts
157
- * const currentCursor = await em.findByCursor(User, {}, {
157
+ * const currentCursor = await em.findByCursor(User, {
158
158
  * first: 10,
159
159
  * after: previousCursor, // cursor instance
160
160
  * orderBy: { id: 'desc' },
161
161
  * });
162
162
  *
163
163
  * // to fetch next page
164
- * const nextCursor = await em.findByCursor(User, {}, {
164
+ * const nextCursor = await em.findByCursor(User, {
165
165
  * first: 10,
166
166
  * after: currentCursor.endCursor, // opaque string
167
167
  * orderBy: { id: 'desc' },
168
168
  * });
169
169
  *
170
170
  * // to fetch next page
171
- * const nextCursor2 = await em.findByCursor(User, {}, {
171
+ * const nextCursor2 = await em.findByCursor(User, {
172
172
  * first: 10,
173
173
  * after: { id: lastSeenId }, // entity-like POJO
174
174
  * orderBy: { id: 'desc' },
@@ -196,7 +196,7 @@ export declare class EntityManager<Driver extends IDatabaseDriver = IDatabaseDri
196
196
  * }
197
197
  * ```
198
198
  */
199
- findByCursor<Entity extends object, Hint extends string = never, Fields extends string = '*', Excludes extends string = never, IncludeCount extends boolean = true>(entityName: EntityName<Entity>, where: FilterQuery<NoInfer<Entity>>, options: FindByCursorOptions<Entity, Hint, Fields, Excludes, IncludeCount>): Promise<Cursor<Entity, Hint, Fields, Excludes, IncludeCount>>;
199
+ findByCursor<Entity extends object, Hint extends string = never, Fields extends string = '*', Excludes extends string = never, IncludeCount extends boolean = true>(entityName: EntityName<Entity>, options: FindByCursorOptions<Entity, Hint, Fields, Excludes, IncludeCount>): Promise<Cursor<Entity, Hint, Fields, Excludes, IncludeCount>>;
200
200
  /**
201
201
  * Refreshes the persistent state of an entity from the database, overriding any local changes that have not yet been
202
202
  * persisted. Returns the same entity instance (same object reference), but re-hydrated. If the entity is no longer
package/EntityManager.js CHANGED
@@ -489,21 +489,21 @@ export class EntityManager {
489
489
  * - POJO/entity instance
490
490
  *
491
491
  * ```ts
492
- * const currentCursor = await em.findByCursor(User, {}, {
492
+ * const currentCursor = await em.findByCursor(User, {
493
493
  * first: 10,
494
494
  * after: previousCursor, // cursor instance
495
495
  * orderBy: { id: 'desc' },
496
496
  * });
497
497
  *
498
498
  * // to fetch next page
499
- * const nextCursor = await em.findByCursor(User, {}, {
499
+ * const nextCursor = await em.findByCursor(User, {
500
500
  * first: 10,
501
501
  * after: currentCursor.endCursor, // opaque string
502
502
  * orderBy: { id: 'desc' },
503
503
  * });
504
504
  *
505
505
  * // to fetch next page
506
- * const nextCursor2 = await em.findByCursor(User, {}, {
506
+ * const nextCursor2 = await em.findByCursor(User, {
507
507
  * first: 10,
508
508
  * after: { id: lastSeenId }, // entity-like POJO
509
509
  * orderBy: { id: 'desc' },
@@ -531,16 +531,17 @@ export class EntityManager {
531
531
  * }
532
532
  * ```
533
533
  */
534
- async findByCursor(entityName, where, options) {
534
+ async findByCursor(entityName, options) {
535
535
  const em = this.getContext(false);
536
536
  entityName = Utils.className(entityName);
537
537
  options.overfetch ??= true;
538
+ options.where ??= {};
538
539
  if (Utils.isEmpty(options.orderBy)) {
539
540
  throw new Error('Explicit `orderBy` option required');
540
541
  }
541
542
  const [entities, count] = options.includeCount !== false
542
- ? await em.findAndCount(entityName, where, options)
543
- : [await em.find(entityName, where, options)];
543
+ ? await em.findAndCount(entityName, options.where, options)
544
+ : [await em.find(entityName, options.where, options)];
544
545
  return new Cursor(entities, count, options, this.metadata.get(entityName));
545
546
  }
546
547
  /**
@@ -166,7 +166,7 @@ export interface FindOptions<Entity, Hint extends string = never, Fields extends
166
166
  /** @internal used to apply filters to the auto-joined relations */
167
167
  em?: EntityManager;
168
168
  }
169
- export interface FindByCursorOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never, I extends boolean = true> extends Omit<FindOptions<T, P, F, E>, 'limit' | 'offset'> {
169
+ export interface FindByCursorOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never, I extends boolean = true> extends Omit<FindAllOptions<T, P, F, E>, 'limit' | 'offset'> {
170
170
  includeCount?: I;
171
171
  }
172
172
  export interface FindOneOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends Omit<FindOptions<T, P, F, E>, 'limit' | 'lockMode'> {
@@ -80,7 +80,7 @@ export declare class EntityRepository<Entity extends object> {
80
80
  /**
81
81
  * @inheritDoc EntityManager.findByCursor
82
82
  */
83
- findByCursor<Hint extends string = never, Fields extends string = '*', Excludes extends string = never, IncludeCount extends boolean = true>(where: FilterQuery<Entity>, options: FindByCursorOptions<Entity, Hint, Fields, Excludes, IncludeCount>): Promise<Cursor<Entity, Hint, Fields, Excludes, IncludeCount>>;
83
+ findByCursor<Hint extends string = never, Fields extends string = '*', Excludes extends string = never, IncludeCount extends boolean = true>(options: FindByCursorOptions<Entity, Hint, Fields, Excludes, IncludeCount>): Promise<Cursor<Entity, Hint, Fields, Excludes, IncludeCount>>;
84
84
  /**
85
85
  * Finds all entities of given type. You can pass additional options via the `options` parameter.
86
86
  */
@@ -90,8 +90,8 @@ export class EntityRepository {
90
90
  /**
91
91
  * @inheritDoc EntityManager.findByCursor
92
92
  */
93
- async findByCursor(where, options) {
94
- return this.getEntityManager().findByCursor(this.entityName, where, options);
93
+ async findByCursor(options) {
94
+ return this.getEntityManager().findByCursor(this.entityName, options);
95
95
  }
96
96
  /**
97
97
  * Finds all entities of given type. You can pass additional options via the `options` parameter.
@@ -21,10 +21,6 @@ export class EntitySchema {
21
21
  if (meta.class && !meta.internal) {
22
22
  EntitySchema.REGISTRY.set(meta.class, this);
23
23
  }
24
- if (meta.tableName || meta.collection) {
25
- Utils.renameKey(meta, 'tableName', 'collection');
26
- meta.tableName = meta.collection;
27
- }
28
24
  Object.assign(this._meta, { className: meta.name }, meta);
29
25
  this._meta.root ??= this._meta;
30
26
  }
@@ -197,7 +193,7 @@ export class EntitySchema {
197
193
  const tableName = this._meta.collection ?? this._meta.tableName;
198
194
  if (tableName?.includes('.') && !this._meta.schema) {
199
195
  this._meta.schema = tableName.substring(0, tableName.indexOf('.'));
200
- this._meta.collection = tableName.substring(tableName.indexOf('.') + 1);
196
+ this._meta.tableName = tableName.substring(tableName.indexOf('.') + 1);
201
197
  }
202
198
  this.initProperties();
203
199
  this.initPrimaryKeys();
@@ -296,9 +296,9 @@ export class MetadataDiscovery {
296
296
  }
297
297
  // if the definition is using EntitySchema we still want it to go through the metadata provider to validate no types are missing
298
298
  this.metadataProvider.loadEntityMetadata(meta);
299
- if (!meta.collection && meta.name) {
299
+ if (!meta.tableName && meta.name) {
300
300
  const entityName = root.discriminatorColumn ? root.name : meta.name;
301
- meta.collection = this.namingStrategy.classToTableName(entityName);
301
+ meta.tableName = this.namingStrategy.classToTableName(entityName);
302
302
  }
303
303
  this.metadataProvider.saveToCache(meta);
304
304
  meta.root = root;
@@ -404,7 +404,7 @@ export class MetadataDiscovery {
404
404
  prop.inverseJoinColumns ??= second.fieldNames;
405
405
  }
406
406
  if (!prop.pivotTable && prop.owner && this.platform.usesPivotTable()) {
407
- prop.pivotTable = this.namingStrategy.joinTableName(meta.tableName, meta2.tableName, prop.name);
407
+ prop.pivotTable = this.namingStrategy.joinTableName(meta.className, meta2.tableName, prop.name, meta.tableName);
408
408
  }
409
409
  if (prop.mappedBy) {
410
410
  const prop2 = meta2.properties[prop.mappedBy];
@@ -417,13 +417,13 @@ export class MetadataDiscovery {
417
417
  prop.inverseJoinColumns = prop2.joinColumns;
418
418
  }
419
419
  prop.referencedColumnNames ??= Utils.flatten(meta.primaryKeys.map(primaryKey => meta.properties[primaryKey].fieldNames));
420
- prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.namingStrategy.joinKeyColumnName(meta.root.className, referencedColumnName, meta.compositePK));
420
+ prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.namingStrategy.joinKeyColumnName(meta.root.className, referencedColumnName, meta.compositePK, meta.root.tableName));
421
421
  prop.inverseJoinColumns ??= this.initManyToOneFieldName(prop, meta2.root.className);
422
422
  }
423
423
  initManyToOneFields(prop) {
424
424
  const meta2 = this.metadata.get(prop.type);
425
425
  const fieldNames = Utils.flatten(meta2.primaryKeys.map(primaryKey => meta2.properties[primaryKey].fieldNames));
426
- Utils.defaultValue(prop, 'referencedTableName', meta2.collection);
426
+ Utils.defaultValue(prop, 'referencedTableName', meta2.tableName);
427
427
  if (!prop.joinColumns) {
428
428
  prop.joinColumns = fieldNames.map(fieldName => this.namingStrategy.joinKeyColumnName(prop.name, fieldName, fieldNames.length > 1));
429
429
  }
@@ -573,8 +573,8 @@ export class MetadataDiscovery {
573
573
  }
574
574
  // handle self-referenced m:n with same default field names
575
575
  if (meta.className === targetType && prop.joinColumns.every((joinColumn, idx) => joinColumn === prop.inverseJoinColumns[idx])) {
576
- prop.joinColumns = prop.referencedColumnNames.map(name => this.namingStrategy.joinKeyColumnName(meta.className + '_1', name, meta.compositePK));
577
- prop.inverseJoinColumns = prop.referencedColumnNames.map(name => this.namingStrategy.joinKeyColumnName(meta.className + '_2', name, meta.compositePK));
576
+ prop.joinColumns = prop.referencedColumnNames.map(name => this.namingStrategy.joinKeyColumnName(meta.tableName + '_1', name, meta.compositePK));
577
+ prop.inverseJoinColumns = prop.referencedColumnNames.map(name => this.namingStrategy.joinKeyColumnName(meta.tableName + '_2', name, meta.compositePK));
578
578
  if (prop.inversedBy) {
579
579
  const prop2 = this.metadata.get(targetType).properties[prop.inversedBy];
580
580
  prop2.inverseJoinColumns = prop.joinColumns;
@@ -916,7 +916,7 @@ export class MetadataDiscovery {
916
916
  newProp.inherited = !meta.root.properties[prop.name];
917
917
  meta.root.addProperty(newProp);
918
918
  });
919
- meta.collection = meta.root.collection;
919
+ meta.tableName = meta.root.tableName;
920
920
  meta.root.indexes = Utils.unique([...meta.root.indexes, ...meta.indexes]);
921
921
  meta.root.uniques = Utils.unique([...meta.root.uniques, ...meta.uniques]);
922
922
  meta.root.checks = Utils.unique([...meta.root.checks, ...meta.checks]);
@@ -26,10 +26,10 @@ export declare abstract class AbstractNamingStrategy implements NamingStrategy {
26
26
  * @inheritDoc
27
27
  */
28
28
  inverseSideName(entityName: string, propertyName: string, kind: ReferenceKind): string;
29
- abstract classToTableName(entityName: string): string;
29
+ abstract classToTableName(entityName: string, tableName?: string): string;
30
30
  abstract joinColumnName(propertyName: string): string;
31
- abstract joinKeyColumnName(entityName: string, referencedColumnName?: string): string;
32
- abstract joinTableName(sourceEntity: string, targetEntity: string, propertyName?: string): string;
31
+ abstract joinKeyColumnName(entityName: string, referencedColumnName?: string, composite?: boolean, tableName?: string): string;
32
+ abstract joinTableName(sourceEntity: string, targetEntity: string, propertyName?: string, tableName?: string): string;
33
33
  abstract propertyToColumnName(propertyName: string, object?: boolean): string;
34
34
  abstract referenceColumnName(): string;
35
35
  }
@@ -3,10 +3,10 @@ import { AbstractNamingStrategy } from './AbstractNamingStrategy.js';
3
3
  * This strategy keeps original entity/property names for table/column.
4
4
  */
5
5
  export declare class EntityCaseNamingStrategy extends AbstractNamingStrategy {
6
- classToTableName(entityName: string): string;
6
+ classToTableName(entityName: string, tableName?: string): string;
7
7
  joinColumnName(propertyName: string): string;
8
- joinKeyColumnName(entityName: string, referencedColumnName?: string, composite?: boolean): string;
9
- joinTableName(sourceEntity: string, targetEntity: string, propertyName: string): string;
8
+ joinKeyColumnName(entityName: string, referencedColumnName?: string, composite?: boolean, tableName?: string): string;
9
+ joinTableName(sourceEntity: string, targetEntity: string, propertyName: string, tableName?: string): string;
10
10
  propertyToColumnName(propertyName: string): string;
11
11
  referenceColumnName(): string;
12
12
  }
@@ -3,21 +3,22 @@ import { AbstractNamingStrategy } from './AbstractNamingStrategy.js';
3
3
  * This strategy keeps original entity/property names for table/column.
4
4
  */
5
5
  export class EntityCaseNamingStrategy extends AbstractNamingStrategy {
6
- classToTableName(entityName) {
7
- return entityName;
6
+ classToTableName(entityName, tableName) {
7
+ return tableName ?? entityName;
8
8
  }
9
9
  joinColumnName(propertyName) {
10
10
  return propertyName;
11
11
  }
12
- joinKeyColumnName(entityName, referencedColumnName, composite = false) {
12
+ joinKeyColumnName(entityName, referencedColumnName, composite, tableName) {
13
+ entityName = this.classToTableName(entityName, tableName);
13
14
  const name = entityName.substr(0, 1).toLowerCase() + entityName.substr(1);
14
15
  if (composite && referencedColumnName) {
15
16
  return name + '_' + referencedColumnName;
16
17
  }
17
18
  return name;
18
19
  }
19
- joinTableName(sourceEntity, targetEntity, propertyName) {
20
- return this.classToTableName(sourceEntity) + '_' + this.propertyToColumnName(propertyName);
20
+ joinTableName(sourceEntity, targetEntity, propertyName, tableName) {
21
+ return this.classToTableName(sourceEntity, tableName) + '_' + this.propertyToColumnName(propertyName);
21
22
  }
22
23
  propertyToColumnName(propertyName) {
23
24
  return propertyName;
@@ -1,9 +1,9 @@
1
1
  import { AbstractNamingStrategy } from './AbstractNamingStrategy.js';
2
2
  export declare class MongoNamingStrategy extends AbstractNamingStrategy {
3
- classToTableName(entityName: string): string;
3
+ classToTableName(entityName: string, tableName?: string): string;
4
4
  joinColumnName(propertyName: string): string;
5
- joinKeyColumnName(entityName: string, referencedColumnName?: string): string;
6
- joinTableName(sourceEntity: string, targetEntity: string, propertyName: string): string;
5
+ joinKeyColumnName(entityName: string, referencedColumnName?: string, composite?: boolean, tableName?: string): string;
6
+ joinTableName(sourceEntity: string, targetEntity: string, propertyName: string, tableName?: string): string;
7
7
  propertyToColumnName(propertyName: string): string;
8
8
  referenceColumnName(): string;
9
9
  }
@@ -1,16 +1,16 @@
1
1
  import { AbstractNamingStrategy } from './AbstractNamingStrategy.js';
2
2
  export class MongoNamingStrategy extends AbstractNamingStrategy {
3
- classToTableName(entityName) {
4
- return entityName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
3
+ classToTableName(entityName, tableName) {
4
+ return tableName ?? entityName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
5
5
  }
6
6
  joinColumnName(propertyName) {
7
7
  return propertyName;
8
8
  }
9
- joinKeyColumnName(entityName, referencedColumnName) {
10
- return entityName;
9
+ joinKeyColumnName(entityName, referencedColumnName, composite, tableName) {
10
+ return tableName ?? entityName;
11
11
  }
12
- joinTableName(sourceEntity, targetEntity, propertyName) {
13
- return this.classToTableName(sourceEntity) + '_' + this.propertyToColumnName(propertyName);
12
+ joinTableName(sourceEntity, targetEntity, propertyName, tableName) {
13
+ return this.classToTableName(sourceEntity, tableName) + '_' + this.propertyToColumnName(propertyName);
14
14
  }
15
15
  propertyToColumnName(propertyName) {
16
16
  return propertyName;
@@ -7,7 +7,7 @@ export interface NamingStrategy {
7
7
  /**
8
8
  * Return a table name for an entity class
9
9
  */
10
- classToTableName(entityName: string): string;
10
+ classToTableName(entityName: string, tableName?: string): string;
11
11
  /**
12
12
  * Return a migration name. This name should allow ordering.
13
13
  */
@@ -67,11 +67,11 @@ export interface NamingStrategy {
67
67
  /**
68
68
  * Return a join table name
69
69
  */
70
- joinTableName(sourceEntity: string, targetEntity: string, propertyName: string): string;
70
+ joinTableName(sourceEntity: string, targetEntity: string, propertyName: string, tableName?: string): string;
71
71
  /**
72
72
  * Return the foreign key column name for the given parameters
73
73
  */
74
- joinKeyColumnName(entityName: string, referencedColumnName?: string, composite?: boolean): string;
74
+ joinKeyColumnName(entityName: string, referencedColumnName?: string, composite?: boolean, tableName?: string): string;
75
75
  /**
76
76
  * Returns key/constraint name for the given type. Some drivers might not support all the types (e.g. mysql and sqlite enforce the PK name).
77
77
  */
@@ -1,9 +1,9 @@
1
1
  import { AbstractNamingStrategy } from './AbstractNamingStrategy.js';
2
2
  export declare class UnderscoreNamingStrategy extends AbstractNamingStrategy {
3
- classToTableName(entityName: string): string;
3
+ classToTableName(entityName: string, tableName?: string): string;
4
4
  joinColumnName(propertyName: string): string;
5
- joinKeyColumnName(entityName: string, referencedColumnName?: string): string;
6
- joinTableName(sourceEntity: string, targetEntity: string, propertyName: string): string;
5
+ joinKeyColumnName(entityName: string, referencedColumnName?: string, composite?: boolean, tableName?: string): string;
6
+ joinTableName(sourceEntity: string, targetEntity: string, propertyName: string, tableName?: string): string;
7
7
  propertyToColumnName(propertyName: string, object?: boolean): string;
8
8
  referenceColumnName(): string;
9
9
  private underscore;
@@ -1,16 +1,16 @@
1
1
  import { AbstractNamingStrategy } from './AbstractNamingStrategy.js';
2
2
  export class UnderscoreNamingStrategy extends AbstractNamingStrategy {
3
- classToTableName(entityName) {
4
- return this.underscore(entityName);
3
+ classToTableName(entityName, tableName) {
4
+ return tableName ?? this.underscore(entityName);
5
5
  }
6
6
  joinColumnName(propertyName) {
7
7
  return this.underscore(propertyName) + '_' + this.referenceColumnName();
8
8
  }
9
- joinKeyColumnName(entityName, referencedColumnName) {
10
- return this.classToTableName(entityName) + '_' + (referencedColumnName || this.referenceColumnName());
9
+ joinKeyColumnName(entityName, referencedColumnName, composite, tableName) {
10
+ return this.classToTableName(entityName, tableName) + '_' + (referencedColumnName || this.referenceColumnName());
11
11
  }
12
- joinTableName(sourceEntity, targetEntity, propertyName) {
13
- return this.classToTableName(sourceEntity) + '_' + this.classToTableName(propertyName);
12
+ joinTableName(sourceEntity, targetEntity, propertyName, tableName) {
13
+ return this.classToTableName(sourceEntity, tableName) + '_' + this.classToTableName(propertyName);
14
14
  }
15
15
  propertyToColumnName(propertyName, object) {
16
16
  return this.underscore(propertyName);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.111",
4
+ "version": "7.0.0-dev.113",
5
5
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
package/utils/Utils.js CHANGED
@@ -123,7 +123,7 @@ export function parseJsonSafe(value) {
123
123
  }
124
124
  export class Utils {
125
125
  static PK_SEPARATOR = '~~~';
126
- static #ORM_VERSION = '7.0.0-dev.111';
126
+ static #ORM_VERSION = '7.0.0-dev.113';
127
127
  /**
128
128
  * Checks if the argument is instance of `Object`. Returns false for arrays.
129
129
  */