@mikro-orm/sql 7.0.15-dev.9 → 7.0.15

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 (89) hide show
  1. package/AbstractSqlConnection.d.ts +94 -58
  2. package/AbstractSqlConnection.js +235 -238
  3. package/AbstractSqlDriver.d.ts +410 -155
  4. package/AbstractSqlDriver.js +2100 -1972
  5. package/AbstractSqlPlatform.d.ts +86 -76
  6. package/AbstractSqlPlatform.js +169 -167
  7. package/PivotCollectionPersister.d.ts +33 -15
  8. package/PivotCollectionPersister.js +158 -160
  9. package/README.md +1 -1
  10. package/SqlEntityManager.d.ts +67 -22
  11. package/SqlEntityManager.js +54 -38
  12. package/SqlEntityRepository.d.ts +14 -14
  13. package/SqlEntityRepository.js +23 -23
  14. package/SqlMikroORM.d.ts +49 -8
  15. package/SqlMikroORM.js +8 -8
  16. package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -12
  17. package/dialects/mssql/MsSqlNativeQueryBuilder.js +199 -201
  18. package/dialects/mysql/BaseMySqlPlatform.d.ts +65 -46
  19. package/dialects/mysql/BaseMySqlPlatform.js +137 -134
  20. package/dialects/mysql/MySqlExceptionConverter.d.ts +6 -6
  21. package/dialects/mysql/MySqlExceptionConverter.js +91 -77
  22. package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +3 -3
  23. package/dialects/mysql/MySqlNativeQueryBuilder.js +66 -69
  24. package/dialects/mysql/MySqlSchemaHelper.d.ts +58 -39
  25. package/dialects/mysql/MySqlSchemaHelper.js +327 -319
  26. package/dialects/oracledb/OracleDialect.d.ts +81 -52
  27. package/dialects/oracledb/OracleDialect.js +155 -149
  28. package/dialects/oracledb/OracleNativeQueryBuilder.d.ts +12 -12
  29. package/dialects/oracledb/OracleNativeQueryBuilder.js +239 -243
  30. package/dialects/postgresql/BasePostgreSqlPlatform.d.ts +110 -107
  31. package/dialects/postgresql/BasePostgreSqlPlatform.js +370 -369
  32. package/dialects/postgresql/FullTextType.d.ts +10 -6
  33. package/dialects/postgresql/FullTextType.js +51 -51
  34. package/dialects/postgresql/PostgreSqlExceptionConverter.d.ts +5 -5
  35. package/dialects/postgresql/PostgreSqlExceptionConverter.js +55 -43
  36. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
  37. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +4 -4
  38. package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +117 -82
  39. package/dialects/postgresql/PostgreSqlSchemaHelper.js +748 -712
  40. package/dialects/sqlite/BaseSqliteConnection.d.ts +3 -5
  41. package/dialects/sqlite/BaseSqliteConnection.js +21 -19
  42. package/dialects/sqlite/NodeSqliteDialect.d.ts +1 -1
  43. package/dialects/sqlite/NodeSqliteDialect.js +23 -23
  44. package/dialects/sqlite/SqliteDriver.d.ts +1 -1
  45. package/dialects/sqlite/SqliteDriver.js +3 -3
  46. package/dialects/sqlite/SqliteExceptionConverter.d.ts +6 -6
  47. package/dialects/sqlite/SqliteExceptionConverter.js +67 -51
  48. package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +2 -2
  49. package/dialects/sqlite/SqliteNativeQueryBuilder.js +7 -7
  50. package/dialects/sqlite/SqlitePlatform.d.ts +64 -73
  51. package/dialects/sqlite/SqlitePlatform.js +143 -143
  52. package/dialects/sqlite/SqliteSchemaHelper.d.ts +78 -61
  53. package/dialects/sqlite/SqliteSchemaHelper.js +541 -522
  54. package/package.json +3 -3
  55. package/plugin/index.d.ts +42 -35
  56. package/plugin/index.js +43 -36
  57. package/plugin/transformer.d.ts +137 -95
  58. package/plugin/transformer.js +1012 -881
  59. package/query/ArrayCriteriaNode.d.ts +4 -4
  60. package/query/ArrayCriteriaNode.js +18 -18
  61. package/query/CriteriaNode.d.ts +35 -25
  62. package/query/CriteriaNode.js +142 -132
  63. package/query/CriteriaNodeFactory.d.ts +49 -6
  64. package/query/CriteriaNodeFactory.js +97 -94
  65. package/query/NativeQueryBuilder.d.ts +120 -120
  66. package/query/NativeQueryBuilder.js +507 -501
  67. package/query/ObjectCriteriaNode.d.ts +12 -12
  68. package/query/ObjectCriteriaNode.js +298 -282
  69. package/query/QueryBuilder.d.ts +1558 -906
  70. package/query/QueryBuilder.js +2346 -2217
  71. package/query/QueryBuilderHelper.d.ts +153 -72
  72. package/query/QueryBuilderHelper.js +1084 -1032
  73. package/query/ScalarCriteriaNode.d.ts +3 -3
  74. package/query/ScalarCriteriaNode.js +53 -46
  75. package/query/enums.d.ts +14 -14
  76. package/query/enums.js +14 -14
  77. package/query/raw.d.ts +16 -6
  78. package/query/raw.js +10 -10
  79. package/schema/DatabaseSchema.d.ts +74 -50
  80. package/schema/DatabaseSchema.js +359 -331
  81. package/schema/DatabaseTable.d.ts +96 -73
  82. package/schema/DatabaseTable.js +1046 -974
  83. package/schema/SchemaComparator.d.ts +70 -66
  84. package/schema/SchemaComparator.js +790 -765
  85. package/schema/SchemaHelper.d.ts +128 -97
  86. package/schema/SchemaHelper.js +683 -668
  87. package/schema/SqlSchemaGenerator.d.ts +79 -59
  88. package/schema/SqlSchemaGenerator.js +525 -495
  89. package/typings.d.ts +405 -275
@@ -1,260 +1,257 @@
1
1
  import { CompiledQuery, Kysely } from 'kysely';
2
- import { Connection, EventType, isRaw, Utils, } from '@mikro-orm/core';
2
+ import { Connection, EventType, isRaw, Utils } from '@mikro-orm/core';
3
3
  import { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
4
4
  /** Base class for SQL database connections, built on top of Kysely. */
5
5
  export class AbstractSqlConnection extends Connection {
6
- #client;
7
- /** Establishes the database connection and runs the onConnect hook. */
8
- async connect(options) {
9
- await this.initClient();
10
- this.connected = true;
11
- if (options?.skipOnConnect !== true) {
12
- await this.onConnect();
13
- }
6
+ #client;
7
+ /** Establishes the database connection and runs the onConnect hook. */
8
+ async connect(options) {
9
+ await this.initClient();
10
+ this.connected = true;
11
+ if (options?.skipOnConnect !== true) {
12
+ await this.onConnect();
14
13
  }
15
- /** Initializes the Kysely client from driver options or a user-provided Kysely instance. */
16
- createKysely() {
17
- let driverOptions = this.options.driverOptions ?? this.config.get('driverOptions');
18
- if (typeof driverOptions === 'function') {
19
- driverOptions = driverOptions();
20
- }
21
- if (driverOptions instanceof Kysely) {
22
- this.logger.log('info', 'Reusing Kysely client provided via `driverOptions`');
23
- this.#client = driverOptions;
24
- }
25
- else if ('createDriver' in driverOptions) {
26
- this.logger.log('info', 'Reusing Kysely dialect provided via `driverOptions`');
27
- this.#client = new Kysely({ dialect: driverOptions });
28
- }
29
- else {
30
- const dialect = this.createKyselyDialect(driverOptions);
31
- if (dialect instanceof Promise) {
32
- return dialect.then(d => {
33
- this.#client = new Kysely({ dialect: d });
34
- });
35
- }
36
- this.#client = new Kysely({ dialect });
37
- }
14
+ }
15
+ /** Initializes the Kysely client from driver options or a user-provided Kysely instance. */
16
+ createKysely() {
17
+ let driverOptions = this.options.driverOptions ?? this.config.get('driverOptions');
18
+ if (typeof driverOptions === 'function') {
19
+ driverOptions = driverOptions();
38
20
  }
39
- /**
40
- * @inheritDoc
41
- */
42
- async close(force) {
43
- await super.close(force);
44
- await this.#client?.destroy();
45
- this.connected = false;
46
- this.#client = undefined;
21
+ if (driverOptions instanceof Kysely) {
22
+ this.logger.log('info', 'Reusing Kysely client provided via `driverOptions`');
23
+ this.#client = driverOptions;
24
+ } else if ('createDriver' in driverOptions) {
25
+ this.logger.log('info', 'Reusing Kysely dialect provided via `driverOptions`');
26
+ this.#client = new Kysely({ dialect: driverOptions });
27
+ } else {
28
+ const dialect = this.createKyselyDialect(driverOptions);
29
+ if (dialect instanceof Promise) {
30
+ return dialect.then(d => {
31
+ this.#client = new Kysely({ dialect: d });
32
+ });
33
+ }
34
+ this.#client = new Kysely({ dialect });
47
35
  }
48
- /**
49
- * @inheritDoc
50
- */
51
- async isConnected() {
52
- const check = await this.checkConnection();
53
- return check.ok;
36
+ }
37
+ /**
38
+ * @inheritDoc
39
+ */
40
+ async close(force) {
41
+ await super.close(force);
42
+ await this.#client?.destroy();
43
+ this.connected = false;
44
+ this.#client = undefined;
45
+ }
46
+ /**
47
+ * @inheritDoc
48
+ */
49
+ async isConnected() {
50
+ const check = await this.checkConnection();
51
+ return check.ok;
52
+ }
53
+ /**
54
+ * @inheritDoc
55
+ */
56
+ async checkConnection() {
57
+ if (!this.connected) {
58
+ return { ok: false, reason: 'Connection not established' };
54
59
  }
55
- /**
56
- * @inheritDoc
57
- */
58
- async checkConnection() {
59
- if (!this.connected) {
60
- return { ok: false, reason: 'Connection not established' };
61
- }
62
- try {
63
- await this.getClient().executeQuery(CompiledQuery.raw('select 1'));
64
- return { ok: true };
65
- }
66
- catch (error) {
67
- return { ok: false, reason: error.message, error };
68
- }
60
+ try {
61
+ await this.getClient().executeQuery(CompiledQuery.raw('select 1'));
62
+ return { ok: true };
63
+ } catch (error) {
64
+ return { ok: false, reason: error.message, error };
69
65
  }
70
- /** Returns the underlying Kysely client, creating it synchronously if needed. */
71
- getClient() {
72
- if (!this.#client) {
73
- const maybePromise = this.createKysely();
74
- /* v8 ignore next */
75
- if (maybePromise instanceof Promise) {
76
- throw new Error('Current driver requires async initialization, use `MikroORM.init()` instead of the constructor');
77
- }
78
- }
79
- return this.#client;
66
+ }
67
+ /** Returns the underlying Kysely client, creating it synchronously if needed. */
68
+ getClient() {
69
+ if (!this.#client) {
70
+ const maybePromise = this.createKysely();
71
+ /* v8 ignore next */
72
+ if (maybePromise instanceof Promise) {
73
+ throw new Error(
74
+ 'Current driver requires async initialization, use `MikroORM.init()` instead of the constructor',
75
+ );
76
+ }
80
77
  }
81
- /** Ensures the Kysely client is initialized, creating it asynchronously if needed. */
82
- async initClient() {
83
- if (!this.#client) {
84
- await this.createKysely();
85
- }
78
+ return this.#client;
79
+ }
80
+ /** Ensures the Kysely client is initialized, creating it asynchronously if needed. */
81
+ async initClient() {
82
+ if (!this.#client) {
83
+ await this.createKysely();
86
84
  }
87
- /** Executes a callback within a transaction, committing on success and rolling back on error. */
88
- async transactional(cb, options = {}) {
89
- const trx = await this.begin(options);
90
- try {
91
- const ret = await cb(trx);
92
- await this.commit(trx, options.eventBroadcaster, options.loggerContext);
93
- return ret;
94
- }
95
- catch (error) {
96
- await this.rollback(trx, options.eventBroadcaster, options.loggerContext);
97
- throw error;
98
- }
85
+ }
86
+ /** Executes a callback within a transaction, committing on success and rolling back on error. */
87
+ async transactional(cb, options = {}) {
88
+ const trx = await this.begin(options);
89
+ try {
90
+ const ret = await cb(trx);
91
+ await this.commit(trx, options.eventBroadcaster, options.loggerContext);
92
+ return ret;
93
+ } catch (error) {
94
+ await this.rollback(trx, options.eventBroadcaster, options.loggerContext);
95
+ throw error;
99
96
  }
100
- /** Begins a new transaction or creates a savepoint if a transaction context already exists. */
101
- async begin(options = {}) {
102
- if (options.ctx) {
103
- const ctx = options.ctx;
104
- await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart, ctx);
105
- ctx.index ??= 0;
106
- const savepointName = `trx${ctx.index + 1}`;
107
- const trx = await options.ctx.savepoint(savepointName).execute();
108
- Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 });
109
- Reflect.defineProperty(trx, 'savepointName', { value: savepointName });
110
- this.logQuery(this.platform.getSavepointSQL(savepointName), options.loggerContext);
111
- await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx);
112
- return trx;
113
- }
114
- await this.ensureConnection();
115
- await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
116
- let trxBuilder = this.getClient().startTransaction();
117
- if (options.isolationLevel) {
118
- trxBuilder = trxBuilder.setIsolationLevel(options.isolationLevel);
119
- }
120
- if (options.readOnly) {
121
- trxBuilder = trxBuilder.setAccessMode('read only');
122
- }
123
- const trx = await trxBuilder.execute();
124
- if (options.ctx) {
125
- const ctx = options.ctx;
126
- ctx.index ??= 0;
127
- const savepointName = `trx${ctx.index + 1}`;
128
- Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 });
129
- Reflect.defineProperty(trx, 'savepointName', { value: savepointName });
130
- this.logQuery(this.platform.getSavepointSQL(savepointName), options.loggerContext);
131
- }
132
- else {
133
- for (const query of this.platform.getBeginTransactionSQL(options)) {
134
- this.logQuery(query, options.loggerContext);
135
- }
136
- }
137
- await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx);
138
- return trx;
97
+ }
98
+ /** Begins a new transaction or creates a savepoint if a transaction context already exists. */
99
+ async begin(options = {}) {
100
+ if (options.ctx) {
101
+ const ctx = options.ctx;
102
+ await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart, ctx);
103
+ ctx.index ??= 0;
104
+ const savepointName = `trx${ctx.index + 1}`;
105
+ const trx = await options.ctx.savepoint(savepointName).execute();
106
+ Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 });
107
+ Reflect.defineProperty(trx, 'savepointName', { value: savepointName });
108
+ this.logQuery(this.platform.getSavepointSQL(savepointName), options.loggerContext);
109
+ await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx);
110
+ return trx;
139
111
  }
140
- /** Commits the transaction or releases the savepoint. */
141
- async commit(ctx, eventBroadcaster, loggerContext) {
142
- if (ctx.isRolledBack) {
143
- return;
144
- }
145
- await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionCommit, ctx);
146
- if ('savepointName' in ctx) {
147
- await ctx.releaseSavepoint(ctx.savepointName).execute();
148
- this.logQuery(this.platform.getReleaseSavepointSQL(ctx.savepointName), loggerContext);
149
- }
150
- else {
151
- await ctx.commit().execute();
152
- this.logQuery(this.platform.getCommitTransactionSQL(), loggerContext);
153
- }
154
- await eventBroadcaster?.dispatchEvent(EventType.afterTransactionCommit, ctx);
112
+ await this.ensureConnection();
113
+ await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
114
+ let trxBuilder = this.getClient().startTransaction();
115
+ if (options.isolationLevel) {
116
+ trxBuilder = trxBuilder.setIsolationLevel(options.isolationLevel);
155
117
  }
156
- /** Rolls back the transaction or rolls back to the savepoint. */
157
- async rollback(ctx, eventBroadcaster, loggerContext) {
158
- await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionRollback, ctx);
159
- if ('savepointName' in ctx) {
160
- await ctx.rollbackToSavepoint(ctx.savepointName).execute();
161
- this.logQuery(this.platform.getRollbackToSavepointSQL(ctx.savepointName), loggerContext);
162
- }
163
- else {
164
- await ctx.rollback().execute();
165
- this.logQuery(this.platform.getRollbackTransactionSQL(), loggerContext);
166
- }
167
- await eventBroadcaster?.dispatchEvent(EventType.afterTransactionRollback, ctx);
118
+ if (options.readOnly) {
119
+ trxBuilder = trxBuilder.setAccessMode('read only');
168
120
  }
169
- prepareQuery(query, params = []) {
170
- if (query instanceof NativeQueryBuilder) {
171
- query = query.toRaw();
172
- }
173
- if (isRaw(query)) {
174
- params = query.params;
175
- query = query.sql;
176
- }
177
- query = this.config.get('onQuery')(query, params);
178
- const formatted = this.platform.formatQuery(query, params);
179
- return { query, params, formatted };
121
+ const trx = await trxBuilder.execute();
122
+ if (options.ctx) {
123
+ const ctx = options.ctx;
124
+ ctx.index ??= 0;
125
+ const savepointName = `trx${ctx.index + 1}`;
126
+ Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 });
127
+ Reflect.defineProperty(trx, 'savepointName', { value: savepointName });
128
+ this.logQuery(this.platform.getSavepointSQL(savepointName), options.loggerContext);
129
+ } else {
130
+ for (const query of this.platform.getBeginTransactionSQL(options)) {
131
+ this.logQuery(query, options.loggerContext);
132
+ }
180
133
  }
181
- /** Executes a SQL query and returns the result based on the method: `'all'` for rows, `'get'` for single row, `'run'` for affected count. */
182
- async execute(query, params = [], method = 'all', ctx, loggerContext) {
183
- await this.ensureConnection();
184
- const q = this.prepareQuery(query, params);
185
- const sql = this.getSql(q.query, q.formatted, loggerContext);
186
- return this.executeQuery(sql, async () => {
187
- const compiled = CompiledQuery.raw(q.formatted);
188
- const res = await (ctx ?? this.#client).executeQuery(compiled);
189
- return this.transformRawResult(res, method);
190
- }, { ...q, ...loggerContext });
134
+ await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx);
135
+ return trx;
136
+ }
137
+ /** Commits the transaction or releases the savepoint. */
138
+ async commit(ctx, eventBroadcaster, loggerContext) {
139
+ if (ctx.isRolledBack) {
140
+ return;
191
141
  }
192
- /** Executes a SQL query and returns an async iterable that yields results row by row. */
193
- async *stream(query, params = [], ctx, loggerContext) {
194
- await this.ensureConnection();
195
- const q = this.prepareQuery(query, params);
196
- const sql = this.getSql(q.query, q.formatted, loggerContext);
197
- // construct the compiled query manually with `kind: 'SelectQueryNode'` to avoid sqlite validation for select queries when streaming
198
- const compiled = {
199
- query: {
200
- kind: 'SelectQueryNode',
201
- },
202
- sql: q.formatted,
203
- parameters: [],
204
- };
205
- try {
206
- const res = (ctx ?? this.getClient()).getExecutor().stream(compiled, 1);
207
- this.logQuery(sql, {
208
- sql,
209
- params,
210
- ...loggerContext,
211
- affected: Utils.isPlainObject(res) ? res.affectedRows : undefined,
212
- });
213
- for await (const items of res) {
214
- for (const row of this.transformRawResult(items, 'all')) {
215
- yield row;
216
- }
217
- }
218
- }
219
- catch (e) {
220
- this.logQuery(sql, { sql, params, ...loggerContext, level: 'error' });
221
- throw e;
222
- }
142
+ await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionCommit, ctx);
143
+ if ('savepointName' in ctx) {
144
+ await ctx.releaseSavepoint(ctx.savepointName).execute();
145
+ this.logQuery(this.platform.getReleaseSavepointSQL(ctx.savepointName), loggerContext);
146
+ } else {
147
+ await ctx.commit().execute();
148
+ this.logQuery(this.platform.getCommitTransactionSQL(), loggerContext);
223
149
  }
224
- /** @inheritDoc */
225
- async executeDump(dump) {
226
- await this.ensureConnection();
227
- try {
228
- const raw = CompiledQuery.raw(dump);
229
- await this.getClient().executeQuery(raw);
230
- }
231
- catch (e) {
232
- /* v8 ignore next */
233
- throw this.platform.getExceptionConverter().convertException(e);
234
- }
150
+ await eventBroadcaster?.dispatchEvent(EventType.afterTransactionCommit, ctx);
151
+ }
152
+ /** Rolls back the transaction or rolls back to the savepoint. */
153
+ async rollback(ctx, eventBroadcaster, loggerContext) {
154
+ await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionRollback, ctx);
155
+ if ('savepointName' in ctx) {
156
+ await ctx.rollbackToSavepoint(ctx.savepointName).execute();
157
+ this.logQuery(this.platform.getRollbackToSavepointSQL(ctx.savepointName), loggerContext);
158
+ } else {
159
+ await ctx.rollback().execute();
160
+ this.logQuery(this.platform.getRollbackTransactionSQL(), loggerContext);
235
161
  }
236
- getSql(query, formatted, context) {
237
- const logger = this.config.getLogger();
238
- if (!logger.isEnabled('query', context)) {
239
- return query;
240
- }
241
- if (logger.isEnabled('query-params', context)) {
242
- return formatted;
243
- }
244
- return query;
162
+ await eventBroadcaster?.dispatchEvent(EventType.afterTransactionRollback, ctx);
163
+ }
164
+ prepareQuery(query, params = []) {
165
+ if (query instanceof NativeQueryBuilder) {
166
+ query = query.toRaw();
245
167
  }
246
- transformRawResult(res, method) {
247
- if (method === 'get') {
248
- return res.rows[0];
249
- }
250
- if (method === 'all') {
251
- return res.rows;
252
- }
253
- return {
254
- affectedRows: Number(res.numAffectedRows ?? res.rows.length),
255
- insertId: res.insertId != null ? Number(res.insertId) : res.insertId,
256
- row: res.rows[0],
257
- rows: res.rows,
258
- };
168
+ if (isRaw(query)) {
169
+ params = query.params;
170
+ query = query.sql;
171
+ }
172
+ query = this.config.get('onQuery')(query, params);
173
+ const formatted = this.platform.formatQuery(query, params);
174
+ return { query, params, formatted };
175
+ }
176
+ /** Executes a SQL query and returns the result based on the method: `'all'` for rows, `'get'` for single row, `'run'` for affected count. */
177
+ async execute(query, params = [], method = 'all', ctx, loggerContext) {
178
+ await this.ensureConnection();
179
+ const q = this.prepareQuery(query, params);
180
+ const sql = this.getSql(q.query, q.formatted, loggerContext);
181
+ return this.executeQuery(
182
+ sql,
183
+ async () => {
184
+ const compiled = CompiledQuery.raw(q.formatted);
185
+ const res = await (ctx ?? this.#client).executeQuery(compiled);
186
+ return this.transformRawResult(res, method);
187
+ },
188
+ { ...q, ...loggerContext },
189
+ );
190
+ }
191
+ /** Executes a SQL query and returns an async iterable that yields results row by row. */
192
+ async *stream(query, params = [], ctx, loggerContext) {
193
+ await this.ensureConnection();
194
+ const q = this.prepareQuery(query, params);
195
+ const sql = this.getSql(q.query, q.formatted, loggerContext);
196
+ // construct the compiled query manually with `kind: 'SelectQueryNode'` to avoid sqlite validation for select queries when streaming
197
+ const compiled = {
198
+ query: {
199
+ kind: 'SelectQueryNode',
200
+ },
201
+ sql: q.formatted,
202
+ parameters: [],
203
+ };
204
+ try {
205
+ const res = (ctx ?? this.getClient()).getExecutor().stream(compiled, 1);
206
+ this.logQuery(sql, {
207
+ sql,
208
+ params,
209
+ ...loggerContext,
210
+ affected: Utils.isPlainObject(res) ? res.affectedRows : undefined,
211
+ });
212
+ for await (const items of res) {
213
+ for (const row of this.transformRawResult(items, 'all')) {
214
+ yield row;
215
+ }
216
+ }
217
+ } catch (e) {
218
+ this.logQuery(sql, { sql, params, ...loggerContext, level: 'error' });
219
+ throw e;
220
+ }
221
+ }
222
+ /** @inheritDoc */
223
+ async executeDump(dump) {
224
+ await this.ensureConnection();
225
+ try {
226
+ const raw = CompiledQuery.raw(dump);
227
+ await this.getClient().executeQuery(raw);
228
+ } catch (e) {
229
+ /* v8 ignore next */
230
+ throw this.platform.getExceptionConverter().convertException(e);
231
+ }
232
+ }
233
+ getSql(query, formatted, context) {
234
+ const logger = this.config.getLogger();
235
+ if (!logger.isEnabled('query', context)) {
236
+ return query;
237
+ }
238
+ if (logger.isEnabled('query-params', context)) {
239
+ return formatted;
240
+ }
241
+ return query;
242
+ }
243
+ transformRawResult(res, method) {
244
+ if (method === 'get') {
245
+ return res.rows[0];
246
+ }
247
+ if (method === 'all') {
248
+ return res.rows;
259
249
  }
250
+ return {
251
+ affectedRows: Number(res.numAffectedRows ?? res.rows.length),
252
+ insertId: res.insertId != null ? Number(res.insertId) : res.insertId,
253
+ row: res.rows[0],
254
+ rows: res.rows,
255
+ };
256
+ }
260
257
  }