@mikro-orm/core 7.0.0-dev.6 → 7.0.0-dev.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/EntityManager.js CHANGED
@@ -149,7 +149,7 @@ export class EntityManager {
149
149
  options._populateWhere = options.populateWhere ?? this.config.get('populateWhere');
150
150
  options.populateWhere = this.createPopulateWhere({ ...where }, options);
151
151
  options.populateFilter = await this.getJoinedFilters(meta, { ...where }, options);
152
- const results = await em.driver.find(entityName, where, { ctx: em.transactionContext, ...options });
152
+ const results = await em.driver.find(entityName, where, { ctx: em.transactionContext, em, ...options });
153
153
  if (results.length === 0) {
154
154
  await em.storeCache(options.cache, cached, []);
155
155
  return [];
@@ -547,6 +547,7 @@ export class EntityManager {
547
547
  options.populateFilter = await this.getJoinedFilters(meta, { ...where }, options);
548
548
  const data = await em.driver.findOne(entityName, where, {
549
549
  ctx: em.transactionContext,
550
+ em,
550
551
  ...options,
551
552
  });
552
553
  if (!data) {
@@ -1,7 +1,7 @@
1
- import type { AnyString, Constructor, Dictionary, EntityClass, FilterQuery } from '../typings.js';
1
+ import type { AnyString, Constructor, Dictionary, EntityClass, ObjectQuery } from '../typings.js';
2
2
  import type { FindOptions } from '../drivers/IDatabaseDriver.js';
3
3
  export declare function Entity<T extends EntityClass<unknown>>(options?: EntityOptions<T>): (target: T) => void;
4
- export type EntityOptions<T> = {
4
+ export type EntityOptions<T, E = T extends EntityClass<infer P> ? P : T> = {
5
5
  tableName?: string;
6
6
  schema?: string;
7
7
  collection?: string;
@@ -13,6 +13,6 @@ export type EntityOptions<T> = {
13
13
  abstract?: boolean;
14
14
  readonly?: boolean;
15
15
  virtual?: boolean;
16
- expression?: string | ((em: any, where: FilterQuery<T>, options: FindOptions<T, any, any, any>) => object);
16
+ expression?: string | ((em: any, where: ObjectQuery<E>, options: FindOptions<E, any, any, any>) => object);
17
17
  repository?: () => Constructor;
18
18
  };
@@ -146,6 +146,8 @@ export interface FindOptions<Entity, Hint extends string = never, Fields extends
146
146
  hintComments?: string | string[];
147
147
  loggerContext?: LogContext;
148
148
  logging?: LoggingOptions;
149
+ /** @internal used to apply filters to the auto-joined relations */
150
+ em?: EntityManager;
149
151
  }
150
152
  export interface FindByCursorOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never> extends Omit<FindOptions<T, P, F, E>, 'limit' | 'offset'> {
151
153
  }
@@ -14,7 +14,7 @@ export declare class ArrayCollection<T extends object, O extends object> {
14
14
  getItems(): T[];
15
15
  toArray<TT extends T>(): EntityDTO<TT>[];
16
16
  toJSON(): EntityDTO<T>[];
17
- getIdentifiers<U extends IPrimaryKey = Primary<T> & IPrimaryKey>(field?: string): U[];
17
+ getIdentifiers<U extends IPrimaryKey = Primary<T> & IPrimaryKey>(field?: string | string[]): U[];
18
18
  add(entity: T | Reference<T> | Iterable<T | Reference<T>>, ...entities: (T | Reference<T>)[]): void;
19
19
  /**
20
20
  * @internal
@@ -40,15 +40,22 @@ export class ArrayCollection {
40
40
  }
41
41
  getIdentifiers(field) {
42
42
  const items = this.getItems();
43
+ const targetMeta = this.property.targetMeta;
43
44
  if (items.length === 0) {
44
45
  return [];
45
46
  }
46
- field ??= this.property.targetMeta.serializedPrimaryKey;
47
+ field ??= targetMeta.compositePK ? targetMeta.primaryKeys : targetMeta.serializedPrimaryKey;
48
+ const cb = (i, f) => {
49
+ if (Utils.isEntity(i[f], true)) {
50
+ return wrap(i[f], true).getPrimaryKey();
51
+ }
52
+ return i[f];
53
+ };
47
54
  return items.map(i => {
48
- if (Utils.isEntity(i[field], true)) {
49
- return wrap(i[field], true).getPrimaryKey();
55
+ if (Array.isArray(field)) {
56
+ return field.map(f => cb(i, f));
50
57
  }
51
- return i[field];
58
+ return cb(i, field);
52
59
  });
53
60
  }
54
61
  add(entity, ...entities) {
@@ -12,7 +12,6 @@ export interface MatchingOptions<T extends object, P extends string = never> ext
12
12
  export declare class Collection<T extends object, O extends object = object> extends ArrayCollection<T, O> {
13
13
  private readonly?;
14
14
  private _populated?;
15
- private _em?;
16
15
  private _snapshot?;
17
16
  constructor(owner: O, items?: T[], initialized?: boolean);
18
17
  /**
@@ -7,7 +7,6 @@ import { helper } from './wrap.js';
7
7
  export class Collection extends ArrayCollection {
8
8
  readonly;
9
9
  _populated;
10
- _em;
11
10
  // this is for some reason needed for TS, otherwise it can fail with `Type instantiation is excessively deep and possibly infinite.`
12
11
  _snapshot;
13
12
  constructor(owner, items, initialized = true) {
@@ -220,9 +219,13 @@ export class Collection extends ArrayCollection {
220
219
  const em = this.getEntityManager();
221
220
  if (options.dataloader ?? [DataloaderType.ALL, DataloaderType.COLLECTION].includes(em.config.getDataloaderType())) {
222
221
  const order = [...this.items]; // copy order of references
223
- const customOrder = !!options.orderBy;
222
+ const orderBy = this.createOrderBy(options.orderBy);
223
+ const customOrder = orderBy.length > 0;
224
224
  // eslint-disable-next-line dot-notation
225
- const items = await em['colLoader'].load([this, options]);
225
+ const items = await em['colLoader'].load([
226
+ this,
227
+ { ...options, orderBy },
228
+ ]);
226
229
  if (!customOrder) {
227
230
  this.reorderItems(items, order);
228
231
  }
@@ -254,7 +257,7 @@ export class Collection extends ArrayCollection {
254
257
  }
255
258
  getEntityManager(items = [], required = true) {
256
259
  const wrapped = helper(this.owner);
257
- let em = (this._em ?? wrapped.__em);
260
+ let em = wrapped.__em;
258
261
  if (!em) {
259
262
  for (const i of items) {
260
263
  if (i && helper(i).__em) {
@@ -263,9 +266,6 @@ export class Collection extends ArrayCollection {
263
266
  }
264
267
  }
265
268
  }
266
- if (em) {
267
- Object.defineProperty(this, '_em', { value: em });
268
- }
269
269
  if (!em && required) {
270
270
  throw ValidationError.entityNotManaged(this.owner);
271
271
  }
@@ -946,6 +946,7 @@ export class MetadataDiscovery {
946
946
  meta.collection = meta.root.collection;
947
947
  meta.root.indexes = Utils.unique([...meta.root.indexes, ...meta.indexes]);
948
948
  meta.root.uniques = Utils.unique([...meta.root.uniques, ...meta.uniques]);
949
+ meta.root.checks = Utils.unique([...meta.root.checks, ...meta.checks]);
949
950
  }
950
951
  createDiscriminatorProperty(meta) {
951
952
  meta.addProperty({
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.6",
4
+ "version": "7.0.0-dev.8",
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",
@@ -55,7 +55,7 @@
55
55
  "dotenv": "16.4.7",
56
56
  "esprima": "4.0.1",
57
57
  "globby": "11.1.0",
58
- "mikro-orm": "7.0.0-dev.6",
58
+ "mikro-orm": "7.0.0-dev.8",
59
59
  "reflect-metadata": "0.2.2"
60
60
  }
61
61
  }
@@ -13,4 +13,5 @@ export declare class BigIntType extends Type<string | bigint | number | null | u
13
13
  toJSON(value: string | bigint | null | undefined): string | bigint | null | undefined;
14
14
  getColumnType(prop: EntityProperty, platform: Platform): string;
15
15
  compareAsType(): string;
16
+ compareValues(a: string, b: string): boolean;
16
17
  }
@@ -42,4 +42,7 @@ export class BigIntType extends Type {
42
42
  compareAsType() {
43
43
  return this.mode ?? 'bigint';
44
44
  }
45
+ compareValues(a, b) {
46
+ return String(a) === String(b);
47
+ }
45
48
  }
package/typings.d.ts CHANGED
@@ -419,7 +419,7 @@ export interface EntityMetadata<T = any> {
419
419
  schema?: string;
420
420
  pivotTable?: boolean;
421
421
  virtual?: boolean;
422
- expression?: string | ((em: any, where: FilterQuery<T>, options: FindOptions<T, any, any, any>) => MaybePromise<RawQueryFragment | object | string>);
422
+ expression?: string | ((em: any, where: ObjectQuery<T>, options: FindOptions<T, any, any, any>) => MaybePromise<RawQueryFragment | object | string>);
423
423
  discriminatorColumn?: EntityKey<T> | AnyString;
424
424
  discriminatorValue?: number | string;
425
425
  discriminatorMap?: Dictionary<string>;
@@ -45,7 +45,7 @@ export class QueryHelper {
45
45
  return false;
46
46
  }
47
47
  if (meta.primaryKeys.every(pk => pk in where) && Utils.getObjectKeysSize(where) === meta.primaryKeys.length) {
48
- return !!key && !GroupOperator[key] && Object.keys(where).every(k => !Utils.isPlainObject(where[k]) || Object.keys(where[k]).every(v => {
48
+ return !!key && !GroupOperator[key] && key !== '$not' && Object.keys(where).every(k => !Utils.isPlainObject(where[k]) || Object.keys(where[k]).every(v => {
49
49
  if (Utils.isOperator(v, false)) {
50
50
  return false;
51
51
  }