kysely 0.18.1 → 0.19.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) 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/mysql/mysql-introspector.js +2 -6
  8. package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +25 -89
  9. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +21 -9
  10. package/dist/cjs/dialect/postgres/postgres-dialect.js +24 -11
  11. package/dist/cjs/dialect/postgres/postgres-driver.d.ts +2 -15
  12. package/dist/cjs/dialect/postgres/postgres-driver.js +5 -36
  13. package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
  14. package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +19 -3
  15. package/dist/cjs/dialect/sqlite/sqlite-dialect.js +19 -3
  16. package/dist/cjs/dialect/sqlite/sqlite-driver.js +3 -37
  17. package/dist/cjs/index.d.ts +153 -2
  18. package/dist/cjs/index.js +142 -2
  19. package/dist/cjs/migration/file-migration-provider.d.ts +21 -5
  20. package/dist/cjs/migration/file-migration-provider.js +14 -13
  21. package/dist/cjs/migration/migrator.d.ts +2 -0
  22. package/dist/cjs/plugin/noop-plugin.d.ts +1 -1
  23. package/dist/cjs/schema/alter-table-builder.d.ts +2 -5
  24. package/dist/cjs/schema/alter-table-builder.js +3 -7
  25. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +24 -207
  26. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +22 -7
  27. package/dist/esm/dialect/mysql/mysql-dialect.js +25 -9
  28. package/dist/esm/dialect/mysql/mysql-driver.d.ts +2 -15
  29. package/dist/esm/dialect/mysql/mysql-driver.js +4 -35
  30. package/dist/esm/dialect/mysql/mysql-introspector.js +2 -6
  31. package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +25 -89
  32. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +21 -9
  33. package/dist/esm/dialect/postgres/postgres-dialect.js +24 -11
  34. package/dist/esm/dialect/postgres/postgres-driver.d.ts +2 -15
  35. package/dist/esm/dialect/postgres/postgres-driver.js +6 -37
  36. package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
  37. package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +19 -3
  38. package/dist/esm/dialect/sqlite/sqlite-dialect.js +19 -3
  39. package/dist/esm/dialect/sqlite/sqlite-driver.js +4 -38
  40. package/dist/esm/index.d.ts +153 -2
  41. package/dist/esm/index.js +142 -2
  42. package/dist/esm/migration/file-migration-provider.d.ts +21 -5
  43. package/dist/esm/migration/file-migration-provider.js +14 -13
  44. package/dist/esm/migration/migrator.d.ts +2 -0
  45. package/dist/esm/plugin/noop-plugin.d.ts +1 -1
  46. package/dist/esm/schema/alter-table-builder.d.ts +2 -5
  47. package/dist/esm/schema/alter-table-builder.js +4 -8
  48. package/package.json +2 -7
  49. package/dist/cjs/index-nodeless.d.ts +0 -152
  50. package/dist/cjs/index-nodeless.js +0 -157
  51. package/dist/esm/index-nodeless.d.ts +0 -152
  52. package/dist/esm/index-nodeless.js +0 -142
@@ -2,223 +2,40 @@ import { DatabaseConnection } from '../../driver/database-connection.js';
2
2
  /**
3
3
  * Config for the MySQL dialect.
4
4
  *
5
- * This interface is equal to `mysql2` library's pool config.
6
- *
7
5
  * https://github.com/sidorares/node-mysql2#using-connection-pools
8
6
  */
9
7
  export interface MysqlDialectConfig {
10
8
  /**
11
- * The MySQL user to authenticate as
12
- */
13
- user?: string;
14
- /**
15
- * The password of that MySQL user
16
- */
17
- password?: string;
18
- /**
19
- * Name of the database to use for this connection
20
- */
21
- database?: string;
22
- /**
23
- * The charset for the connection. This is called 'collation' in the SQL-level of MySQL (like utf8_general_ci).
24
- * If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used.
25
- * (Default: 'UTF8_GENERAL_CI')
26
- */
27
- charset?: string;
28
- /**
29
- * The hostname of the database you are connecting to. (Default: localhost)
30
- */
31
- host?: string;
32
- /**
33
- * The port number to connect to. (Default: 3306)
34
- */
35
- port?: number;
36
- /**
37
- * The source IP address to use for TCP connection
38
- */
39
- localAddress?: string;
40
- /**
41
- * The path to a unix domain socket to connect to. When used host and port are ignored
42
- */
43
- socketPath?: string;
44
- /**
45
- * The timezone used to store local dates. (Default: 'local')
46
- */
47
- timezone?: string | 'local';
48
- /**
49
- * The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10 seconds)
50
- */
51
- connectTimeout?: number;
52
- /**
53
- * Stringify objects instead of converting to values. (Default: 'false')
54
- */
55
- stringifyObjects?: boolean;
56
- /**
57
- * Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false)
58
- */
59
- insecureAuth?: boolean;
60
- /**
61
- * Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
62
- * to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
63
- *
64
- * You can also specify a function (field: any, next: () => void) => {} to do the type casting yourself.
65
- *
66
- * WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once.
67
- *
68
- * field.string()
69
- * field.buffer()
70
- * field.geometry()
71
- *
72
- * are aliases for
73
- *
74
- * parser.parseLengthCodedString()
75
- * parser.parseLengthCodedBuffer()
76
- * parser.parseGeometryValue()
77
- *
78
- * You can find which field function you need to use by looking at: RowDataPacket.prototype._typeCast
79
- */
80
- typeCast?: boolean | ((field: any, next: () => void) => any);
81
- /**
82
- * A custom query format function
83
- */
84
- queryFormat?: (query: string, values: any) => void;
85
- /**
86
- * When dealing with big numbers (BIGINT and DECIMAL columns) in the database, you should enable this option
87
- * (Default: false)
88
- */
89
- supportBigNumbers?: boolean;
90
- /**
91
- * Enabling both supportBigNumbers and bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be
92
- * always returned as JavaScript String objects (Default: false). Enabling supportBigNumbers but leaving
93
- * bigNumberStrings disabled will return big numbers as String objects only when they cannot be accurately
94
- * represented with [JavaScript Number objects] (http://ecma262-5.com/ELS5_HTML.htm#Section_8.5)
95
- * (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects.
96
- * This option is ignored if supportBigNumbers is disabled.
97
- */
98
- bigNumberStrings?: boolean;
99
- /**
100
- * Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript Date
101
- * objects. Can be true/false or an array of type names to keep as strings.
9
+ * A mysql2 Pool instance or a function that returns one.
102
10
  *
103
- * (Default: false)
104
- */
105
- dateStrings?: boolean | Array<'TIMESTAMP' | 'DATETIME' | 'DATE'>;
106
- /**
107
- * This will print all incoming and outgoing packets on stdout.
108
- * You can also restrict debugging to packet types by passing an array of types (strings) to debug;
11
+ * If a function is provided, it's called once when the first query is executed.
109
12
  *
110
- * (Default: false)
13
+ * https://github.com/sidorares/node-mysql2#using-connection-pools
111
14
  */
112
- debug?: any;
113
- /**
114
- * Generates stack traces on Error to include call site of library entrance ('long stack traces'). Slight
115
- * performance penalty for most calls. (Default: true)
116
- */
117
- trace?: boolean;
118
- /**
119
- * Allow multiple MySQL statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
120
- */
121
- multipleStatements?: boolean;
122
- /**
123
- * List of connection flags to use other than the default ones. It is also possible to blacklist default ones
124
- */
125
- flags?: Array<string>;
126
- /**
127
- * object with ssl parameters or a string containing name of ssl profile
128
- */
129
- ssl?: string | MysqlSslOptions;
130
- /**
131
- * Return each row as an array, not as an object.
132
- * This is useful when you have duplicate column names.
133
- * This can also be set in the `QueryOption` object to be applied per-query.
134
- */
135
- rowsAsArray?: boolean;
136
- /**
137
- * The milliseconds before a timeout occurs during the connection acquisition. This is slightly different from connectTimeout,
138
- * because acquiring a pool connection does not always involve making a connection. (Default: 10 seconds)
139
- */
140
- acquireTimeout?: number;
141
- /**
142
- * Determines the pool's action when no connections are available and the limit has been reached. If true, the pool will queue
143
- * the connection request and call it when one becomes available. If false, the pool will immediately call back with an error.
144
- * (Default: true)
145
- */
146
- waitForConnections?: boolean;
147
- /**
148
- * The maximum number of connections to create at once. (Default: 10)
149
- */
150
- connectionLimit?: number;
151
- /**
152
- * The maximum number of connection requests the pool will queue before returning an error from getConnection. If set to 0, there
153
- * is no limit to the number of queued connection requests. (Default: 0)
154
- */
155
- queueLimit?: number;
156
- /**
157
- * Enable keep-alive on the socket. It's disabled by default, but the
158
- * user can enable it and supply an initial delay.
159
- */
160
- enableKeepAlive?: true;
161
- /**
162
- * If keep-alive is enabled users can supply an initial delay.
163
- */
164
- keepAliveInitialDelay?: number;
165
- charsetNumber?: number;
166
- compress?: boolean;
167
- authSwitchHandler?: (data: any, callback: () => void) => any;
168
- connectAttributes?: {
169
- [param: string]: any;
170
- };
171
- decimalNumbers?: boolean;
172
- isServer?: boolean;
173
- maxPreparedStatements?: number;
174
- namedPlaceholders?: boolean;
175
- nestTables?: boolean | string;
176
- passwordSha1?: string;
177
- pool?: any;
178
- stream?: any;
179
- uri?: string;
180
- Promise?: any;
181
- authPlugins?: {
182
- [key: string]: any;
183
- };
15
+ pool: MysqlPool | (() => Promise<MysqlPool>);
184
16
  /**
185
17
  * Called once for each created connection.
186
- *
187
- * This is a Kysely specific feature and does not come from the `mysql2` module.
188
18
  */
189
19
  onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
190
20
  }
191
- export interface MysqlSslOptions {
192
- /**
193
- * A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates
194
- */
195
- pfx?: string;
196
- /**
197
- * A string holding the PEM encoded private key
198
- */
199
- key?: string;
200
- /**
201
- * A string of passphrase for the private key or pfx
202
- */
203
- passphrase?: string;
204
- /**
205
- * A string holding the PEM encoded certificate
206
- */
207
- cert?: string;
208
- /**
209
- * Either a string or list of strings of PEM encoded CA certificates to trust.
210
- */
211
- ca?: string | string[];
212
- /**
213
- * Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List)
214
- */
215
- crl?: string | string[];
216
- /**
217
- * A string describing the ciphers to use or exclude
218
- */
219
- ciphers?: string;
220
- /**
221
- * You can also connect to a MySQL server without properly providing the appropriate CA to trust. You should not do this.
222
- */
223
- rejectUnauthorized?: boolean;
21
+ /**
22
+ * This interface is the subset of mysql2 driver's `Pool` class that
23
+ * kysely needs.
24
+ *
25
+ * We don't use the type from `mysql2` here to not have a dependency to it.
26
+ *
27
+ * https://github.com/sidorares/node-mysql2#using-connection-pools
28
+ */
29
+ export interface MysqlPool {
30
+ getConnection(callback: (error: unknown, connection: MysqlPoolConnection) => void): void;
31
+ end(callback: (error: unknown) => void): void;
32
+ }
33
+ export interface MysqlPoolConnection {
34
+ query(sql: string, parameters: ReadonlyArray<unknown>, callback: (error: unknown, result: MysqlQueryResult) => void): void;
35
+ release(): void;
36
+ }
37
+ export interface MysqlOkPacket {
38
+ affectedRows: number;
39
+ insertId: number;
224
40
  }
41
+ export declare type MysqlQueryResult = MysqlOkPacket | Record<string, unknown>[];
@@ -3,26 +3,41 @@ import { Kysely } from '../../kysely.js';
3
3
  import { QueryCompiler } from '../../query-compiler/query-compiler.js';
4
4
  import { Dialect } from '../dialect.js';
5
5
  import { DatabaseIntrospector } from '../database-introspector.js';
6
- import { MysqlPool } from './mysql-driver.js';
7
6
  import { DialectAdapter } from '../dialect-adapter.js';
8
7
  import { MysqlDialectConfig } from './mysql-dialect-config.js';
9
8
  /**
10
9
  * MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
11
10
  *
12
- * The {@link MysqlDialectConfig | configuration} passed to the constructor
13
- * is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
14
- * method.
11
+ * The constructor takes an instance of {@link MysqlDialectConfig}.
15
12
  *
16
- * You can also provide an existing mysql2 `Pool` instance:
13
+ * ```ts
14
+ * import { createPool } from 'mysql2'
15
+ *
16
+ * new MysqlDialect({
17
+ * pool: createPool({
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:
17
26
  *
18
27
  * ```ts
19
28
  * import { createPool } from 'mysql2'
20
- * new MysqlDialect(createPool(config))
29
+ *
30
+ * new MysqlDialect({
31
+ * pool: async () => createPool({
32
+ * database: 'some_db',
33
+ * host: 'localhost',
34
+ * })
35
+ * })
21
36
  * ```
22
37
  */
23
38
  export declare class MysqlDialect implements Dialect {
24
39
  #private;
25
- constructor(configOrPool: MysqlDialectConfig | MysqlPool);
40
+ constructor(config: MysqlDialectConfig);
26
41
  /**
27
42
  * Creates a driver for the dialect.
28
43
  */
@@ -6,24 +6,40 @@ import { MysqlAdapter } from './mysql-adapter.js';
6
6
  /**
7
7
  * MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
8
8
  *
9
- * The {@link MysqlDialectConfig | configuration} passed to the constructor
10
- * is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
11
- * method.
9
+ * The constructor takes an instance of {@link MysqlDialectConfig}.
12
10
  *
13
- * You can also provide an existing mysql2 `Pool` instance:
11
+ * ```ts
12
+ * import { createPool } from 'mysql2'
13
+ *
14
+ * new MysqlDialect({
15
+ * pool: createPool({
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:
14
24
  *
15
25
  * ```ts
16
26
  * import { createPool } from 'mysql2'
17
- * new MysqlDialect(createPool(config))
27
+ *
28
+ * new MysqlDialect({
29
+ * pool: async () => createPool({
30
+ * database: 'some_db',
31
+ * host: 'localhost',
32
+ * })
33
+ * })
18
34
  * ```
19
35
  */
20
36
  export class MysqlDialect {
21
- #configOrPool;
22
- constructor(configOrPool) {
23
- this.#configOrPool = configOrPool;
37
+ #config;
38
+ constructor(config) {
39
+ this.#config = config;
24
40
  }
25
41
  createDriver() {
26
- return new MysqlDriver(this.#configOrPool);
42
+ return new MysqlDriver(this.#config);
27
43
  }
28
44
  createQueryCompiler() {
29
45
  return new MysqlQueryCompiler();
@@ -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 { MysqlDialectConfig } from './mysql-dialect-config.js';
4
+ import { MysqlDialectConfig, MysqlPoolConnection } from './mysql-dialect-config.js';
5
5
  declare const PRIVATE_RELEASE_METHOD: unique symbol;
6
6
  export declare class MysqlDriver implements Driver {
7
7
  #private;
8
- constructor(configOrPool: MysqlDialectConfig | MysqlPool);
8
+ constructor(configOrPool: MysqlDialectConfig);
9
9
  /**
10
10
  * Initializes the driver.
11
11
  *
@@ -43,19 +43,6 @@ export declare class MysqlDriver implements Driver {
43
43
  */
44
44
  destroy(): Promise<void>;
45
45
  }
46
- export interface MysqlPool {
47
- getConnection(callback: (error: unknown, connection: MysqlPoolConnection) => void): void;
48
- end(callback: (error: unknown) => void): void;
49
- }
50
- interface MysqlPoolConnection {
51
- query(sql: string, parameters: ReadonlyArray<unknown>, callback: (error: unknown, result: MysqlQueryResult) => void): void;
52
- release(): void;
53
- }
54
- interface MysqlOkPacket {
55
- affectedRows: number;
56
- insertId: number;
57
- }
58
- declare type MysqlQueryResult = MysqlOkPacket | Record<string, unknown>[];
59
46
  declare class MysqlConnection implements DatabaseConnection {
60
47
  #private;
61
48
  constructor(rawConnection: MysqlPoolConnection);
@@ -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,6 +1,7 @@
1
1
  /// <reference types="./mysql-introspector.d.ts" />
2
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';
4
5
  export class MysqlIntrospector {
5
6
  #db;
6
7
  constructor(db) {
@@ -24,12 +25,7 @@ export class MysqlIntrospector {
24
25
  'is_nullable',
25
26
  'data_type',
26
27
  ])
27
- .where('table_schema', 'not in', [
28
- 'information_schema',
29
- 'performance_schema',
30
- 'mysql',
31
- 'sys',
32
- ])
28
+ .where('table_schema', '=', sql `database()`)
33
29
  .castTo();
34
30
  if (!options.withInternalKyselyTables) {
35
31
  query = query
@@ -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
  */