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,25 +1,125 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const fs_1 = require("fs");
|
|
3
|
+
exports.Migrator = exports.NO_MIGRATIONS = exports.MIGRATION_LOCK_ID = exports.MIGRATION_LOCK_TABLE = exports.MIGRATION_TABLE = void 0;
|
|
6
4
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
+
const private_symbols_js_1 = require("../util/private-symbols.js");
|
|
7
6
|
exports.MIGRATION_TABLE = 'kysely_migration';
|
|
8
7
|
exports.MIGRATION_LOCK_TABLE = 'kysely_migration_lock';
|
|
9
8
|
exports.MIGRATION_LOCK_ID = 'migration_lock';
|
|
10
9
|
exports.NO_MIGRATIONS = (0, object_utils_js_1.freeze)({ __noMigrations__: true });
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
/**
|
|
11
|
+
* A class for running migrations.
|
|
12
|
+
*
|
|
13
|
+
* ### Example
|
|
14
|
+
*
|
|
15
|
+
* This example uses the {@link FileMigrationProvider} that reads migrations
|
|
16
|
+
* files from a single folder. You can easily implement your own
|
|
17
|
+
* {@link MigrationProvider} if you want to provide migrations some
|
|
18
|
+
* other way.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* const migrator = new Migrator({
|
|
22
|
+
* db,
|
|
23
|
+
* // The constructor signature of FileMigrationProvider is a
|
|
24
|
+
* // bit weird to keep Kysely independent of node.js or any
|
|
25
|
+
* // other environment.
|
|
26
|
+
* provider: new FileMigrationProvider(
|
|
27
|
+
* fs.readdir,
|
|
28
|
+
* path.join,
|
|
29
|
+
* // Path to the folder that contains all your migrations.
|
|
30
|
+
* 'migrations'
|
|
31
|
+
* )
|
|
32
|
+
* })
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
class Migrator {
|
|
36
|
+
#props;
|
|
37
|
+
constructor(props) {
|
|
38
|
+
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
17
39
|
}
|
|
18
|
-
|
|
19
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Runs all migrations that have not yet been run.
|
|
42
|
+
*
|
|
43
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
44
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
45
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
46
|
+
* were executed and which failed. See the examples below.
|
|
47
|
+
*
|
|
48
|
+
* This method goes through all possible migrations provided by the provider and runs the
|
|
49
|
+
* ones whose names come alphabetically after the last migration that has been run. If the
|
|
50
|
+
* list of executed migrations doesn't match the beginning of the list of possible migrations
|
|
51
|
+
* an error is thrown.
|
|
52
|
+
*
|
|
53
|
+
* ### Examples
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* import * as path from 'path'
|
|
57
|
+
* import * as fs from 'fs/promises'
|
|
58
|
+
*
|
|
59
|
+
* const db = new Kysely<Database>({
|
|
60
|
+
* dialect: new PostgresDialect({
|
|
61
|
+
* host: 'localhost',
|
|
62
|
+
* database: 'kysely_test',
|
|
63
|
+
* }),
|
|
64
|
+
* })
|
|
65
|
+
*
|
|
66
|
+
* const migrator = new Migrator({
|
|
67
|
+
* db,
|
|
68
|
+
* // The constructor signature of FileMigrationProvider is a
|
|
69
|
+
* // bit weird to keep Kysely independent of node.js or any
|
|
70
|
+
* // other environment.
|
|
71
|
+
* provider: new FileMigrationProvider(
|
|
72
|
+
* fs.readdir,
|
|
73
|
+
* path.join,
|
|
74
|
+
* // Path to the folder that contains all your migrations.
|
|
75
|
+
* 'migrations'
|
|
76
|
+
* )
|
|
77
|
+
* })
|
|
78
|
+
*
|
|
79
|
+
* const { error, results } = await migrator.migrateToLatest()
|
|
80
|
+
*
|
|
81
|
+
* results?.forEach((it) => {
|
|
82
|
+
* if (it.status === 'Success') {
|
|
83
|
+
* console.log(`migration "${it.migrationName}" was executed successfully`)
|
|
84
|
+
* } else if (it.status === 'Error') {
|
|
85
|
+
* console.error(`failed to execute migration "${it.migrationName}"`)
|
|
86
|
+
* }
|
|
87
|
+
* })
|
|
88
|
+
*
|
|
89
|
+
* if (error) {
|
|
90
|
+
* console.error('failed to run `migrateToLatest`')
|
|
91
|
+
* console.error(error)
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
async migrateToLatest() {
|
|
96
|
+
return this.#migrate(({ migrations }) => migrations.length - 1);
|
|
20
97
|
}
|
|
21
|
-
|
|
22
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Migrate up/down to a specific migration.
|
|
100
|
+
*
|
|
101
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
102
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
103
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
104
|
+
* were executed and which failed.
|
|
105
|
+
*
|
|
106
|
+
* ### Examples
|
|
107
|
+
*
|
|
108
|
+
* ```ts
|
|
109
|
+
* await migrator.migrateTo('some_migration')
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* If you specify the name of the first migration, this method migrates
|
|
113
|
+
* down to the first migration, but doesn't run the `down` method of
|
|
114
|
+
* the first migration. In case you want to migrate all the way down,
|
|
115
|
+
* you can use a special constant `NO_MIGRATIONS`:
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* await migrator.migrateTo(NO_MIGRATIONS)
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
async migrateTo(targetMigrationName) {
|
|
122
|
+
return this.#migrate(({ migrations }) => {
|
|
23
123
|
if (targetMigrationName === exports.NO_MIGRATIONS) {
|
|
24
124
|
return -1;
|
|
25
125
|
}
|
|
@@ -30,16 +130,44 @@ class MigrationModule {
|
|
|
30
130
|
return index;
|
|
31
131
|
});
|
|
32
132
|
}
|
|
33
|
-
|
|
34
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Migrate one step up.
|
|
135
|
+
*
|
|
136
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
137
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
138
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
139
|
+
* were executed and which failed.
|
|
140
|
+
*
|
|
141
|
+
* ### Examples
|
|
142
|
+
*
|
|
143
|
+
* ```ts
|
|
144
|
+
* await migrator.migrateUp()
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
async migrateUp() {
|
|
148
|
+
return this.#migrate(({ currentIndex, migrations }) => Math.min(currentIndex + 1, migrations.length - 1));
|
|
35
149
|
}
|
|
36
|
-
|
|
37
|
-
|
|
150
|
+
/**
|
|
151
|
+
* Migrate one step down.
|
|
152
|
+
*
|
|
153
|
+
* This method returns a {@link MigrationResultSet} instance and _never_ throws.
|
|
154
|
+
* {@link MigrationResultSet.error} holds the error if something went wrong.
|
|
155
|
+
* {@link MigrationResultSet.results} contains information about which migrations
|
|
156
|
+
* were executed and which failed.
|
|
157
|
+
*
|
|
158
|
+
* ### Examples
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* await migrator.migrateDown()
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
async migrateDown() {
|
|
165
|
+
return this.#migrate(({ currentIndex }) => Math.max(currentIndex - 1, -1));
|
|
38
166
|
}
|
|
39
|
-
async #migrate(
|
|
167
|
+
async #migrate(getTargetMigrationIndex) {
|
|
40
168
|
try {
|
|
41
169
|
await this.#ensureMigrationTablesExists();
|
|
42
|
-
return await this.#runMigrations(
|
|
170
|
+
return await this.#runMigrations(getTargetMigrationIndex);
|
|
43
171
|
}
|
|
44
172
|
catch (error) {
|
|
45
173
|
if (error instanceof MigrationResultSetError) {
|
|
@@ -56,7 +184,7 @@ class MigrationModule {
|
|
|
56
184
|
async #ensureMigrationTableExists() {
|
|
57
185
|
if (!(await this.#doesTableExists(exports.MIGRATION_TABLE))) {
|
|
58
186
|
try {
|
|
59
|
-
await this.#db.schema
|
|
187
|
+
await this.#props.db.schema
|
|
60
188
|
.createTable(exports.MIGRATION_TABLE)
|
|
61
189
|
.ifNotExists()
|
|
62
190
|
.addColumn('name', 'varchar(255)', (col) => col.notNull().primaryKey())
|
|
@@ -78,7 +206,7 @@ class MigrationModule {
|
|
|
78
206
|
async #ensureMigrationLockTableExists() {
|
|
79
207
|
if (!(await this.#doesTableExists(exports.MIGRATION_LOCK_TABLE))) {
|
|
80
208
|
try {
|
|
81
|
-
await this.#db.schema
|
|
209
|
+
await this.#props.db.schema
|
|
82
210
|
.createTable(exports.MIGRATION_LOCK_TABLE)
|
|
83
211
|
.ifNotExists()
|
|
84
212
|
.addColumn('id', 'varchar(255)', (col) => col.notNull().primaryKey())
|
|
@@ -98,7 +226,7 @@ class MigrationModule {
|
|
|
98
226
|
async #ensureLockRowExists() {
|
|
99
227
|
if (!(await this.#doesLockRowExists())) {
|
|
100
228
|
try {
|
|
101
|
-
await this.#db
|
|
229
|
+
await this.#props.db
|
|
102
230
|
.insertInto(exports.MIGRATION_LOCK_TABLE)
|
|
103
231
|
.values({ id: exports.MIGRATION_LOCK_ID })
|
|
104
232
|
.execute();
|
|
@@ -111,28 +239,28 @@ class MigrationModule {
|
|
|
111
239
|
}
|
|
112
240
|
}
|
|
113
241
|
async #doesTableExists(tableName) {
|
|
114
|
-
const metadata = await this.#db.introspection.getMetadata({
|
|
242
|
+
const metadata = await this.#props.db.introspection.getMetadata({
|
|
115
243
|
withInternalKyselyTables: true,
|
|
116
244
|
});
|
|
117
245
|
return !!metadata.tables.find((it) => it.name === tableName);
|
|
118
246
|
}
|
|
119
247
|
async #doesLockRowExists() {
|
|
120
|
-
const lockRow = await this.#db
|
|
248
|
+
const lockRow = await this.#props.db
|
|
121
249
|
.selectFrom(exports.MIGRATION_LOCK_TABLE)
|
|
122
250
|
.where('id', '=', exports.MIGRATION_LOCK_ID)
|
|
123
251
|
.select('id')
|
|
124
252
|
.executeTakeFirst();
|
|
125
253
|
return !!lockRow;
|
|
126
254
|
}
|
|
127
|
-
async #runMigrations(
|
|
255
|
+
async #runMigrations(getTargetMigrationIndex) {
|
|
128
256
|
const run = async (db) => {
|
|
129
257
|
try {
|
|
130
|
-
await this.#
|
|
131
|
-
const state = await this.#getState(db
|
|
258
|
+
await this.#props.db[private_symbols_js_1.PRIVATE_ADAPTER].acquireMigrationLock(db);
|
|
259
|
+
const state = await this.#getState(db);
|
|
132
260
|
if (state.migrations.length === 0) {
|
|
133
261
|
return { results: [] };
|
|
134
262
|
}
|
|
135
|
-
const targetIndex =
|
|
263
|
+
const targetIndex = getTargetMigrationIndex(state);
|
|
136
264
|
if (targetIndex === undefined) {
|
|
137
265
|
return { results: [] };
|
|
138
266
|
}
|
|
@@ -145,18 +273,18 @@ class MigrationModule {
|
|
|
145
273
|
return { results: [] };
|
|
146
274
|
}
|
|
147
275
|
finally {
|
|
148
|
-
await this.#
|
|
276
|
+
await this.#props.db[private_symbols_js_1.PRIVATE_ADAPTER].releaseMigrationLock(db);
|
|
149
277
|
}
|
|
150
278
|
};
|
|
151
|
-
if (this.#
|
|
152
|
-
return this.#db.transaction().execute(run);
|
|
279
|
+
if (this.#props.db[private_symbols_js_1.PRIVATE_ADAPTER].supportsTransactionalDdl) {
|
|
280
|
+
return this.#props.db.transaction().execute(run);
|
|
153
281
|
}
|
|
154
282
|
else {
|
|
155
|
-
return this.#db.connection().execute(run);
|
|
283
|
+
return this.#props.db.connection().execute(run);
|
|
156
284
|
}
|
|
157
285
|
}
|
|
158
|
-
async #getState(db
|
|
159
|
-
const migrations = await this.#resolveMigrations(
|
|
286
|
+
async #getState(db) {
|
|
287
|
+
const migrations = await this.#resolveMigrations();
|
|
160
288
|
const executedMigrations = await this.#getExecutedMigrations(db);
|
|
161
289
|
this.#ensureMigrationsNotCorrupted(migrations, executedMigrations);
|
|
162
290
|
return (0, object_utils_js_1.freeze)({
|
|
@@ -164,10 +292,8 @@ class MigrationModule {
|
|
|
164
292
|
currentIndex: migrations.findIndex((it) => it.name === (0, object_utils_js_1.getLast)(executedMigrations)),
|
|
165
293
|
});
|
|
166
294
|
}
|
|
167
|
-
async #resolveMigrations(
|
|
168
|
-
const allMigrations =
|
|
169
|
-
? await this.#readMigrationsFromFolder(migrationsOrFolderPath)
|
|
170
|
-
: migrationsOrFolderPath;
|
|
295
|
+
async #resolveMigrations() {
|
|
296
|
+
const allMigrations = await this.#props.provider.getMigrations();
|
|
171
297
|
return Object.keys(allMigrations)
|
|
172
298
|
.sort()
|
|
173
299
|
.map((name) => ({
|
|
@@ -175,20 +301,6 @@ class MigrationModule {
|
|
|
175
301
|
name,
|
|
176
302
|
}));
|
|
177
303
|
}
|
|
178
|
-
async #readMigrationsFromFolder(migrationsFolderPath) {
|
|
179
|
-
const files = await fs_1.promises.readdir(migrationsFolderPath);
|
|
180
|
-
const migrations = {};
|
|
181
|
-
for (const file of files) {
|
|
182
|
-
if ((file.endsWith('.js') || file.endsWith('.ts')) &&
|
|
183
|
-
!file.endsWith('.d.ts')) {
|
|
184
|
-
const migration = await Promise.resolve().then(() => require(path.join(migrationsFolderPath, file)));
|
|
185
|
-
if (isMigration(migration)) {
|
|
186
|
-
migrations[file.substring(0, file.length - 3)] = migration;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return migrations;
|
|
191
|
-
}
|
|
192
304
|
async #getExecutedMigrations(db) {
|
|
193
305
|
const executedMigrations = await db
|
|
194
306
|
.selectFrom(exports.MIGRATION_TABLE)
|
|
@@ -204,7 +316,7 @@ class MigrationModule {
|
|
|
204
316
|
}
|
|
205
317
|
}
|
|
206
318
|
// Now we know all executed migrations exist in the `migrations` list.
|
|
207
|
-
// Next we need to make sure that the executed
|
|
319
|
+
// Next we need to make sure that the executed migratiosns are the first
|
|
208
320
|
// ones in the migration list.
|
|
209
321
|
for (let i = 0; i < executedMigrations.length; ++i) {
|
|
210
322
|
if (migrations[i].name !== executedMigrations[i]) {
|
|
@@ -292,7 +404,7 @@ class MigrationModule {
|
|
|
292
404
|
return { results };
|
|
293
405
|
}
|
|
294
406
|
}
|
|
295
|
-
exports.
|
|
407
|
+
exports.Migrator = Migrator;
|
|
296
408
|
class MigrationResultSetError extends Error {
|
|
297
409
|
#resultSet;
|
|
298
410
|
constructor(result) {
|
|
@@ -303,6 +415,3 @@ class MigrationResultSetError extends Error {
|
|
|
303
415
|
return this.#resultSet;
|
|
304
416
|
}
|
|
305
417
|
}
|
|
306
|
-
function isMigration(obj) {
|
|
307
|
-
return (0, object_utils_js_1.isObject)(obj) && (0, object_utils_js_1.isFunction)(obj.up);
|
|
308
|
-
}
|
|
@@ -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 {};
|
|
@@ -4,7 +4,6 @@ exports.parseInsertObjectOrList = void 0;
|
|
|
4
4
|
const column_node_js_1 = require("../operation-node/column-node.js");
|
|
5
5
|
const primitive_value_list_node_js_1 = require("../operation-node/primitive-value-list-node.js");
|
|
6
6
|
const value_list_node_js_1 = require("../operation-node/value-list-node.js");
|
|
7
|
-
const generated_placeholder_js_1 = require("../util/generated-placeholder.js");
|
|
8
7
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
9
8
|
const value_parser_js_1 = require("./value-parser.js");
|
|
10
9
|
const values_node_js_1 = require("../operation-node/values-node.js");
|
|
@@ -24,7 +23,7 @@ function parseColumnNamesAndIndexes(rows) {
|
|
|
24
23
|
for (const row of rows) {
|
|
25
24
|
const cols = Object.keys(row);
|
|
26
25
|
for (const col of cols) {
|
|
27
|
-
if (!columns.has(col) &&
|
|
26
|
+
if (!columns.has(col) && row[col] !== undefined) {
|
|
28
27
|
columns.set(col, columns.size);
|
|
29
28
|
}
|
|
30
29
|
}
|
|
@@ -37,7 +36,7 @@ function parseRowValues(ctx, row, columns) {
|
|
|
37
36
|
for (const col of rowColumns) {
|
|
38
37
|
const columnIdx = columns.get(col);
|
|
39
38
|
const value = row[col];
|
|
40
|
-
if (columnIdx !== undefined
|
|
39
|
+
if (columnIdx !== undefined) {
|
|
41
40
|
rowValues[columnIdx] = value;
|
|
42
41
|
}
|
|
43
42
|
}
|
|
@@ -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
|
*
|