kysely 0.14.1 → 0.15.3

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.
Files changed (72) hide show
  1. package/README.md +95 -39
  2. package/dist/cjs/dialect/mysql/mysql-introspector.js +3 -3
  3. package/dist/cjs/dialect/postgres/postgres-introspector.js +3 -3
  4. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +3 -3
  5. package/dist/cjs/index-nodeless.d.ts +148 -0
  6. package/dist/cjs/index-nodeless.js +149 -0
  7. package/dist/cjs/index.d.ts +6 -151
  8. package/dist/cjs/index.js +6 -140
  9. package/dist/cjs/kysely.d.ts +7 -39
  10. package/dist/cjs/kysely.js +8 -40
  11. package/dist/cjs/migration/file-migration-provider.d.ts +52 -0
  12. package/dist/cjs/migration/file-migration-provider.js +52 -0
  13. package/dist/cjs/migration/{migration.d.ts → migrator.d.ts} +159 -134
  14. package/dist/cjs/migration/{migration.js → migrator.js} +166 -57
  15. package/dist/cjs/parser/insert-values-parser.d.ts +4 -4
  16. package/dist/cjs/parser/insert-values-parser.js +2 -3
  17. package/dist/cjs/parser/reference-parser.d.ts +2 -1
  18. package/dist/cjs/parser/select-parser.d.ts +9 -3
  19. package/dist/cjs/parser/update-set-parser.d.ts +2 -1
  20. package/dist/cjs/query-builder/delete-query-builder.d.ts +2 -4
  21. package/dist/cjs/query-builder/insert-query-builder.d.ts +5 -21
  22. package/dist/cjs/query-builder/insert-query-builder.js +2 -7
  23. package/dist/cjs/query-builder/on-conflict-builder.d.ts +2 -2
  24. package/dist/cjs/query-builder/on-conflict-builder.js +2 -2
  25. package/dist/cjs/query-builder/returning-interface.d.ts +2 -4
  26. package/dist/cjs/query-builder/select-query-builder.d.ts +1 -1
  27. package/dist/cjs/query-builder/update-query-builder.d.ts +2 -4
  28. package/dist/cjs/query-creator.d.ts +0 -2
  29. package/dist/cjs/query-creator.js +0 -2
  30. package/dist/cjs/util/column-type.d.ts +165 -0
  31. package/dist/cjs/util/column-type.js +2 -0
  32. package/dist/cjs/util/private-symbols.d.ts +5 -0
  33. package/dist/cjs/util/private-symbols.js +8 -0
  34. package/dist/cjs/util/type-utils.d.ts +0 -40
  35. package/dist/esm/dialect/mysql/mysql-introspector.js +1 -1
  36. package/dist/esm/dialect/postgres/postgres-introspector.js +1 -1
  37. package/dist/esm/dialect/sqlite/sqlite-introspector.js +1 -1
  38. package/dist/esm/index-nodeless.d.ts +148 -0
  39. package/dist/esm/index-nodeless.js +137 -0
  40. package/dist/esm/index.d.ts +6 -151
  41. package/dist/esm/index.js +6 -140
  42. package/dist/esm/kysely.d.ts +7 -39
  43. package/dist/esm/kysely.js +8 -40
  44. package/dist/esm/migration/file-migration-provider.d.ts +52 -0
  45. package/dist/esm/migration/file-migration-provider.js +48 -0
  46. package/dist/esm/migration/{migration.d.ts → migrator.d.ts} +159 -134
  47. package/dist/esm/migration/{migration.js → migrator.js} +165 -56
  48. package/dist/esm/parser/insert-values-parser.d.ts +4 -4
  49. package/dist/esm/parser/insert-values-parser.js +2 -3
  50. package/dist/esm/parser/reference-parser.d.ts +2 -1
  51. package/dist/esm/parser/select-parser.d.ts +9 -3
  52. package/dist/esm/parser/update-set-parser.d.ts +2 -1
  53. package/dist/esm/query-builder/delete-query-builder.d.ts +2 -4
  54. package/dist/esm/query-builder/insert-query-builder.d.ts +5 -21
  55. package/dist/esm/query-builder/insert-query-builder.js +1 -6
  56. package/dist/esm/query-builder/on-conflict-builder.d.ts +2 -2
  57. package/dist/esm/query-builder/on-conflict-builder.js +2 -2
  58. package/dist/esm/query-builder/returning-interface.d.ts +2 -4
  59. package/dist/esm/query-builder/select-query-builder.d.ts +1 -1
  60. package/dist/esm/query-builder/update-query-builder.d.ts +2 -4
  61. package/dist/esm/query-creator.d.ts +0 -2
  62. package/dist/esm/query-creator.js +0 -2
  63. package/dist/esm/util/column-type.d.ts +165 -0
  64. package/dist/esm/util/column-type.js +1 -0
  65. package/dist/esm/util/private-symbols.d.ts +5 -0
  66. package/dist/esm/util/private-symbols.js +5 -0
  67. package/dist/esm/util/type-utils.d.ts +0 -40
  68. package/package.json +4 -2
  69. package/dist/cjs/util/generated-placeholder.d.ts +0 -3
  70. package/dist/cjs/util/generated-placeholder.js +0 -11
  71. package/dist/esm/util/generated-placeholder.d.ts +0 -3
  72. 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.MigrationModule = exports.NO_MIGRATIONS = exports.MIGRATION_LOCK_ID = exports.MIGRATION_LOCK_TABLE = exports.MIGRATION_TABLE = void 0;
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
- class MigrationModule {
12
- #db;
13
- #adapter;
14
- constructor(db, adapter) {
15
- this.#db = db;
16
- this.#adapter = adapter;
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
- async migrateToLatest(migrationsOrFolderPath) {
19
- return this.#migrate(migrationsOrFolderPath, ({ migrations }) => migrations.length - 1);
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
- async migrateTo(migrationsOrFolderPath, targetMigrationName) {
22
- return this.#migrate(migrationsOrFolderPath, ({ migrations }) => {
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
- async migrateUp(migrationsOrFolderPath) {
34
- return this.#migrate(migrationsOrFolderPath, ({ currentIndex, migrations }) => Math.min(currentIndex + 1, migrations.length - 1));
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
- async migrateDown(migrationsOrFolderPath) {
37
- return this.#migrate(migrationsOrFolderPath, ({ currentIndex }) => Math.max(currentIndex - 1, -1));
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(migrationsOrFolderPath, getTargetMigration) {
167
+ async #migrate(getTargetMigrationIndex) {
40
168
  try {
41
169
  await this.#ensureMigrationTablesExists();
42
- return await this.#runMigrations(migrationsOrFolderPath, getTargetMigration);
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(migrationsOrFolderPath, getTargetMigration) {
255
+ async #runMigrations(getTargetMigrationIndex) {
128
256
  const run = async (db) => {
129
257
  try {
130
- await this.#adapter.acquireMigrationLock(db);
131
- const state = await this.#getState(db, migrationsOrFolderPath);
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 = getTargetMigration(state);
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.#adapter.releaseMigrationLock(db);
276
+ await this.#props.db[private_symbols_js_1.PRIVATE_ADAPTER].releaseMigrationLock(db);
149
277
  }
150
278
  };
151
- if (this.#adapter.supportsTransactionalDdl) {
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, migrationsOrFolderPath) {
159
- const migrations = await this.#resolveMigrations(migrationsOrFolderPath);
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(migrationsOrFolderPath) {
168
- const allMigrations = (0, object_utils_js_1.isString)(migrationsOrFolderPath)
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 migrations are the first
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.MigrationModule = MigrationModule;
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 keyof DB[TB]]: InsertValueExpression<DB, TB, DB[TB][C]>;
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) && !(0, generated_placeholder_js_1.isGeneratedPlaceholder)(row[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 && !(0, generated_placeholder_js_1.isGeneratedPlaceholder)(value)) {
39
+ if (columnIdx !== undefined) {
41
40
  rowValues[columnIdx] = value;
42
41
  }
43
42
  }
@@ -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> : 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
+ 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, RowType, ValueType } from '../util/type-utils.js';
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 & RowType<DB, S>>;
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 keyof DB[TB]]?: ValueExpression<DB, TB, DB[TB][C]>;
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, select queries or the
27
- * {@link Kysely.generated | generated} placeholder.
27
+ * {@link Kysely.raw | raw} instances or select queries.
28
28
  *
29
- * You must provide all values defined by the interface of the table you
30
- * are inserting into. Values that are generated by the database like auto incrementing
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
  *