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
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/// <reference types="./migrator.d.ts" />
|
|
2
|
+
import { NoopPlugin } from '../plugin/noop-plugin.js';
|
|
3
|
+
import { WithSchemaPlugin } from '../plugin/with-schema/with-schema-plugin.js';
|
|
2
4
|
import { freeze, getLast } from '../util/object-utils.js';
|
|
3
|
-
export const
|
|
4
|
-
export const
|
|
5
|
+
export const DEFAULT_MIGRATION_TABLE = 'kysely_migration';
|
|
6
|
+
export const DEFAULT_MIGRATION_LOCK_TABLE = 'kysely_migration_lock';
|
|
5
7
|
export const MIGRATION_LOCK_ID = 'migration_lock';
|
|
6
8
|
export const NO_MIGRATIONS = freeze({ __noMigrations__: true });
|
|
7
9
|
/**
|
|
@@ -35,9 +37,10 @@ export class Migrator {
|
|
|
35
37
|
* The returned array is sorted by migration name.
|
|
36
38
|
*/
|
|
37
39
|
async getMigrations() {
|
|
38
|
-
const executedMigrations = (await this.#doesTableExists(
|
|
40
|
+
const executedMigrations = (await this.#doesTableExists(this.#migrationTable))
|
|
39
41
|
? await this.#props.db
|
|
40
|
-
.
|
|
42
|
+
.withPlugin(this.#schemaPlugin)
|
|
43
|
+
.selectFrom(this.#migrationTable)
|
|
41
44
|
.select(['name', 'timestamp'])
|
|
42
45
|
.execute()
|
|
43
46
|
: [];
|
|
@@ -182,16 +185,61 @@ export class Migrator {
|
|
|
182
185
|
return { error };
|
|
183
186
|
}
|
|
184
187
|
}
|
|
188
|
+
get #migrationTableSchema() {
|
|
189
|
+
return this.#props.migrationTableSchema;
|
|
190
|
+
}
|
|
191
|
+
get #migrationTable() {
|
|
192
|
+
return this.#props.migrationTableName ?? DEFAULT_MIGRATION_TABLE;
|
|
193
|
+
}
|
|
194
|
+
get #migrationLockTable() {
|
|
195
|
+
return this.#props.migrationLockTableName ?? DEFAULT_MIGRATION_LOCK_TABLE;
|
|
196
|
+
}
|
|
197
|
+
get #schemaPlugin() {
|
|
198
|
+
if (this.#migrationTableSchema) {
|
|
199
|
+
return new WithSchemaPlugin(this.#migrationTableSchema);
|
|
200
|
+
}
|
|
201
|
+
return new NoopPlugin();
|
|
202
|
+
}
|
|
185
203
|
async #ensureMigrationTablesExists() {
|
|
204
|
+
await this.#ensureMigrationTableSchemaExists();
|
|
186
205
|
await this.#ensureMigrationTableExists();
|
|
187
206
|
await this.#ensureMigrationLockTableExists();
|
|
188
207
|
await this.#ensureLockRowExists();
|
|
189
208
|
}
|
|
209
|
+
async #ensureMigrationTableSchemaExists() {
|
|
210
|
+
if (!this.#migrationTableSchema) {
|
|
211
|
+
// Use default schema. Nothing to do.
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (!(await this.#doesSchemaExists())) {
|
|
215
|
+
try {
|
|
216
|
+
await this.#props.db.schema
|
|
217
|
+
.createSchema(this.#migrationTableSchema)
|
|
218
|
+
.ifNotExists()
|
|
219
|
+
.execute();
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
// At least on PostgreSQL, `if not exists` doesn't guarantee the `create schema`
|
|
223
|
+
// query doesn't throw if the schema already exits. That's why we check if
|
|
224
|
+
// the schema exist here and ignore the error if it does.
|
|
225
|
+
if (!(await this.#doesSchemaExists())) {
|
|
226
|
+
throw error;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
190
231
|
async #ensureMigrationTableExists() {
|
|
191
|
-
if (!(await this.#doesTableExists(
|
|
232
|
+
if (!(await this.#doesTableExists(this.#migrationTable))) {
|
|
192
233
|
try {
|
|
234
|
+
if (this.#migrationTableSchema) {
|
|
235
|
+
await this.#props.db.schema
|
|
236
|
+
.createSchema(this.#migrationTableSchema)
|
|
237
|
+
.ifNotExists()
|
|
238
|
+
.execute();
|
|
239
|
+
}
|
|
193
240
|
await this.#props.db.schema
|
|
194
|
-
.
|
|
241
|
+
.withPlugin(this.#schemaPlugin)
|
|
242
|
+
.createTable(this.#migrationTable)
|
|
195
243
|
.ifNotExists()
|
|
196
244
|
.addColumn('name', 'varchar(255)', (col) => col.notNull().primaryKey())
|
|
197
245
|
// The migration run time as ISO string. This is not a real date type as we
|
|
@@ -203,17 +251,18 @@ export class Migrator {
|
|
|
203
251
|
// At least on PostgreSQL, `if not exists` doesn't guarantee the `create table`
|
|
204
252
|
// query doesn't throw if the table already exits. That's why we check if
|
|
205
253
|
// the table exist here and ignore the error if it does.
|
|
206
|
-
if (!(await this.#doesTableExists(
|
|
254
|
+
if (!(await this.#doesTableExists(this.#migrationTable))) {
|
|
207
255
|
throw error;
|
|
208
256
|
}
|
|
209
257
|
}
|
|
210
258
|
}
|
|
211
259
|
}
|
|
212
260
|
async #ensureMigrationLockTableExists() {
|
|
213
|
-
if (!(await this.#doesTableExists(
|
|
261
|
+
if (!(await this.#doesTableExists(this.#migrationLockTable))) {
|
|
214
262
|
try {
|
|
215
263
|
await this.#props.db.schema
|
|
216
|
-
.
|
|
264
|
+
.withPlugin(this.#schemaPlugin)
|
|
265
|
+
.createTable(this.#migrationLockTable)
|
|
217
266
|
.ifNotExists()
|
|
218
267
|
.addColumn('id', 'varchar(255)', (col) => col.notNull().primaryKey())
|
|
219
268
|
.addColumn('is_locked', 'integer', (col) => col.notNull().defaultTo(0))
|
|
@@ -223,7 +272,7 @@ export class Migrator {
|
|
|
223
272
|
// At least on PostgreSQL, `if not exists` doesn't guarantee the `create table`
|
|
224
273
|
// query doesn't throw if the table already exits. That's why we check if
|
|
225
274
|
// the table exist here and ignore the error if it does.
|
|
226
|
-
if (!(await this.#doesTableExists(
|
|
275
|
+
if (!(await this.#doesTableExists(this.#migrationLockTable))) {
|
|
227
276
|
throw error;
|
|
228
277
|
}
|
|
229
278
|
}
|
|
@@ -233,7 +282,8 @@ export class Migrator {
|
|
|
233
282
|
if (!(await this.#doesLockRowExists())) {
|
|
234
283
|
try {
|
|
235
284
|
await this.#props.db
|
|
236
|
-
.
|
|
285
|
+
.withPlugin(this.#schemaPlugin)
|
|
286
|
+
.insertInto(this.#migrationLockTable)
|
|
237
287
|
.values({ id: MIGRATION_LOCK_ID, is_locked: 0 })
|
|
238
288
|
.execute();
|
|
239
289
|
}
|
|
@@ -244,15 +294,21 @@ export class Migrator {
|
|
|
244
294
|
}
|
|
245
295
|
}
|
|
246
296
|
}
|
|
297
|
+
async #doesSchemaExists() {
|
|
298
|
+
const schemas = await this.#props.db.introspection.getSchemas();
|
|
299
|
+
return schemas.some((it) => it.name === this.#migrationTableSchema);
|
|
300
|
+
}
|
|
247
301
|
async #doesTableExists(tableName) {
|
|
248
|
-
const
|
|
302
|
+
const schema = this.#migrationTableSchema;
|
|
303
|
+
const tables = await this.#props.db.introspection.getTables({
|
|
249
304
|
withInternalKyselyTables: true,
|
|
250
305
|
});
|
|
251
|
-
return
|
|
306
|
+
return tables.some((it) => it.name === tableName && (!schema || it.schema === schema));
|
|
252
307
|
}
|
|
253
308
|
async #doesLockRowExists() {
|
|
254
309
|
const lockRow = await this.#props.db
|
|
255
|
-
.
|
|
310
|
+
.withPlugin(this.#schemaPlugin)
|
|
311
|
+
.selectFrom(this.#migrationLockTable)
|
|
256
312
|
.where('id', '=', MIGRATION_LOCK_ID)
|
|
257
313
|
.select('id')
|
|
258
314
|
.executeTakeFirst();
|
|
@@ -310,7 +366,8 @@ export class Migrator {
|
|
|
310
366
|
}
|
|
311
367
|
async #getExecutedMigrations(db) {
|
|
312
368
|
const executedMigrations = await db
|
|
313
|
-
.
|
|
369
|
+
.withPlugin(this.#schemaPlugin)
|
|
370
|
+
.selectFrom(this.#migrationTable)
|
|
314
371
|
.select('name')
|
|
315
372
|
.orderBy('name')
|
|
316
373
|
.execute();
|
|
@@ -346,7 +403,8 @@ export class Migrator {
|
|
|
346
403
|
if (migration.down) {
|
|
347
404
|
await migration.down(db);
|
|
348
405
|
await db
|
|
349
|
-
.
|
|
406
|
+
.withPlugin(this.#schemaPlugin)
|
|
407
|
+
.deleteFrom(this.#migrationTable)
|
|
350
408
|
.where('name', '=', migration.name)
|
|
351
409
|
.execute();
|
|
352
410
|
results[i] = {
|
|
@@ -384,7 +442,8 @@ export class Migrator {
|
|
|
384
442
|
try {
|
|
385
443
|
await migration.up(db);
|
|
386
444
|
await db
|
|
387
|
-
.
|
|
445
|
+
.withPlugin(this.#schemaPlugin)
|
|
446
|
+
.insertInto(this.#migrationTable)
|
|
388
447
|
.values({
|
|
389
448
|
name: migration.name,
|
|
390
449
|
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
|
}>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/// <reference types="./alias-node.d.ts" />
|
|
2
2
|
import { freeze } from '../util/object-utils.js';
|
|
3
|
-
import { IdentifierNode } from './identifier-node.js';
|
|
4
3
|
/**
|
|
5
4
|
* @internal
|
|
6
5
|
*/
|
|
@@ -12,7 +11,7 @@ export const AliasNode = freeze({
|
|
|
12
11
|
return freeze({
|
|
13
12
|
kind: 'AliasNode',
|
|
14
13
|
node,
|
|
15
|
-
alias
|
|
14
|
+
alias,
|
|
16
15
|
});
|
|
17
16
|
},
|
|
18
17
|
});
|
|
@@ -331,6 +331,7 @@ export class OperationNodeTransformer {
|
|
|
331
331
|
return {
|
|
332
332
|
kind: 'UpdateQueryNode',
|
|
333
333
|
table: this.transformNode(node.table),
|
|
334
|
+
from: this.transformNode(node.from),
|
|
334
335
|
joins: this.transformNodeList(node.joins),
|
|
335
336
|
where: this.transformNode(node.where),
|
|
336
337
|
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
|
}>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="./update-query-node.d.ts" />
|
|
2
2
|
import { freeze } from '../util/object-utils.js';
|
|
3
|
+
import { FromNode } from './from-node.js';
|
|
3
4
|
/**
|
|
4
5
|
* @internal
|
|
5
6
|
*/
|
|
@@ -8,11 +9,19 @@ export const UpdateQueryNode = freeze({
|
|
|
8
9
|
return node.kind === 'UpdateQueryNode';
|
|
9
10
|
},
|
|
10
11
|
create(table, withNode) {
|
|
11
|
-
return {
|
|
12
|
+
return freeze({
|
|
12
13
|
kind: 'UpdateQueryNode',
|
|
13
14
|
table,
|
|
14
15
|
...(withNode && { with: withNode }),
|
|
15
|
-
};
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
cloneWithFromItems(updateQuery, fromItems) {
|
|
19
|
+
return freeze({
|
|
20
|
+
...updateQuery,
|
|
21
|
+
from: updateQuery.from
|
|
22
|
+
? FromNode.cloneWithFroms(updateQuery.from, fromItems)
|
|
23
|
+
: FromNode.create(fromItems),
|
|
24
|
+
});
|
|
16
25
|
},
|
|
17
26
|
cloneWithUpdates(updateQuery, updates) {
|
|
18
27
|
return freeze({
|
|
@@ -6,6 +6,7 @@ import { TableNode } from '../operation-node/table-node.js';
|
|
|
6
6
|
import { isReadonlyArray, isString } from '../util/object-utils.js';
|
|
7
7
|
import { parseComplexExpression, } from './complex-expression-parser.js';
|
|
8
8
|
import { isDynamicReferenceBuilder, } from '../dynamic/dynamic-reference-builder.js';
|
|
9
|
+
import { IdentifierNode } from '../operation-node/identifier-node.js';
|
|
9
10
|
export function parseReferenceExpressionOrList(arg) {
|
|
10
11
|
if (isReadonlyArray(arg)) {
|
|
11
12
|
return arg.map((it) => parseReferenceExpression(it));
|
|
@@ -45,7 +46,7 @@ export function parseAliasedStringReference(ref) {
|
|
|
45
46
|
const ALIAS_SEPARATOR = ' as ';
|
|
46
47
|
if (ref.includes(ALIAS_SEPARATOR)) {
|
|
47
48
|
const [columnRef, alias] = ref.split(ALIAS_SEPARATOR).map(trim);
|
|
48
|
-
return AliasNode.create(parseStringReference(columnRef), alias);
|
|
49
|
+
return AliasNode.create(parseStringReference(columnRef), IdentifierNode.create(alias));
|
|
49
50
|
}
|
|
50
51
|
else {
|
|
51
52
|
return parseStringReference(ref);
|
|
@@ -3,6 +3,7 @@ import { isReadonlyArray, isString } from '../util/object-utils.js';
|
|
|
3
3
|
import { AliasNode } from '../operation-node/alias-node.js';
|
|
4
4
|
import { TableNode } from '../operation-node/table-node.js';
|
|
5
5
|
import { parseAliasedComplexExpression, } from './complex-expression-parser.js';
|
|
6
|
+
import { IdentifierNode } from '../operation-node/identifier-node.js';
|
|
6
7
|
export function parseTableExpressionOrList(table) {
|
|
7
8
|
if (isReadonlyArray(table)) {
|
|
8
9
|
return table.map((it) => parseTableExpression(it));
|
|
@@ -23,7 +24,7 @@ export function parseAliasedTable(from) {
|
|
|
23
24
|
const ALIAS_SEPARATOR = ' as ';
|
|
24
25
|
if (from.includes(ALIAS_SEPARATOR)) {
|
|
25
26
|
const [table, alias] = from.split(ALIAS_SEPARATOR).map(trim);
|
|
26
|
-
return AliasNode.create(parseTable(table), alias);
|
|
27
|
+
return AliasNode.create(parseTable(table), IdentifierNode.create(alias));
|
|
27
28
|
}
|
|
28
29
|
else {
|
|
29
30
|
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
|
+
}
|
|
@@ -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>;
|
|
@@ -13,6 +13,7 @@ import { freeze } from '../util/object-utils.js';
|
|
|
13
13
|
import { parseGroupBy } from '../parser/group-by-parser.js';
|
|
14
14
|
import { parseUnion } from '../parser/union-parser.js';
|
|
15
15
|
import { NoResultError } from './no-result-error.js';
|
|
16
|
+
import { IdentifierNode } from '../operation-node/identifier-node.js';
|
|
16
17
|
export class SelectQueryBuilder {
|
|
17
18
|
#props;
|
|
18
19
|
constructor(props) {
|
|
@@ -513,7 +514,7 @@ export class SelectQueryBuilder {
|
|
|
513
514
|
* ### Examples
|
|
514
515
|
*
|
|
515
516
|
* ```ts
|
|
516
|
-
* await db.selectFrom('pet')
|
|
517
|
+
* const pets = await db.selectFrom('pet')
|
|
517
518
|
* .selectAll('pet')
|
|
518
519
|
* .select(
|
|
519
520
|
* (qb) => qb.selectFrom('person')
|
|
@@ -522,6 +523,8 @@ export class SelectQueryBuilder {
|
|
|
522
523
|
* .as('owner_first_name')
|
|
523
524
|
* )
|
|
524
525
|
* .execute()
|
|
526
|
+
*
|
|
527
|
+
* pets[0].owner_first_name
|
|
525
528
|
* ```
|
|
526
529
|
*/
|
|
527
530
|
as(alias) {
|
|
@@ -610,9 +613,6 @@ export class AliasedQueryBuilder {
|
|
|
610
613
|
}
|
|
611
614
|
toOperationNode() {
|
|
612
615
|
const node = this.#queryBuilder.toOperationNode();
|
|
613
|
-
|
|
614
|
-
return AliasNode.create(node, this.#alias);
|
|
615
|
-
}
|
|
616
|
-
throw new Error('only select queries can be aliased');
|
|
616
|
+
return AliasNode.create(node, IdentifierNode.create(this.#alias));
|
|
617
617
|
}
|
|
618
618
|
}
|