@mikro-orm/sql 7.0.3 → 7.0.4-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.
- package/AbstractSqlConnection.d.ts +58 -94
- package/AbstractSqlConnection.js +238 -235
- package/AbstractSqlDriver.d.ts +155 -410
- package/AbstractSqlDriver.js +1941 -2064
- package/AbstractSqlPlatform.d.ts +73 -83
- package/AbstractSqlPlatform.js +158 -162
- package/PivotCollectionPersister.d.ts +15 -33
- package/PivotCollectionPersister.js +160 -158
- package/README.md +1 -1
- package/SqlEntityManager.d.ts +22 -67
- package/SqlEntityManager.js +38 -54
- package/SqlEntityRepository.d.ts +14 -14
- package/SqlEntityRepository.js +23 -23
- package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -12
- package/dialects/mssql/MsSqlNativeQueryBuilder.js +194 -192
- package/dialects/mysql/BaseMySqlPlatform.d.ts +45 -64
- package/dialects/mysql/BaseMySqlPlatform.js +131 -134
- package/dialects/mysql/MySqlExceptionConverter.d.ts +6 -6
- package/dialects/mysql/MySqlExceptionConverter.js +77 -91
- package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +3 -3
- package/dialects/mysql/MySqlNativeQueryBuilder.js +69 -66
- package/dialects/mysql/MySqlSchemaHelper.d.ts +39 -39
- package/dialects/mysql/MySqlSchemaHelper.js +319 -327
- package/dialects/oracledb/OracleDialect.d.ts +52 -81
- package/dialects/oracledb/OracleDialect.js +149 -155
- package/dialects/oracledb/OracleNativeQueryBuilder.d.ts +12 -12
- package/dialects/oracledb/OracleNativeQueryBuilder.js +236 -232
- package/dialects/postgresql/BasePostgreSqlPlatform.d.ts +105 -108
- package/dialects/postgresql/BasePostgreSqlPlatform.js +350 -351
- package/dialects/postgresql/FullTextType.d.ts +6 -10
- package/dialects/postgresql/FullTextType.js +51 -51
- package/dialects/postgresql/PostgreSqlExceptionConverter.d.ts +5 -5
- package/dialects/postgresql/PostgreSqlExceptionConverter.js +43 -55
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +4 -4
- package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +82 -102
- package/dialects/postgresql/PostgreSqlSchemaHelper.js +705 -733
- package/dialects/sqlite/BaseSqliteConnection.d.ts +5 -3
- package/dialects/sqlite/BaseSqliteConnection.js +19 -21
- package/dialects/sqlite/NodeSqliteDialect.d.ts +1 -1
- package/dialects/sqlite/NodeSqliteDialect.js +23 -23
- package/dialects/sqlite/SqliteDriver.d.ts +1 -1
- package/dialects/sqlite/SqliteDriver.js +3 -3
- package/dialects/sqlite/SqliteExceptionConverter.d.ts +6 -6
- package/dialects/sqlite/SqliteExceptionConverter.js +51 -67
- package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +2 -2
- package/dialects/sqlite/SqliteNativeQueryBuilder.js +7 -7
- package/dialects/sqlite/SqlitePlatform.d.ts +72 -63
- package/dialects/sqlite/SqlitePlatform.js +139 -139
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +60 -70
- package/dialects/sqlite/SqliteSchemaHelper.js +520 -533
- package/package.json +2 -2
- package/plugin/index.d.ts +35 -42
- package/plugin/index.js +36 -43
- package/plugin/transformer.d.ts +94 -117
- package/plugin/transformer.js +881 -890
- package/query/ArrayCriteriaNode.d.ts +4 -4
- package/query/ArrayCriteriaNode.js +18 -18
- package/query/CriteriaNode.d.ts +25 -35
- package/query/CriteriaNode.js +123 -133
- package/query/CriteriaNodeFactory.d.ts +6 -49
- package/query/CriteriaNodeFactory.js +94 -97
- package/query/NativeQueryBuilder.d.ts +118 -118
- package/query/NativeQueryBuilder.js +480 -484
- package/query/ObjectCriteriaNode.d.ts +12 -12
- package/query/ObjectCriteriaNode.js +282 -298
- package/query/QueryBuilder.d.ts +904 -1546
- package/query/QueryBuilder.js +2164 -2294
- package/query/QueryBuilderHelper.d.ts +72 -153
- package/query/QueryBuilderHelper.js +1028 -1079
- package/query/ScalarCriteriaNode.d.ts +3 -3
- package/query/ScalarCriteriaNode.js +46 -53
- package/query/enums.d.ts +14 -14
- package/query/enums.js +14 -14
- package/query/raw.d.ts +6 -16
- package/query/raw.js +10 -10
- package/schema/DatabaseSchema.d.ts +50 -73
- package/schema/DatabaseSchema.js +307 -331
- package/schema/DatabaseTable.d.ts +73 -96
- package/schema/DatabaseTable.js +927 -1012
- package/schema/SchemaComparator.d.ts +66 -70
- package/schema/SchemaComparator.js +740 -766
- package/schema/SchemaHelper.d.ts +95 -109
- package/schema/SchemaHelper.js +659 -675
- package/schema/SqlSchemaGenerator.d.ts +58 -78
- package/schema/SqlSchemaGenerator.js +501 -535
- package/typings.d.ts +266 -380
package/AbstractSqlConnection.js
CHANGED
|
@@ -1,257 +1,260 @@
|
|
|
1
1
|
import { CompiledQuery, Kysely } from 'kysely';
|
|
2
|
-
import { Connection, EventType, RawQueryFragment, Utils } from '@mikro-orm/core';
|
|
2
|
+
import { Connection, EventType, RawQueryFragment, 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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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();
|
|
20
|
-
}
|
|
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 });
|
|
35
|
-
}
|
|
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' };
|
|
59
|
-
}
|
|
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 };
|
|
65
|
-
}
|
|
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
|
-
}
|
|
77
|
-
}
|
|
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();
|
|
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
|
+
}
|
|
84
14
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
+
}
|
|
96
38
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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;
|
|
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;
|
|
111
47
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
48
|
+
/**
|
|
49
|
+
* @inheritDoc
|
|
50
|
+
*/
|
|
51
|
+
async isConnected() {
|
|
52
|
+
const check = await this.checkConnection();
|
|
53
|
+
return check.ok;
|
|
117
54
|
}
|
|
118
|
-
|
|
119
|
-
|
|
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
|
+
}
|
|
120
69
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
this.logQuery(query, options.loggerContext);
|
|
132
|
-
}
|
|
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;
|
|
133
80
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (ctx.isRolledBack) {
|
|
140
|
-
return;
|
|
81
|
+
/** Ensures the Kysely client is initialized, creating it asynchronously if needed. */
|
|
82
|
+
async initClient() {
|
|
83
|
+
if (!this.#client) {
|
|
84
|
+
await this.createKysely();
|
|
85
|
+
}
|
|
141
86
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
+
}
|
|
149
99
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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;
|
|
161
139
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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);
|
|
167
155
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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);
|
|
171
168
|
}
|
|
172
|
-
query =
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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;
|
|
169
|
+
prepareQuery(query, params = []) {
|
|
170
|
+
if (query instanceof NativeQueryBuilder) {
|
|
171
|
+
query = query.toRaw();
|
|
172
|
+
}
|
|
173
|
+
if (query instanceof RawQueryFragment) {
|
|
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 };
|
|
220
180
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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 });
|
|
231
191
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
+
}
|
|
237
223
|
}
|
|
238
|
-
|
|
239
|
-
|
|
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
|
+
}
|
|
240
235
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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;
|
|
246
245
|
}
|
|
247
|
-
|
|
248
|
-
|
|
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
|
+
};
|
|
249
259
|
}
|
|
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
|
-
}
|
|
257
260
|
}
|