kysely 0.14.1 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +92 -32
  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.d.ts +3 -1
  6. package/dist/cjs/index.js +3 -1
  7. package/dist/cjs/kysely.d.ts +7 -39
  8. package/dist/cjs/kysely.js +8 -40
  9. package/dist/cjs/migration/file-migration-provider.d.ts +52 -0
  10. package/dist/cjs/migration/file-migration-provider.js +52 -0
  11. package/dist/cjs/migration/{migration.d.ts → migrator.d.ts} +159 -134
  12. package/dist/cjs/migration/{migration.js → migrator.js} +166 -57
  13. package/dist/cjs/parser/insert-values-parser.d.ts +4 -4
  14. package/dist/cjs/parser/insert-values-parser.js +2 -3
  15. package/dist/cjs/parser/reference-parser.d.ts +2 -1
  16. package/dist/cjs/parser/select-parser.d.ts +9 -3
  17. package/dist/cjs/parser/update-set-parser.d.ts +2 -1
  18. package/dist/cjs/query-builder/delete-query-builder.d.ts +0 -3
  19. package/dist/cjs/query-builder/insert-query-builder.d.ts +3 -20
  20. package/dist/cjs/query-builder/insert-query-builder.js +2 -7
  21. package/dist/cjs/query-builder/on-conflict-builder.d.ts +2 -2
  22. package/dist/cjs/query-builder/on-conflict-builder.js +2 -2
  23. package/dist/cjs/query-builder/returning-interface.d.ts +0 -3
  24. package/dist/cjs/query-builder/update-query-builder.d.ts +0 -3
  25. package/dist/cjs/query-creator.d.ts +0 -2
  26. package/dist/cjs/query-creator.js +0 -2
  27. package/dist/cjs/util/column-type.d.ts +165 -0
  28. package/dist/cjs/util/column-type.js +2 -0
  29. package/dist/cjs/util/private-symbols.d.ts +5 -0
  30. package/dist/cjs/util/private-symbols.js +8 -0
  31. package/dist/cjs/util/type-utils.d.ts +0 -40
  32. package/dist/esm/dialect/mysql/mysql-introspector.js +1 -1
  33. package/dist/esm/dialect/postgres/postgres-introspector.js +1 -1
  34. package/dist/esm/dialect/sqlite/sqlite-introspector.js +1 -1
  35. package/dist/esm/index.d.ts +3 -1
  36. package/dist/esm/index.js +3 -1
  37. package/dist/esm/kysely.d.ts +7 -39
  38. package/dist/esm/kysely.js +8 -40
  39. package/dist/esm/migration/file-migration-provider.d.ts +52 -0
  40. package/dist/esm/migration/file-migration-provider.js +48 -0
  41. package/dist/esm/migration/{migration.d.ts → migrator.d.ts} +159 -134
  42. package/dist/esm/migration/{migration.js → migrator.js} +165 -56
  43. package/dist/esm/parser/insert-values-parser.d.ts +4 -4
  44. package/dist/esm/parser/insert-values-parser.js +2 -3
  45. package/dist/esm/parser/reference-parser.d.ts +2 -1
  46. package/dist/esm/parser/select-parser.d.ts +9 -3
  47. package/dist/esm/parser/update-set-parser.d.ts +2 -1
  48. package/dist/esm/query-builder/delete-query-builder.d.ts +0 -3
  49. package/dist/esm/query-builder/insert-query-builder.d.ts +3 -20
  50. package/dist/esm/query-builder/insert-query-builder.js +1 -6
  51. package/dist/esm/query-builder/on-conflict-builder.d.ts +2 -2
  52. package/dist/esm/query-builder/on-conflict-builder.js +2 -2
  53. package/dist/esm/query-builder/returning-interface.d.ts +0 -3
  54. package/dist/esm/query-builder/update-query-builder.d.ts +0 -3
  55. package/dist/esm/query-creator.d.ts +0 -2
  56. package/dist/esm/query-creator.js +0 -2
  57. package/dist/esm/util/column-type.d.ts +165 -0
  58. package/dist/esm/util/column-type.js +1 -0
  59. package/dist/esm/util/private-symbols.d.ts +5 -0
  60. package/dist/esm/util/private-symbols.js +5 -0
  61. package/dist/esm/util/type-utils.d.ts +0 -40
  62. package/package.json +1 -1
  63. package/dist/cjs/util/generated-placeholder.d.ts +0 -3
  64. package/dist/cjs/util/generated-placeholder.js +0 -11
  65. package/dist/esm/util/generated-placeholder.d.ts +0 -3
  66. 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>;
@@ -539,7 +539,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
539
539
  * const { id } = await db
540
540
  * .insertInto('person')
541
541
  * .values({
542
- * id: db.generated,
543
542
  * first_name: 'Jennifer',
544
543
  * last_name: 'Aniston'
545
544
  * })
@@ -553,7 +552,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
553
552
  * const { id, first_name } = await db
554
553
  * .insertInto('person')
555
554
  * .values({
556
- * id: db.generated,
557
555
  * first_name: 'Jennifer',
558
556
  * last_name: 'Aniston'
559
557
  * })
@@ -569,7 +567,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
569
567
  * const { id, full_name, first_pet_id } = await db
570
568
  * .insertInto('person')
571
569
  * .values({
572
- * id: db.generated,
573
570
  * first_name: 'Jennifer',
574
571
  * last_name: 'Aniston'
575
572
  * })
@@ -23,13 +23,10 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
23
23
  *
24
24
  * This method takes an object whose keys are column names and values are
25
25
  * values to insert. In addition to the column's type, the values can be
26
- * {@link Kysely.raw | raw} instances, select queries or the
27
- * {@link Kysely.generated | generated} placeholder.
26
+ * {@link Kysely.raw | raw} instances or select queries.
28
27
  *
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.
28
+ * You must provide all fields you haven't explicitly marked as nullable
29
+ * or optional using {@link Generated} or {@link ColumnType}.
33
30
  *
34
31
  * The return value of an `insert` query is an instance of {@link InsertResult}. The
35
32
  * {@link InsertResult.insertId | insertId} field holds the auto incremented primary
@@ -48,7 +45,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
48
45
  * const id = await db
49
46
  * .insertInto('person')
50
47
  * .values({
51
- * id: db.generated,
52
48
  * first_name: 'Jennifer',
53
49
  * last_name: 'Aniston'
54
50
  * })
@@ -70,11 +66,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
70
66
  * await db
71
67
  * .insertInto('person')
72
68
  * .values([{
73
- * id: db.generated,
74
69
  * first_name: 'Jennifer',
75
70
  * last_name: 'Aniston'
76
71
  * }, {
77
- * id: db.generated,
78
72
  * first_name: 'Arnold',
79
73
  * last_name: 'Schwarzenegger',
80
74
  * }])
@@ -95,7 +89,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
95
89
  * const row = await db
96
90
  * .insertInto('person')
97
91
  * .values({
98
- * id: db.generated,
99
92
  * first_name: 'Jennifer',
100
93
  * last_name: 'Aniston'
101
94
  * })
@@ -118,7 +111,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
118
111
  * const result = await db
119
112
  * .insertInto('person')
120
113
  * .values({
121
- * id: db.generated,
122
114
  * first_name: 'Jennifer',
123
115
  * last_name: db.raw('? || ?', ['Ani', 'ston']),
124
116
  * age: db.selectFrom('person').select(raw('avg(age)')),
@@ -144,7 +136,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
144
136
  * .select(['id', 'first_name', 'gender'])
145
137
  * .limit(1)
146
138
  * ).insertInto('pet').values({
147
- * id: db.generated,
148
139
  * owner_id: (eb) => eb.selectFrom('jennifer').select('id'),
149
140
  * name: (eb) => eb.selectFrom('jennifer').select('first_name'),
150
141
  * species: 'cat',
@@ -229,7 +220,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
229
220
  * await db
230
221
  * .insertInto('pet')
231
222
  * .values({
232
- * id: db.generated,
233
223
  * name: 'Catto',
234
224
  * species: 'cat',
235
225
  * })
@@ -255,7 +245,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
255
245
  * await db
256
246
  * .insertInto('pet')
257
247
  * .values({
258
- * id: db.generated,
259
248
  * name: 'Catto',
260
249
  * species: 'cat',
261
250
  * })
@@ -283,7 +272,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
283
272
  * await db
284
273
  * .insertInto('pet')
285
274
  * .values({
286
- * id: db.generated,
287
275
  * name: 'Catto',
288
276
  * species: 'cat',
289
277
  * })
@@ -309,7 +297,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
309
297
  * await db
310
298
  * .insertInto('pet')
311
299
  * .values({
312
- * id: db.generated,
313
300
  * name: 'Catto',
314
301
  * species: 'cat',
315
302
  * })
@@ -337,7 +324,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
337
324
  * await db
338
325
  * .insertInto('pet')
339
326
  * .values({
340
- * id: db.generated,
341
327
  * name: 'Catto',
342
328
  * species: 'cat',
343
329
  * })
@@ -396,7 +382,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
396
382
  * const { id } = await db
397
383
  * .insertInto('person')
398
384
  * .values({
399
- * id: db.generated,
400
385
  * first_name: 'Jennifer',
401
386
  * last_name: 'Aniston'
402
387
  * })
@@ -410,7 +395,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
410
395
  * const { id, first_name } = await db
411
396
  * .insertInto('person')
412
397
  * .values({
413
- * id: db.generated,
414
398
  * first_name: 'Jennifer',
415
399
  * last_name: 'Aniston'
416
400
  * })
@@ -426,7 +410,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
426
410
  * const { id, full_name, first_pet_id } = await db
427
411
  * .insertInto('person')
428
412
  * .values({
429
- * id: db.generated,
430
413
  * first_name: 'Jennifer',
431
414
  * last_name: 'Aniston'
432
415
  * })
@@ -13,8 +13,8 @@ const insert_result_js_1 = require("./insert-result.js");
13
13
  const no_result_error_js_1 = require("./no-result-error.js");
14
14
  const complex_expression_parser_js_1 = require("../parser/complex-expression-parser.js");
15
15
  const column_node_js_1 = require("../operation-node/column-node.js");
16
- const index_js_1 = require("../index.js");
17
16
  const on_conflict_builder_js_1 = require("./on-conflict-builder.js");
17
+ const on_conflict_node_js_1 = require("../operation-node/on-conflict-node.js");
18
18
  class InsertQueryBuilder {
19
19
  #props;
20
20
  constructor(props) {
@@ -127,7 +127,6 @@ class InsertQueryBuilder {
127
127
  * await db
128
128
  * .insertInto('pet')
129
129
  * .values({
130
- * id: db.generated,
131
130
  * name: 'Catto',
132
131
  * species: 'cat',
133
132
  * })
@@ -153,7 +152,6 @@ class InsertQueryBuilder {
153
152
  * await db
154
153
  * .insertInto('pet')
155
154
  * .values({
156
- * id: db.generated,
157
155
  * name: 'Catto',
158
156
  * species: 'cat',
159
157
  * })
@@ -181,7 +179,6 @@ class InsertQueryBuilder {
181
179
  * await db
182
180
  * .insertInto('pet')
183
181
  * .values({
184
- * id: db.generated,
185
182
  * name: 'Catto',
186
183
  * species: 'cat',
187
184
  * })
@@ -207,7 +204,6 @@ class InsertQueryBuilder {
207
204
  * await db
208
205
  * .insertInto('pet')
209
206
  * .values({
210
- * id: db.generated,
211
207
  * name: 'Catto',
212
208
  * species: 'cat',
213
209
  * })
@@ -235,7 +231,6 @@ class InsertQueryBuilder {
235
231
  * await db
236
232
  * .insertInto('pet')
237
233
  * .values({
238
- * id: db.generated,
239
234
  * name: 'Catto',
240
235
  * species: 'cat',
241
236
  * })
@@ -259,7 +254,7 @@ class InsertQueryBuilder {
259
254
  ...this.#props,
260
255
  queryNode: insert_query_node_js_1.InsertQueryNode.cloneWith(this.#props.queryNode, {
261
256
  onConflict: callback(new on_conflict_builder_js_1.OnConflictBuilder({
262
- onConflictNode: index_js_1.OnConflictNode.create(),
257
+ onConflictNode: on_conflict_node_js_1.OnConflictNode.create(),
263
258
  parseContext: this.#props.parseContext,
264
259
  })).toOperationNode(),
265
260
  }),
@@ -337,7 +337,7 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
337
337
  * ```ts
338
338
  * await db
339
339
  * .insertInto('person')
340
- * .values({ id: db.generated, first_name, pic })
340
+ * .values({ first_name, pic })
341
341
  * .onConflict((oc) => oc
342
342
  * .column('pic')
343
343
  * .doNothing()
@@ -361,7 +361,7 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
361
361
  * ```ts
362
362
  * await db
363
363
  * .insertInto('person')
364
- * .values({ id: db.generated, first_name, pic })
364
+ * .values({ first_name, pic })
365
365
  * .onConflict((oc) => oc
366
366
  * .column('pic')
367
367
  * .doUpdateSet({ first_name })