kysely 0.14.1 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -32
- package/dist/cjs/dialect/mysql/mysql-introspector.js +3 -3
- package/dist/cjs/dialect/postgres/postgres-introspector.js +3 -3
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +3 -3
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/kysely.d.ts +7 -39
- package/dist/cjs/kysely.js +8 -40
- package/dist/cjs/migration/file-migration-provider.d.ts +52 -0
- package/dist/cjs/migration/file-migration-provider.js +52 -0
- package/dist/cjs/migration/{migration.d.ts → migrator.d.ts} +159 -134
- package/dist/cjs/migration/{migration.js → migrator.js} +166 -57
- package/dist/cjs/parser/insert-values-parser.d.ts +4 -4
- package/dist/cjs/parser/insert-values-parser.js +2 -3
- package/dist/cjs/parser/reference-parser.d.ts +2 -1
- package/dist/cjs/parser/select-parser.d.ts +9 -3
- package/dist/cjs/parser/update-set-parser.d.ts +2 -1
- package/dist/cjs/query-builder/delete-query-builder.d.ts +0 -3
- package/dist/cjs/query-builder/insert-query-builder.d.ts +3 -20
- package/dist/cjs/query-builder/insert-query-builder.js +2 -7
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +2 -2
- package/dist/cjs/query-builder/on-conflict-builder.js +2 -2
- package/dist/cjs/query-builder/returning-interface.d.ts +0 -3
- package/dist/cjs/query-builder/update-query-builder.d.ts +0 -3
- package/dist/cjs/query-creator.d.ts +0 -2
- package/dist/cjs/query-creator.js +0 -2
- package/dist/cjs/util/column-type.d.ts +165 -0
- package/dist/cjs/util/column-type.js +2 -0
- package/dist/cjs/util/private-symbols.d.ts +5 -0
- package/dist/cjs/util/private-symbols.js +8 -0
- package/dist/cjs/util/type-utils.d.ts +0 -40
- package/dist/esm/dialect/mysql/mysql-introspector.js +1 -1
- package/dist/esm/dialect/postgres/postgres-introspector.js +1 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +1 -1
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/kysely.d.ts +7 -39
- package/dist/esm/kysely.js +8 -40
- package/dist/esm/migration/file-migration-provider.d.ts +52 -0
- package/dist/esm/migration/file-migration-provider.js +48 -0
- package/dist/esm/migration/{migration.d.ts → migrator.d.ts} +159 -134
- package/dist/esm/migration/{migration.js → migrator.js} +165 -56
- package/dist/esm/parser/insert-values-parser.d.ts +4 -4
- package/dist/esm/parser/insert-values-parser.js +2 -3
- package/dist/esm/parser/reference-parser.d.ts +2 -1
- package/dist/esm/parser/select-parser.d.ts +9 -3
- package/dist/esm/parser/update-set-parser.d.ts +2 -1
- package/dist/esm/query-builder/delete-query-builder.d.ts +0 -3
- package/dist/esm/query-builder/insert-query-builder.d.ts +3 -20
- package/dist/esm/query-builder/insert-query-builder.js +1 -6
- package/dist/esm/query-builder/on-conflict-builder.d.ts +2 -2
- package/dist/esm/query-builder/on-conflict-builder.js +2 -2
- package/dist/esm/query-builder/returning-interface.d.ts +0 -3
- package/dist/esm/query-builder/update-query-builder.d.ts +0 -3
- package/dist/esm/query-creator.d.ts +0 -2
- package/dist/esm/query-creator.js +0 -2
- package/dist/esm/util/column-type.d.ts +165 -0
- package/dist/esm/util/column-type.js +1 -0
- package/dist/esm/util/private-symbols.d.ts +5 -0
- package/dist/esm/util/private-symbols.js +5 -0
- package/dist/esm/util/type-utils.d.ts +0 -40
- package/package.json +1 -1
- package/dist/cjs/util/generated-placeholder.d.ts +0 -3
- package/dist/cjs/util/generated-placeholder.js +0 -11
- package/dist/esm/util/generated-placeholder.d.ts +0 -3
- package/dist/esm/util/generated-placeholder.js +0 -7
|
@@ -1,22 +1,122 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { freeze, getLast, isFunction, isObject, isString, } from '../util/object-utils.js';
|
|
1
|
+
import { freeze, getLast } from '../util/object-utils.js';
|
|
2
|
+
import { PRIVATE_ADAPTER } from '../util/private-symbols.js';
|
|
4
3
|
export const MIGRATION_TABLE = 'kysely_migration';
|
|
5
4
|
export const MIGRATION_LOCK_TABLE = 'kysely_migration_lock';
|
|
6
5
|
export const MIGRATION_LOCK_ID = 'migration_lock';
|
|
7
6
|
export const NO_MIGRATIONS = freeze({ __noMigrations__: true });
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
/**
|
|
8
|
+
* A class for running migrations.
|
|
9
|
+
*
|
|
10
|
+
* ### Example
|
|
11
|
+
*
|
|
12
|
+
* This example uses the {@link FileMigrationProvider} that reads migrations
|
|
13
|
+
* files from a single folder. You can easily implement your own
|
|
14
|
+
* {@link MigrationProvider} if you want to provide migrations some
|
|
15
|
+
* other way.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const migrator = new Migrator({
|
|
19
|
+
* db,
|
|
20
|
+
* // The constructor signature of FileMigrationProvider is a
|
|
21
|
+
* // bit weird to keep Kysely independent of node.js or any
|
|
22
|
+
* // other environment.
|
|
23
|
+
* provider: new FileMigrationProvider(
|
|
24
|
+
* fs.readdir,
|
|
25
|
+
* path.join,
|
|
26
|
+
* // Path to the folder that contains all your migrations.
|
|
27
|
+
* 'migrations'
|
|
28
|
+
* )
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export class Migrator {
|
|
33
|
+
#props;
|
|
34
|
+
constructor(props) {
|
|
35
|
+
this.#props = freeze(props);
|
|
14
36
|
}
|
|
15
|
-
|
|
16
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Runs all migrations that have not yet been run.
|
|
39
|
+
*
|
|
40
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
41
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
42
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
43
|
+
* were executed and which failed. See the examples below.
|
|
44
|
+
*
|
|
45
|
+
* This method goes through all possible migrations provided by the provider and runs the
|
|
46
|
+
* ones whose names come alphabetically after the last migration that has been run. If the
|
|
47
|
+
* list of executed migrations doesn't match the beginning of the list of possible migrations
|
|
48
|
+
* an error is thrown.
|
|
49
|
+
*
|
|
50
|
+
* ### Examples
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* import * as path from 'path'
|
|
54
|
+
* import * as fs from 'fs/promises'
|
|
55
|
+
*
|
|
56
|
+
* const db = new Kysely<Database>({
|
|
57
|
+
* dialect: new PostgresDialect({
|
|
58
|
+
* host: 'localhost',
|
|
59
|
+
* database: 'kysely_test',
|
|
60
|
+
* }),
|
|
61
|
+
* })
|
|
62
|
+
*
|
|
63
|
+
* const migrator = new Migrator({
|
|
64
|
+
* db,
|
|
65
|
+
* // The constructor signature of FileMigrationProvider is a
|
|
66
|
+
* // bit weird to keep Kysely independent of node.js or any
|
|
67
|
+
* // other environment.
|
|
68
|
+
* provider: new FileMigrationProvider(
|
|
69
|
+
* fs.readdir,
|
|
70
|
+
* path.join,
|
|
71
|
+
* // Path to the folder that contains all your migrations.
|
|
72
|
+
* 'migrations'
|
|
73
|
+
* )
|
|
74
|
+
* })
|
|
75
|
+
*
|
|
76
|
+
* const { error, results } = await migrator.migrateToLatest()
|
|
77
|
+
*
|
|
78
|
+
* results?.forEach((it) => {
|
|
79
|
+
* if (it.status === 'Success') {
|
|
80
|
+
* console.log(`migration "${it.migrationName}" was executed successfully`)
|
|
81
|
+
* } else if (it.status === 'Error') {
|
|
82
|
+
* console.error(`failed to execute migration "${it.migrationName}"`)
|
|
83
|
+
* }
|
|
84
|
+
* })
|
|
85
|
+
*
|
|
86
|
+
* if (error) {
|
|
87
|
+
* console.error('failed to run `migrateToLatest`')
|
|
88
|
+
* console.error(error)
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
async migrateToLatest() {
|
|
93
|
+
return this.#migrate(({ migrations }) => migrations.length - 1);
|
|
17
94
|
}
|
|
18
|
-
|
|
19
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Migrate up/down to a specific migration.
|
|
97
|
+
*
|
|
98
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
99
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
100
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
101
|
+
* were executed and which failed.
|
|
102
|
+
*
|
|
103
|
+
* ### Examples
|
|
104
|
+
*
|
|
105
|
+
* ```ts
|
|
106
|
+
* await migrator.migrateTo('some_migration')
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* If you specify the name of the first migration, this method migrates
|
|
110
|
+
* down to the first migration, but doesn't run the `down` method of
|
|
111
|
+
* the first migration. In case you want to migrate all the way down,
|
|
112
|
+
* you can use a special constant `NO_MIGRATIONS`:
|
|
113
|
+
*
|
|
114
|
+
* ```ts
|
|
115
|
+
* await migrator.migrateTo(NO_MIGRATIONS)
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
async migrateTo(targetMigrationName) {
|
|
119
|
+
return this.#migrate(({ migrations }) => {
|
|
20
120
|
if (targetMigrationName === NO_MIGRATIONS) {
|
|
21
121
|
return -1;
|
|
22
122
|
}
|
|
@@ -27,16 +127,44 @@ export class MigrationModule {
|
|
|
27
127
|
return index;
|
|
28
128
|
});
|
|
29
129
|
}
|
|
30
|
-
|
|
31
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Migrate one step up.
|
|
132
|
+
*
|
|
133
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
134
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
135
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
136
|
+
* were executed and which failed.
|
|
137
|
+
*
|
|
138
|
+
* ### Examples
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* await migrator.migrateUp()
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
async migrateUp() {
|
|
145
|
+
return this.#migrate(({ currentIndex, migrations }) => Math.min(currentIndex + 1, migrations.length - 1));
|
|
32
146
|
}
|
|
33
|
-
|
|
34
|
-
|
|
147
|
+
/**
|
|
148
|
+
* Migrate one step down.
|
|
149
|
+
*
|
|
150
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
151
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
152
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
153
|
+
* were executed and which failed.
|
|
154
|
+
*
|
|
155
|
+
* ### Examples
|
|
156
|
+
*
|
|
157
|
+
* ```ts
|
|
158
|
+
* await migrator.migrateDown()
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
async migrateDown() {
|
|
162
|
+
return this.#migrate(({ currentIndex }) => Math.max(currentIndex - 1, -1));
|
|
35
163
|
}
|
|
36
|
-
async #migrate(
|
|
164
|
+
async #migrate(getTargetMigrationIndex) {
|
|
37
165
|
try {
|
|
38
166
|
await this.#ensureMigrationTablesExists();
|
|
39
|
-
return await this.#runMigrations(
|
|
167
|
+
return await this.#runMigrations(getTargetMigrationIndex);
|
|
40
168
|
}
|
|
41
169
|
catch (error) {
|
|
42
170
|
if (error instanceof MigrationResultSetError) {
|
|
@@ -53,7 +181,7 @@ export class MigrationModule {
|
|
|
53
181
|
async #ensureMigrationTableExists() {
|
|
54
182
|
if (!(await this.#doesTableExists(MIGRATION_TABLE))) {
|
|
55
183
|
try {
|
|
56
|
-
await this.#db.schema
|
|
184
|
+
await this.#props.db.schema
|
|
57
185
|
.createTable(MIGRATION_TABLE)
|
|
58
186
|
.ifNotExists()
|
|
59
187
|
.addColumn('name', 'varchar(255)', (col) => col.notNull().primaryKey())
|
|
@@ -75,7 +203,7 @@ export class MigrationModule {
|
|
|
75
203
|
async #ensureMigrationLockTableExists() {
|
|
76
204
|
if (!(await this.#doesTableExists(MIGRATION_LOCK_TABLE))) {
|
|
77
205
|
try {
|
|
78
|
-
await this.#db.schema
|
|
206
|
+
await this.#props.db.schema
|
|
79
207
|
.createTable(MIGRATION_LOCK_TABLE)
|
|
80
208
|
.ifNotExists()
|
|
81
209
|
.addColumn('id', 'varchar(255)', (col) => col.notNull().primaryKey())
|
|
@@ -95,7 +223,7 @@ export class MigrationModule {
|
|
|
95
223
|
async #ensureLockRowExists() {
|
|
96
224
|
if (!(await this.#doesLockRowExists())) {
|
|
97
225
|
try {
|
|
98
|
-
await this.#db
|
|
226
|
+
await this.#props.db
|
|
99
227
|
.insertInto(MIGRATION_LOCK_TABLE)
|
|
100
228
|
.values({ id: MIGRATION_LOCK_ID })
|
|
101
229
|
.execute();
|
|
@@ -108,28 +236,28 @@ export class MigrationModule {
|
|
|
108
236
|
}
|
|
109
237
|
}
|
|
110
238
|
async #doesTableExists(tableName) {
|
|
111
|
-
const metadata = await this.#db.introspection.getMetadata({
|
|
239
|
+
const metadata = await this.#props.db.introspection.getMetadata({
|
|
112
240
|
withInternalKyselyTables: true,
|
|
113
241
|
});
|
|
114
242
|
return !!metadata.tables.find((it) => it.name === tableName);
|
|
115
243
|
}
|
|
116
244
|
async #doesLockRowExists() {
|
|
117
|
-
const lockRow = await this.#db
|
|
245
|
+
const lockRow = await this.#props.db
|
|
118
246
|
.selectFrom(MIGRATION_LOCK_TABLE)
|
|
119
247
|
.where('id', '=', MIGRATION_LOCK_ID)
|
|
120
248
|
.select('id')
|
|
121
249
|
.executeTakeFirst();
|
|
122
250
|
return !!lockRow;
|
|
123
251
|
}
|
|
124
|
-
async #runMigrations(
|
|
252
|
+
async #runMigrations(getTargetMigrationIndex) {
|
|
125
253
|
const run = async (db) => {
|
|
126
254
|
try {
|
|
127
|
-
await this.#
|
|
128
|
-
const state = await this.#getState(db
|
|
255
|
+
await this.#props.db[PRIVATE_ADAPTER].acquireMigrationLock(db);
|
|
256
|
+
const state = await this.#getState(db);
|
|
129
257
|
if (state.migrations.length === 0) {
|
|
130
258
|
return { results: [] };
|
|
131
259
|
}
|
|
132
|
-
const targetIndex =
|
|
260
|
+
const targetIndex = getTargetMigrationIndex(state);
|
|
133
261
|
if (targetIndex === undefined) {
|
|
134
262
|
return { results: [] };
|
|
135
263
|
}
|
|
@@ -142,18 +270,18 @@ export class MigrationModule {
|
|
|
142
270
|
return { results: [] };
|
|
143
271
|
}
|
|
144
272
|
finally {
|
|
145
|
-
await this.#
|
|
273
|
+
await this.#props.db[PRIVATE_ADAPTER].releaseMigrationLock(db);
|
|
146
274
|
}
|
|
147
275
|
};
|
|
148
|
-
if (this.#
|
|
149
|
-
return this.#db.transaction().execute(run);
|
|
276
|
+
if (this.#props.db[PRIVATE_ADAPTER].supportsTransactionalDdl) {
|
|
277
|
+
return this.#props.db.transaction().execute(run);
|
|
150
278
|
}
|
|
151
279
|
else {
|
|
152
|
-
return this.#db.connection().execute(run);
|
|
280
|
+
return this.#props.db.connection().execute(run);
|
|
153
281
|
}
|
|
154
282
|
}
|
|
155
|
-
async #getState(db
|
|
156
|
-
const migrations = await this.#resolveMigrations(
|
|
283
|
+
async #getState(db) {
|
|
284
|
+
const migrations = await this.#resolveMigrations();
|
|
157
285
|
const executedMigrations = await this.#getExecutedMigrations(db);
|
|
158
286
|
this.#ensureMigrationsNotCorrupted(migrations, executedMigrations);
|
|
159
287
|
return freeze({
|
|
@@ -161,10 +289,8 @@ export class MigrationModule {
|
|
|
161
289
|
currentIndex: migrations.findIndex((it) => it.name === getLast(executedMigrations)),
|
|
162
290
|
});
|
|
163
291
|
}
|
|
164
|
-
async #resolveMigrations(
|
|
165
|
-
const allMigrations =
|
|
166
|
-
? await this.#readMigrationsFromFolder(migrationsOrFolderPath)
|
|
167
|
-
: migrationsOrFolderPath;
|
|
292
|
+
async #resolveMigrations() {
|
|
293
|
+
const allMigrations = await this.#props.provider.getMigrations();
|
|
168
294
|
return Object.keys(allMigrations)
|
|
169
295
|
.sort()
|
|
170
296
|
.map((name) => ({
|
|
@@ -172,20 +298,6 @@ export class MigrationModule {
|
|
|
172
298
|
name,
|
|
173
299
|
}));
|
|
174
300
|
}
|
|
175
|
-
async #readMigrationsFromFolder(migrationsFolderPath) {
|
|
176
|
-
const files = await fs.readdir(migrationsFolderPath);
|
|
177
|
-
const migrations = {};
|
|
178
|
-
for (const file of files) {
|
|
179
|
-
if ((file.endsWith('.js') || file.endsWith('.ts')) &&
|
|
180
|
-
!file.endsWith('.d.ts')) {
|
|
181
|
-
const migration = await import(path.join(migrationsFolderPath, file));
|
|
182
|
-
if (isMigration(migration)) {
|
|
183
|
-
migrations[file.substring(0, file.length - 3)] = migration;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return migrations;
|
|
188
|
-
}
|
|
189
301
|
async #getExecutedMigrations(db) {
|
|
190
302
|
const executedMigrations = await db
|
|
191
303
|
.selectFrom(MIGRATION_TABLE)
|
|
@@ -201,7 +313,7 @@ export class MigrationModule {
|
|
|
201
313
|
}
|
|
202
314
|
}
|
|
203
315
|
// Now we know all executed migrations exist in the `migrations` list.
|
|
204
|
-
// Next we need to make sure that the executed
|
|
316
|
+
// Next we need to make sure that the executed migratiosns are the first
|
|
205
317
|
// ones in the migration list.
|
|
206
318
|
for (let i = 0; i < executedMigrations.length; ++i) {
|
|
207
319
|
if (migrations[i].name !== executedMigrations[i]) {
|
|
@@ -299,6 +411,3 @@ class MigrationResultSetError extends Error {
|
|
|
299
411
|
return this.#resultSet;
|
|
300
412
|
}
|
|
301
413
|
}
|
|
302
|
-
function isMigration(obj) {
|
|
303
|
-
return isObject(obj) && isFunction(obj.up);
|
|
304
|
-
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { ColumnNode } from '../operation-node/column-node.js';
|
|
2
|
-
import { GeneratedPlaceholder } from '../util/type-utils.js';
|
|
3
2
|
import { ParseContext } from './parse-context.js';
|
|
4
3
|
import { ValueExpression } from './value-parser.js';
|
|
5
4
|
import { ValuesNode } from '../operation-node/values-node.js';
|
|
5
|
+
import { NonNullableInsertKeys, NullableInsertKeys, InsertType } from '../util/column-type.js';
|
|
6
6
|
export declare type InsertObject<DB, TB extends keyof DB> = {
|
|
7
|
-
[C in
|
|
7
|
+
[C in NonNullableInsertKeys<DB[TB]>]: ValueExpression<DB, TB, InsertType<DB[TB][C]>>;
|
|
8
|
+
} & {
|
|
9
|
+
[C in NullableInsertKeys<DB[TB]>]?: ValueExpression<DB, TB, InsertType<DB[TB][C]>>;
|
|
8
10
|
};
|
|
9
11
|
export declare type InsertObjectOrList<DB, TB extends keyof DB> = InsertObject<DB, TB> | ReadonlyArray<InsertObject<DB, TB>>;
|
|
10
|
-
declare type InsertValueExpression<DB, TB extends keyof DB, T> = ValueExpression<DB, TB, T> | GeneratedPlaceholder;
|
|
11
12
|
export declare function parseInsertObjectOrList(ctx: ParseContext, args: InsertObjectOrList<any, any>): [ReadonlyArray<ColumnNode>, ValuesNode];
|
|
12
|
-
export {};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ColumnNode } from '../operation-node/column-node.js';
|
|
2
2
|
import { PrimitiveValueListNode } from '../operation-node/primitive-value-list-node.js';
|
|
3
3
|
import { ValueListNode } from '../operation-node/value-list-node.js';
|
|
4
|
-
import { isGeneratedPlaceholder } from '../util/generated-placeholder.js';
|
|
5
4
|
import { freeze, isPrimitive } from '../util/object-utils.js';
|
|
6
5
|
import { parseValueExpression } from './value-parser.js';
|
|
7
6
|
import { ValuesNode } from '../operation-node/values-node.js';
|
|
@@ -20,7 +19,7 @@ function parseColumnNamesAndIndexes(rows) {
|
|
|
20
19
|
for (const row of rows) {
|
|
21
20
|
const cols = Object.keys(row);
|
|
22
21
|
for (const col of cols) {
|
|
23
|
-
if (!columns.has(col) &&
|
|
22
|
+
if (!columns.has(col) && row[col] !== undefined) {
|
|
24
23
|
columns.set(col, columns.size);
|
|
25
24
|
}
|
|
26
25
|
}
|
|
@@ -33,7 +32,7 @@ function parseRowValues(ctx, row, columns) {
|
|
|
33
32
|
for (const col of rowColumns) {
|
|
34
33
|
const columnIdx = columns.get(col);
|
|
35
34
|
const value = row[col];
|
|
36
|
-
if (columnIdx !== undefined
|
|
35
|
+
if (columnIdx !== undefined) {
|
|
37
36
|
rowValues[columnIdx] = value;
|
|
38
37
|
}
|
|
39
38
|
}
|
|
@@ -9,10 +9,11 @@ import { SelectQueryBuilder } from '../query-builder/select-query-builder.js';
|
|
|
9
9
|
import { ParseContext } from './parse-context.js';
|
|
10
10
|
import { ComplexExpression } from './complex-expression-parser.js';
|
|
11
11
|
import { DynamicReferenceBuilder } from '../dynamic/dynamic-reference-builder.js';
|
|
12
|
+
import { SelectType } from '../util/column-type.js';
|
|
12
13
|
export declare type ReferenceExpression<DB, TB extends keyof DB> = StringReference<DB, TB> | DynamicReferenceBuilder<any> | ComplexExpression<DB, TB>;
|
|
13
14
|
export declare type ReferenceExpressionOrList<DB, TB extends keyof DB> = ReferenceExpression<DB, TB> | ReadonlyArray<ReferenceExpression<DB, TB>>;
|
|
14
15
|
export declare type StringReference<DB, TB extends keyof DB> = AnyColumn<DB, TB> | AnyColumnWithTable<DB, TB>;
|
|
15
|
-
export declare type ExtractTypeFromReferenceExpression<DB, TB extends keyof DB, RE> = RE extends string ? ExtractTypeFromStringReference<DB, TB, RE
|
|
16
|
+
export declare type ExtractTypeFromReferenceExpression<DB, TB extends keyof DB, RE> = RE extends string ? SelectType<ExtractTypeFromStringReference<DB, TB, RE>> : RE extends RawBuilder<infer O> ? O : RE extends (qb: any) => RawBuilder<infer O> ? O : RE extends SelectQueryBuilder<any, any, infer O> ? ValueType<O> : RE extends (qb: any) => SelectQueryBuilder<any, any, infer O> ? ValueType<O> : PrimitiveValue;
|
|
16
17
|
declare type ExtractTypeFromStringReference<DB, TB extends keyof DB, RE extends string> = RE extends `${infer SC}.${infer T}.${infer C}` ? `${SC}.${T}` extends TB ? C extends keyof DB[`${SC}.${T}`] ? DB[`${SC}.${T}`][C] : never : never : RE extends `${infer T}.${infer C}` ? T extends TB ? C extends keyof DB[T] ? DB[T][C] : never : never : RE extends AnyColumn<DB, TB> ? ExtractColumnType<DB, TB, RE> : PrimitiveValue;
|
|
17
18
|
export declare function parseReferenceExpressionOrList(ctx: ParseContext, arg: ReferenceExpressionOrList<any, any>): ReferenceExpressionNode[];
|
|
18
19
|
export declare function parseReferenceExpression(ctx: ParseContext, exp: ReferenceExpression<any, any>): ReferenceExpressionNode;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { AliasedRawBuilder } from '../raw-builder/raw-builder.js';
|
|
2
2
|
import { AliasedQueryBuilder, SelectQueryBuilder } from '../query-builder/select-query-builder.js';
|
|
3
3
|
import { SelectionNode } from '../operation-node/selection-node.js';
|
|
4
|
-
import { AnyAliasedColumn, AnyAliasedColumnWithTable, AnyColumn, AnyColumnWithTable, ExtractColumnType,
|
|
4
|
+
import { AnyAliasedColumn, AnyAliasedColumnWithTable, AnyColumn, AnyColumnWithTable, ExtractColumnType, ValueType } from '../util/type-utils.js';
|
|
5
5
|
import { DynamicReferenceBuilder } from '../dynamic/dynamic-reference-builder.js';
|
|
6
6
|
import { ParseContext } from './parse-context.js';
|
|
7
7
|
import { AliasedComplexExpression } from './complex-expression-parser.js';
|
|
8
|
+
import { Selectable, SelectType } from '../util/column-type.js';
|
|
8
9
|
export declare type SelectExpression<DB, TB extends keyof DB> = AnyAliasedColumnWithTable<DB, TB> | AnyAliasedColumn<DB, TB> | AnyColumnWithTable<DB, TB> | AnyColumn<DB, TB> | DynamicReferenceBuilder<any> | AliasedComplexExpression<DB, TB>;
|
|
9
10
|
export declare type SelectExpressionOrList<DB, TB extends keyof DB> = SelectExpression<DB, TB> | ReadonlyArray<SelectExpression<DB, TB>>;
|
|
10
11
|
/**
|
|
@@ -15,14 +16,19 @@ export declare type QueryBuilderWithSelection<DB, TB extends keyof DB, O, SE> =
|
|
|
15
16
|
/**
|
|
16
17
|
* `selectAll` output query builder type.
|
|
17
18
|
*/
|
|
18
|
-
export declare type SelectAllQueryBuilder<DB, TB extends keyof DB, O, S extends keyof DB> = SelectQueryBuilder<DB, TB, O &
|
|
19
|
+
export declare type SelectAllQueryBuilder<DB, TB extends keyof DB, O, S extends keyof DB> = SelectQueryBuilder<DB, TB, O & AllSelection<DB, S>>;
|
|
19
20
|
export declare type Selection<DB, TB extends keyof DB, SE> = {
|
|
20
|
-
[A in ExtractAliasFromSelectExpression<SE>]: ExtractTypeFromSelectExpression<DB, TB, SE, A
|
|
21
|
+
[A in ExtractAliasFromSelectExpression<SE>]: SelectType<ExtractTypeFromSelectExpression<DB, TB, SE, A>>;
|
|
21
22
|
};
|
|
22
23
|
declare type ExtractAliasFromSelectExpression<SE> = SE extends string ? ExtractAliasFromStringSelectExpression<SE> : SE extends AliasedRawBuilder<any, infer RA> ? RA : SE extends (qb: any) => AliasedRawBuilder<any, infer RA> ? RA : SE extends AliasedQueryBuilder<any, any, any, infer QA> ? QA : SE extends (qb: any) => AliasedQueryBuilder<any, any, any, infer QA> ? QA : SE extends DynamicReferenceBuilder<infer RA> ? ExtractAliasFromStringSelectExpression<RA> : never;
|
|
23
24
|
declare type ExtractAliasFromStringSelectExpression<SE extends string> = SE extends `${string}.${string}.${string} as ${infer A}` ? A : SE extends `${string}.${string} as ${infer A}` ? A : SE extends `${string} as ${infer A}` ? A : SE extends `${string}.${string}.${infer C}` ? C : SE extends `${string}.${infer C}` ? C : SE;
|
|
24
25
|
declare type ExtractTypeFromSelectExpression<DB, TB extends keyof DB, SE, A extends keyof any> = SE extends string ? ExtractTypeFromStringSelectExpression<DB, TB, SE, A> : SE extends AliasedRawBuilder<infer O, infer RA> ? RA extends A ? O : never : SE extends (qb: any) => AliasedRawBuilder<infer O, infer RA> ? RA extends A ? O : never : SE extends AliasedQueryBuilder<any, any, infer O, infer QA> ? QA extends A ? ValueType<O> : never : SE extends (qb: any) => AliasedQueryBuilder<any, any, infer O, infer QA> ? QA extends A ? ValueType<O> : never : SE extends DynamicReferenceBuilder<infer RA> ? A extends ExtractAliasFromStringSelectExpression<RA> ? ExtractTypeFromStringSelectExpression<DB, TB, RA, A> | undefined : never : never;
|
|
25
26
|
declare type ExtractTypeFromStringSelectExpression<DB, TB extends keyof DB, SE extends string, A extends keyof any> = SE extends `${infer SC}.${infer T}.${infer C} as ${infer RA}` ? RA extends A ? `${SC}.${T}` extends TB ? C extends keyof DB[`${SC}.${T}`] ? DB[`${SC}.${T}`][C] : never : never : never : SE extends `${infer T}.${infer C} as ${infer RA}` ? RA extends A ? T extends TB ? C extends keyof DB[T] ? DB[T][C] : never : never : never : SE extends `${infer C} as ${infer RA}` ? RA extends A ? C extends AnyColumn<DB, TB> ? ExtractColumnType<DB, TB, C> : never : never : SE extends `${infer SC}.${infer T}.${infer C}` ? C extends A ? `${SC}.${T}` extends TB ? C extends keyof DB[`${SC}.${T}`] ? DB[`${SC}.${T}`][C] : never : never : never : SE extends `${infer T}.${infer C}` ? C extends A ? T extends TB ? C extends keyof DB[T] ? DB[T][C] : never : never : never : SE extends A ? SE extends AnyColumn<DB, TB> ? ExtractColumnType<DB, TB, SE> : never : never;
|
|
27
|
+
declare type AllSelection<DB, TB extends keyof DB> = Selectable<{
|
|
28
|
+
[C in AnyColumn<DB, TB>]: {
|
|
29
|
+
[T in TB]: C extends keyof DB[T] ? DB[T][C] : never;
|
|
30
|
+
}[TB];
|
|
31
|
+
}>;
|
|
26
32
|
export declare function parseSelectExpressionOrList(ctx: ParseContext, selection: SelectExpressionOrList<any, any>): SelectionNode[];
|
|
27
33
|
export declare function parseSelectAll(table?: string | string[]): SelectionNode[];
|
|
28
34
|
export {};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ColumnUpdateNode } from '../operation-node/column-update-node.js';
|
|
2
|
+
import { UpdateKeys, UpdateType } from '../util/column-type.js';
|
|
2
3
|
import { ParseContext } from './parse-context.js';
|
|
3
4
|
import { ValueExpression } from './value-parser.js';
|
|
4
5
|
export declare type MutationObject<DB, TB extends keyof DB> = {
|
|
5
|
-
[C in
|
|
6
|
+
[C in UpdateKeys<DB[TB]>]?: ValueExpression<DB, TB, UpdateType<DB[TB][C]>>;
|
|
6
7
|
};
|
|
7
8
|
export declare function parseUpdateObject(ctx: ParseContext, row: MutationObject<any, any>): ReadonlyArray<ColumnUpdateNode>;
|
|
@@ -539,7 +539,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
539
539
|
* const { id } = await db
|
|
540
540
|
* .insertInto('person')
|
|
541
541
|
* .values({
|
|
542
|
-
* id: db.generated,
|
|
543
542
|
* first_name: 'Jennifer',
|
|
544
543
|
* last_name: 'Aniston'
|
|
545
544
|
* })
|
|
@@ -553,7 +552,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
553
552
|
* const { id, first_name } = await db
|
|
554
553
|
* .insertInto('person')
|
|
555
554
|
* .values({
|
|
556
|
-
* id: db.generated,
|
|
557
555
|
* first_name: 'Jennifer',
|
|
558
556
|
* last_name: 'Aniston'
|
|
559
557
|
* })
|
|
@@ -569,7 +567,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
569
567
|
* const { id, full_name, first_pet_id } = await db
|
|
570
568
|
* .insertInto('person')
|
|
571
569
|
* .values({
|
|
572
|
-
* id: db.generated,
|
|
573
570
|
* first_name: 'Jennifer',
|
|
574
571
|
* last_name: 'Aniston'
|
|
575
572
|
* })
|
|
@@ -23,13 +23,10 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
23
23
|
*
|
|
24
24
|
* This method takes an object whose keys are column names and values are
|
|
25
25
|
* values to insert. In addition to the column's type, the values can be
|
|
26
|
-
* {@link Kysely.raw | raw} instances
|
|
27
|
-
* {@link Kysely.generated | generated} placeholder.
|
|
26
|
+
* {@link Kysely.raw | raw} instances or select queries.
|
|
28
27
|
*
|
|
29
|
-
* You must provide all
|
|
30
|
-
*
|
|
31
|
-
* identifiers can be marked with the {@link Kysely.generated} placeholder unless you
|
|
32
|
-
* want to insert a specific value instead of the generated default.
|
|
28
|
+
* You must provide all fields you haven't explicitly marked as nullable
|
|
29
|
+
* or optional using {@link Generated} or {@link ColumnType}.
|
|
33
30
|
*
|
|
34
31
|
* The return value of an `insert` query is an instance of {@link InsertResult}. The
|
|
35
32
|
* {@link InsertResult.insertId | insertId} field holds the auto incremented primary
|
|
@@ -48,7 +45,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
48
45
|
* const id = await db
|
|
49
46
|
* .insertInto('person')
|
|
50
47
|
* .values({
|
|
51
|
-
* id: db.generated,
|
|
52
48
|
* first_name: 'Jennifer',
|
|
53
49
|
* last_name: 'Aniston'
|
|
54
50
|
* })
|
|
@@ -70,11 +66,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
70
66
|
* await db
|
|
71
67
|
* .insertInto('person')
|
|
72
68
|
* .values([{
|
|
73
|
-
* id: db.generated,
|
|
74
69
|
* first_name: 'Jennifer',
|
|
75
70
|
* last_name: 'Aniston'
|
|
76
71
|
* }, {
|
|
77
|
-
* id: db.generated,
|
|
78
72
|
* first_name: 'Arnold',
|
|
79
73
|
* last_name: 'Schwarzenegger',
|
|
80
74
|
* }])
|
|
@@ -95,7 +89,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
95
89
|
* const row = await db
|
|
96
90
|
* .insertInto('person')
|
|
97
91
|
* .values({
|
|
98
|
-
* id: db.generated,
|
|
99
92
|
* first_name: 'Jennifer',
|
|
100
93
|
* last_name: 'Aniston'
|
|
101
94
|
* })
|
|
@@ -118,7 +111,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
118
111
|
* const result = await db
|
|
119
112
|
* .insertInto('person')
|
|
120
113
|
* .values({
|
|
121
|
-
* id: db.generated,
|
|
122
114
|
* first_name: 'Jennifer',
|
|
123
115
|
* last_name: db.raw('? || ?', ['Ani', 'ston']),
|
|
124
116
|
* age: db.selectFrom('person').select(raw('avg(age)')),
|
|
@@ -144,7 +136,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
144
136
|
* .select(['id', 'first_name', 'gender'])
|
|
145
137
|
* .limit(1)
|
|
146
138
|
* ).insertInto('pet').values({
|
|
147
|
-
* id: db.generated,
|
|
148
139
|
* owner_id: (eb) => eb.selectFrom('jennifer').select('id'),
|
|
149
140
|
* name: (eb) => eb.selectFrom('jennifer').select('first_name'),
|
|
150
141
|
* species: 'cat',
|
|
@@ -229,7 +220,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
229
220
|
* await db
|
|
230
221
|
* .insertInto('pet')
|
|
231
222
|
* .values({
|
|
232
|
-
* id: db.generated,
|
|
233
223
|
* name: 'Catto',
|
|
234
224
|
* species: 'cat',
|
|
235
225
|
* })
|
|
@@ -255,7 +245,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
255
245
|
* await db
|
|
256
246
|
* .insertInto('pet')
|
|
257
247
|
* .values({
|
|
258
|
-
* id: db.generated,
|
|
259
248
|
* name: 'Catto',
|
|
260
249
|
* species: 'cat',
|
|
261
250
|
* })
|
|
@@ -283,7 +272,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
283
272
|
* await db
|
|
284
273
|
* .insertInto('pet')
|
|
285
274
|
* .values({
|
|
286
|
-
* id: db.generated,
|
|
287
275
|
* name: 'Catto',
|
|
288
276
|
* species: 'cat',
|
|
289
277
|
* })
|
|
@@ -309,7 +297,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
309
297
|
* await db
|
|
310
298
|
* .insertInto('pet')
|
|
311
299
|
* .values({
|
|
312
|
-
* id: db.generated,
|
|
313
300
|
* name: 'Catto',
|
|
314
301
|
* species: 'cat',
|
|
315
302
|
* })
|
|
@@ -337,7 +324,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
337
324
|
* await db
|
|
338
325
|
* .insertInto('pet')
|
|
339
326
|
* .values({
|
|
340
|
-
* id: db.generated,
|
|
341
327
|
* name: 'Catto',
|
|
342
328
|
* species: 'cat',
|
|
343
329
|
* })
|
|
@@ -396,7 +382,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
396
382
|
* const { id } = await db
|
|
397
383
|
* .insertInto('person')
|
|
398
384
|
* .values({
|
|
399
|
-
* id: db.generated,
|
|
400
385
|
* first_name: 'Jennifer',
|
|
401
386
|
* last_name: 'Aniston'
|
|
402
387
|
* })
|
|
@@ -410,7 +395,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
410
395
|
* const { id, first_name } = await db
|
|
411
396
|
* .insertInto('person')
|
|
412
397
|
* .values({
|
|
413
|
-
* id: db.generated,
|
|
414
398
|
* first_name: 'Jennifer',
|
|
415
399
|
* last_name: 'Aniston'
|
|
416
400
|
* })
|
|
@@ -426,7 +410,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
426
410
|
* const { id, full_name, first_pet_id } = await db
|
|
427
411
|
* .insertInto('person')
|
|
428
412
|
* .values({
|
|
429
|
-
* id: db.generated,
|
|
430
413
|
* first_name: 'Jennifer',
|
|
431
414
|
* last_name: 'Aniston'
|
|
432
415
|
* })
|
|
@@ -10,8 +10,8 @@ import { InsertResult } from './insert-result.js';
|
|
|
10
10
|
import { NoResultError } from './no-result-error.js';
|
|
11
11
|
import { parseComplexExpression, } from '../parser/complex-expression-parser.js';
|
|
12
12
|
import { ColumnNode } from '../operation-node/column-node.js';
|
|
13
|
-
import { OnConflictNode } from '../index.js';
|
|
14
13
|
import { OnConflictBuilder, } from './on-conflict-builder.js';
|
|
14
|
+
import { OnConflictNode } from '../operation-node/on-conflict-node.js';
|
|
15
15
|
export class InsertQueryBuilder {
|
|
16
16
|
#props;
|
|
17
17
|
constructor(props) {
|
|
@@ -124,7 +124,6 @@ export class InsertQueryBuilder {
|
|
|
124
124
|
* await db
|
|
125
125
|
* .insertInto('pet')
|
|
126
126
|
* .values({
|
|
127
|
-
* id: db.generated,
|
|
128
127
|
* name: 'Catto',
|
|
129
128
|
* species: 'cat',
|
|
130
129
|
* })
|
|
@@ -150,7 +149,6 @@ export class InsertQueryBuilder {
|
|
|
150
149
|
* await db
|
|
151
150
|
* .insertInto('pet')
|
|
152
151
|
* .values({
|
|
153
|
-
* id: db.generated,
|
|
154
152
|
* name: 'Catto',
|
|
155
153
|
* species: 'cat',
|
|
156
154
|
* })
|
|
@@ -178,7 +176,6 @@ export class InsertQueryBuilder {
|
|
|
178
176
|
* await db
|
|
179
177
|
* .insertInto('pet')
|
|
180
178
|
* .values({
|
|
181
|
-
* id: db.generated,
|
|
182
179
|
* name: 'Catto',
|
|
183
180
|
* species: 'cat',
|
|
184
181
|
* })
|
|
@@ -204,7 +201,6 @@ export class InsertQueryBuilder {
|
|
|
204
201
|
* await db
|
|
205
202
|
* .insertInto('pet')
|
|
206
203
|
* .values({
|
|
207
|
-
* id: db.generated,
|
|
208
204
|
* name: 'Catto',
|
|
209
205
|
* species: 'cat',
|
|
210
206
|
* })
|
|
@@ -232,7 +228,6 @@ export class InsertQueryBuilder {
|
|
|
232
228
|
* await db
|
|
233
229
|
* .insertInto('pet')
|
|
234
230
|
* .values({
|
|
235
|
-
* id: db.generated,
|
|
236
231
|
* name: 'Catto',
|
|
237
232
|
* species: 'cat',
|
|
238
233
|
* })
|
|
@@ -337,7 +337,7 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
|
|
|
337
337
|
* ```ts
|
|
338
338
|
* await db
|
|
339
339
|
* .insertInto('person')
|
|
340
|
-
* .values({
|
|
340
|
+
* .values({ first_name, pic })
|
|
341
341
|
* .onConflict((oc) => oc
|
|
342
342
|
* .column('pic')
|
|
343
343
|
* .doNothing()
|
|
@@ -361,7 +361,7 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
|
|
|
361
361
|
* ```ts
|
|
362
362
|
* await db
|
|
363
363
|
* .insertInto('person')
|
|
364
|
-
* .values({
|
|
364
|
+
* .values({ first_name, pic })
|
|
365
365
|
* .onConflict((oc) => oc
|
|
366
366
|
* .column('pic')
|
|
367
367
|
* .doUpdateSet({ first_name })
|
|
@@ -160,7 +160,7 @@ export class OnConflictBuilder {
|
|
|
160
160
|
* ```ts
|
|
161
161
|
* await db
|
|
162
162
|
* .insertInto('person')
|
|
163
|
-
* .values({
|
|
163
|
+
* .values({ first_name, pic })
|
|
164
164
|
* .onConflict((oc) => oc
|
|
165
165
|
* .column('pic')
|
|
166
166
|
* .doNothing()
|
|
@@ -191,7 +191,7 @@ export class OnConflictBuilder {
|
|
|
191
191
|
* ```ts
|
|
192
192
|
* await db
|
|
193
193
|
* .insertInto('person')
|
|
194
|
-
* .values({
|
|
194
|
+
* .values({ first_name, pic })
|
|
195
195
|
* .onConflict((oc) => oc
|
|
196
196
|
* .column('pic')
|
|
197
197
|
* .doUpdateSet({ first_name })
|