@mikro-orm/knex 6.4.6-dev.9 → 7.0.0-dev.0

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 (101) hide show
  1. package/AbstractSqlConnection.d.ts +15 -25
  2. package/AbstractSqlConnection.js +98 -126
  3. package/AbstractSqlDriver.d.ts +20 -13
  4. package/AbstractSqlDriver.js +73 -54
  5. package/AbstractSqlPlatform.d.ts +15 -3
  6. package/AbstractSqlPlatform.js +32 -11
  7. package/README.md +0 -2
  8. package/SqlEntityManager.d.ts +5 -6
  9. package/SqlEntityManager.js +5 -5
  10. package/SqlEntityRepository.d.ts +1 -6
  11. package/SqlEntityRepository.js +0 -6
  12. package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -0
  13. package/dialects/mssql/MsSqlNativeQueryBuilder.js +161 -0
  14. package/dialects/mssql/index.d.ts +1 -1
  15. package/dialects/mssql/index.js +1 -1
  16. package/dialects/mysql/MySqlExceptionConverter.js +1 -0
  17. package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +7 -0
  18. package/dialects/mysql/MySqlNativeQueryBuilder.js +81 -0
  19. package/dialects/mysql/MySqlPlatform.d.ts +5 -1
  20. package/dialects/mysql/MySqlPlatform.js +9 -1
  21. package/dialects/mysql/MySqlSchemaHelper.d.ts +6 -12
  22. package/dialects/mysql/MySqlSchemaHelper.js +42 -75
  23. package/dialects/mysql/index.d.ts +1 -3
  24. package/dialects/mysql/index.js +1 -3
  25. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +5 -0
  26. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +12 -0
  27. package/dialects/postgresql/index.d.ts +1 -1
  28. package/dialects/postgresql/index.js +1 -1
  29. package/dialects/sqlite/BaseSqliteConnection.d.ts +0 -5
  30. package/dialects/sqlite/BaseSqliteConnection.js +4 -42
  31. package/dialects/sqlite/BaseSqlitePlatform.d.ts +15 -3
  32. package/dialects/sqlite/BaseSqlitePlatform.js +20 -4
  33. package/dialects/sqlite/SqliteExceptionConverter.d.ts +9 -0
  34. package/dialects/sqlite/SqliteExceptionConverter.js +55 -0
  35. package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +6 -0
  36. package/dialects/sqlite/SqliteNativeQueryBuilder.js +15 -0
  37. package/dialects/sqlite/SqliteSchemaHelper.d.ts +38 -0
  38. package/dialects/sqlite/SqliteSchemaHelper.js +384 -0
  39. package/dialects/sqlite/index.d.ts +3 -5
  40. package/dialects/sqlite/index.js +3 -5
  41. package/index.d.ts +1 -2
  42. package/index.js +3 -5
  43. package/index.mjs +10 -13
  44. package/package.json +4 -18
  45. package/query/CriteriaNodeFactory.js +5 -5
  46. package/query/NativeQueryBuilder.d.ts +108 -0
  47. package/query/NativeQueryBuilder.js +429 -0
  48. package/query/ObjectCriteriaNode.js +3 -3
  49. package/query/QueryBuilder.d.ts +30 -34
  50. package/query/QueryBuilder.js +112 -123
  51. package/query/QueryBuilderHelper.d.ts +27 -23
  52. package/query/QueryBuilderHelper.js +174 -168
  53. package/query/ScalarCriteriaNode.js +4 -0
  54. package/query/index.d.ts +1 -0
  55. package/query/index.js +1 -0
  56. package/schema/DatabaseSchema.js +9 -6
  57. package/schema/DatabaseTable.d.ts +2 -1
  58. package/schema/DatabaseTable.js +9 -5
  59. package/schema/SchemaComparator.d.ts +1 -2
  60. package/schema/SchemaComparator.js +31 -18
  61. package/schema/SchemaHelper.d.ts +27 -33
  62. package/schema/SchemaHelper.js +294 -184
  63. package/schema/SqlSchemaGenerator.d.ts +3 -9
  64. package/schema/SqlSchemaGenerator.js +105 -229
  65. package/typings.d.ts +7 -17
  66. package/MonkeyPatchable.d.ts +0 -18
  67. package/MonkeyPatchable.js +0 -60
  68. package/dialects/mssql/MsSqlColumnCompiler.d.ts +0 -4
  69. package/dialects/mssql/MsSqlColumnCompiler.js +0 -10
  70. package/dialects/mssql/MsSqlKnexDialect.d.ts +0 -6
  71. package/dialects/mssql/MsSqlKnexDialect.js +0 -22
  72. package/dialects/mssql/MsSqlQueryCompiler.d.ts +0 -12
  73. package/dialects/mssql/MsSqlQueryCompiler.js +0 -94
  74. package/dialects/mssql/MsSqlTableCompiler.d.ts +0 -9
  75. package/dialects/mssql/MsSqlTableCompiler.js +0 -40
  76. package/dialects/mysql/MariaDbKnexDialect.d.ts +0 -6
  77. package/dialects/mysql/MariaDbKnexDialect.js +0 -16
  78. package/dialects/mysql/MySqlColumnCompiler.d.ts +0 -7
  79. package/dialects/mysql/MySqlColumnCompiler.js +0 -26
  80. package/dialects/mysql/MySqlConnection.d.ts +0 -8
  81. package/dialects/mysql/MySqlConnection.js +0 -43
  82. package/dialects/mysql/MySqlKnexDialect.d.ts +0 -5
  83. package/dialects/mysql/MySqlKnexDialect.js +0 -17
  84. package/dialects/mysql/MySqlQueryCompiler.d.ts +0 -5
  85. package/dialects/mysql/MySqlQueryCompiler.js +0 -23
  86. package/dialects/postgresql/PostgreSqlKnexDialect.d.ts +0 -7
  87. package/dialects/postgresql/PostgreSqlKnexDialect.js +0 -20
  88. package/dialects/postgresql/PostgreSqlQueryCompiler.d.ts +0 -4
  89. package/dialects/postgresql/PostgreSqlQueryCompiler.js +0 -13
  90. package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +0 -11
  91. package/dialects/postgresql/PostgreSqlTableCompiler.js +0 -89
  92. package/dialects/sqlite/BaseSqliteSchemaHelper.d.ts +0 -28
  93. package/dialects/sqlite/BaseSqliteSchemaHelper.js +0 -200
  94. package/dialects/sqlite/BetterSqliteKnexDialect.d.ts +0 -5
  95. package/dialects/sqlite/BetterSqliteKnexDialect.js +0 -15
  96. package/dialects/sqlite/LibSqlKnexDialect.d.ts +0 -11
  97. package/dialects/sqlite/LibSqlKnexDialect.js +0 -85
  98. package/dialects/sqlite/SqliteKnexDialect.d.ts +0 -8
  99. package/dialects/sqlite/SqliteKnexDialect.js +0 -67
  100. package/dialects/sqlite/SqliteTableCompiler.d.ts +0 -6
  101. package/dialects/sqlite/SqliteTableCompiler.js +0 -71
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.4.6-dev.9",
3
+ "version": "7.0.0-dev.0",
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",
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "homepage": "https://mikro-orm.io",
48
48
  "engines": {
49
- "node": ">= 18.12.0"
49
+ "node": ">= 22.11.0"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "yarn clean && yarn compile && yarn copy && yarn run -T gen-esm-wrapper index.js index.mjs",
@@ -59,27 +59,13 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "fs-extra": "11.3.0",
62
- "knex": "3.1.0",
62
+ "kysely": "https://pkg.pr.new/kysely-org/kysely/kysely@74aed5d",
63
63
  "sqlstring": "2.3.3"
64
64
  },
65
65
  "devDependencies": {
66
66
  "@mikro-orm/core": "^6.4.5"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.4.6-dev.9",
70
- "better-sqlite3": "*",
71
- "libsql": "*",
72
- "mariadb": "*"
73
- },
74
- "peerDependenciesMeta": {
75
- "better-sqlite3": {
76
- "optional": true
77
- },
78
- "libsql": {
79
- "optional": true
80
- },
81
- "mariadb": {
82
- "optional": true
83
- }
69
+ "@mikro-orm/core": "7.0.0-dev.0"
84
70
  }
85
71
  }
@@ -11,7 +11,7 @@ const ScalarCriteriaNode_1 = require("./ScalarCriteriaNode");
11
11
  class CriteriaNodeFactory {
12
12
  static createNode(metadata, entityName, payload, parent, key) {
13
13
  const customExpression = core_1.RawQueryFragment.isKnownFragment(key || '');
14
- const scalar = core_1.Utils.isPrimaryKey(payload) || core_1.Utils.isRawSql(payload) || payload instanceof RegExp || payload instanceof Date || customExpression;
14
+ const scalar = core_1.Utils.isPrimaryKey(payload) || (0, core_1.isRaw)(payload) || payload instanceof RegExp || payload instanceof Date || customExpression;
15
15
  if (Array.isArray(payload) && !scalar) {
16
16
  return this.createArrayNode(metadata, entityName, payload, parent, key);
17
17
  }
@@ -40,10 +40,10 @@ class CriteriaNodeFactory {
40
40
  static createObjectNode(metadata, entityName, payload, parent, key) {
41
41
  const meta = metadata.find(entityName);
42
42
  const node = new ObjectCriteriaNode_1.ObjectCriteriaNode(metadata, entityName, parent, key);
43
- node.payload = Object.keys(payload).reduce((o, item) => {
44
- o[item] = this.createObjectItemNode(metadata, entityName, node, payload, item, meta);
45
- return o;
46
- }, {});
43
+ node.payload = {};
44
+ for (const key of Object.keys(payload)) {
45
+ node.payload[key] = this.createObjectItemNode(metadata, entityName, node, payload, key, meta);
46
+ }
47
47
  return node;
48
48
  }
49
49
  static createObjectItemNode(metadata, entityName, node, payload, key, meta) {
@@ -0,0 +1,108 @@
1
+ import { type Dictionary, LockMode, type QueryFlag, RawQueryFragment } from '@mikro-orm/core';
2
+ import { QueryType } from './enums';
3
+ import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
4
+ interface Options {
5
+ tableName?: string | RawQueryFragment;
6
+ indexHint?: string;
7
+ select?: (string | RawQueryFragment)[];
8
+ distinct?: boolean;
9
+ distinctOn?: string[];
10
+ joins?: {
11
+ sql: string;
12
+ params: unknown[];
13
+ }[];
14
+ groupBy?: (string | RawQueryFragment)[];
15
+ where?: {
16
+ sql: string;
17
+ params: unknown[];
18
+ };
19
+ having?: {
20
+ sql: string;
21
+ params: unknown[];
22
+ };
23
+ orderBy?: string;
24
+ limit?: number;
25
+ offset?: number;
26
+ data?: Dictionary;
27
+ onConflict?: OnConflictClause;
28
+ lockMode?: LockMode;
29
+ lockTables?: string[];
30
+ returning?: (string | RawQueryFragment)[];
31
+ comment?: string[];
32
+ hintComment?: string[];
33
+ flags?: Set<QueryFlag>;
34
+ wrap?: [prefix: string, suffix: string];
35
+ }
36
+ interface TableOptions {
37
+ schema?: string;
38
+ indexHint?: string;
39
+ alias?: string;
40
+ }
41
+ interface OnConflictClause {
42
+ fields: string[] | RawQueryFragment;
43
+ ignore?: boolean;
44
+ merge?: Dictionary | (string | RawQueryFragment)[];
45
+ where?: {
46
+ sql: string;
47
+ params: unknown[];
48
+ };
49
+ }
50
+ /** @internal */
51
+ export declare class NativeQueryBuilder {
52
+ protected readonly platform: AbstractSqlPlatform;
53
+ protected type?: QueryType;
54
+ protected parts: string[];
55
+ protected params: unknown[];
56
+ protected options: Options;
57
+ constructor(platform: AbstractSqlPlatform);
58
+ select(fields: string | RawQueryFragment | (string | RawQueryFragment)[]): this;
59
+ count(fields?: string | RawQueryFragment | (string | RawQueryFragment)[], distinct?: boolean): this;
60
+ into(tableName: string | RawQueryFragment | NativeQueryBuilder, options?: TableOptions): this;
61
+ from(tableName: string | RawQueryFragment | NativeQueryBuilder, options?: TableOptions): this;
62
+ where(sql: string, params: unknown[]): this;
63
+ having(sql: string, params: unknown[]): this;
64
+ groupBy(groupBy: (string | RawQueryFragment)[]): this;
65
+ join(sql: string, params: unknown[]): this;
66
+ orderBy(orderBy: string): this;
67
+ toString(): string;
68
+ compile(): {
69
+ sql: string;
70
+ params: unknown[];
71
+ };
72
+ protected addLockClause(): void;
73
+ protected addOnConflictClause(): void;
74
+ protected combineParts(): {
75
+ sql: string;
76
+ params: unknown[];
77
+ };
78
+ limit(limit: number): this;
79
+ offset(offset: number): this;
80
+ insert(data: Dictionary): this;
81
+ update(data: Dictionary): this;
82
+ delete(): this;
83
+ truncate(): this;
84
+ distinct(): this;
85
+ distinctOn(fields: string[]): this;
86
+ onConflict(options: OnConflictClause): OnConflictClause;
87
+ returning(fields: (string | RawQueryFragment)[]): this;
88
+ lockMode(lockMode: LockMode, lockTables?: string[]): this;
89
+ comment(comment: string | string[]): this;
90
+ hintComment(comment: string | string[]): this;
91
+ setFlags(flags: Set<QueryFlag>): this;
92
+ clear(clause: keyof Options): this;
93
+ wrap(prefix: string, suffix: string): this;
94
+ as(alias: string): this;
95
+ toRaw(): RawQueryFragment;
96
+ protected compileSelect(): void;
97
+ protected getFields(): string;
98
+ protected compileInsert(): void;
99
+ protected addOutputClause(type: 'inserted' | 'deleted'): void;
100
+ protected processInsertData(): string[];
101
+ protected compileUpdate(): void;
102
+ protected compileDelete(): void;
103
+ protected compileTruncate(): void;
104
+ protected addHintComment(): void;
105
+ protected getTableName(): string;
106
+ protected quote(id: string | RawQueryFragment | NativeQueryBuilder): string;
107
+ }
108
+ export {};
@@ -0,0 +1,429 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NativeQueryBuilder = void 0;
4
+ const core_1 = require("@mikro-orm/core");
5
+ const enums_1 = require("./enums");
6
+ /** @internal */
7
+ class NativeQueryBuilder {
8
+ platform;
9
+ type;
10
+ parts = [];
11
+ params = [];
12
+ options = {};
13
+ constructor(platform) {
14
+ this.platform = platform;
15
+ }
16
+ select(fields) {
17
+ this.type = enums_1.QueryType.SELECT;
18
+ this.options.select ??= [];
19
+ this.options.select.push(...core_1.Utils.asArray(fields));
20
+ return this;
21
+ }
22
+ count(fields = '*', distinct) {
23
+ this.type = enums_1.QueryType.COUNT;
24
+ this.options.select = core_1.Utils.asArray(fields);
25
+ this.options.distinct = distinct;
26
+ return this;
27
+ }
28
+ into(tableName, options) {
29
+ return this.from(tableName, options);
30
+ }
31
+ from(tableName, options) {
32
+ if (tableName instanceof NativeQueryBuilder) {
33
+ const { sql, params } = tableName.compile();
34
+ tableName = (0, core_1.raw)(sql, params);
35
+ }
36
+ if (typeof tableName === 'string') {
37
+ const alias = options?.alias ? ` as ${this.platform.quoteIdentifier(options.alias)}` : '';
38
+ const schema = options?.schema && options.schema !== this.platform.getDefaultSchemaName() ? `${options.schema}.` : '';
39
+ tableName = this.quote(schema + tableName) + alias;
40
+ }
41
+ this.options.tableName = tableName;
42
+ this.options.indexHint = options?.indexHint;
43
+ return this;
44
+ }
45
+ where(sql, params) {
46
+ this.options.where = { sql, params };
47
+ return this;
48
+ }
49
+ having(sql, params) {
50
+ this.options.having = { sql, params };
51
+ return this;
52
+ }
53
+ groupBy(groupBy) {
54
+ this.options.groupBy = groupBy;
55
+ return this;
56
+ }
57
+ join(sql, params) {
58
+ this.options.joins ??= [];
59
+ this.options.joins.push({ sql, params });
60
+ return this;
61
+ }
62
+ orderBy(orderBy) {
63
+ this.options.orderBy = orderBy;
64
+ return this;
65
+ }
66
+ toString() {
67
+ const { sql, params } = this.compile();
68
+ return this.platform.formatQuery(sql, params);
69
+ }
70
+ compile() {
71
+ if (!this.type) {
72
+ throw new Error('No query type provided');
73
+ }
74
+ this.parts.length = 0;
75
+ this.params.length = 0;
76
+ if (this.options.comment) {
77
+ this.parts.push(...this.options.comment.map(comment => `/* ${comment} */`));
78
+ }
79
+ switch (this.type) {
80
+ case enums_1.QueryType.SELECT:
81
+ case enums_1.QueryType.COUNT:
82
+ this.compileSelect();
83
+ break;
84
+ case enums_1.QueryType.INSERT:
85
+ this.compileInsert();
86
+ break;
87
+ case enums_1.QueryType.UPDATE:
88
+ this.compileUpdate();
89
+ break;
90
+ case enums_1.QueryType.DELETE:
91
+ this.compileDelete();
92
+ break;
93
+ case enums_1.QueryType.TRUNCATE:
94
+ this.compileTruncate();
95
+ break;
96
+ }
97
+ this.addOnConflictClause();
98
+ if (this.options.returning && this.platform.usesReturningStatement()) {
99
+ const fields = this.options.returning.map(field => this.quote(field));
100
+ this.parts.push(`returning ${fields.join(', ')}`);
101
+ }
102
+ this.addLockClause();
103
+ return this.combineParts();
104
+ }
105
+ addLockClause() {
106
+ if (!this.options.lockMode) {
107
+ return;
108
+ }
109
+ if ([core_1.LockMode.PESSIMISTIC_READ, core_1.LockMode.PESSIMISTIC_PARTIAL_READ, core_1.LockMode.PESSIMISTIC_READ_OR_FAIL].includes(this.options.lockMode)) {
110
+ this.parts.push('for share');
111
+ }
112
+ if ([core_1.LockMode.PESSIMISTIC_WRITE, core_1.LockMode.PESSIMISTIC_PARTIAL_WRITE, core_1.LockMode.PESSIMISTIC_WRITE_OR_FAIL].includes(this.options.lockMode)) {
113
+ this.parts.push('for update');
114
+ }
115
+ if (this.options.lockTables?.length) {
116
+ const fields = this.options.lockTables.map(field => this.quote(field));
117
+ this.parts.push(`of ${fields.join(', ')}`);
118
+ }
119
+ if ([core_1.LockMode.PESSIMISTIC_PARTIAL_READ, core_1.LockMode.PESSIMISTIC_PARTIAL_WRITE].includes(this.options.lockMode)) {
120
+ this.parts.push('skip locked');
121
+ }
122
+ if ([core_1.LockMode.PESSIMISTIC_READ_OR_FAIL, core_1.LockMode.PESSIMISTIC_WRITE_OR_FAIL].includes(this.options.lockMode)) {
123
+ this.parts.push('nowait');
124
+ }
125
+ }
126
+ addOnConflictClause() {
127
+ const clause = this.options.onConflict;
128
+ if (!clause) {
129
+ return;
130
+ }
131
+ this.parts.push('on conflict');
132
+ if (clause.fields instanceof core_1.RawQueryFragment) {
133
+ this.parts.push(clause.fields.sql);
134
+ this.params.push(...clause.fields.params);
135
+ }
136
+ else if (clause.fields.length > 0) {
137
+ const fields = clause.fields.map(field => this.quote(field));
138
+ this.parts.push(`(${fields.join(', ')})`);
139
+ }
140
+ if (clause.ignore) {
141
+ this.parts.push('do nothing');
142
+ }
143
+ if (core_1.Utils.isObject(clause.merge)) {
144
+ this.parts.push('do update set');
145
+ const fields = Object.keys(clause.merge).map(field => {
146
+ this.params.push(clause.merge[field]);
147
+ return `${this.quote(field)} = ?`;
148
+ });
149
+ this.parts.push(fields.join(', '));
150
+ }
151
+ else if (clause.merge) {
152
+ this.parts.push('do update set');
153
+ if (clause.merge.length) {
154
+ const fields = clause.merge.map(field => `${this.quote(field)} = excluded.${this.quote(field)}`);
155
+ this.parts.push(fields.join(', '));
156
+ }
157
+ else {
158
+ const dataAsArray = core_1.Utils.asArray(this.options.data);
159
+ const keys = Object.keys(dataAsArray[0]);
160
+ const fields = keys.map(field => `${this.quote(field)} = excluded.${this.quote(field)}`);
161
+ this.parts.push(fields.join(', '));
162
+ }
163
+ }
164
+ if (clause.where) {
165
+ this.parts.push(`where ${clause.where.sql}`);
166
+ this.params.push(...clause.where.params);
167
+ }
168
+ }
169
+ combineParts() {
170
+ let sql = this.parts.join(' ');
171
+ if (this.options.wrap) {
172
+ const [a, b] = this.options.wrap;
173
+ sql = `${a}${sql}${b}`;
174
+ }
175
+ return { sql, params: this.params };
176
+ }
177
+ limit(limit) {
178
+ this.options.limit = limit;
179
+ return this;
180
+ }
181
+ offset(offset) {
182
+ this.options.offset = offset;
183
+ return this;
184
+ }
185
+ insert(data) {
186
+ this.type = enums_1.QueryType.INSERT;
187
+ this.options.data = data;
188
+ return this;
189
+ }
190
+ update(data) {
191
+ this.type = enums_1.QueryType.UPDATE;
192
+ this.options.data ??= {};
193
+ Object.assign(this.options.data, data);
194
+ return this;
195
+ }
196
+ delete() {
197
+ this.type = enums_1.QueryType.DELETE;
198
+ return this;
199
+ }
200
+ truncate() {
201
+ this.type = enums_1.QueryType.TRUNCATE;
202
+ return this;
203
+ }
204
+ distinct() {
205
+ this.options.distinct = true;
206
+ return this;
207
+ }
208
+ distinctOn(fields) {
209
+ this.options.distinctOn = fields;
210
+ return this;
211
+ }
212
+ onConflict(options) {
213
+ this.options.onConflict = options;
214
+ return options;
215
+ }
216
+ returning(fields) {
217
+ this.options.returning = fields;
218
+ return this;
219
+ }
220
+ lockMode(lockMode, lockTables) {
221
+ this.options.lockMode = lockMode;
222
+ this.options.lockTables = lockTables;
223
+ return this;
224
+ }
225
+ comment(comment) {
226
+ this.options.comment ??= [];
227
+ this.options.comment.push(...core_1.Utils.asArray(comment));
228
+ return this;
229
+ }
230
+ hintComment(comment) {
231
+ this.options.hintComment ??= [];
232
+ this.options.hintComment.push(...core_1.Utils.asArray(comment));
233
+ return this;
234
+ }
235
+ setFlags(flags) {
236
+ this.options.flags = flags;
237
+ return this;
238
+ }
239
+ clear(clause) {
240
+ delete this.options[clause];
241
+ return this;
242
+ }
243
+ wrap(prefix, suffix) {
244
+ this.options.wrap = [prefix, suffix];
245
+ return this;
246
+ }
247
+ as(alias) {
248
+ this.wrap('(', `) as ${this.platform.quoteIdentifier(alias)}`);
249
+ return this;
250
+ }
251
+ toRaw() {
252
+ const { sql, params } = this.compile();
253
+ return (0, core_1.raw)(sql, params);
254
+ }
255
+ compileSelect() {
256
+ this.parts.push('select');
257
+ this.addHintComment();
258
+ this.parts.push(`${this.getFields()} from ${this.getTableName()}`);
259
+ if (this.options.joins) {
260
+ for (const join of this.options.joins) {
261
+ this.parts.push(join.sql);
262
+ this.params.push(...join.params);
263
+ }
264
+ }
265
+ if (this.options.where?.sql.trim()) {
266
+ this.parts.push(`where ${this.options.where.sql}`);
267
+ this.params.push(...this.options.where.params);
268
+ }
269
+ if (this.options.groupBy) {
270
+ const fields = this.options.groupBy.map(field => this.quote(field));
271
+ this.parts.push(`group by ${fields.join(', ')}`);
272
+ }
273
+ if (this.options.having) {
274
+ this.parts.push(`having ${this.options.having.sql}`);
275
+ this.params.push(...this.options.having.params);
276
+ }
277
+ if (this.options.orderBy) {
278
+ this.parts.push(`order by ${this.options.orderBy}`);
279
+ }
280
+ if (this.options.limit != null) {
281
+ this.parts.push(`limit ?`);
282
+ this.params.push(this.options.limit);
283
+ }
284
+ if (this.options.offset != null) {
285
+ this.parts.push(`offset ?`);
286
+ this.params.push(this.options.offset);
287
+ }
288
+ }
289
+ getFields() {
290
+ if (!this.options.select || this.options.select.length === 0) {
291
+ throw new Error('No fields selected');
292
+ }
293
+ let fields = this.options.select.map(field => this.quote(field)).join(', ');
294
+ if (this.options.distinct) {
295
+ fields = `distinct ${fields}`;
296
+ }
297
+ else if (this.options.distinctOn) {
298
+ fields = `distinct on (${this.options.distinctOn.map(field => this.quote(field)).join(', ')}) ${fields}`;
299
+ }
300
+ if (this.type === enums_1.QueryType.COUNT) {
301
+ fields = `count(${fields}) as ${this.quote('count')}`;
302
+ }
303
+ return fields;
304
+ }
305
+ compileInsert() {
306
+ if (!this.options.data) {
307
+ throw new Error('No data provided');
308
+ }
309
+ this.parts.push('insert');
310
+ this.addHintComment();
311
+ this.parts.push(`into ${this.getTableName()}`);
312
+ if (Object.keys(this.options.data).length === 0) {
313
+ this.addOutputClause('inserted');
314
+ this.parts.push('default values');
315
+ return;
316
+ }
317
+ const parts = this.processInsertData();
318
+ this.parts.push(parts.join(', '));
319
+ }
320
+ addOutputClause(type) {
321
+ if (this.options.returning && this.platform.usesOutputStatement()) {
322
+ const fields = this.options.returning.map(field => `${type}.${this.quote(field)}`);
323
+ this.parts.push(`output ${fields.join(', ')}`);
324
+ }
325
+ }
326
+ processInsertData() {
327
+ const dataAsArray = core_1.Utils.asArray(this.options.data);
328
+ const keys = Object.keys(dataAsArray[0]);
329
+ const values = keys.map(() => '?');
330
+ const parts = [];
331
+ this.parts.push(`(${keys.map(key => this.quote(key)).join(', ')})`);
332
+ this.addOutputClause('inserted');
333
+ this.parts.push('values');
334
+ for (const data of dataAsArray) {
335
+ for (const key of keys) {
336
+ if (typeof data[key] === 'undefined') {
337
+ this.params.push(this.platform.usesDefaultKeyword() ? (0, core_1.raw)('default') : null);
338
+ }
339
+ else {
340
+ this.params.push(data[key]);
341
+ }
342
+ }
343
+ parts.push(`(${values.join(', ')})`);
344
+ }
345
+ return parts;
346
+ }
347
+ compileUpdate() {
348
+ if (!this.options.data || Object.keys(this.options.data).length === 0) {
349
+ throw new Error('No data provided');
350
+ }
351
+ this.parts.push('update');
352
+ this.addHintComment();
353
+ this.parts.push(this.getTableName());
354
+ if (this.options.joins) {
355
+ for (const join of this.options.joins) {
356
+ this.parts.push(join.sql);
357
+ this.params.push(...join.params);
358
+ }
359
+ }
360
+ this.parts.push('set');
361
+ if (this.options.data) {
362
+ const parts = [];
363
+ for (const key of Object.keys(this.options.data)) {
364
+ parts.push(`${this.quote(key)} = ?`);
365
+ this.params.push(this.options.data[key]);
366
+ }
367
+ this.parts.push(parts.join(', '));
368
+ }
369
+ this.addOutputClause('inserted');
370
+ if (this.options.where?.sql.trim()) {
371
+ this.parts.push(`where ${this.options.where.sql}`);
372
+ this.params.push(...this.options.where.params);
373
+ }
374
+ }
375
+ compileDelete() {
376
+ this.parts.push('delete');
377
+ this.addHintComment();
378
+ this.parts.push(`from ${this.getTableName()}`);
379
+ this.addOutputClause('deleted');
380
+ if (this.options.where?.sql.trim()) {
381
+ this.parts.push(`where ${this.options.where.sql}`);
382
+ this.params.push(...this.options.where.params);
383
+ }
384
+ }
385
+ compileTruncate() {
386
+ const sql = `truncate table ${this.getTableName()}`;
387
+ this.parts.push(sql);
388
+ }
389
+ addHintComment() {
390
+ if (this.options.hintComment) {
391
+ this.parts.push(`/*+ ${this.options.hintComment.join(' ')} */`);
392
+ }
393
+ }
394
+ getTableName() {
395
+ if (!this.options.tableName) {
396
+ throw new Error('No table name provided');
397
+ }
398
+ const indexHint = this.options.indexHint ? ' ' + this.options.indexHint : '';
399
+ if (this.options.tableName instanceof core_1.RawQueryFragment) {
400
+ this.params.push(...this.options.tableName.params);
401
+ return this.options.tableName.sql + indexHint;
402
+ }
403
+ return this.options.tableName + indexHint;
404
+ }
405
+ quote(id) {
406
+ if (id instanceof core_1.RawQueryFragment) {
407
+ return this.platform.formatQuery(id.sql, id.params);
408
+ }
409
+ if (id instanceof NativeQueryBuilder) {
410
+ const { sql, params } = id.compile();
411
+ return this.platform.formatQuery(sql, params);
412
+ }
413
+ if (id.endsWith('.*')) {
414
+ const schema = this.platform.quoteIdentifier(id.substring(0, id.indexOf('.')));
415
+ return schema + '.*';
416
+ }
417
+ if (id.toLowerCase().includes(' as ')) {
418
+ const parts = id.split(/ as /i);
419
+ const a = this.platform.quoteIdentifier(parts[0]);
420
+ const b = this.platform.quoteIdentifier(parts[1]);
421
+ return `${a} as ${b}`;
422
+ }
423
+ if (id === '*') {
424
+ return id;
425
+ }
426
+ return this.platform.quoteIdentifier(id);
427
+ }
428
+ }
429
+ exports.NativeQueryBuilder = NativeQueryBuilder;
@@ -50,7 +50,7 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
50
50
  }
51
51
  const op = key === '$some' ? '$in' : '$nin';
52
52
  $and.push({
53
- [core_1.Utils.getPrimaryKeyHash(primaryKeys)]: { [op]: sub.getKnexQuery() },
53
+ [core_1.Utils.getPrimaryKeyHash(primaryKeys)]: { [op]: sub.getNativeQuery().toRaw() },
54
54
  });
55
55
  }
56
56
  if ($and.length === 1) {
@@ -68,7 +68,7 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
68
68
  // we need to keep the prefixing for formulas otherwise we would lose aliasing context when nesting inside group operators
69
69
  const virtual = childNode.prop?.persist === false && !childNode.prop?.formula;
70
70
  // if key is missing, we are inside group operator and we need to prefix with alias
71
- const primaryKey = this.key && this.metadata.find(this.entityName).primaryKeys.includes(field);
71
+ const primaryKey = this.key && this.metadata.find(this.entityName)?.primaryKeys.includes(field);
72
72
  if (childNode.shouldInline(payload)) {
73
73
  const childAlias = qb.getAliasForJoinPath(childNode.getPath(), options);
74
74
  const a = qb.helper.isTableNameAliasRequired(qb.type) ? alias : undefined;
@@ -81,7 +81,7 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
81
81
  const rawField = core_1.RawQueryFragment.getKnownFragment(field);
82
82
  o[(0, core_1.raw)(rawField.sql.replaceAll(core_1.ALIAS_REPLACEMENT, alias), rawField.params)] = payload;
83
83
  }
84
- else if (primaryKey || virtual || operator || field.includes('.') || ![enums_1.QueryType.SELECT, enums_1.QueryType.COUNT].includes(qb.type ?? enums_1.QueryType.SELECT)) {
84
+ else if (primaryKey || virtual || operator || field.includes('.') || ![enums_1.QueryType.SELECT, enums_1.QueryType.COUNT].includes(qb.type)) {
85
85
  this.inlineCondition(field.replaceAll(core_1.ALIAS_REPLACEMENT, alias), o, payload);
86
86
  }
87
87
  else {