@mikro-orm/knex 6.0.0-dev.157 → 6.0.0-dev.159

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.157",
3
+ "version": "6.0.0-dev.159",
4
4
  "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.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -66,6 +66,6 @@
66
66
  "@mikro-orm/core": "^5.9.2"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.0.0-dev.157"
69
+ "@mikro-orm/core": "6.0.0-dev.159"
70
70
  }
71
71
  }
@@ -5,5 +5,6 @@ import type { IQueryBuilder } from '../typings';
5
5
  */
6
6
  export declare class ArrayCriteriaNode<T extends object> extends CriteriaNode<T> {
7
7
  process(qb: IQueryBuilder<T>, alias?: string): any;
8
+ unwrap(): any;
8
9
  willAutoJoin(qb: IQueryBuilder<T>, alias?: string): any;
9
10
  }
@@ -11,6 +11,11 @@ class ArrayCriteriaNode extends CriteriaNode_1.CriteriaNode {
11
11
  return node.process(qb, alias);
12
12
  });
13
13
  }
14
+ unwrap() {
15
+ return this.payload.map((node) => {
16
+ return node.unwrap();
17
+ });
18
+ }
14
19
  willAutoJoin(qb, alias) {
15
20
  return this.payload.some((node) => {
16
21
  return node.willAutoJoin(qb, alias);
@@ -17,6 +17,7 @@ export declare class CriteriaNode<T extends object> implements ICriteriaNode<T>
17
17
  index?: number;
18
18
  constructor(metadata: MetadataStorage, entityName: string, parent?: ICriteriaNode<T> | undefined, key?: EntityKey<T> | undefined, validate?: boolean);
19
19
  process(qb: IQueryBuilder<T>, alias?: string): any;
20
+ unwrap(): any;
20
21
  shouldInline(payload: any): boolean;
21
22
  willAutoJoin(qb: IQueryBuilder<T>, alias?: string): boolean;
22
23
  shouldRename(payload: any): boolean;
@@ -40,6 +40,9 @@ class CriteriaNode {
40
40
  process(qb, alias) {
41
41
  return this.payload;
42
42
  }
43
+ unwrap() {
44
+ return this.payload;
45
+ }
43
46
  shouldInline(payload) {
44
47
  return false;
45
48
  }
@@ -51,7 +54,9 @@ class CriteriaNode {
51
54
  const composite = this.prop?.joinColumns ? this.prop.joinColumns.length > 1 : false;
52
55
  const customExpression = CriteriaNode.isCustomExpression(this.key);
53
56
  const scalar = payload === null || core_1.Utils.isPrimaryKey(payload) || payload instanceof RegExp || payload instanceof Date || customExpression;
54
- const operator = core_1.Utils.isPlainObject(payload) && Object.keys(payload).every(k => core_1.Utils.isOperator(k, false));
57
+ const plainObject = core_1.Utils.isPlainObject(payload);
58
+ const keys = plainObject ? Object.keys(payload) : [];
59
+ const operator = plainObject && keys.every(k => core_1.Utils.isOperator(k, false));
55
60
  if (composite) {
56
61
  return true;
57
62
  }
@@ -5,6 +5,7 @@ import type { IQueryBuilder } from '../typings';
5
5
  */
6
6
  export declare class ObjectCriteriaNode<T extends object> extends CriteriaNode<T> {
7
7
  process(qb: IQueryBuilder<T>, alias?: string): any;
8
+ unwrap(): any;
8
9
  willAutoJoin(qb: IQueryBuilder<T>, alias?: string): boolean;
9
10
  shouldInline(payload: any): boolean;
10
11
  private inlineChildPayload;
@@ -11,13 +11,34 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
11
11
  process(qb, alias) {
12
12
  const nestedAlias = qb.getAliasForJoinPath(this.getPath());
13
13
  const ownerAlias = alias || qb.alias;
14
+ const keys = Object.keys(this.payload);
14
15
  if (nestedAlias) {
15
16
  alias = nestedAlias;
16
17
  }
17
18
  if (this.shouldAutoJoin(nestedAlias)) {
19
+ if (keys.some(k => ['$some', '$none', '$every'].includes(k))) {
20
+ const $and = [];
21
+ const primaryKeys = this.metadata.find(this.entityName).primaryKeys.map(pk => `${alias}.${pk}`);
22
+ for (const key of keys) {
23
+ const payload = this.payload[key].unwrap();
24
+ const sub = qb.clone(true).innerJoin(this.key, qb.getNextAlias(this.prop.type));
25
+ sub.select(this.prop.targetMeta.primaryKeys);
26
+ if (key === '$every') {
27
+ sub.where({ $not: { [this.key]: payload } });
28
+ }
29
+ else {
30
+ sub.where({ [this.key]: payload });
31
+ }
32
+ const op = key === '$some' ? '$in' : '$nin';
33
+ $and.push({
34
+ [core_1.Utils.getPrimaryKeyHash(primaryKeys)]: { [op]: sub.getKnexQuery() },
35
+ });
36
+ }
37
+ return { $and };
38
+ }
18
39
  alias = this.autoJoin(qb, ownerAlias);
19
40
  }
20
- return Object.keys(this.payload).reduce((o, field) => {
41
+ return keys.reduce((o, field) => {
21
42
  const childNode = this.payload[field];
22
43
  const payload = childNode.process(qb, this.prop ? alias : ownerAlias);
23
44
  const operator = core_1.Utils.isOperator(field);
@@ -46,16 +67,23 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
46
67
  return o;
47
68
  }, {});
48
69
  }
70
+ unwrap() {
71
+ return Object.keys(this.payload).reduce((o, field) => {
72
+ o[field] = this.payload[field].unwrap();
73
+ return o;
74
+ }, {});
75
+ }
49
76
  willAutoJoin(qb, alias) {
50
77
  const nestedAlias = qb.getAliasForJoinPath(this.getPath());
51
78
  const ownerAlias = alias || qb.alias;
79
+ const keys = Object.keys(this.payload);
52
80
  if (nestedAlias) {
53
81
  alias = nestedAlias;
54
82
  }
55
83
  if (this.shouldAutoJoin(nestedAlias)) {
56
- return true;
84
+ return !keys.some(k => ['$some', '$none', '$every'].includes(k));
57
85
  }
58
- return Object.keys(this.payload).some(field => {
86
+ return keys.some(field => {
59
87
  const childNode = this.payload[field];
60
88
  return childNode.willAutoJoin(qb, this.prop ? alias : ownerAlias);
61
89
  });
@@ -76,7 +104,8 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
76
104
  }
77
105
  else if (this.isPrefixed(k) || core_1.Utils.isOperator(k) || !childAlias) {
78
106
  const idx = prop.referencedPKs.indexOf(k);
79
- const key = idx !== -1 && !childAlias ? prop.joinColumns[idx] : k;
107
+ // FIXME maybe other kinds should be supported too?
108
+ const key = idx !== -1 && !childAlias && ![core_1.ReferenceKind.ONE_TO_MANY, core_1.ReferenceKind.MANY_TO_MANY].includes(prop.kind) ? prop.joinColumns[idx] : k;
80
109
  if (key in o) {
81
110
  const $and = o.$and ?? [];
82
111
  $and.push({ [key]: o[key] }, { [key]: payload[k] });
@@ -85,9 +114,7 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
85
114
  }
86
115
  else if (core_1.Utils.isOperator(k) && Array.isArray(payload[k])) {
87
116
  o[key] = payload[k].map((child) => Object.keys(child).reduce((o, childKey) => {
88
- const key = (this.isPrefixed(childKey) || core_1.Utils.isOperator(childKey))
89
- ? childKey
90
- : `${childAlias}.${childKey}`;
117
+ const key = (this.isPrefixed(childKey) || core_1.Utils.isOperator(childKey)) ? childKey : `${childAlias}.${childKey}`;
91
118
  o[key] = child[childKey];
92
119
  return o;
93
120
  }, {}));
@@ -217,7 +217,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
217
217
  * You can provide `EntityName.propName` as alias, then the field name will be used based on the metadata
218
218
  */
219
219
  as(alias: string): Knex.QueryBuilder;
220
- clone(): QueryBuilder<T>;
220
+ clone(reset?: boolean): QueryBuilder<T>;
221
221
  getKnex(): Knex.QueryBuilder;
222
222
  private joinReference;
223
223
  private prepareFields;
@@ -663,8 +663,11 @@ class QueryBuilder {
663
663
  Object.defineProperty(ret, '__as', { enumerable: false, value: alias });
664
664
  return ret;
665
665
  }
666
- clone() {
666
+ clone(reset) {
667
667
  const qb = new QueryBuilder(this.mainAlias.entityName, this.metadata, this.driver, this.context, this.mainAlias.aliasName, this.connectionType, this.em);
668
+ if (reset) {
669
+ return qb;
670
+ }
668
671
  Object.assign(qb, this);
669
672
  // clone array/object properties
670
673
  const properties = [
package/typings.d.ts CHANGED
@@ -126,6 +126,7 @@ export interface IQueryBuilder<T> {
126
126
  truncate(): this;
127
127
  count(field?: string | string[], distinct?: boolean): this;
128
128
  join(field: string, alias: string, cond?: QBFilterQuery, type?: JoinType, path?: string): this;
129
+ innerJoin(field: string, alias: string, cond?: QBFilterQuery): this;
129
130
  leftJoin(field: string, alias: string, cond?: QBFilterQuery): this;
130
131
  joinAndSelect(field: string, alias: string, cond?: QBFilterQuery): this;
131
132
  leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[]): this;
@@ -142,6 +143,7 @@ export interface IQueryBuilder<T> {
142
143
  having(cond?: QBFilterQuery | string, params?: any[]): this;
143
144
  getAliasForJoinPath(path: string): string | undefined;
144
145
  getNextAlias(entityName?: string): string;
146
+ clone(reset?: boolean): IQueryBuilder<T>;
145
147
  }
146
148
  export interface ICriteriaNode<T extends object> {
147
149
  readonly entityName: string;