kysely 0.14.0 → 0.15.2
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 +95 -39
- 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-nodeless.d.ts +148 -0
- package/dist/cjs/index-nodeless.js +149 -0
- package/dist/cjs/index.d.ts +6 -150
- package/dist/cjs/index.js +6 -139
- 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 +4 -3
- package/dist/cjs/parser/select-parser.d.ts +10 -4
- package/dist/cjs/parser/update-set-parser.d.ts +2 -1
- package/dist/cjs/query-builder/delete-query-builder.d.ts +2 -4
- package/dist/cjs/query-builder/insert-query-builder.d.ts +5 -21
- package/dist/cjs/query-builder/insert-query-builder.js +2 -7
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +5 -3
- package/dist/cjs/query-builder/on-conflict-builder.js +2 -2
- package/dist/cjs/query-builder/returning-interface.d.ts +2 -4
- package/dist/cjs/query-builder/select-query-builder.d.ts +1 -1
- package/dist/cjs/query-builder/update-query-builder.d.ts +2 -4
- 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 +6 -42
- 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-nodeless.d.ts +148 -0
- package/dist/esm/index-nodeless.js +137 -0
- package/dist/esm/index.d.ts +6 -150
- package/dist/esm/index.js +6 -139
- 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 +4 -3
- package/dist/esm/parser/select-parser.d.ts +10 -4
- package/dist/esm/parser/update-set-parser.d.ts +2 -1
- package/dist/esm/query-builder/delete-query-builder.d.ts +2 -4
- package/dist/esm/query-builder/insert-query-builder.d.ts +5 -21
- package/dist/esm/query-builder/insert-query-builder.js +1 -6
- package/dist/esm/query-builder/on-conflict-builder.d.ts +5 -3
- package/dist/esm/query-builder/on-conflict-builder.js +2 -2
- package/dist/esm/query-builder/returning-interface.d.ts +2 -4
- package/dist/esm/query-builder/select-query-builder.d.ts +1 -1
- package/dist/esm/query-builder/update-query-builder.d.ts +2 -4
- 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 +6 -42
- package/package.json +1 -1
- package/test.js +3 -0
- 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
|
}
|
|
@@ -3,17 +3,18 @@ import { ColumnNode } from '../operation-node/column-node.js';
|
|
|
3
3
|
import { ReferenceExpressionNode } from '../operation-node/operation-node-utils.js';
|
|
4
4
|
import { ReferenceNode } from '../operation-node/reference-node.js';
|
|
5
5
|
import { PrimitiveValue } from '../util/object-utils.js';
|
|
6
|
-
import { AnyColumn, AnyColumnWithTable,
|
|
6
|
+
import { AnyColumn, AnyColumnWithTable, ExtractColumnType, ValueType } from '../util/type-utils.js';
|
|
7
7
|
import { RawBuilder } from '../raw-builder/raw-builder.js';
|
|
8
8
|
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
|
-
declare type ExtractTypeFromStringReference<DB, TB extends keyof DB, RE extends string
|
|
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;
|
|
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;
|
|
19
20
|
export declare function parseStringReference(ref: string): ColumnNode | ReferenceNode;
|
|
@@ -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,
|
|
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
|
-
declare type ExtractTypeFromStringSelectExpression<DB, TB extends keyof DB, SE extends string, A extends keyof any
|
|
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>;
|
|
@@ -17,6 +17,7 @@ import { JoinInterface } from './join-interface.js';
|
|
|
17
17
|
import { ReturningInterface } from './returning-interface.js';
|
|
18
18
|
import { NoResultErrorConstructor } from './no-result-error.js';
|
|
19
19
|
import { DeleteQueryNode } from '../operation-node/delete-query-node.js';
|
|
20
|
+
import { Selectable } from '../util/column-type.js';
|
|
20
21
|
export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, JoinInterface<DB, TB>, ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
|
|
21
22
|
#private;
|
|
22
23
|
constructor(props: DeleteQueryBuilderProps);
|
|
@@ -539,7 +540,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
539
540
|
* const { id } = await db
|
|
540
541
|
* .insertInto('person')
|
|
541
542
|
* .values({
|
|
542
|
-
* id: db.generated,
|
|
543
543
|
* first_name: 'Jennifer',
|
|
544
544
|
* last_name: 'Aniston'
|
|
545
545
|
* })
|
|
@@ -553,7 +553,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
553
553
|
* const { id, first_name } = await db
|
|
554
554
|
* .insertInto('person')
|
|
555
555
|
* .values({
|
|
556
|
-
* id: db.generated,
|
|
557
556
|
* first_name: 'Jennifer',
|
|
558
557
|
* last_name: 'Aniston'
|
|
559
558
|
* })
|
|
@@ -569,7 +568,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
569
568
|
* const { id, full_name, first_pet_id } = await db
|
|
570
569
|
* .insertInto('person')
|
|
571
570
|
* .values({
|
|
572
|
-
* id: db.generated,
|
|
573
571
|
* first_name: 'Jennifer',
|
|
574
572
|
* last_name: 'Aniston'
|
|
575
573
|
* })
|
|
@@ -587,7 +585,7 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
587
585
|
* Adds a `returning *` to an insert/update/delete query on databases
|
|
588
586
|
* that support `returning` such as PostgreSQL.
|
|
589
587
|
*/
|
|
590
|
-
returningAll(): DeleteQueryBuilder<DB, TB, DB[TB]
|
|
588
|
+
returningAll(): DeleteQueryBuilder<DB, TB, Selectable<DB[TB]>>;
|
|
591
589
|
/**
|
|
592
590
|
* Simply calls the given function passing `this` as the only argument.
|
|
593
591
|
*
|
|
@@ -15,6 +15,7 @@ import { NoResultErrorConstructor } from './no-result-error.js';
|
|
|
15
15
|
import { ComplexExpression } from '../parser/complex-expression-parser.js';
|
|
16
16
|
import { ReturningInterface } from './returning-interface.js';
|
|
17
17
|
import { OnConflictBuilder, OnConflictDoNothingBuilder, OnConflictUpdateBuilder } from './on-conflict-builder.js';
|
|
18
|
+
import { Selectable } from '../util/column-type.js';
|
|
18
19
|
export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
|
|
19
20
|
#private;
|
|
20
21
|
constructor(props: InsertQueryBuilderProps);
|
|
@@ -23,13 +24,10 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
23
24
|
*
|
|
24
25
|
* This method takes an object whose keys are column names and values are
|
|
25
26
|
* 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.
|
|
27
|
+
* {@link Kysely.raw | raw} instances or select queries.
|
|
28
28
|
*
|
|
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.
|
|
29
|
+
* You must provide all fields you haven't explicitly marked as nullable
|
|
30
|
+
* or optional using {@link Generated} or {@link ColumnType}.
|
|
33
31
|
*
|
|
34
32
|
* The return value of an `insert` query is an instance of {@link InsertResult}. The
|
|
35
33
|
* {@link InsertResult.insertId | insertId} field holds the auto incremented primary
|
|
@@ -48,7 +46,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
48
46
|
* const id = await db
|
|
49
47
|
* .insertInto('person')
|
|
50
48
|
* .values({
|
|
51
|
-
* id: db.generated,
|
|
52
49
|
* first_name: 'Jennifer',
|
|
53
50
|
* last_name: 'Aniston'
|
|
54
51
|
* })
|
|
@@ -70,11 +67,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
70
67
|
* await db
|
|
71
68
|
* .insertInto('person')
|
|
72
69
|
* .values([{
|
|
73
|
-
* id: db.generated,
|
|
74
70
|
* first_name: 'Jennifer',
|
|
75
71
|
* last_name: 'Aniston'
|
|
76
72
|
* }, {
|
|
77
|
-
* id: db.generated,
|
|
78
73
|
* first_name: 'Arnold',
|
|
79
74
|
* last_name: 'Schwarzenegger',
|
|
80
75
|
* }])
|
|
@@ -95,7 +90,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
95
90
|
* const row = await db
|
|
96
91
|
* .insertInto('person')
|
|
97
92
|
* .values({
|
|
98
|
-
* id: db.generated,
|
|
99
93
|
* first_name: 'Jennifer',
|
|
100
94
|
* last_name: 'Aniston'
|
|
101
95
|
* })
|
|
@@ -118,7 +112,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
118
112
|
* const result = await db
|
|
119
113
|
* .insertInto('person')
|
|
120
114
|
* .values({
|
|
121
|
-
* id: db.generated,
|
|
122
115
|
* first_name: 'Jennifer',
|
|
123
116
|
* last_name: db.raw('? || ?', ['Ani', 'ston']),
|
|
124
117
|
* age: db.selectFrom('person').select(raw('avg(age)')),
|
|
@@ -144,7 +137,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
144
137
|
* .select(['id', 'first_name', 'gender'])
|
|
145
138
|
* .limit(1)
|
|
146
139
|
* ).insertInto('pet').values({
|
|
147
|
-
* id: db.generated,
|
|
148
140
|
* owner_id: (eb) => eb.selectFrom('jennifer').select('id'),
|
|
149
141
|
* name: (eb) => eb.selectFrom('jennifer').select('first_name'),
|
|
150
142
|
* species: 'cat',
|
|
@@ -229,7 +221,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
229
221
|
* await db
|
|
230
222
|
* .insertInto('pet')
|
|
231
223
|
* .values({
|
|
232
|
-
* id: db.generated,
|
|
233
224
|
* name: 'Catto',
|
|
234
225
|
* species: 'cat',
|
|
235
226
|
* })
|
|
@@ -255,7 +246,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
255
246
|
* await db
|
|
256
247
|
* .insertInto('pet')
|
|
257
248
|
* .values({
|
|
258
|
-
* id: db.generated,
|
|
259
249
|
* name: 'Catto',
|
|
260
250
|
* species: 'cat',
|
|
261
251
|
* })
|
|
@@ -283,7 +273,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
283
273
|
* await db
|
|
284
274
|
* .insertInto('pet')
|
|
285
275
|
* .values({
|
|
286
|
-
* id: db.generated,
|
|
287
276
|
* name: 'Catto',
|
|
288
277
|
* species: 'cat',
|
|
289
278
|
* })
|
|
@@ -309,7 +298,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
309
298
|
* await db
|
|
310
299
|
* .insertInto('pet')
|
|
311
300
|
* .values({
|
|
312
|
-
* id: db.generated,
|
|
313
301
|
* name: 'Catto',
|
|
314
302
|
* species: 'cat',
|
|
315
303
|
* })
|
|
@@ -337,7 +325,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
337
325
|
* await db
|
|
338
326
|
* .insertInto('pet')
|
|
339
327
|
* .values({
|
|
340
|
-
* id: db.generated,
|
|
341
328
|
* name: 'Catto',
|
|
342
329
|
* species: 'cat',
|
|
343
330
|
* })
|
|
@@ -396,7 +383,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
396
383
|
* const { id } = await db
|
|
397
384
|
* .insertInto('person')
|
|
398
385
|
* .values({
|
|
399
|
-
* id: db.generated,
|
|
400
386
|
* first_name: 'Jennifer',
|
|
401
387
|
* last_name: 'Aniston'
|
|
402
388
|
* })
|
|
@@ -410,7 +396,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
410
396
|
* const { id, first_name } = await db
|
|
411
397
|
* .insertInto('person')
|
|
412
398
|
* .values({
|
|
413
|
-
* id: db.generated,
|
|
414
399
|
* first_name: 'Jennifer',
|
|
415
400
|
* last_name: 'Aniston'
|
|
416
401
|
* })
|
|
@@ -426,7 +411,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
426
411
|
* const { id, full_name, first_pet_id } = await db
|
|
427
412
|
* .insertInto('person')
|
|
428
413
|
* .values({
|
|
429
|
-
* id: db.generated,
|
|
430
414
|
* first_name: 'Jennifer',
|
|
431
415
|
* last_name: 'Aniston'
|
|
432
416
|
* })
|
|
@@ -444,7 +428,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
444
428
|
* Adds a `returning *` to an insert/update/delete query on databases
|
|
445
429
|
* that support `returning` such as PostgreSQL.
|
|
446
430
|
*/
|
|
447
|
-
returningAll(): InsertQueryBuilder<DB, TB, DB[TB]
|
|
431
|
+
returningAll(): InsertQueryBuilder<DB, TB, Selectable<DB[TB]>>;
|
|
448
432
|
/**
|
|
449
433
|
* Simply calls the given function passing `this` as the only argument.
|
|
450
434
|
*
|