kysely 0.18.1 → 0.19.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 (44) hide show
  1. package/README.md +22 -17
  2. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +24 -207
  3. package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +22 -7
  4. package/dist/cjs/dialect/mysql/mysql-dialect.js +25 -9
  5. package/dist/cjs/dialect/mysql/mysql-driver.d.ts +2 -15
  6. package/dist/cjs/dialect/mysql/mysql-driver.js +4 -35
  7. package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +25 -89
  8. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +21 -9
  9. package/dist/cjs/dialect/postgres/postgres-dialect.js +24 -11
  10. package/dist/cjs/dialect/postgres/postgres-driver.d.ts +2 -15
  11. package/dist/cjs/dialect/postgres/postgres-driver.js +5 -36
  12. package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
  13. package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +19 -3
  14. package/dist/cjs/dialect/sqlite/sqlite-dialect.js +19 -3
  15. package/dist/cjs/dialect/sqlite/sqlite-driver.js +3 -37
  16. package/dist/cjs/index.d.ts +153 -2
  17. package/dist/cjs/index.js +142 -2
  18. package/dist/cjs/migration/file-migration-provider.d.ts +21 -5
  19. package/dist/cjs/migration/file-migration-provider.js +14 -13
  20. package/dist/cjs/plugin/noop-plugin.d.ts +1 -1
  21. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +24 -207
  22. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +22 -7
  23. package/dist/esm/dialect/mysql/mysql-dialect.js +25 -9
  24. package/dist/esm/dialect/mysql/mysql-driver.d.ts +2 -15
  25. package/dist/esm/dialect/mysql/mysql-driver.js +4 -35
  26. package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +25 -89
  27. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +21 -9
  28. package/dist/esm/dialect/postgres/postgres-dialect.js +24 -11
  29. package/dist/esm/dialect/postgres/postgres-driver.d.ts +2 -15
  30. package/dist/esm/dialect/postgres/postgres-driver.js +6 -37
  31. package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
  32. package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +19 -3
  33. package/dist/esm/dialect/sqlite/sqlite-dialect.js +19 -3
  34. package/dist/esm/dialect/sqlite/sqlite-driver.js +4 -38
  35. package/dist/esm/index.d.ts +153 -2
  36. package/dist/esm/index.js +142 -2
  37. package/dist/esm/migration/file-migration-provider.d.ts +21 -5
  38. package/dist/esm/migration/file-migration-provider.js +14 -13
  39. package/dist/esm/plugin/noop-plugin.d.ts +1 -1
  40. package/package.json +2 -7
  41. package/dist/cjs/index-nodeless.d.ts +0 -152
  42. package/dist/cjs/index-nodeless.js +0 -157
  43. package/dist/esm/index-nodeless.d.ts +0 -152
  44. package/dist/esm/index-nodeless.js +0 -142
@@ -8,23 +8,12 @@ export class MysqlDriver {
8
8
  #connections = new WeakMap();
9
9
  #pool;
10
10
  constructor(configOrPool) {
11
- if (isMysqlPool(configOrPool)) {
12
- this.#pool = configOrPool;
13
- }
14
- else {
15
- this.#config = freeze({ ...configOrPool });
16
- }
11
+ this.#config = freeze({ ...configOrPool });
17
12
  }
18
13
  async init() {
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
- }
14
+ this.#pool = isFunction(this.#config.pool)
15
+ ? await this.#config.pool()
16
+ : this.#config.pool;
28
17
  }
29
18
  async acquireConnection() {
30
19
  const rawConnection = await this.#acquireConnection();
@@ -82,29 +71,9 @@ export class MysqlDriver {
82
71
  });
83
72
  }
84
73
  }
85
- function isMysqlPool(obj) {
86
- return isObject(obj) && isFunction(obj.getConnection) && isFunction(obj.end);
87
- }
88
74
  function isOkPacket(obj) {
89
75
  return isObject(obj) && 'insertId' in obj && 'affectedRows' in obj;
90
76
  }
91
- async function importMysqlPoolFactory() {
92
- try {
93
- // The imported module name must be a string literal to make
94
- // some bundlers work. So don't move this code behind a helper
95
- // for example.
96
- const mysqlModule = await import('mysql2');
97
- if (isFunction(mysqlModule.createPool)) {
98
- return mysqlModule.createPool;
99
- }
100
- else {
101
- return mysqlModule.default.createPool;
102
- }
103
- }
104
- catch (error) {
105
- throw new Error('MySQL client not installed. Please run `npm install mysql2`');
106
- }
107
- }
108
77
  class MysqlConnection {
109
78
  #rawConnection;
110
79
  constructor(rawConnection) {
@@ -1,103 +1,39 @@
1
- /// <reference types="node" />
2
- import * as stream from 'stream';
3
- import { ConnectionOptions } from 'tls';
4
1
  import { DatabaseConnection } from '../../driver/database-connection.js';
5
2
  /**
6
3
  * Config for the PostgreSQL dialect.
7
- *
8
- * This interface is equal to `pg` library's `Pool` config:
9
- *
10
- * https://node-postgres.com/api/pool
11
- * https://node-postgres.com/api/client
12
4
  */
13
5
  export interface PostgresDialectConfig {
14
6
  /**
15
- * defaults process.env.PGUSER || process.env.USER.
16
- */
17
- user?: string;
18
- /**
19
- * default process.env.PGDATABASE || process.env.USER
20
- */
21
- database?: string;
22
- /**
23
- * default process.env.PGPASSWORD
24
- */
25
- password?: string | (() => string | Promise<string>);
26
- /**
27
- * default process.env.PGPORT
28
- */
29
- port?: number;
30
- /**
31
- * default process.env.PGHOST
32
- */
33
- host?: string;
34
- /**
35
- * e.g. postgres:*user:password@host:5432/database
36
- */
37
- connectionString?: string;
38
- /**
39
- * passed directly to node.TLSSocket, supports all tls.connect options
40
- */
41
- ssl?: boolean | ConnectionOptions;
42
- /**
43
- * Number of milliseconds to wait before timing out when connecting a new client
44
- * by default this is 0 which means no timeout.
45
- */
46
- connectionTimeoutMillis?: number;
47
- /**
48
- * Maximum number of clients the pool should contain. by default this is set to 10.
49
- */
50
- max?: number;
51
- /**
52
- * Minimum number of clients the pool should contain
53
- */
54
- min?: number;
55
- /**
56
- * Number of milliseconds a client must sit idle in the pool and not be checked out
57
- * before it is disconnected from the backend and discarded.
7
+ * A postgres Pool instance or a function that returns one.
58
8
  *
59
- * default is 10000 (10 seconds) - set to 0 to disable auto-disconnection of idle clients.
60
- */
61
- idleTimeoutMillis?: number;
62
- /**
63
- * Default behavior is the pool will keep clients open & connected to the backend
64
- * until idleTimeoutMillis expire for each client and node will maintain a ref
65
- * to the socket on the client, keeping the event loop alive until all clients are closed
66
- * after being idle or the pool is manually shutdown with `pool.end()`.
9
+ * If a function is provided, it's called once when the first query is executed.
67
10
  *
68
- * Setting `allowExitOnIdle: true` in the config will allow the node event loop to exit
69
- * as soon as all clients in the pool are idle, even if their socket is still open
70
- * to the PostgreSQL server. This can be handy in scripts & tests
71
- * where you don't want to wait for your clients to go idle before your process exits.
72
- */
73
- allowExitOnIdle?: boolean;
74
- log?: (...messages: any[]) => void;
75
- /**
76
- * Number of milliseconds before a statement in query will time out, default is no timeout.
11
+ * https://node-postgres.com/api/pool
77
12
  */
78
- statement_timeout?: false | number;
79
- /**
80
- * Number of milliseconds before terminating any session with an open idle transaction.
81
- * Default is no timeout.
82
- */
83
- idle_in_transaction_session_timeout?: number;
84
- /**
85
- * Number of milliseconds before a query call will timeout, default is no timeout.
86
- */
87
- query_timeout?: number;
88
- /**
89
- * The name of the application that created this Client instance.
90
- */
91
- application_name?: string;
13
+ pool: PostgresPool | (() => Promise<PostgresPool>);
92
14
  /**
93
15
  * Called once for each created connection.
94
- *
95
- * This is a Kysely specific feature and does not come from the `pg` module.
96
16
  */
97
17
  onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
98
- keepAlive?: boolean;
99
- keepAliveInitialDelayMillis?: number;
100
- types?: any;
101
- stream?: stream.Duplex;
102
- parseInputDatesAsUTC?: boolean;
18
+ }
19
+ /**
20
+ * This interface is the subset of pg driver's `Pool` class that
21
+ * kysely needs.
22
+ *
23
+ * We don't use the type from `pg` here to not have a dependency to it.
24
+ *
25
+ * https://node-postgres.com/api/pool
26
+ */
27
+ export interface PostgresPool {
28
+ connect(): Promise<PostgresPoolClient>;
29
+ end(): Promise<void>;
30
+ }
31
+ export interface PostgresPoolClient {
32
+ query<R>(sql: string, parameters: ReadonlyArray<unknown>): Promise<PostgresQueryResult<R>>;
33
+ release(): void;
34
+ }
35
+ export interface PostgresQueryResult<R> {
36
+ command: 'UPDATE' | 'DELETE' | 'INSERT' | 'SELECT';
37
+ rowCount: number;
38
+ rows: R[];
103
39
  }
@@ -2,30 +2,42 @@ 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';
6
5
  import { DatabaseIntrospector } from '../database-introspector.js';
7
6
  import { DialectAdapter } from '../dialect-adapter.js';
8
7
  import { PostgresDialectConfig } from './postgres-dialect-config.js';
9
8
  /**
10
9
  * PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
11
10
  *
12
- * The {@link PostgresDialectConfig | configuration} passed to the constructor
13
- * is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
14
- * constructor. See the following two links for more documentation:
11
+ * The constructor takes an instance of {@link PostgresDialectConfig}.
15
12
  *
16
- * https://node-postgres.com/api/pool
17
- * https://node-postgres.com/api/client
13
+ * ```ts
14
+ * import { Pool } from 'pg'
18
15
  *
19
- * You can also provide an existing pg `Pool` instance:
16
+ * new PostgresDialect({
17
+ * pool: new Pool({
18
+ * database: 'some_db',
19
+ * host: 'localhost',
20
+ * })
21
+ * })
22
+ * ```
23
+ *
24
+ * If you want the pool to only be created once it's first used, `pool`
25
+ * can be a function:
20
26
  *
21
27
  * ```ts
22
28
  * import { Pool } from 'pg'
23
- * new PostgresDialect(new Pool(config))
29
+ *
30
+ * new PostgresDialect({
31
+ * pool: async () => new Pool({
32
+ * database: 'some_db',
33
+ * host: 'localhost',
34
+ * })
35
+ * })
24
36
  * ```
25
37
  */
26
38
  export declare class PostgresDialect implements Dialect {
27
39
  #private;
28
- constructor(configOrPool: PostgresDialectConfig | PostgresPool);
40
+ constructor(config: PostgresDialectConfig);
29
41
  /**
30
42
  * Creates a driver for the dialect.
31
43
  */
@@ -6,27 +6,40 @@ import { PostgresAdapter } from './postgres-adapter.js';
6
6
  /**
7
7
  * PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
8
8
  *
9
- * The {@link PostgresDialectConfig | configuration} passed to the constructor
10
- * is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
11
- * constructor. See the following two links for more documentation:
9
+ * The constructor takes an instance of {@link PostgresDialectConfig}.
12
10
  *
13
- * https://node-postgres.com/api/pool
14
- * https://node-postgres.com/api/client
11
+ * ```ts
12
+ * import { Pool } from 'pg'
15
13
  *
16
- * You can also provide an existing pg `Pool` instance:
14
+ * new PostgresDialect({
15
+ * pool: new Pool({
16
+ * database: 'some_db',
17
+ * host: 'localhost',
18
+ * })
19
+ * })
20
+ * ```
21
+ *
22
+ * If you want the pool to only be created once it's first used, `pool`
23
+ * can be a function:
17
24
  *
18
25
  * ```ts
19
26
  * import { Pool } from 'pg'
20
- * new PostgresDialect(new Pool(config))
27
+ *
28
+ * new PostgresDialect({
29
+ * pool: async () => new Pool({
30
+ * database: 'some_db',
31
+ * host: 'localhost',
32
+ * })
33
+ * })
21
34
  * ```
22
35
  */
23
36
  export class PostgresDialect {
24
- #configOrPool;
25
- constructor(configOrPool) {
26
- this.#configOrPool = configOrPool;
37
+ #config;
38
+ constructor(config) {
39
+ this.#config = config;
27
40
  }
28
41
  createDriver() {
29
- return new PostgresDriver(this.#configOrPool);
42
+ return new PostgresDriver(this.#config);
30
43
  }
31
44
  createQueryCompiler() {
32
45
  return new PostgresQueryCompiler();
@@ -1,11 +1,11 @@
1
1
  import { DatabaseConnection, QueryResult } from '../../driver/database-connection.js';
2
2
  import { Driver, TransactionSettings } from '../../driver/driver.js';
3
3
  import { CompiledQuery } from '../../query-compiler/compiled-query.js';
4
- import { PostgresDialectConfig } from './postgres-dialect-config.js';
4
+ import { PostgresDialectConfig, PostgresPoolClient } from './postgres-dialect-config.js';
5
5
  declare const PRIVATE_RELEASE_METHOD: unique symbol;
6
6
  export declare class PostgresDriver implements Driver {
7
7
  #private;
8
- constructor(configOrPool: PostgresDialectConfig | PostgresPool);
8
+ constructor(config: PostgresDialectConfig);
9
9
  /**
10
10
  * Initializes the driver.
11
11
  *
@@ -43,19 +43,6 @@ export declare class PostgresDriver implements Driver {
43
43
  */
44
44
  destroy(): Promise<void>;
45
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
46
  declare class PostgresConnection implements DatabaseConnection {
60
47
  #private;
61
48
  constructor(client: PostgresPoolClient);
@@ -1,30 +1,19 @@
1
1
  /// <reference types="./postgres-driver.d.ts" />
2
2
  import { CompiledQuery } from '../../query-compiler/compiled-query.js';
3
- import { isFunction, freeze, isObject } from '../../util/object-utils.js';
3
+ import { isFunction, freeze } from '../../util/object-utils.js';
4
4
  import { extendStackTrace } from '../../util/stack-trace-utils.js';
5
5
  const PRIVATE_RELEASE_METHOD = Symbol();
6
6
  export class PostgresDriver {
7
7
  #config;
8
8
  #connections = new WeakMap();
9
9
  #pool;
10
- constructor(configOrPool) {
11
- if (isPostgresPool(configOrPool)) {
12
- this.#pool = configOrPool;
13
- }
14
- else {
15
- this.#config = freeze({ ...configOrPool });
16
- }
10
+ constructor(config) {
11
+ this.#config = freeze({ ...config });
17
12
  }
18
13
  async init() {
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
- }
14
+ this.#pool = isFunction(this.#config.pool)
15
+ ? await this.#config.pool()
16
+ : this.#config.pool;
28
17
  }
29
18
  async acquireConnection() {
30
19
  const client = await this.#pool.connect();
@@ -66,26 +55,6 @@ export class PostgresDriver {
66
55
  }
67
56
  }
68
57
  }
69
- function isPostgresPool(obj) {
70
- return isObject(obj) && isFunction(obj.connect) && isFunction(obj.end);
71
- }
72
- async function importPgPool() {
73
- try {
74
- // The imported module name must be a string literal to make
75
- // some bundlers work. So don't move this code behind a helper
76
- // for example.
77
- const pgModule = await import('pg');
78
- if (isFunction(pgModule.Pool)) {
79
- return pgModule.Pool;
80
- }
81
- else {
82
- return pgModule.default.Pool;
83
- }
84
- }
85
- catch (error) {
86
- throw new Error('Postgres client not installed. Please run `npm install pg`');
87
- }
88
- }
89
58
  class PostgresConnection {
90
59
  #client;
91
60
  constructor(client) {
@@ -1,39 +1,16 @@
1
1
  import { DatabaseConnection } from '../../driver/database-connection.js';
2
2
  /**
3
3
  * Config for the SQLite dialect.
4
- *
5
- * This interface is equal to `better-sqlite3` library's `Databse` class's constructor parameters.
6
- *
7
- * https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options
8
4
  */
9
5
  export interface SqliteDialectConfig {
10
6
  /**
11
- * The database file path.
7
+ * An sqlite Database instance or a function that returns one.
12
8
  *
13
- * This is passed as the first argument for the [Database](https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
14
- * class constructor.
15
- */
16
- databasePath: string;
17
- /**
18
- * Open the database connection in readonly mode (default: false)
19
- */
20
- readonly?: boolean;
21
- /**
22
- * If the database does not exist, an Error will be thrown instead of creating a new file.
23
- * This option is ignored for in-memory, temporary, or readonly database connections
24
- * (default: false)
25
- */
26
- fileMustExist?: boolean;
27
- /**
28
- * The number of milliseconds to wait when executing queries on a locked database,
29
- * before throwing a SQLITE_BUSY error (default: 5000)
30
- */
31
- timeout?: number;
32
- /**
33
- * Provide a function that gets called with every SQL string executed by the database connection
34
- * (default: null)
9
+ * If a function is provided, it's called once when the first query is executed.
10
+ *
11
+ * https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options
35
12
  */
36
- verbose?: (sql: string) => void;
13
+ database: SqliteDatabase | (() => Promise<SqliteDatabase>);
37
14
  /**
38
15
  * Called once when the first query is executed.
39
16
  *
@@ -41,3 +18,23 @@ export interface SqliteDialectConfig {
41
18
  */
42
19
  onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
43
20
  }
21
+ /**
22
+ * This interface is the subset of better-sqlite3 driver's `Database` class that
23
+ * kysely needs.
24
+ *
25
+ * We don't use the type from `better-sqlite3` here to not have a dependency to it.
26
+ *
27
+ * https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options
28
+ */
29
+ export interface SqliteDatabase {
30
+ close(): void;
31
+ prepare(sql: string): SqliteStatement;
32
+ }
33
+ export interface SqliteStatement {
34
+ readonly reader: boolean;
35
+ all(parameters: ReadonlyArray<unknown>): unknown[];
36
+ run(parameters: ReadonlyArray<unknown>): {
37
+ changes: number | bigint;
38
+ lastInsertRowid: number | bigint;
39
+ };
40
+ }
@@ -8,9 +8,25 @@ import { SqliteDialectConfig } from './sqlite-dialect-config.js';
8
8
  /**
9
9
  * SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library.
10
10
  *
11
- * The {@link SqliteDialectConfig | configuration} is used to construct an instance of
12
- * [Database](https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
13
- * class. The configuration values are passed directly to the `Database` constructor.
11
+ * The constructor takes an instance of {@link SqliteDialectConfig}.
12
+ *
13
+ * ```ts
14
+ * import Database from 'better-sqlite3'
15
+ *
16
+ * new SqliteDialect({
17
+ * database: new Database('db.sqlite')
18
+ * })
19
+ * ```
20
+ *
21
+ * If you want the pool to only be created once it's first used, `database`
22
+ * can be a function:
23
+ *
24
+ * ```ts
25
+ * import Database from 'better-sqlite3'
26
+ *
27
+ * new SqliteDialect({
28
+ * database: async () => new Database('db.sqlite')
29
+ * })
14
30
  */
15
31
  export declare class SqliteDialect implements Dialect {
16
32
  #private;
@@ -7,9 +7,25 @@ import { freeze } from '../../util/object-utils.js';
7
7
  /**
8
8
  * SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library.
9
9
  *
10
- * The {@link SqliteDialectConfig | configuration} is used to construct an instance of
11
- * [Database](https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
12
- * class. The configuration values are passed directly to the `Database` constructor.
10
+ * The constructor takes an instance of {@link SqliteDialectConfig}.
11
+ *
12
+ * ```ts
13
+ * import Database from 'better-sqlite3'
14
+ *
15
+ * new SqliteDialect({
16
+ * database: new Database('db.sqlite')
17
+ * })
18
+ * ```
19
+ *
20
+ * If you want the pool to only be created once it's first used, `database`
21
+ * can be a function:
22
+ *
23
+ * ```ts
24
+ * import Database from 'better-sqlite3'
25
+ *
26
+ * new SqliteDialect({
27
+ * database: async () => new Database('db.sqlite')
28
+ * })
13
29
  */
14
30
  export class SqliteDialect {
15
31
  #config;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="./sqlite-driver.d.ts" />
2
2
  import { CompiledQuery } from '../../query-compiler/compiled-query.js';
3
- import { freeze, isBoolean, isFunction, isNumber, } from '../../util/object-utils.js';
3
+ import { freeze, isFunction } from '../../util/object-utils.js';
4
4
  export class SqliteDriver {
5
5
  #config;
6
6
  #connectionMutex = new ConnectionMutex();
@@ -10,26 +10,9 @@ export class SqliteDriver {
10
10
  this.#config = freeze({ ...config });
11
11
  }
12
12
  async init() {
13
- // Import the `better-sqlite3` module here instead at the top of the file
14
- // so that this file can be loaded by node without `better-sqlite3` driver
15
- // installed. As you can see, there IS an import from `better-sqlite3` at
16
- // the 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 DatabaseConstructor = await importBetterSqlite3Database();
19
- const options = {};
20
- if (isBoolean(this.#config.readonly)) {
21
- options.readonly = this.#config.readonly;
22
- }
23
- if (isBoolean(this.#config.fileMustExist)) {
24
- options.fileMustExist = this.#config.fileMustExist;
25
- }
26
- if (isNumber(this.#config.timeout)) {
27
- options.timeout = this.#config.timeout;
28
- }
29
- if (isFunction(this.#config.verbose)) {
30
- options.verbose = this.#config.verbose;
31
- }
32
- this.#db = new DatabaseConstructor(this.#config.databasePath, options);
13
+ this.#db = isFunction(this.#config.database)
14
+ ? await this.#config.database()
15
+ : this.#config.database;
33
16
  this.#connection = new SqliteConnection(this.#db);
34
17
  if (this.#config.onCreateConnection) {
35
18
  await this.#config.onCreateConnection(this.#connection);
@@ -57,23 +40,6 @@ export class SqliteDriver {
57
40
  this.#db?.close();
58
41
  }
59
42
  }
60
- async function importBetterSqlite3Database() {
61
- try {
62
- // The imported module name must be a string literal to make
63
- // some bundlers work. So don't move this code behind a helper
64
- // for example.
65
- const sqliteModule = await import('better-sqlite3');
66
- if (isFunction(sqliteModule)) {
67
- return sqliteModule;
68
- }
69
- else {
70
- return sqliteModule.default;
71
- }
72
- }
73
- catch (error) {
74
- throw new Error('SQLite client not installed. Please run `npm install better-sqlite3`');
75
- }
76
- }
77
43
  class SqliteConnection {
78
44
  #db;
79
45
  constructor(db) {