kysely 0.18.0 → 0.18.1
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/dist/cjs/dialect/database-introspector.d.ts +13 -0
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +9 -1
- package/dist/cjs/dialect/mysql/mysql-dialect.js +11 -5
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +25 -3
- package/dist/cjs/dialect/mysql/mysql-driver.js +48 -35
- package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +11 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.js +29 -7
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +9 -1
- package/dist/cjs/dialect/postgres/postgres-dialect.js +11 -5
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +25 -3
- package/dist/cjs/dialect/postgres/postgres-driver.js +37 -24
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +11 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.js +15 -4
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +11 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +11 -4
- package/dist/cjs/index-nodeless.js +5 -1
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/migration/migrator.d.ts +44 -2
- package/dist/cjs/migration/migrator.js +77 -18
- package/dist/cjs/plugin/noop-plugin.d.ts +42 -0
- package/dist/cjs/plugin/noop-plugin.js +12 -0
- package/dist/cjs/query-creator.d.ts +9 -0
- package/dist/cjs/query-creator.js +18 -0
- package/dist/cjs/schema/schema.d.ts +9 -0
- package/dist/cjs/schema/schema.js +12 -0
- package/dist/cjs/util/column-type.d.ts +9 -1
- package/dist/cjs/util/stack-trace-utils.d.ts +1 -0
- package/dist/cjs/util/stack-trace-utils.js +17 -0
- package/dist/esm/dialect/database-introspector.d.ts +13 -0
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +9 -1
- package/dist/esm/dialect/mysql/mysql-dialect.js +11 -5
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +25 -3
- package/dist/esm/dialect/mysql/mysql-driver.js +48 -35
- package/dist/esm/dialect/mysql/mysql-introspector.d.ts +11 -1
- package/dist/esm/dialect/mysql/mysql-introspector.js +30 -8
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +9 -1
- package/dist/esm/dialect/postgres/postgres-dialect.js +11 -5
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +25 -3
- package/dist/esm/dialect/postgres/postgres-driver.js +38 -25
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +11 -1
- package/dist/esm/dialect/postgres/postgres-introspector.js +16 -5
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +11 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +12 -5
- package/dist/esm/migration/migrator.d.ts +44 -2
- package/dist/esm/migration/migrator.js +76 -17
- package/dist/esm/plugin/noop-plugin.d.ts +42 -0
- package/dist/esm/plugin/noop-plugin.js +9 -0
- package/dist/esm/query-creator.d.ts +9 -0
- package/dist/esm/query-creator.js +18 -0
- package/dist/esm/schema/schema.d.ts +9 -0
- package/dist/esm/schema/schema.js +12 -0
- package/dist/esm/util/column-type.d.ts +9 -1
- package/dist/esm/util/stack-trace-utils.d.ts +1 -0
- package/dist/esm/util/stack-trace-utils.js +14 -0
- package/package.json +9 -9
|
@@ -8,7 +8,11 @@ class SqliteIntrospector {
|
|
|
8
8
|
constructor(db) {
|
|
9
9
|
this.#db = db;
|
|
10
10
|
}
|
|
11
|
-
async
|
|
11
|
+
async getSchemas() {
|
|
12
|
+
// Sqlite doesn't support schemas.
|
|
13
|
+
return [];
|
|
14
|
+
}
|
|
15
|
+
async getTables(options = { withInternalKyselyTables: false }) {
|
|
12
16
|
let query = this.#db
|
|
13
17
|
.selectFrom('sqlite_schema')
|
|
14
18
|
.where('type', '=', 'table')
|
|
@@ -17,12 +21,15 @@ class SqliteIntrospector {
|
|
|
17
21
|
.castTo();
|
|
18
22
|
if (!options.withInternalKyselyTables) {
|
|
19
23
|
query = query
|
|
20
|
-
.where('name', '!=', migrator_js_1.
|
|
21
|
-
.where('name', '!=', migrator_js_1.
|
|
24
|
+
.where('name', '!=', migrator_js_1.DEFAULT_MIGRATION_TABLE)
|
|
25
|
+
.where('name', '!=', migrator_js_1.DEFAULT_MIGRATION_LOCK_TABLE);
|
|
22
26
|
}
|
|
23
27
|
const tables = await query.execute();
|
|
28
|
+
return Promise.all(tables.map(({ name }) => this.#getTableMetadata(name)));
|
|
29
|
+
}
|
|
30
|
+
async getMetadata(options) {
|
|
24
31
|
return {
|
|
25
|
-
tables: await
|
|
32
|
+
tables: await this.getTables(options),
|
|
26
33
|
};
|
|
27
34
|
}
|
|
28
35
|
async #getTableMetadata(table) {
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Kysely } from '../kysely.js';
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const
|
|
2
|
+
export declare const DEFAULT_MIGRATION_TABLE = "kysely_migration";
|
|
3
|
+
export declare const DEFAULT_MIGRATION_LOCK_TABLE = "kysely_migration_lock";
|
|
4
4
|
export declare const MIGRATION_LOCK_ID = "migration_lock";
|
|
5
5
|
export declare const NO_MIGRATIONS: NoMigrations;
|
|
6
6
|
export interface Migration {
|
|
@@ -148,6 +148,48 @@ export declare class Migrator {
|
|
|
148
148
|
export interface MigratorProps {
|
|
149
149
|
readonly db: Kysely<any>;
|
|
150
150
|
readonly provider: MigrationProvider;
|
|
151
|
+
/**
|
|
152
|
+
* The name of the internal migration table. Defaults to `kysely_migration`.
|
|
153
|
+
*
|
|
154
|
+
* If you do specify this, you need to ALWAYS use the same value. Kysely doesn't
|
|
155
|
+
* support changing the table on the fly. If you run the migrator even once with a
|
|
156
|
+
* table name X and then change the table name to Y, kysely will create a new empty
|
|
157
|
+
* migration table and attempt to run the migrations again, which will obviously
|
|
158
|
+
* fail.
|
|
159
|
+
*
|
|
160
|
+
* If you do specify this, ALWAYS ALWAYS use the same value from the beginning of
|
|
161
|
+
* the project, to the end of time or prepare to manually migrate the migration
|
|
162
|
+
* tables.
|
|
163
|
+
*/
|
|
164
|
+
readonly migrationTableName?: string;
|
|
165
|
+
/**
|
|
166
|
+
* The name of the internal migration lock table. Defaults to `kysely_migration_lock`.
|
|
167
|
+
*
|
|
168
|
+
* If you do specify this, you need to ALWAYS use the same value. Kysely doesn't
|
|
169
|
+
* support changing the table on the fly. If you run the migrator even once with a
|
|
170
|
+
* table name X and then change the table name to Y, kysely will create a new empty
|
|
171
|
+
* lock table.
|
|
172
|
+
*
|
|
173
|
+
* If you do specify this, ALWAYS ALWAYS use the same value from the beginning of
|
|
174
|
+
* the project, to the end of time or prepare to manually migrate the migration
|
|
175
|
+
* tables.
|
|
176
|
+
*/
|
|
177
|
+
readonly migrationLockTableName?: string;
|
|
178
|
+
/**
|
|
179
|
+
* The schema of the internal migration tables. Defaults to the default schema
|
|
180
|
+
* on dialects that support schemas.
|
|
181
|
+
*
|
|
182
|
+
* If you do specify this, you need to ALWAYS use the same value. Kysely doesn't
|
|
183
|
+
* support changing the schema on the fly. If you run the migrator even once with a
|
|
184
|
+
* schema name X and then change the schema name to Y, kysely will create a new empty
|
|
185
|
+
* migration tables in the new schema and attempt to run the migrations again, which
|
|
186
|
+
* will obviously fail.
|
|
187
|
+
*
|
|
188
|
+
* If you do specify this, ALWAYS ALWAYS use the same value from the beginning of
|
|
189
|
+
* the project, to the end of time or prepare to manually migrate the migration
|
|
190
|
+
* tables.
|
|
191
|
+
*/
|
|
192
|
+
readonly migrationTableSchema?: string;
|
|
151
193
|
}
|
|
152
194
|
/**
|
|
153
195
|
* All migration methods ({@link Migrator.migrateTo | migrateTo},
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Migrator = exports.NO_MIGRATIONS = exports.MIGRATION_LOCK_ID = exports.
|
|
3
|
+
exports.Migrator = exports.NO_MIGRATIONS = exports.MIGRATION_LOCK_ID = exports.DEFAULT_MIGRATION_LOCK_TABLE = exports.DEFAULT_MIGRATION_TABLE = void 0;
|
|
4
|
+
const noop_plugin_js_1 = require("../plugin/noop-plugin.js");
|
|
5
|
+
const with_schema_plugin_js_1 = require("../plugin/with-schema/with-schema-plugin.js");
|
|
4
6
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
-
exports.
|
|
6
|
-
exports.
|
|
7
|
+
exports.DEFAULT_MIGRATION_TABLE = 'kysely_migration';
|
|
8
|
+
exports.DEFAULT_MIGRATION_LOCK_TABLE = 'kysely_migration_lock';
|
|
7
9
|
exports.MIGRATION_LOCK_ID = 'migration_lock';
|
|
8
10
|
exports.NO_MIGRATIONS = (0, object_utils_js_1.freeze)({ __noMigrations__: true });
|
|
9
11
|
/**
|
|
@@ -37,9 +39,10 @@ class Migrator {
|
|
|
37
39
|
* The returned array is sorted by migration name.
|
|
38
40
|
*/
|
|
39
41
|
async getMigrations() {
|
|
40
|
-
const executedMigrations = (await this.#doesTableExists(
|
|
42
|
+
const executedMigrations = (await this.#doesTableExists(this.#migrationTable))
|
|
41
43
|
? await this.#props.db
|
|
42
|
-
.
|
|
44
|
+
.withPlugin(this.#schemaPlugin)
|
|
45
|
+
.selectFrom(this.#migrationTable)
|
|
43
46
|
.select(['name', 'timestamp'])
|
|
44
47
|
.execute()
|
|
45
48
|
: [];
|
|
@@ -184,16 +187,61 @@ class Migrator {
|
|
|
184
187
|
return { error };
|
|
185
188
|
}
|
|
186
189
|
}
|
|
190
|
+
get #migrationTableSchema() {
|
|
191
|
+
return this.#props.migrationTableSchema;
|
|
192
|
+
}
|
|
193
|
+
get #migrationTable() {
|
|
194
|
+
return this.#props.migrationTableName ?? exports.DEFAULT_MIGRATION_TABLE;
|
|
195
|
+
}
|
|
196
|
+
get #migrationLockTable() {
|
|
197
|
+
return this.#props.migrationLockTableName ?? exports.DEFAULT_MIGRATION_LOCK_TABLE;
|
|
198
|
+
}
|
|
199
|
+
get #schemaPlugin() {
|
|
200
|
+
if (this.#migrationTableSchema) {
|
|
201
|
+
return new with_schema_plugin_js_1.WithSchemaPlugin(this.#migrationTableSchema);
|
|
202
|
+
}
|
|
203
|
+
return new noop_plugin_js_1.NoopPlugin();
|
|
204
|
+
}
|
|
187
205
|
async #ensureMigrationTablesExists() {
|
|
206
|
+
await this.#ensureMigrationTableSchemaExists();
|
|
188
207
|
await this.#ensureMigrationTableExists();
|
|
189
208
|
await this.#ensureMigrationLockTableExists();
|
|
190
209
|
await this.#ensureLockRowExists();
|
|
191
210
|
}
|
|
211
|
+
async #ensureMigrationTableSchemaExists() {
|
|
212
|
+
if (!this.#migrationTableSchema) {
|
|
213
|
+
// Use default schema. Nothing to do.
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (!(await this.#doesSchemaExists())) {
|
|
217
|
+
try {
|
|
218
|
+
await this.#props.db.schema
|
|
219
|
+
.createSchema(this.#migrationTableSchema)
|
|
220
|
+
.ifNotExists()
|
|
221
|
+
.execute();
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
// At least on PostgreSQL, `if not exists` doesn't guarantee the `create schema`
|
|
225
|
+
// query doesn't throw if the schema already exits. That's why we check if
|
|
226
|
+
// the schema exist here and ignore the error if it does.
|
|
227
|
+
if (!(await this.#doesSchemaExists())) {
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
192
233
|
async #ensureMigrationTableExists() {
|
|
193
|
-
if (!(await this.#doesTableExists(
|
|
234
|
+
if (!(await this.#doesTableExists(this.#migrationTable))) {
|
|
194
235
|
try {
|
|
236
|
+
if (this.#migrationTableSchema) {
|
|
237
|
+
await this.#props.db.schema
|
|
238
|
+
.createSchema(this.#migrationTableSchema)
|
|
239
|
+
.ifNotExists()
|
|
240
|
+
.execute();
|
|
241
|
+
}
|
|
195
242
|
await this.#props.db.schema
|
|
196
|
-
.
|
|
243
|
+
.withPlugin(this.#schemaPlugin)
|
|
244
|
+
.createTable(this.#migrationTable)
|
|
197
245
|
.ifNotExists()
|
|
198
246
|
.addColumn('name', 'varchar(255)', (col) => col.notNull().primaryKey())
|
|
199
247
|
// The migration run time as ISO string. This is not a real date type as we
|
|
@@ -205,17 +253,18 @@ class Migrator {
|
|
|
205
253
|
// At least on PostgreSQL, `if not exists` doesn't guarantee the `create table`
|
|
206
254
|
// query doesn't throw if the table already exits. That's why we check if
|
|
207
255
|
// the table exist here and ignore the error if it does.
|
|
208
|
-
if (!(await this.#doesTableExists(
|
|
256
|
+
if (!(await this.#doesTableExists(this.#migrationTable))) {
|
|
209
257
|
throw error;
|
|
210
258
|
}
|
|
211
259
|
}
|
|
212
260
|
}
|
|
213
261
|
}
|
|
214
262
|
async #ensureMigrationLockTableExists() {
|
|
215
|
-
if (!(await this.#doesTableExists(
|
|
263
|
+
if (!(await this.#doesTableExists(this.#migrationLockTable))) {
|
|
216
264
|
try {
|
|
217
265
|
await this.#props.db.schema
|
|
218
|
-
.
|
|
266
|
+
.withPlugin(this.#schemaPlugin)
|
|
267
|
+
.createTable(this.#migrationLockTable)
|
|
219
268
|
.ifNotExists()
|
|
220
269
|
.addColumn('id', 'varchar(255)', (col) => col.notNull().primaryKey())
|
|
221
270
|
.addColumn('is_locked', 'integer', (col) => col.notNull().defaultTo(0))
|
|
@@ -225,7 +274,7 @@ class Migrator {
|
|
|
225
274
|
// At least on PostgreSQL, `if not exists` doesn't guarantee the `create table`
|
|
226
275
|
// query doesn't throw if the table already exits. That's why we check if
|
|
227
276
|
// the table exist here and ignore the error if it does.
|
|
228
|
-
if (!(await this.#doesTableExists(
|
|
277
|
+
if (!(await this.#doesTableExists(this.#migrationLockTable))) {
|
|
229
278
|
throw error;
|
|
230
279
|
}
|
|
231
280
|
}
|
|
@@ -235,7 +284,8 @@ class Migrator {
|
|
|
235
284
|
if (!(await this.#doesLockRowExists())) {
|
|
236
285
|
try {
|
|
237
286
|
await this.#props.db
|
|
238
|
-
.
|
|
287
|
+
.withPlugin(this.#schemaPlugin)
|
|
288
|
+
.insertInto(this.#migrationLockTable)
|
|
239
289
|
.values({ id: exports.MIGRATION_LOCK_ID, is_locked: 0 })
|
|
240
290
|
.execute();
|
|
241
291
|
}
|
|
@@ -246,15 +296,21 @@ class Migrator {
|
|
|
246
296
|
}
|
|
247
297
|
}
|
|
248
298
|
}
|
|
299
|
+
async #doesSchemaExists() {
|
|
300
|
+
const schemas = await this.#props.db.introspection.getSchemas();
|
|
301
|
+
return schemas.some((it) => it.name === this.#migrationTableSchema);
|
|
302
|
+
}
|
|
249
303
|
async #doesTableExists(tableName) {
|
|
250
|
-
const
|
|
304
|
+
const schema = this.#migrationTableSchema;
|
|
305
|
+
const tables = await this.#props.db.introspection.getTables({
|
|
251
306
|
withInternalKyselyTables: true,
|
|
252
307
|
});
|
|
253
|
-
return
|
|
308
|
+
return tables.some((it) => it.name === tableName && (!schema || it.schema === schema));
|
|
254
309
|
}
|
|
255
310
|
async #doesLockRowExists() {
|
|
256
311
|
const lockRow = await this.#props.db
|
|
257
|
-
.
|
|
312
|
+
.withPlugin(this.#schemaPlugin)
|
|
313
|
+
.selectFrom(this.#migrationLockTable)
|
|
258
314
|
.where('id', '=', exports.MIGRATION_LOCK_ID)
|
|
259
315
|
.select('id')
|
|
260
316
|
.executeTakeFirst();
|
|
@@ -312,7 +368,8 @@ class Migrator {
|
|
|
312
368
|
}
|
|
313
369
|
async #getExecutedMigrations(db) {
|
|
314
370
|
const executedMigrations = await db
|
|
315
|
-
.
|
|
371
|
+
.withPlugin(this.#schemaPlugin)
|
|
372
|
+
.selectFrom(this.#migrationTable)
|
|
316
373
|
.select('name')
|
|
317
374
|
.orderBy('name')
|
|
318
375
|
.execute();
|
|
@@ -348,7 +405,8 @@ class Migrator {
|
|
|
348
405
|
if (migration.down) {
|
|
349
406
|
await migration.down(db);
|
|
350
407
|
await db
|
|
351
|
-
.
|
|
408
|
+
.withPlugin(this.#schemaPlugin)
|
|
409
|
+
.deleteFrom(this.#migrationTable)
|
|
352
410
|
.where('name', '=', migration.name)
|
|
353
411
|
.execute();
|
|
354
412
|
results[i] = {
|
|
@@ -386,7 +444,8 @@ class Migrator {
|
|
|
386
444
|
try {
|
|
387
445
|
await migration.up(db);
|
|
388
446
|
await db
|
|
389
|
-
.
|
|
447
|
+
.withPlugin(this.#schemaPlugin)
|
|
448
|
+
.insertInto(this.#migrationTable)
|
|
390
449
|
.values({
|
|
391
450
|
name: migration.name,
|
|
392
451
|
timestamp: new Date().toISOString(),
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { QueryResult } from '../driver/database-connection.js';
|
|
2
|
+
import { UnknownRow } from '../index-nodeless.js';
|
|
3
|
+
import { RootOperationNode } from '../query-compiler/query-compiler.js';
|
|
4
|
+
import { KyselyPlugin, PluginTransformQueryArgs, PluginTransformResultArgs } from './kysely-plugin.js';
|
|
5
|
+
export declare class NoopPlugin implements KyselyPlugin {
|
|
6
|
+
/**
|
|
7
|
+
* This is called for each query before it is executed. You can modify the query by
|
|
8
|
+
* transforming its {@link OperationNode} tree provided in {@link PluginTransformQueryArgs.node | args.node}
|
|
9
|
+
* and returning the transformed tree. You'd usually want to use an {@link OperationNodeTransformer}
|
|
10
|
+
* for this.
|
|
11
|
+
*
|
|
12
|
+
* If you need to pass some query-related data between this method and `transformResult` you
|
|
13
|
+
* can use a `WeakMap` with {@link PluginTransformQueryArgs.queryId | args.queryId} as the key:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const plugin = {
|
|
17
|
+
* data: new WeakMap<QueryId, SomeData>(),
|
|
18
|
+
*
|
|
19
|
+
* transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
|
|
20
|
+
* this.data.set(args.queryId, something)
|
|
21
|
+
* return args.node
|
|
22
|
+
* },
|
|
23
|
+
*
|
|
24
|
+
* transformResult(args: PluginTransformResultArgs): QueryResult<UnknownRow> {
|
|
25
|
+
* const data = this.data.get(args.queryId)
|
|
26
|
+
* return args.result
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* You should use a `WeakMap` instead of a `Map` or some other strong references because `transformQuery`
|
|
32
|
+
* is not always matched by a call to `transformResult` which would leave orphaned items in the map
|
|
33
|
+
* and cause a memory leak.
|
|
34
|
+
*/
|
|
35
|
+
transformQuery(args: PluginTransformQueryArgs): RootOperationNode;
|
|
36
|
+
/**
|
|
37
|
+
* This method is called for each query after it has been executed. The result
|
|
38
|
+
* of the query can be accessed through {@link PluginTransformResultArgs.result | args.result}.
|
|
39
|
+
* You can modify the result and return the modifier result.
|
|
40
|
+
*/
|
|
41
|
+
transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>>;
|
|
42
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NoopPlugin = void 0;
|
|
4
|
+
class NoopPlugin {
|
|
5
|
+
transformQuery(args) {
|
|
6
|
+
return args.node;
|
|
7
|
+
}
|
|
8
|
+
async transformResult(args) {
|
|
9
|
+
return args.result;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.NoopPlugin = NoopPlugin;
|
|
@@ -9,6 +9,7 @@ import { WithNode } from './operation-node/with-node.js';
|
|
|
9
9
|
import { InsertResult } from './query-builder/insert-result.js';
|
|
10
10
|
import { DeleteResult } from './query-builder/delete-result.js';
|
|
11
11
|
import { UpdateResult } from './query-builder/update-result.js';
|
|
12
|
+
import { KyselyPlugin } from './plugin/kysely-plugin.js';
|
|
12
13
|
export declare class QueryCreator<DB> {
|
|
13
14
|
#private;
|
|
14
15
|
constructor(props: QueryCreatorProps);
|
|
@@ -252,6 +253,14 @@ export declare class QueryCreator<DB> {
|
|
|
252
253
|
* See the {@link with} method for examples and more documentation.
|
|
253
254
|
*/
|
|
254
255
|
withRecursive<N extends string, E extends RecursiveCommonTableExpression<DB, N>>(name: N, expression: E): QueryCreatorWithCommonTableExpression<DB, N, E>;
|
|
256
|
+
/**
|
|
257
|
+
* Returns a copy of this query creator instance with the given plugin installed.
|
|
258
|
+
*/
|
|
259
|
+
withPlugin(plugin: KyselyPlugin): QueryCreator<DB>;
|
|
260
|
+
/**
|
|
261
|
+
* Returns a copy of this query creator instance without any plugins.
|
|
262
|
+
*/
|
|
263
|
+
withoutPlugins(): QueryCreator<DB>;
|
|
255
264
|
/**
|
|
256
265
|
* Sets the schema to be used for all table references that don't explicitly
|
|
257
266
|
* specify a schema.
|
|
@@ -192,6 +192,24 @@ class QueryCreator {
|
|
|
192
192
|
: with_node_js_1.WithNode.create(cte, { recursive: true }),
|
|
193
193
|
});
|
|
194
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Returns a copy of this query creator instance with the given plugin installed.
|
|
197
|
+
*/
|
|
198
|
+
withPlugin(plugin) {
|
|
199
|
+
return new QueryCreator({
|
|
200
|
+
...this.#props,
|
|
201
|
+
executor: this.#props.executor.withPlugin(plugin),
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Returns a copy of this query creator instance without any plugins.
|
|
206
|
+
*/
|
|
207
|
+
withoutPlugins() {
|
|
208
|
+
return new QueryCreator({
|
|
209
|
+
...this.#props,
|
|
210
|
+
executor: this.#props.executor.withoutPlugins(),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
195
213
|
/**
|
|
196
214
|
* Sets the schema to be used for all table references that don't explicitly
|
|
197
215
|
* specify a schema.
|
|
@@ -8,6 +8,7 @@ import { DropSchemaBuilder } from './drop-schema-builder.js';
|
|
|
8
8
|
import { DropTableBuilder } from './drop-table-builder.js';
|
|
9
9
|
import { CreateViewBuilder } from './create-view-builder.js';
|
|
10
10
|
import { DropViewBuilder } from './drop-view-builder.js';
|
|
11
|
+
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
11
12
|
/**
|
|
12
13
|
* Provides methods for building database schema.
|
|
13
14
|
*/
|
|
@@ -169,6 +170,14 @@ export declare class SchemaModule {
|
|
|
169
170
|
* ```
|
|
170
171
|
*/
|
|
171
172
|
dropView(viewName: string): DropViewBuilder;
|
|
173
|
+
/**
|
|
174
|
+
* Returns a copy of this schema module with the given plugin installed.
|
|
175
|
+
*/
|
|
176
|
+
withPlugin(plugin: KyselyPlugin): SchemaModule;
|
|
177
|
+
/**
|
|
178
|
+
* Returns a copy of this schema module without any plugins.
|
|
179
|
+
*/
|
|
180
|
+
withoutPlugins(): SchemaModule;
|
|
172
181
|
/**
|
|
173
182
|
* See {@link QueryCreator.withSchema}
|
|
174
183
|
*/
|
|
@@ -239,6 +239,18 @@ class SchemaModule {
|
|
|
239
239
|
dropViewNode: drop_view_node_js_1.DropViewNode.create(viewName),
|
|
240
240
|
});
|
|
241
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Returns a copy of this schema module with the given plugin installed.
|
|
244
|
+
*/
|
|
245
|
+
withPlugin(plugin) {
|
|
246
|
+
return new SchemaModule(this.#executor.withPlugin(plugin));
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Returns a copy of this schema module without any plugins.
|
|
250
|
+
*/
|
|
251
|
+
withoutPlugins() {
|
|
252
|
+
return new SchemaModule(this.#executor.withoutPlugins());
|
|
253
|
+
}
|
|
242
254
|
/**
|
|
243
255
|
* See {@link QueryCreator.withSchema}
|
|
244
256
|
*/
|
|
@@ -43,13 +43,21 @@ export declare type ColumnType<SelectType, InsertType = SelectType, UpdateType =
|
|
|
43
43
|
* is the same for all selects, inserts and updates but the
|
|
44
44
|
* column is optional for inserts and updates.
|
|
45
45
|
*
|
|
46
|
-
*
|
|
46
|
+
* The implementation:
|
|
47
47
|
*
|
|
48
48
|
* ```ts
|
|
49
|
+
* // The update type is `S` instead of `S | undefined` because
|
|
50
|
+
* // updates are always optional --> no need to specify optionality.
|
|
49
51
|
* type Generated<S> = ColumnType<S, S | undefined, S>
|
|
50
52
|
* ```
|
|
51
53
|
*/
|
|
52
54
|
export declare type Generated<S> = ColumnType<S, S | undefined, S>;
|
|
55
|
+
/**
|
|
56
|
+
* A shortcut for defining columns that are only database-generated
|
|
57
|
+
* (like postgres GENERATED ALWAYS AS IDENTITY). No insert/update
|
|
58
|
+
* is allowed.
|
|
59
|
+
*/
|
|
60
|
+
export declare type GeneratedAlways<S> = ColumnType<S, never, never>;
|
|
53
61
|
/**
|
|
54
62
|
* Evaluates to `K` if `T` can be `null` or `undefined`.
|
|
55
63
|
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function extendStackTrace(err: unknown, stackError: Error): unknown;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extendStackTrace = void 0;
|
|
4
|
+
const object_utils_js_1 = require("./object-utils.js");
|
|
5
|
+
function extendStackTrace(err, stackError) {
|
|
6
|
+
if (isStackHolder(err) && stackError.stack) {
|
|
7
|
+
// Remove the first line that just says `Error`.
|
|
8
|
+
const stackExtension = stackError.stack.split('\n').slice(1).join('\n');
|
|
9
|
+
err.stack += `\n${stackExtension}`;
|
|
10
|
+
return err;
|
|
11
|
+
}
|
|
12
|
+
return err;
|
|
13
|
+
}
|
|
14
|
+
exports.extendStackTrace = extendStackTrace;
|
|
15
|
+
function isStackHolder(obj) {
|
|
16
|
+
return (0, object_utils_js_1.isObject)(obj) && (0, object_utils_js_1.isString)(obj.stack);
|
|
17
|
+
}
|
|
@@ -3,8 +3,18 @@ import { ColumnDataType } from '../operation-node/data-type-node.js';
|
|
|
3
3
|
* An interface for getting the database metadata (names of the tables and columns etc.)
|
|
4
4
|
*/
|
|
5
5
|
export interface DatabaseIntrospector {
|
|
6
|
+
/**
|
|
7
|
+
* Get schema metadata.
|
|
8
|
+
*/
|
|
9
|
+
getSchemas(): Promise<SchemaMetadata[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Get table metadata.
|
|
12
|
+
*/
|
|
13
|
+
getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
|
|
6
14
|
/**
|
|
7
15
|
* Get the database metadata such as table and column names.
|
|
16
|
+
*
|
|
17
|
+
* @deprecated Use getTables() instead.
|
|
8
18
|
*/
|
|
9
19
|
getMetadata(options?: DatabaseMetadataOptions): Promise<DatabaseMetadata>;
|
|
10
20
|
}
|
|
@@ -15,6 +25,9 @@ export interface DatabaseMetadataOptions {
|
|
|
15
25
|
*/
|
|
16
26
|
withInternalKyselyTables: boolean;
|
|
17
27
|
}
|
|
28
|
+
export interface SchemaMetadata {
|
|
29
|
+
readonly name: string;
|
|
30
|
+
}
|
|
18
31
|
export interface DatabaseMetadata {
|
|
19
32
|
/**
|
|
20
33
|
* The tables found in the database.
|
|
@@ -3,6 +3,7 @@ import { Kysely } from '../../kysely.js';
|
|
|
3
3
|
import { QueryCompiler } from '../../query-compiler/query-compiler.js';
|
|
4
4
|
import { Dialect } from '../dialect.js';
|
|
5
5
|
import { DatabaseIntrospector } from '../database-introspector.js';
|
|
6
|
+
import { MysqlPool } from './mysql-driver.js';
|
|
6
7
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
7
8
|
import { MysqlDialectConfig } from './mysql-dialect-config.js';
|
|
8
9
|
/**
|
|
@@ -11,10 +12,17 @@ import { MysqlDialectConfig } from './mysql-dialect-config.js';
|
|
|
11
12
|
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
12
13
|
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
13
14
|
* method.
|
|
15
|
+
*
|
|
16
|
+
* You can also provide an existing mysql2 `Pool` instance:
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { createPool } from 'mysql2'
|
|
20
|
+
* new MysqlDialect(createPool(config))
|
|
21
|
+
* ```
|
|
14
22
|
*/
|
|
15
23
|
export declare class MysqlDialect implements Dialect {
|
|
16
24
|
#private;
|
|
17
|
-
constructor(
|
|
25
|
+
constructor(configOrPool: MysqlDialectConfig | MysqlPool);
|
|
18
26
|
/**
|
|
19
27
|
* Creates a driver for the dialect.
|
|
20
28
|
*/
|
|
@@ -3,21 +3,27 @@ import { MysqlDriver } from './mysql-driver.js';
|
|
|
3
3
|
import { MysqlQueryCompiler } from './mysql-query-compiler.js';
|
|
4
4
|
import { MysqlIntrospector } from './mysql-introspector.js';
|
|
5
5
|
import { MysqlAdapter } from './mysql-adapter.js';
|
|
6
|
-
import { freeze } from '../../util/object-utils.js';
|
|
7
6
|
/**
|
|
8
7
|
* MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
|
|
9
8
|
*
|
|
10
9
|
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
11
10
|
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
12
11
|
* method.
|
|
12
|
+
*
|
|
13
|
+
* You can also provide an existing mysql2 `Pool` instance:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { createPool } from 'mysql2'
|
|
17
|
+
* new MysqlDialect(createPool(config))
|
|
18
|
+
* ```
|
|
13
19
|
*/
|
|
14
20
|
export class MysqlDialect {
|
|
15
|
-
#
|
|
16
|
-
constructor(
|
|
17
|
-
this.#
|
|
21
|
+
#configOrPool;
|
|
22
|
+
constructor(configOrPool) {
|
|
23
|
+
this.#configOrPool = configOrPool;
|
|
18
24
|
}
|
|
19
25
|
createDriver() {
|
|
20
|
-
return new MysqlDriver(this.#
|
|
26
|
+
return new MysqlDriver(this.#configOrPool);
|
|
21
27
|
}
|
|
22
28
|
createQueryCompiler() {
|
|
23
29
|
return new MysqlQueryCompiler();
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
1
|
+
import { DatabaseConnection, QueryResult } from '../../driver/database-connection.js';
|
|
2
2
|
import { Driver, TransactionSettings } from '../../driver/driver.js';
|
|
3
|
+
import { CompiledQuery } from '../../query-compiler/compiled-query.js';
|
|
3
4
|
import { MysqlDialectConfig } from './mysql-dialect-config.js';
|
|
5
|
+
declare const PRIVATE_RELEASE_METHOD: unique symbol;
|
|
4
6
|
export declare class MysqlDriver implements Driver {
|
|
5
7
|
#private;
|
|
6
|
-
constructor(
|
|
8
|
+
constructor(configOrPool: MysqlDialectConfig | MysqlPool);
|
|
7
9
|
/**
|
|
8
10
|
* Initializes the driver.
|
|
9
11
|
*
|
|
@@ -35,9 +37,29 @@ export declare class MysqlDriver implements Driver {
|
|
|
35
37
|
/**
|
|
36
38
|
* Releases a connection back to the pool.
|
|
37
39
|
*/
|
|
38
|
-
releaseConnection(connection:
|
|
40
|
+
releaseConnection(connection: MysqlConnection): Promise<void>;
|
|
39
41
|
/**
|
|
40
42
|
* Destroys the driver and releases all resources.
|
|
41
43
|
*/
|
|
42
44
|
destroy(): Promise<void>;
|
|
43
45
|
}
|
|
46
|
+
export interface MysqlPool {
|
|
47
|
+
getConnection(callback: (error: unknown, connection: MysqlPoolConnection) => void): void;
|
|
48
|
+
end(callback: (error: unknown) => void): void;
|
|
49
|
+
}
|
|
50
|
+
interface MysqlPoolConnection {
|
|
51
|
+
query(sql: string, parameters: ReadonlyArray<unknown>, callback: (error: unknown, result: MysqlQueryResult) => void): void;
|
|
52
|
+
release(): void;
|
|
53
|
+
}
|
|
54
|
+
interface MysqlOkPacket {
|
|
55
|
+
affectedRows: number;
|
|
56
|
+
insertId: number;
|
|
57
|
+
}
|
|
58
|
+
declare type MysqlQueryResult = MysqlOkPacket | Record<string, unknown>[];
|
|
59
|
+
declare class MysqlConnection implements DatabaseConnection {
|
|
60
|
+
#private;
|
|
61
|
+
constructor(rawConnection: MysqlPoolConnection);
|
|
62
|
+
executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
|
|
63
|
+
[PRIVATE_RELEASE_METHOD](): void;
|
|
64
|
+
}
|
|
65
|
+
export {};
|