kysely 0.17.2 → 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/README.md +2 -0
- package/dist/cjs/dialect/database-introspector.d.ts +13 -0
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +19 -19
- 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/operation-node/alias-node.d.ts +2 -2
- package/dist/cjs/operation-node/alias-node.js +1 -2
- package/dist/cjs/operation-node/operation-node-transformer.js +1 -0
- package/dist/cjs/operation-node/update-query-node.d.ts +3 -0
- package/dist/cjs/operation-node/update-query-node.js +11 -2
- package/dist/cjs/parser/reference-parser.js +2 -1
- package/dist/cjs/parser/table-parser.js +2 -1
- package/dist/cjs/parser/update-set-parser.d.ts +3 -3
- package/dist/cjs/parser/with-parser.d.ts +2 -2
- package/dist/cjs/plugin/noop-plugin.d.ts +42 -0
- package/dist/cjs/plugin/noop-plugin.js +12 -0
- package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -1
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +1 -1
- package/dist/cjs/query-builder/select-query-builder.d.ts +3 -1
- package/dist/cjs/query-builder/select-query-builder.js +5 -5
- package/dist/cjs/query-builder/update-query-builder.d.ts +57 -28
- package/dist/cjs/query-builder/update-query-builder.js +7 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +4 -0
- package/dist/cjs/query-creator.d.ts +10 -1
- package/dist/cjs/query-creator.js +18 -0
- package/dist/cjs/raw-builder/raw-builder.d.ts +32 -2
- package/dist/cjs/raw-builder/raw-builder.js +5 -26
- 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-config.d.ts +19 -19
- 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/operation-node/alias-node.d.ts +2 -2
- package/dist/esm/operation-node/alias-node.js +1 -2
- package/dist/esm/operation-node/operation-node-transformer.js +1 -0
- package/dist/esm/operation-node/update-query-node.d.ts +3 -0
- package/dist/esm/operation-node/update-query-node.js +11 -2
- package/dist/esm/parser/reference-parser.js +2 -1
- package/dist/esm/parser/table-parser.js +2 -1
- package/dist/esm/parser/update-set-parser.d.ts +3 -3
- package/dist/esm/parser/with-parser.d.ts +2 -2
- package/dist/esm/plugin/noop-plugin.d.ts +42 -0
- package/dist/esm/plugin/noop-plugin.js +9 -0
- package/dist/esm/query-builder/insert-query-builder.d.ts +1 -1
- package/dist/esm/query-builder/on-conflict-builder.d.ts +1 -1
- package/dist/esm/query-builder/select-query-builder.d.ts +3 -1
- package/dist/esm/query-builder/select-query-builder.js +5 -5
- package/dist/esm/query-builder/update-query-builder.d.ts +57 -28
- package/dist/esm/query-builder/update-query-builder.js +7 -0
- package/dist/esm/query-compiler/default-query-compiler.js +4 -0
- package/dist/esm/query-creator.d.ts +10 -1
- package/dist/esm/query-creator.js +18 -0
- package/dist/esm/raw-builder/raw-builder.d.ts +32 -2
- package/dist/esm/raw-builder/raw-builder.js +5 -26
- 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,15 @@ class PostgresIntrospector {
|
|
|
8
8
|
constructor(db) {
|
|
9
9
|
this.#db = db;
|
|
10
10
|
}
|
|
11
|
-
async
|
|
11
|
+
async getSchemas() {
|
|
12
|
+
let rawSchemas = await this.#db
|
|
13
|
+
.selectFrom('pg_catalog.pg_namespace')
|
|
14
|
+
.select('nspname')
|
|
15
|
+
.castTo()
|
|
16
|
+
.execute();
|
|
17
|
+
return rawSchemas.map((it) => ({ name: it.nspname }));
|
|
18
|
+
}
|
|
19
|
+
async getTables(options = { withInternalKyselyTables: false }) {
|
|
12
20
|
let query = this.#db
|
|
13
21
|
.selectFrom('pg_catalog.pg_attribute as a')
|
|
14
22
|
.innerJoin('pg_catalog.pg_class as c', 'a.attrelid', 'c.oid')
|
|
@@ -28,12 +36,15 @@ class PostgresIntrospector {
|
|
|
28
36
|
.castTo();
|
|
29
37
|
if (!options.withInternalKyselyTables) {
|
|
30
38
|
query = query
|
|
31
|
-
.where('t.tablename', '!=', migrator_js_1.
|
|
32
|
-
.where('t.tablename', '!=', migrator_js_1.
|
|
39
|
+
.where('t.tablename', '!=', migrator_js_1.DEFAULT_MIGRATION_TABLE)
|
|
40
|
+
.where('t.tablename', '!=', migrator_js_1.DEFAULT_MIGRATION_LOCK_TABLE);
|
|
33
41
|
}
|
|
34
42
|
const rawColumns = await query.execute();
|
|
43
|
+
return this.#parseTableMetadata(rawColumns);
|
|
44
|
+
}
|
|
45
|
+
async getMetadata(options) {
|
|
35
46
|
return {
|
|
36
|
-
tables: this
|
|
47
|
+
tables: await this.getTables(options),
|
|
37
48
|
};
|
|
38
49
|
}
|
|
39
50
|
#parseTableMetadata(columns) {
|
|
@@ -1,10 +1,20 @@
|
|
|
1
|
-
import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions } from '../database-introspector.js';
|
|
1
|
+
import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions, SchemaMetadata, TableMetadata } from '../database-introspector.js';
|
|
2
2
|
import { Kysely } from '../../kysely.js';
|
|
3
3
|
export declare class SqliteIntrospector implements DatabaseIntrospector {
|
|
4
4
|
#private;
|
|
5
5
|
constructor(db: Kysely<any>);
|
|
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
|
}
|
|
@@ -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(),
|
|
@@ -9,12 +9,12 @@ export declare type AliasNodeChild = ColumnNode | ReferenceNode | TableNode | Se
|
|
|
9
9
|
export interface AliasNode extends OperationNode {
|
|
10
10
|
readonly kind: 'AliasNode';
|
|
11
11
|
readonly node: AliasNodeChild;
|
|
12
|
-
readonly alias: IdentifierNode;
|
|
12
|
+
readonly alias: IdentifierNode | RawNode;
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
* @internal
|
|
16
16
|
*/
|
|
17
17
|
export declare const AliasNode: Readonly<{
|
|
18
18
|
is(node: OperationNode): node is AliasNode;
|
|
19
|
-
create(node: AliasNodeChild, alias:
|
|
19
|
+
create(node: AliasNodeChild, alias: IdentifierNode | RawNode): AliasNode;
|
|
20
20
|
}>;
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AliasNode = void 0;
|
|
4
4
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
-
const identifier_node_js_1 = require("./identifier-node.js");
|
|
6
5
|
/**
|
|
7
6
|
* @internal
|
|
8
7
|
*/
|
|
@@ -14,7 +13,7 @@ exports.AliasNode = (0, object_utils_js_1.freeze)({
|
|
|
14
13
|
return (0, object_utils_js_1.freeze)({
|
|
15
14
|
kind: 'AliasNode',
|
|
16
15
|
node,
|
|
17
|
-
alias
|
|
16
|
+
alias,
|
|
18
17
|
});
|
|
19
18
|
},
|
|
20
19
|
});
|
|
@@ -333,6 +333,7 @@ class OperationNodeTransformer {
|
|
|
333
333
|
return {
|
|
334
334
|
kind: 'UpdateQueryNode',
|
|
335
335
|
table: this.transformNode(node.table),
|
|
336
|
+
from: this.transformNode(node.from),
|
|
336
337
|
joins: this.transformNodeList(node.joins),
|
|
337
338
|
where: this.transformNode(node.where),
|
|
338
339
|
updates: this.transformNodeList(node.updates),
|
|
@@ -7,10 +7,12 @@ import { ReturningNode } from './returning-node.js';
|
|
|
7
7
|
import { ValueListNode } from './value-list-node.js';
|
|
8
8
|
import { WhereNode } from './where-node.js';
|
|
9
9
|
import { WithNode } from './with-node.js';
|
|
10
|
+
import { FromNode } from './from-node.js';
|
|
10
11
|
export declare type UpdateValuesNode = ValueListNode | PrimitiveValueListNode;
|
|
11
12
|
export interface UpdateQueryNode extends OperationNode {
|
|
12
13
|
readonly kind: 'UpdateQueryNode';
|
|
13
14
|
readonly table: TableExpressionNode;
|
|
15
|
+
readonly from?: FromNode;
|
|
14
16
|
readonly joins?: ReadonlyArray<JoinNode>;
|
|
15
17
|
readonly where?: WhereNode;
|
|
16
18
|
readonly updates?: ReadonlyArray<ColumnUpdateNode>;
|
|
@@ -23,5 +25,6 @@ export interface UpdateQueryNode extends OperationNode {
|
|
|
23
25
|
export declare const UpdateQueryNode: Readonly<{
|
|
24
26
|
is(node: OperationNode): node is UpdateQueryNode;
|
|
25
27
|
create(table: TableExpressionNode, withNode?: WithNode | undefined): UpdateQueryNode;
|
|
28
|
+
cloneWithFromItems(updateQuery: UpdateQueryNode, fromItems: ReadonlyArray<TableExpressionNode>): UpdateQueryNode;
|
|
26
29
|
cloneWithUpdates(updateQuery: UpdateQueryNode, updates: ReadonlyArray<ColumnUpdateNode>): UpdateQueryNode;
|
|
27
30
|
}>;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UpdateQueryNode = void 0;
|
|
4
4
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
+
const from_node_js_1 = require("./from-node.js");
|
|
5
6
|
/**
|
|
6
7
|
* @internal
|
|
7
8
|
*/
|
|
@@ -10,11 +11,19 @@ exports.UpdateQueryNode = (0, object_utils_js_1.freeze)({
|
|
|
10
11
|
return node.kind === 'UpdateQueryNode';
|
|
11
12
|
},
|
|
12
13
|
create(table, withNode) {
|
|
13
|
-
return {
|
|
14
|
+
return (0, object_utils_js_1.freeze)({
|
|
14
15
|
kind: 'UpdateQueryNode',
|
|
15
16
|
table,
|
|
16
17
|
...(withNode && { with: withNode }),
|
|
17
|
-
};
|
|
18
|
+
});
|
|
19
|
+
},
|
|
20
|
+
cloneWithFromItems(updateQuery, fromItems) {
|
|
21
|
+
return (0, object_utils_js_1.freeze)({
|
|
22
|
+
...updateQuery,
|
|
23
|
+
from: updateQuery.from
|
|
24
|
+
? from_node_js_1.FromNode.cloneWithFroms(updateQuery.from, fromItems)
|
|
25
|
+
: from_node_js_1.FromNode.create(fromItems),
|
|
26
|
+
});
|
|
18
27
|
},
|
|
19
28
|
cloneWithUpdates(updateQuery, updates) {
|
|
20
29
|
return (0, object_utils_js_1.freeze)({
|
|
@@ -8,6 +8,7 @@ const table_node_js_1 = require("../operation-node/table-node.js");
|
|
|
8
8
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
9
9
|
const complex_expression_parser_js_1 = require("./complex-expression-parser.js");
|
|
10
10
|
const dynamic_reference_builder_js_1 = require("../dynamic/dynamic-reference-builder.js");
|
|
11
|
+
const identifier_node_js_1 = require("../operation-node/identifier-node.js");
|
|
11
12
|
function parseReferenceExpressionOrList(arg) {
|
|
12
13
|
if ((0, object_utils_js_1.isReadonlyArray)(arg)) {
|
|
13
14
|
return arg.map((it) => parseReferenceExpression(it));
|
|
@@ -50,7 +51,7 @@ function parseAliasedStringReference(ref) {
|
|
|
50
51
|
const ALIAS_SEPARATOR = ' as ';
|
|
51
52
|
if (ref.includes(ALIAS_SEPARATOR)) {
|
|
52
53
|
const [columnRef, alias] = ref.split(ALIAS_SEPARATOR).map(trim);
|
|
53
|
-
return alias_node_js_1.AliasNode.create(parseStringReference(columnRef), alias);
|
|
54
|
+
return alias_node_js_1.AliasNode.create(parseStringReference(columnRef), identifier_node_js_1.IdentifierNode.create(alias));
|
|
54
55
|
}
|
|
55
56
|
else {
|
|
56
57
|
return parseStringReference(ref);
|
|
@@ -5,6 +5,7 @@ const object_utils_js_1 = require("../util/object-utils.js");
|
|
|
5
5
|
const alias_node_js_1 = require("../operation-node/alias-node.js");
|
|
6
6
|
const table_node_js_1 = require("../operation-node/table-node.js");
|
|
7
7
|
const complex_expression_parser_js_1 = require("./complex-expression-parser.js");
|
|
8
|
+
const identifier_node_js_1 = require("../operation-node/identifier-node.js");
|
|
8
9
|
function parseTableExpressionOrList(table) {
|
|
9
10
|
if ((0, object_utils_js_1.isReadonlyArray)(table)) {
|
|
10
11
|
return table.map((it) => parseTableExpression(it));
|
|
@@ -27,7 +28,7 @@ function parseAliasedTable(from) {
|
|
|
27
28
|
const ALIAS_SEPARATOR = ' as ';
|
|
28
29
|
if (from.includes(ALIAS_SEPARATOR)) {
|
|
29
30
|
const [table, alias] = from.split(ALIAS_SEPARATOR).map(trim);
|
|
30
|
-
return alias_node_js_1.AliasNode.create(parseTable(table), alias);
|
|
31
|
+
return alias_node_js_1.AliasNode.create(parseTable(table), identifier_node_js_1.IdentifierNode.create(alias));
|
|
31
32
|
}
|
|
32
33
|
else {
|
|
33
34
|
return parseTable(from);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ColumnUpdateNode } from '../operation-node/column-update-node.js';
|
|
2
2
|
import { UpdateKeys, UpdateType } from '../util/column-type.js';
|
|
3
3
|
import { ValueExpression } from './value-parser.js';
|
|
4
|
-
export declare type MutationObject<DB, TB extends keyof DB> = {
|
|
5
|
-
[C in UpdateKeys<DB[
|
|
4
|
+
export declare type MutationObject<DB, TB extends keyof DB, TM extends keyof DB> = {
|
|
5
|
+
[C in UpdateKeys<DB[TM]>]?: ValueExpression<DB, TB, UpdateType<DB[TM][C]>>;
|
|
6
6
|
};
|
|
7
|
-
export declare function parseUpdateObject(row: MutationObject<any, any>): ReadonlyArray<ColumnUpdateNode>;
|
|
7
|
+
export declare function parseUpdateObject(row: MutationObject<any, any, any>): ReadonlyArray<ColumnUpdateNode>;
|
|
@@ -8,14 +8,14 @@ import { RawBuilder } from '../raw-builder/raw-builder.js';
|
|
|
8
8
|
export declare type CommonTableExpression<DB, CN extends string> = (creator: QueryCreator<DB>) => CommonTableExpressionOutput<DB, CN>;
|
|
9
9
|
export declare type RecursiveCommonTableExpression<DB, CN extends string> = (creator: QueryCreator<DB & Record<ExtractTableFromCommonTableExpressionName<CN>, ExtractRowFromCommonTableExpressionName<CN>>>) => CommonTableExpressionOutput<DB, CN>;
|
|
10
10
|
export declare type QueryCreatorWithCommonTableExpression<DB, CN extends string, CTE> = QueryCreator<DB & Record<ExtractTableFromCommonTableExpressionName<CN>, ExtractRowFromCommonTableExpression<CTE>>>;
|
|
11
|
-
declare type CommonTableExpressionOutput<DB, CN extends string> = SelectQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>> | InsertQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>> | UpdateQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>> | DeleteQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>> | RawBuilder<ExtractRowFromCommonTableExpressionName<CN>>;
|
|
11
|
+
declare type CommonTableExpressionOutput<DB, CN extends string> = SelectQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>> | InsertQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>> | UpdateQueryBuilder<DB, any, any, ExtractRowFromCommonTableExpressionName<CN>> | DeleteQueryBuilder<DB, any, ExtractRowFromCommonTableExpressionName<CN>> | RawBuilder<ExtractRowFromCommonTableExpressionName<CN>>;
|
|
12
12
|
/**
|
|
13
13
|
* Given a common CommonTableExpression CTE extracts the row type from it.
|
|
14
14
|
*
|
|
15
15
|
* For example a CTE `(db) => db.selectFrom('person').select(['id', 'first_name'])`
|
|
16
16
|
* would result in `Pick<Person, 'id' | 'first_name'>`.
|
|
17
17
|
*/
|
|
18
|
-
declare type ExtractRowFromCommonTableExpression<CTE> = CTE extends (creator: QueryCreator<any>) => infer Q ? Q extends SelectQueryBuilder<any, any, infer QO> ? QO : Q extends InsertQueryBuilder<any, any, infer QO> ? QO : Q extends UpdateQueryBuilder<any, any, infer QO> ? QO : Q extends DeleteQueryBuilder<any, any, infer QO> ? QO : Q extends RawBuilder<infer RO> ? RO : never : never;
|
|
18
|
+
declare type ExtractRowFromCommonTableExpression<CTE> = CTE extends (creator: QueryCreator<any>) => infer Q ? Q extends SelectQueryBuilder<any, any, infer QO> ? QO : Q extends InsertQueryBuilder<any, any, infer QO> ? QO : Q extends UpdateQueryBuilder<any, any, any, infer QO> ? QO : Q extends DeleteQueryBuilder<any, any, infer QO> ? QO : Q extends RawBuilder<infer RO> ? RO : never : never;
|
|
19
19
|
/**
|
|
20
20
|
* Extracts 'person' from a string like 'person(id, first_name)'.
|
|
21
21
|
*/
|
|
@@ -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;
|
|
@@ -382,7 +382,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
382
382
|
* .onDuplicateKeyUpdate({ species: 'hamster' })
|
|
383
383
|
* ```
|
|
384
384
|
*/
|
|
385
|
-
onDuplicateKeyUpdate(updates: MutationObject<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
385
|
+
onDuplicateKeyUpdate(updates: MutationObject<DB, TB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
386
386
|
/**
|
|
387
387
|
* Allows you to return data from modified rows.
|
|
388
388
|
*
|
|
@@ -386,7 +386,7 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
|
|
|
386
386
|
* do update set "first_name" = $3
|
|
387
387
|
* ```
|
|
388
388
|
*/
|
|
389
|
-
doUpdateSet(updates: MutationObject<OnConflictDatabase<DB, TB>, OnConflictTables<TB>>): OnConflictUpdateBuilder<OnConflictDatabase<DB, TB>, OnConflictTables<TB>>;
|
|
389
|
+
doUpdateSet(updates: MutationObject<OnConflictDatabase<DB, TB>, OnConflictTables<TB>, OnConflictTables<TB>>): OnConflictUpdateBuilder<OnConflictDatabase<DB, TB>, OnConflictTables<TB>>;
|
|
390
390
|
}
|
|
391
391
|
export interface OnConflictBuilderProps {
|
|
392
392
|
readonly onConflictNode: OnConflictNode;
|
|
@@ -1212,7 +1212,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1212
1212
|
* ### Examples
|
|
1213
1213
|
*
|
|
1214
1214
|
* ```ts
|
|
1215
|
-
* await db.selectFrom('pet')
|
|
1215
|
+
* const pets = await db.selectFrom('pet')
|
|
1216
1216
|
* .selectAll('pet')
|
|
1217
1217
|
* .select(
|
|
1218
1218
|
* (qb) => qb.selectFrom('person')
|
|
@@ -1221,6 +1221,8 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1221
1221
|
* .as('owner_first_name')
|
|
1222
1222
|
* )
|
|
1223
1223
|
* .execute()
|
|
1224
|
+
*
|
|
1225
|
+
* pets[0].owner_first_name
|
|
1224
1226
|
* ```
|
|
1225
1227
|
*/
|
|
1226
1228
|
as<A extends string>(alias: A): AliasedQueryBuilder<DB, TB, O, A>;
|