kysely 0.18.0 → 0.18.1

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 (55) hide show
  1. package/dist/cjs/dialect/database-introspector.d.ts +13 -0
  2. package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +9 -1
  3. package/dist/cjs/dialect/mysql/mysql-dialect.js +11 -5
  4. package/dist/cjs/dialect/mysql/mysql-driver.d.ts +25 -3
  5. package/dist/cjs/dialect/mysql/mysql-driver.js +48 -35
  6. package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +11 -1
  7. package/dist/cjs/dialect/mysql/mysql-introspector.js +29 -7
  8. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +9 -1
  9. package/dist/cjs/dialect/postgres/postgres-dialect.js +11 -5
  10. package/dist/cjs/dialect/postgres/postgres-driver.d.ts +25 -3
  11. package/dist/cjs/dialect/postgres/postgres-driver.js +37 -24
  12. package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +11 -1
  13. package/dist/cjs/dialect/postgres/postgres-introspector.js +15 -4
  14. package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +11 -1
  15. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +11 -4
  16. package/dist/cjs/index-nodeless.js +5 -1
  17. package/dist/cjs/index.js +5 -1
  18. package/dist/cjs/migration/migrator.d.ts +44 -2
  19. package/dist/cjs/migration/migrator.js +77 -18
  20. package/dist/cjs/plugin/noop-plugin.d.ts +42 -0
  21. package/dist/cjs/plugin/noop-plugin.js +12 -0
  22. package/dist/cjs/query-creator.d.ts +9 -0
  23. package/dist/cjs/query-creator.js +18 -0
  24. package/dist/cjs/schema/schema.d.ts +9 -0
  25. package/dist/cjs/schema/schema.js +12 -0
  26. package/dist/cjs/util/column-type.d.ts +9 -1
  27. package/dist/cjs/util/stack-trace-utils.d.ts +1 -0
  28. package/dist/cjs/util/stack-trace-utils.js +17 -0
  29. package/dist/esm/dialect/database-introspector.d.ts +13 -0
  30. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +9 -1
  31. package/dist/esm/dialect/mysql/mysql-dialect.js +11 -5
  32. package/dist/esm/dialect/mysql/mysql-driver.d.ts +25 -3
  33. package/dist/esm/dialect/mysql/mysql-driver.js +48 -35
  34. package/dist/esm/dialect/mysql/mysql-introspector.d.ts +11 -1
  35. package/dist/esm/dialect/mysql/mysql-introspector.js +30 -8
  36. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +9 -1
  37. package/dist/esm/dialect/postgres/postgres-dialect.js +11 -5
  38. package/dist/esm/dialect/postgres/postgres-driver.d.ts +25 -3
  39. package/dist/esm/dialect/postgres/postgres-driver.js +38 -25
  40. package/dist/esm/dialect/postgres/postgres-introspector.d.ts +11 -1
  41. package/dist/esm/dialect/postgres/postgres-introspector.js +16 -5
  42. package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +11 -1
  43. package/dist/esm/dialect/sqlite/sqlite-introspector.js +12 -5
  44. package/dist/esm/migration/migrator.d.ts +44 -2
  45. package/dist/esm/migration/migrator.js +76 -17
  46. package/dist/esm/plugin/noop-plugin.d.ts +42 -0
  47. package/dist/esm/plugin/noop-plugin.js +9 -0
  48. package/dist/esm/query-creator.d.ts +9 -0
  49. package/dist/esm/query-creator.js +18 -0
  50. package/dist/esm/schema/schema.d.ts +9 -0
  51. package/dist/esm/schema/schema.js +12 -0
  52. package/dist/esm/util/column-type.d.ts +9 -1
  53. package/dist/esm/util/stack-trace-utils.d.ts +1 -0
  54. package/dist/esm/util/stack-trace-utils.js +14 -0
  55. package/package.json +9 -9
@@ -1,24 +1,30 @@
1
1
  /// <reference types="./mysql-driver.d.ts" />
2
2
  import { CompiledQuery } from '../../query-compiler/compiled-query.js';
3
3
  import { isFunction, isObject, freeze } from '../../util/object-utils.js';
4
+ import { extendStackTrace } from '../../util/stack-trace-utils.js';
4
5
  const PRIVATE_RELEASE_METHOD = Symbol();
5
6
  export class MysqlDriver {
6
7
  #config;
7
8
  #connections = new WeakMap();
8
9
  #pool;
9
- constructor(config) {
10
- this.#config = freeze({ ...config });
10
+ constructor(configOrPool) {
11
+ if (isMysqlPool(configOrPool)) {
12
+ this.#pool = configOrPool;
13
+ }
14
+ else {
15
+ this.#config = freeze({ ...configOrPool });
16
+ }
11
17
  }
12
18
  async init() {
13
- // Import the `mysql2` module here instead at the top of the file
14
- // so that this file can be loaded by node without `mysql2` driver
15
- // installed. As you can see, there IS an import from `mysql2` at the
16
- // top level too, but that's only for types. It doesn't get compiled
17
- // into javascript. You can check the built javascript code.
18
- const poolFactory = await importMysqlPoolFactory();
19
- // Use the `mysql2` module's own pool. All drivers should use the
20
- // pool provided by the database library if possible.
21
- this.#pool = poolFactory(this.#config);
19
+ if (this.#config) {
20
+ // Import the `mysql2` module here instead at the top of the file
21
+ // so that this file can be loaded by node without `mysql2` driver
22
+ // installed.
23
+ const poolFactory = await importMysqlPoolFactory();
24
+ // Use the `mysql2` module's own pool. All drivers should use the
25
+ // pool provided by the database library if possible.
26
+ this.#pool = poolFactory(this.#config);
27
+ }
22
28
  }
23
29
  async acquireConnection() {
24
30
  const rawConnection = await this.#acquireConnection();
@@ -29,7 +35,7 @@ export class MysqlDriver {
29
35
  // The driver must take care of calling `onCreateConnection` when a new
30
36
  // connection is created. The `mysql2` module doesn't provide an async hook
31
37
  // for the connection creation. We need to call the method explicitly.
32
- if (this.#config.onCreateConnection) {
38
+ if (this.#config?.onCreateConnection) {
33
39
  await this.#config.onCreateConnection(connection);
34
40
  }
35
41
  }
@@ -61,8 +67,7 @@ export class MysqlDriver {
61
67
  await connection.executeQuery(CompiledQuery.raw('rollback'));
62
68
  }
63
69
  async releaseConnection(connection) {
64
- const mysqlConnection = connection;
65
- mysqlConnection[PRIVATE_RELEASE_METHOD]();
70
+ connection[PRIVATE_RELEASE_METHOD]();
66
71
  }
67
72
  async destroy() {
68
73
  return new Promise((resolve, reject) => {
@@ -77,6 +82,12 @@ export class MysqlDriver {
77
82
  });
78
83
  }
79
84
  }
85
+ function isMysqlPool(obj) {
86
+ return isObject(obj) && isFunction(obj.getConnection) && isFunction(obj.end);
87
+ }
88
+ function isOkPacket(obj) {
89
+ return isObject(obj) && 'insertId' in obj && 'affectedRows' in obj;
90
+ }
80
91
  async function importMysqlPoolFactory() {
81
92
  try {
82
93
  // The imported module name must be a string literal to make
@@ -100,29 +111,34 @@ class MysqlConnection {
100
111
  this.#rawConnection = rawConnection;
101
112
  }
102
113
  async executeQuery(compiledQuery) {
103
- const result = await this.#executeQuery(compiledQuery);
104
- if (isOkPacket(result)) {
105
- const { insertId, affectedRows } = result;
114
+ try {
115
+ const result = await this.#executeQuery(compiledQuery);
116
+ if (isOkPacket(result)) {
117
+ const { insertId, affectedRows } = result;
118
+ return {
119
+ insertId: insertId !== undefined &&
120
+ insertId !== null &&
121
+ insertId.toString() !== '0'
122
+ ? BigInt(insertId)
123
+ : undefined,
124
+ numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
125
+ ? BigInt(affectedRows)
126
+ : undefined,
127
+ rows: [],
128
+ };
129
+ }
130
+ else if (Array.isArray(result)) {
131
+ return {
132
+ rows: result,
133
+ };
134
+ }
106
135
  return {
107
- insertId: insertId !== undefined &&
108
- insertId !== null &&
109
- insertId.toString() !== '0'
110
- ? BigInt(insertId)
111
- : undefined,
112
- numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
113
- ? BigInt(affectedRows)
114
- : undefined,
115
136
  rows: [],
116
137
  };
117
138
  }
118
- else if (Array.isArray(result)) {
119
- return {
120
- rows: result,
121
- };
139
+ catch (err) {
140
+ throw extendStackTrace(err, new Error());
122
141
  }
123
- return {
124
- rows: [],
125
- };
126
142
  }
127
143
  #executeQuery(compiledQuery) {
128
144
  return new Promise((resolve, reject) => {
@@ -140,6 +156,3 @@ class MysqlConnection {
140
156
  this.#rawConnection.release();
141
157
  }
142
158
  }
143
- function isOkPacket(obj) {
144
- return isObject(obj) && 'insertId' in obj && 'affectedRows' in obj;
145
- }
@@ -1,10 +1,20 @@
1
- import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions } from '../database-introspector.js';
1
+ import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions, SchemaMetadata, TableMetadata } from '../database-introspector.js';
2
2
  import { Kysely } from '../../kysely.js';
3
3
  export declare class MysqlIntrospector implements DatabaseIntrospector {
4
4
  #private;
5
5
  constructor(db: Kysely<any>);
6
+ /**
7
+ * Get schema metadata.
8
+ */
9
+ getSchemas(): Promise<SchemaMetadata[]>;
10
+ /**
11
+ * Get table metadata.
12
+ */
13
+ getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
6
14
  /**
7
15
  * Get the database metadata such as table and column names.
16
+ *
17
+ * @deprecated Use getTables() instead.
8
18
  */
9
19
  getMetadata(options?: DatabaseMetadataOptions): Promise<DatabaseMetadata>;
10
20
  }
@@ -1,26 +1,47 @@
1
1
  /// <reference types="./mysql-introspector.d.ts" />
2
- import { MIGRATION_LOCK_TABLE, MIGRATION_TABLE, } from '../../migration/migrator.js';
2
+ import { DEFAULT_MIGRATION_LOCK_TABLE, DEFAULT_MIGRATION_TABLE, } from '../../migration/migrator.js';
3
3
  import { freeze } from '../../util/object-utils.js';
4
- import { sql } from '../../raw-builder/sql.js';
5
4
  export class MysqlIntrospector {
6
5
  #db;
7
6
  constructor(db) {
8
7
  this.#db = db;
9
8
  }
10
- async getMetadata(options = { withInternalKyselyTables: false }) {
9
+ async getSchemas() {
10
+ let rawSchemas = await this.#db
11
+ .selectFrom('information_schema.schemata')
12
+ .select('schema_name')
13
+ .castTo()
14
+ .execute();
15
+ return rawSchemas.map((it) => ({ name: it.SCHEMA_NAME }));
16
+ }
17
+ async getTables(options = { withInternalKyselyTables: false }) {
11
18
  let query = this.#db
12
19
  .selectFrom('information_schema.columns')
13
- .selectAll()
14
- .where('table_schema', '=', sql `database()`)
20
+ .select([
21
+ 'column_name',
22
+ 'table_name',
23
+ 'table_schema',
24
+ 'is_nullable',
25
+ 'data_type',
26
+ ])
27
+ .where('table_schema', 'not in', [
28
+ 'information_schema',
29
+ 'performance_schema',
30
+ 'mysql',
31
+ 'sys',
32
+ ])
15
33
  .castTo();
16
34
  if (!options.withInternalKyselyTables) {
17
35
  query = query
18
- .where('table_name', '!=', MIGRATION_TABLE)
19
- .where('table_name', '!=', MIGRATION_LOCK_TABLE);
36
+ .where('table_name', '!=', DEFAULT_MIGRATION_TABLE)
37
+ .where('table_name', '!=', DEFAULT_MIGRATION_LOCK_TABLE);
20
38
  }
21
39
  const rawColumns = await query.execute();
40
+ return this.#parseTableMetadata(rawColumns);
41
+ }
42
+ async getMetadata(options) {
22
43
  return {
23
- tables: this.#parseTableMetadata(rawColumns),
44
+ tables: await this.getTables(options),
24
45
  };
25
46
  }
26
47
  #parseTableMetadata(columns) {
@@ -29,6 +50,7 @@ export class MysqlIntrospector {
29
50
  if (!table) {
30
51
  table = freeze({
31
52
  name: it.TABLE_NAME,
53
+ schema: it.TABLE_SCHEMA,
32
54
  columns: [],
33
55
  });
34
56
  tables.push(table);
@@ -2,6 +2,7 @@ import { Driver } from '../../driver/driver.js';
2
2
  import { Kysely } from '../../kysely.js';
3
3
  import { QueryCompiler } from '../../query-compiler/query-compiler.js';
4
4
  import { Dialect } from '../dialect.js';
5
+ import { PostgresPool } from './postgres-driver.js';
5
6
  import { DatabaseIntrospector } from '../database-introspector.js';
6
7
  import { DialectAdapter } from '../dialect-adapter.js';
7
8
  import { PostgresDialectConfig } from './postgres-dialect-config.js';
@@ -14,10 +15,17 @@ import { PostgresDialectConfig } from './postgres-dialect-config.js';
14
15
  *
15
16
  * https://node-postgres.com/api/pool
16
17
  * https://node-postgres.com/api/client
18
+ *
19
+ * You can also provide an existing pg `Pool` instance:
20
+ *
21
+ * ```ts
22
+ * import { Pool } from 'pg'
23
+ * new PostgresDialect(new Pool(config))
24
+ * ```
17
25
  */
18
26
  export declare class PostgresDialect implements Dialect {
19
27
  #private;
20
- constructor(config: PostgresDialectConfig);
28
+ constructor(configOrPool: PostgresDialectConfig | PostgresPool);
21
29
  /**
22
30
  * Creates a driver for the dialect.
23
31
  */
@@ -3,7 +3,6 @@ import { PostgresDriver } from './postgres-driver.js';
3
3
  import { PostgresIntrospector } from './postgres-introspector.js';
4
4
  import { PostgresQueryCompiler } from './postgres-query-compiler.js';
5
5
  import { PostgresAdapter } from './postgres-adapter.js';
6
- import { freeze } from '../../util/object-utils.js';
7
6
  /**
8
7
  * PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
9
8
  *
@@ -13,14 +12,21 @@ import { freeze } from '../../util/object-utils.js';
13
12
  *
14
13
  * https://node-postgres.com/api/pool
15
14
  * https://node-postgres.com/api/client
15
+ *
16
+ * You can also provide an existing pg `Pool` instance:
17
+ *
18
+ * ```ts
19
+ * import { Pool } from 'pg'
20
+ * new PostgresDialect(new Pool(config))
21
+ * ```
16
22
  */
17
23
  export class PostgresDialect {
18
- #config;
19
- constructor(config) {
20
- this.#config = freeze({ ...config });
24
+ #configOrPool;
25
+ constructor(configOrPool) {
26
+ this.#configOrPool = configOrPool;
21
27
  }
22
28
  createDriver() {
23
- return new PostgresDriver(this.#config);
29
+ return new PostgresDriver(this.#configOrPool);
24
30
  }
25
31
  createQueryCompiler() {
26
32
  return new PostgresQueryCompiler();
@@ -1,9 +1,11 @@
1
- import { DatabaseConnection } from '../../driver/database-connection.js';
1
+ import { DatabaseConnection, QueryResult } from '../../driver/database-connection.js';
2
2
  import { Driver, TransactionSettings } from '../../driver/driver.js';
3
+ import { CompiledQuery } from '../../query-compiler/compiled-query.js';
3
4
  import { PostgresDialectConfig } from './postgres-dialect-config.js';
5
+ declare const PRIVATE_RELEASE_METHOD: unique symbol;
4
6
  export declare class PostgresDriver implements Driver {
5
7
  #private;
6
- constructor(config: PostgresDialectConfig);
8
+ constructor(configOrPool: PostgresDialectConfig | PostgresPool);
7
9
  /**
8
10
  * Initializes the driver.
9
11
  *
@@ -35,9 +37,29 @@ export declare class PostgresDriver implements Driver {
35
37
  /**
36
38
  * Releases a connection back to the pool.
37
39
  */
38
- releaseConnection(connection: DatabaseConnection): Promise<void>;
40
+ releaseConnection(connection: PostgresConnection): Promise<void>;
39
41
  /**
40
42
  * Destroys the driver and releases all resources.
41
43
  */
42
44
  destroy(): Promise<void>;
43
45
  }
46
+ export interface PostgresPool {
47
+ connect(): Promise<PostgresPoolClient>;
48
+ end(): Promise<void>;
49
+ }
50
+ interface PostgresPoolClient {
51
+ query<R>(sql: string, parameters: ReadonlyArray<unknown>): Promise<PostgresQueryResult<R>>;
52
+ release(): void;
53
+ }
54
+ interface PostgresQueryResult<R> {
55
+ command: 'UPDATE' | 'DELETE' | 'INSERT' | 'SELECT';
56
+ rowCount: number;
57
+ rows: R[];
58
+ }
59
+ declare class PostgresConnection implements DatabaseConnection {
60
+ #private;
61
+ constructor(client: PostgresPoolClient);
62
+ executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
63
+ [PRIVATE_RELEASE_METHOD](): void;
64
+ }
65
+ export {};
@@ -1,24 +1,30 @@
1
1
  /// <reference types="./postgres-driver.d.ts" />
2
2
  import { CompiledQuery } from '../../query-compiler/compiled-query.js';
3
- import { isFunction, freeze } from '../../util/object-utils.js';
3
+ import { isFunction, freeze, isObject } from '../../util/object-utils.js';
4
+ import { extendStackTrace } from '../../util/stack-trace-utils.js';
4
5
  const PRIVATE_RELEASE_METHOD = Symbol();
5
6
  export class PostgresDriver {
6
7
  #config;
7
8
  #connections = new WeakMap();
8
- #pool = null;
9
- constructor(config) {
10
- this.#config = freeze({ ...config });
9
+ #pool;
10
+ constructor(configOrPool) {
11
+ if (isPostgresPool(configOrPool)) {
12
+ this.#pool = configOrPool;
13
+ }
14
+ else {
15
+ this.#config = freeze({ ...configOrPool });
16
+ }
11
17
  }
12
18
  async init() {
13
- // Import the `pg` module here instead at the top of the file
14
- // so that this file can be loaded by node without `pg` driver
15
- // installed. As you can see, there IS an import from `pg` at the
16
- // top level too, but that's only for types. It doesn't get compiled
17
- // into javascript. You can check the built javascript code.
18
- const PoolConstructor = await importPgPool();
19
- // Use the `pg` module's own pool. All drivers should use the
20
- // pool provided by the database library if possible.
21
- this.#pool = new PoolConstructor(this.#config);
19
+ if (this.#config) {
20
+ // Import the `pg` module here instead at the top of the file
21
+ // so that this file can be loaded by node without `pg` driver
22
+ // installed.
23
+ const PoolConstructor = await importPgPool();
24
+ // Use the `pg` module's own pool. All drivers should use the
25
+ // pool provided by the database library if possible.
26
+ this.#pool = new PoolConstructor(this.#config);
27
+ }
22
28
  }
23
29
  async acquireConnection() {
24
30
  const client = await this.#pool.connect();
@@ -29,7 +35,7 @@ export class PostgresDriver {
29
35
  // The driver must take care of calling `onCreateConnection` when a new
30
36
  // connection is created. The `pg` module doesn't provide an async hook
31
37
  // for the connection creation. We need to call the method explicitly.
32
- if (this.#config.onCreateConnection) {
38
+ if (this.#config?.onCreateConnection) {
33
39
  await this.#config.onCreateConnection(connection);
34
40
  }
35
41
  }
@@ -50,17 +56,19 @@ export class PostgresDriver {
50
56
  await connection.executeQuery(CompiledQuery.raw('rollback'));
51
57
  }
52
58
  async releaseConnection(connection) {
53
- const pgConnection = connection;
54
- pgConnection[PRIVATE_RELEASE_METHOD]();
59
+ connection[PRIVATE_RELEASE_METHOD]();
55
60
  }
56
61
  async destroy() {
57
62
  if (this.#pool) {
58
63
  const pool = this.#pool;
59
- this.#pool = null;
64
+ this.#pool = undefined;
60
65
  await pool.end();
61
66
  }
62
67
  }
63
68
  }
69
+ function isPostgresPool(obj) {
70
+ return isObject(obj) && isFunction(obj.connect) && isFunction(obj.end);
71
+ }
64
72
  async function importPgPool() {
65
73
  try {
66
74
  // The imported module name must be a string literal to make
@@ -84,18 +92,23 @@ class PostgresConnection {
84
92
  this.#client = client;
85
93
  }
86
94
  async executeQuery(compiledQuery) {
87
- const result = await this.#client.query(compiledQuery.sql, [
88
- ...compiledQuery.parameters,
89
- ]);
90
- if (result.command === 'UPDATE' || result.command === 'DELETE') {
95
+ try {
96
+ const result = await this.#client.query(compiledQuery.sql, [
97
+ ...compiledQuery.parameters,
98
+ ]);
99
+ if (result.command === 'UPDATE' || result.command === 'DELETE') {
100
+ return {
101
+ numUpdatedOrDeletedRows: BigInt(result.rowCount),
102
+ rows: result.rows ?? [],
103
+ };
104
+ }
91
105
  return {
92
- numUpdatedOrDeletedRows: BigInt(result.rowCount),
93
106
  rows: result.rows ?? [],
94
107
  };
95
108
  }
96
- return {
97
- rows: result.rows ?? [],
98
- };
109
+ catch (err) {
110
+ throw extendStackTrace(err, new Error());
111
+ }
99
112
  }
100
113
  [PRIVATE_RELEASE_METHOD]() {
101
114
  this.#client.release();
@@ -1,10 +1,20 @@
1
- import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions } from '../database-introspector.js';
1
+ import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions, SchemaMetadata, TableMetadata } from '../database-introspector.js';
2
2
  import { Kysely } from '../../kysely.js';
3
3
  export declare class PostgresIntrospector implements DatabaseIntrospector {
4
4
  #private;
5
5
  constructor(db: Kysely<any>);
6
+ /**
7
+ * Get schema metadata.
8
+ */
9
+ getSchemas(): Promise<SchemaMetadata[]>;
10
+ /**
11
+ * Get table metadata.
12
+ */
13
+ getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
6
14
  /**
7
15
  * Get the database metadata such as table and column names.
16
+ *
17
+ * @deprecated Use getTables() instead.
8
18
  */
9
19
  getMetadata(options?: DatabaseMetadataOptions): Promise<DatabaseMetadata>;
10
20
  }
@@ -1,12 +1,20 @@
1
1
  /// <reference types="./postgres-introspector.d.ts" />
2
- import { MIGRATION_LOCK_TABLE, MIGRATION_TABLE, } from '../../migration/migrator.js';
2
+ import { DEFAULT_MIGRATION_LOCK_TABLE, DEFAULT_MIGRATION_TABLE, } from '../../migration/migrator.js';
3
3
  import { freeze } from '../../util/object-utils.js';
4
4
  export class PostgresIntrospector {
5
5
  #db;
6
6
  constructor(db) {
7
7
  this.#db = db;
8
8
  }
9
- async getMetadata(options = { withInternalKyselyTables: false }) {
9
+ async getSchemas() {
10
+ let rawSchemas = await this.#db
11
+ .selectFrom('pg_catalog.pg_namespace')
12
+ .select('nspname')
13
+ .castTo()
14
+ .execute();
15
+ return rawSchemas.map((it) => ({ name: it.nspname }));
16
+ }
17
+ async getTables(options = { withInternalKyselyTables: false }) {
10
18
  let query = this.#db
11
19
  .selectFrom('pg_catalog.pg_attribute as a')
12
20
  .innerJoin('pg_catalog.pg_class as c', 'a.attrelid', 'c.oid')
@@ -26,12 +34,15 @@ export class PostgresIntrospector {
26
34
  .castTo();
27
35
  if (!options.withInternalKyselyTables) {
28
36
  query = query
29
- .where('t.tablename', '!=', MIGRATION_TABLE)
30
- .where('t.tablename', '!=', MIGRATION_LOCK_TABLE);
37
+ .where('t.tablename', '!=', DEFAULT_MIGRATION_TABLE)
38
+ .where('t.tablename', '!=', DEFAULT_MIGRATION_LOCK_TABLE);
31
39
  }
32
40
  const rawColumns = await query.execute();
41
+ return this.#parseTableMetadata(rawColumns);
42
+ }
43
+ async getMetadata(options) {
33
44
  return {
34
- tables: this.#parseTableMetadata(rawColumns),
45
+ tables: await this.getTables(options),
35
46
  };
36
47
  }
37
48
  #parseTableMetadata(columns) {
@@ -1,10 +1,20 @@
1
- import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions } from '../database-introspector.js';
1
+ import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions, SchemaMetadata, TableMetadata } from '../database-introspector.js';
2
2
  import { Kysely } from '../../kysely.js';
3
3
  export declare class SqliteIntrospector implements DatabaseIntrospector {
4
4
  #private;
5
5
  constructor(db: Kysely<any>);
6
+ /**
7
+ * Get schema metadata.
8
+ */
9
+ getSchemas(): Promise<SchemaMetadata[]>;
10
+ /**
11
+ * Get table metadata.
12
+ */
13
+ getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
6
14
  /**
7
15
  * Get the database metadata such as table and column names.
16
+ *
17
+ * @deprecated Use getTables() instead.
8
18
  */
9
19
  getMetadata(options?: DatabaseMetadataOptions): Promise<DatabaseMetadata>;
10
20
  }
@@ -1,12 +1,16 @@
1
1
  /// <reference types="./sqlite-introspector.d.ts" />
2
- import { MIGRATION_LOCK_TABLE, MIGRATION_TABLE, } from '../../migration/migrator.js';
2
+ import { DEFAULT_MIGRATION_LOCK_TABLE, DEFAULT_MIGRATION_TABLE, } from '../../migration/migrator.js';
3
3
  import { sql } from '../../raw-builder/sql.js';
4
4
  export class SqliteIntrospector {
5
5
  #db;
6
6
  constructor(db) {
7
7
  this.#db = db;
8
8
  }
9
- async getMetadata(options = { withInternalKyselyTables: false }) {
9
+ async getSchemas() {
10
+ // Sqlite doesn't support schemas.
11
+ return [];
12
+ }
13
+ async getTables(options = { withInternalKyselyTables: false }) {
10
14
  let query = this.#db
11
15
  .selectFrom('sqlite_schema')
12
16
  .where('type', '=', 'table')
@@ -15,12 +19,15 @@ export class SqliteIntrospector {
15
19
  .castTo();
16
20
  if (!options.withInternalKyselyTables) {
17
21
  query = query
18
- .where('name', '!=', MIGRATION_TABLE)
19
- .where('name', '!=', MIGRATION_LOCK_TABLE);
22
+ .where('name', '!=', DEFAULT_MIGRATION_TABLE)
23
+ .where('name', '!=', DEFAULT_MIGRATION_LOCK_TABLE);
20
24
  }
21
25
  const tables = await query.execute();
26
+ return Promise.all(tables.map(({ name }) => this.#getTableMetadata(name)));
27
+ }
28
+ async getMetadata(options) {
22
29
  return {
23
- tables: await Promise.all(tables.map(({ name }) => this.#getTableMetadata(name))),
30
+ tables: await this.getTables(options),
24
31
  };
25
32
  }
26
33
  async #getTableMetadata(table) {
@@ -1,6 +1,6 @@
1
1
  import { Kysely } from '../kysely.js';
2
- export declare const MIGRATION_TABLE = "kysely_migration";
3
- export declare const MIGRATION_LOCK_TABLE = "kysely_migration_lock";
2
+ export declare const DEFAULT_MIGRATION_TABLE = "kysely_migration";
3
+ export declare const DEFAULT_MIGRATION_LOCK_TABLE = "kysely_migration_lock";
4
4
  export declare const MIGRATION_LOCK_ID = "migration_lock";
5
5
  export declare const NO_MIGRATIONS: NoMigrations;
6
6
  export interface Migration {
@@ -148,6 +148,48 @@ export declare class Migrator {
148
148
  export interface MigratorProps {
149
149
  readonly db: Kysely<any>;
150
150
  readonly provider: MigrationProvider;
151
+ /**
152
+ * The name of the internal migration table. Defaults to `kysely_migration`.
153
+ *
154
+ * If you do specify this, you need to ALWAYS use the same value. Kysely doesn't
155
+ * support changing the table on the fly. If you run the migrator even once with a
156
+ * table name X and then change the table name to Y, kysely will create a new empty
157
+ * migration table and attempt to run the migrations again, which will obviously
158
+ * fail.
159
+ *
160
+ * If you do specify this, ALWAYS ALWAYS use the same value from the beginning of
161
+ * the project, to the end of time or prepare to manually migrate the migration
162
+ * tables.
163
+ */
164
+ readonly migrationTableName?: string;
165
+ /**
166
+ * The name of the internal migration lock table. Defaults to `kysely_migration_lock`.
167
+ *
168
+ * If you do specify this, you need to ALWAYS use the same value. Kysely doesn't
169
+ * support changing the table on the fly. If you run the migrator even once with a
170
+ * table name X and then change the table name to Y, kysely will create a new empty
171
+ * lock table.
172
+ *
173
+ * If you do specify this, ALWAYS ALWAYS use the same value from the beginning of
174
+ * the project, to the end of time or prepare to manually migrate the migration
175
+ * tables.
176
+ */
177
+ readonly migrationLockTableName?: string;
178
+ /**
179
+ * The schema of the internal migration tables. Defaults to the default schema
180
+ * on dialects that support schemas.
181
+ *
182
+ * If you do specify this, you need to ALWAYS use the same value. Kysely doesn't
183
+ * support changing the schema on the fly. If you run the migrator even once with a
184
+ * schema name X and then change the schema name to Y, kysely will create a new empty
185
+ * migration tables in the new schema and attempt to run the migrations again, which
186
+ * will obviously fail.
187
+ *
188
+ * If you do specify this, ALWAYS ALWAYS use the same value from the beginning of
189
+ * the project, to the end of time or prepare to manually migrate the migration
190
+ * tables.
191
+ */
192
+ readonly migrationTableSchema?: string;
151
193
  }
152
194
  /**
153
195
  * All migration methods ({@link Migrator.migrateTo | migrateTo},