kysely 0.9.3 → 0.9.4
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.
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +9 -0
- package/dist/cjs/dialect/mysql/mysql-dialect.js +7 -0
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +11 -1
- package/dist/cjs/dialect/postgres/postgres-dialect.js +10 -0
- package/dist/cjs/driver/runtime-driver.d.ts +3 -2
- package/dist/cjs/driver/runtime-driver.js +43 -4
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/kysely.d.ts +26 -9
- package/dist/cjs/kysely.js +16 -9
- package/dist/cjs/migration/migration.d.ts +32 -0
- package/dist/cjs/parser/join-parser.d.ts +4 -4
- package/dist/cjs/query-builder/query-builder.d.ts +35 -0
- package/dist/cjs/query-builder/query-builder.js +37 -0
- package/dist/cjs/util/log.d.ts +8 -0
- package/dist/cjs/util/log.js +21 -0
- package/dist/cjs/util/random-string.js +6 -3
- package/dist/cjs/util/type-utils.d.ts +4 -4
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +9 -0
- package/dist/esm/dialect/mysql/mysql-dialect.js +7 -0
- package/dist/esm/dialect/mysql/mysql-driver.js +1 -1
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +11 -1
- package/dist/esm/dialect/postgres/postgres-dialect.js +10 -0
- package/dist/esm/driver/runtime-driver.d.ts +3 -2
- package/dist/esm/driver/runtime-driver.js +43 -4
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/kysely.d.ts +26 -9
- package/dist/esm/kysely.js +16 -9
- package/dist/esm/migration/migration.d.ts +32 -0
- package/dist/esm/parser/join-parser.d.ts +4 -4
- package/dist/esm/query-builder/query-builder.d.ts +35 -0
- package/dist/esm/query-builder/query-builder.js +37 -0
- package/dist/esm/util/log.d.ts +8 -0
- package/dist/esm/util/log.js +17 -0
- package/dist/esm/util/random-string.js +6 -3
- package/dist/esm/util/type-utils.d.ts +4 -4
- package/package.json +1 -1
|
@@ -5,6 +5,13 @@ import { Dialect } from '../dialect.js';
|
|
|
5
5
|
import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
|
|
6
6
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
7
7
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
8
|
+
/**
|
|
9
|
+
* MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
|
|
10
|
+
*
|
|
11
|
+
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
12
|
+
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
13
|
+
* method.
|
|
14
|
+
*/
|
|
8
15
|
export declare class MysqlDialect implements Dialect {
|
|
9
16
|
#private;
|
|
10
17
|
constructor(config: MysqlDialectConfig);
|
|
@@ -17,6 +24,8 @@ export declare class MysqlDialect implements Dialect {
|
|
|
17
24
|
* Config for the mysql dialect.
|
|
18
25
|
*
|
|
19
26
|
* This interface is equal to `mysql2` library's pool config.
|
|
27
|
+
*
|
|
28
|
+
* https://github.com/sidorares/node-mysql2#using-connection-pools
|
|
20
29
|
*/
|
|
21
30
|
export interface MysqlDialectConfig {
|
|
22
31
|
/**
|
|
@@ -5,6 +5,13 @@ const mysql_driver_js_1 = require("./mysql-driver.js");
|
|
|
5
5
|
const mysql_query_compiler_js_1 = require("./mysql-query-compiler.js");
|
|
6
6
|
const mysql_introspector_js_1 = require("./mysql-introspector.js");
|
|
7
7
|
const mysql_adapter_js_1 = require("./mysql-adapter.js");
|
|
8
|
+
/**
|
|
9
|
+
* MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
|
|
10
|
+
*
|
|
11
|
+
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
12
|
+
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
13
|
+
* method.
|
|
14
|
+
*/
|
|
8
15
|
class MysqlDialect {
|
|
9
16
|
#config;
|
|
10
17
|
constructor(config) {
|
|
@@ -8,6 +8,16 @@ import { Dialect } from '../dialect.js';
|
|
|
8
8
|
import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
|
|
9
9
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
10
10
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
11
|
+
/**
|
|
12
|
+
* PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
|
|
13
|
+
*
|
|
14
|
+
* The {@link PostgresDialectConfig | configuration} passed to the constructor
|
|
15
|
+
* is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
|
|
16
|
+
* constructor. See the following two links for more documentation:
|
|
17
|
+
*
|
|
18
|
+
* https://node-postgres.com/api/pool
|
|
19
|
+
* https://node-postgres.com/api/client
|
|
20
|
+
*/
|
|
11
21
|
export declare class PostgresDialect implements Dialect {
|
|
12
22
|
#private;
|
|
13
23
|
constructor(config: PostgresDialectConfig);
|
|
@@ -19,7 +29,7 @@ export declare class PostgresDialect implements Dialect {
|
|
|
19
29
|
/**
|
|
20
30
|
* Config for the postgres dialect.
|
|
21
31
|
*
|
|
22
|
-
* This interface is equal to `pg` library's
|
|
32
|
+
* This interface is equal to `pg` library's `Pool` config:
|
|
23
33
|
*
|
|
24
34
|
* https://node-postgres.com/api/pool
|
|
25
35
|
* https://node-postgres.com/api/client
|
|
@@ -5,6 +5,16 @@ const postgres_driver_js_1 = require("./postgres-driver.js");
|
|
|
5
5
|
const postgres_introspector_js_1 = require("./postgres-introspector.js");
|
|
6
6
|
const postgres_query_compiler_js_1 = require("./postgres-query-compiler.js");
|
|
7
7
|
const postgres_adapter_js_1 = require("./postgres-adapter.js");
|
|
8
|
+
/**
|
|
9
|
+
* PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
|
|
10
|
+
*
|
|
11
|
+
* The {@link PostgresDialectConfig | configuration} passed to the constructor
|
|
12
|
+
* is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
|
|
13
|
+
* constructor. See the following two links for more documentation:
|
|
14
|
+
*
|
|
15
|
+
* https://node-postgres.com/api/pool
|
|
16
|
+
* https://node-postgres.com/api/client
|
|
17
|
+
*/
|
|
8
18
|
class PostgresDialect {
|
|
9
19
|
#config;
|
|
10
20
|
constructor(config) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Log } from '../util/log.js';
|
|
1
2
|
import { DatabaseConnection } from './database-connection.js';
|
|
2
3
|
import { Driver, TransactionSettings } from './driver.js';
|
|
3
4
|
/**
|
|
@@ -7,10 +8,10 @@ import { Driver, TransactionSettings } from './driver.js';
|
|
|
7
8
|
*/
|
|
8
9
|
export declare class RuntimeDriver implements Driver {
|
|
9
10
|
#private;
|
|
10
|
-
constructor(driver: Driver);
|
|
11
|
+
constructor(driver: Driver, log: Log);
|
|
11
12
|
init(): Promise<void>;
|
|
12
13
|
acquireConnection(): Promise<DatabaseConnection>;
|
|
13
|
-
releaseConnection(
|
|
14
|
+
releaseConnection(runtimeConnection: DatabaseConnection): Promise<void>;
|
|
14
15
|
beginTransaction(connection: DatabaseConnection, settings: TransactionSettings): Promise<void>;
|
|
15
16
|
commitTransaction(connection: DatabaseConnection): Promise<void>;
|
|
16
17
|
rollbackTransaction(connection: DatabaseConnection): Promise<void>;
|
|
@@ -8,10 +8,13 @@ exports.RuntimeDriver = void 0;
|
|
|
8
8
|
*/
|
|
9
9
|
class RuntimeDriver {
|
|
10
10
|
#driver;
|
|
11
|
+
#log;
|
|
11
12
|
#initPromise;
|
|
12
13
|
#destroyPromise;
|
|
13
|
-
|
|
14
|
+
#connections = new WeakMap();
|
|
15
|
+
constructor(driver, log) {
|
|
14
16
|
this.#driver = driver;
|
|
17
|
+
this.#log = log;
|
|
15
18
|
}
|
|
16
19
|
async init() {
|
|
17
20
|
if (!this.#initPromise) {
|
|
@@ -24,10 +27,18 @@ class RuntimeDriver {
|
|
|
24
27
|
}
|
|
25
28
|
async acquireConnection() {
|
|
26
29
|
await this.init();
|
|
27
|
-
|
|
30
|
+
const connection = await this.#driver.acquireConnection();
|
|
31
|
+
let runtimeConnection = this.#connections.get(connection);
|
|
32
|
+
if (!runtimeConnection) {
|
|
33
|
+
runtimeConnection = new RuntimeConnection(connection, this.#log);
|
|
34
|
+
this.#connections.set(connection, runtimeConnection);
|
|
35
|
+
}
|
|
36
|
+
return runtimeConnection;
|
|
28
37
|
}
|
|
29
|
-
releaseConnection(
|
|
30
|
-
|
|
38
|
+
async releaseConnection(runtimeConnection) {
|
|
39
|
+
if (runtimeConnection instanceof RuntimeConnection) {
|
|
40
|
+
await this.#driver.releaseConnection(runtimeConnection.connection);
|
|
41
|
+
}
|
|
31
42
|
}
|
|
32
43
|
beginTransaction(connection, settings) {
|
|
33
44
|
return this.#driver.beginTransaction(connection, settings);
|
|
@@ -53,3 +64,31 @@ class RuntimeDriver {
|
|
|
53
64
|
}
|
|
54
65
|
}
|
|
55
66
|
exports.RuntimeDriver = RuntimeDriver;
|
|
67
|
+
class RuntimeConnection {
|
|
68
|
+
#connection;
|
|
69
|
+
#log;
|
|
70
|
+
get connection() {
|
|
71
|
+
return this.#connection;
|
|
72
|
+
}
|
|
73
|
+
constructor(connection, log) {
|
|
74
|
+
this.#connection = connection;
|
|
75
|
+
this.#log = log;
|
|
76
|
+
}
|
|
77
|
+
async executeQuery(compiledQuery) {
|
|
78
|
+
const startTime = process.hrtime.bigint();
|
|
79
|
+
try {
|
|
80
|
+
return await this.#connection.executeQuery(compiledQuery);
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
this.#log.query((log) => {
|
|
84
|
+
log(compiledQuery.sql);
|
|
85
|
+
log(`duration: ${this.#calculateDurationMillis(startTime)}ms`);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
#calculateDurationMillis(startTime) {
|
|
90
|
+
const endTime = process.hrtime.bigint();
|
|
91
|
+
const durationTensMillis = Number((endTime - startTime) / 100000n);
|
|
92
|
+
return durationTensMillis / 10;
|
|
93
|
+
}
|
|
94
|
+
}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -110,7 +110,7 @@ export * from './operation-node/operation-node-source.js';
|
|
|
110
110
|
export * from './operation-node/order-by-node.js';
|
|
111
111
|
export * from './introspection/database-introspector.js';
|
|
112
112
|
export * from './util/compilable.js';
|
|
113
|
-
export { AnyColumn, UnknownRow
|
|
113
|
+
export { AnyColumn, UnknownRow, AnyQueryBuilder } from './util/type-utils.js';
|
|
114
114
|
export { SelectExpression, SelectExpressionOrList, } from './parser/select-parser.js';
|
|
115
115
|
export { ReferenceExpression, ReferenceExpressionOrList, } from './parser/reference-parser.js';
|
|
116
116
|
export { ValueExpression, ValueExpressionOrList, } from './parser/value-parser.js';
|
package/dist/cjs/kysely.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { DatabaseIntrospector } from './introspection/database-introspector.js';
|
|
|
10
10
|
import { Driver, IsolationLevel } from './driver/driver.js';
|
|
11
11
|
import { ParseContext } from './parser/parse-context.js';
|
|
12
12
|
import { FunctionBuilder } from './query-builder/function-builder.js';
|
|
13
|
+
import { LogLevel } from './util/log.js';
|
|
13
14
|
/**
|
|
14
15
|
* The main Kysely class.
|
|
15
16
|
*
|
|
@@ -127,15 +128,20 @@ export declare class Kysely<DB> extends QueryCreator<DB> {
|
|
|
127
128
|
*/
|
|
128
129
|
get fn(): FunctionBuilder<DB, keyof DB>;
|
|
129
130
|
/**
|
|
130
|
-
*
|
|
131
|
-
* otherwise it's committed.
|
|
131
|
+
* Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
|
|
132
132
|
*
|
|
133
|
-
* @
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
133
|
+
* The returned {@link TransactionBuilder} can be used to configure the transaction. The
|
|
134
|
+
* {@link TransactionBuilder.execute} method can then be called to run the transaction.
|
|
135
|
+
* {@link TransactionBuilder.execute} takes a function that is run inside the
|
|
136
|
+
* transaction. If the function throws, the transaction is rolled back. Otherwise
|
|
137
|
+
* the transaction is committed.
|
|
138
|
+
*
|
|
139
|
+
* The callback function passed to the {@link TransactionBuilder.execute | execute}
|
|
140
|
+
* method gets the transaction object as its only argument. The transaction is
|
|
141
|
+
* of type {@link Transaction} which inherits {@link Kysely}. Any query
|
|
142
|
+
* started through the transaction object is executed inside the transaction.
|
|
138
143
|
*
|
|
144
|
+
* @example
|
|
139
145
|
* ```ts
|
|
140
146
|
* const catto = await db.transaction().execute(async (trx) => {
|
|
141
147
|
* const jennifer = await trx.insertInto('person')
|
|
@@ -220,8 +226,19 @@ export interface KyselyProps {
|
|
|
220
226
|
}
|
|
221
227
|
export declare function isKyselyProps(obj: unknown): obj is KyselyProps;
|
|
222
228
|
export interface KyselyConfig {
|
|
223
|
-
dialect: Dialect;
|
|
224
|
-
plugins?: KyselyPlugin[];
|
|
229
|
+
readonly dialect: Dialect;
|
|
230
|
+
readonly plugins?: KyselyPlugin[];
|
|
231
|
+
/**
|
|
232
|
+
* A list of log levels to log.
|
|
233
|
+
*
|
|
234
|
+
* Currently there's only one level: `query` and it's logged using
|
|
235
|
+
* `console.log`. This will be expanded based on user request later.
|
|
236
|
+
*
|
|
237
|
+
* Log levels:
|
|
238
|
+
*
|
|
239
|
+
* - query: Log each query's SQL and duration.
|
|
240
|
+
*/
|
|
241
|
+
readonly log?: ReadonlyArray<LogLevel>;
|
|
225
242
|
}
|
|
226
243
|
export declare class ConnectionBuilder<DB> {
|
|
227
244
|
#private;
|
package/dist/cjs/kysely.js
CHANGED
|
@@ -15,6 +15,7 @@ const driver_js_1 = require("./driver/driver.js");
|
|
|
15
15
|
const prevent_await_js_1 = require("./util/prevent-await.js");
|
|
16
16
|
const parse_context_js_1 = require("./parser/parse-context.js");
|
|
17
17
|
const function_builder_js_1 = require("./query-builder/function-builder.js");
|
|
18
|
+
const log_js_1 = require("./util/log.js");
|
|
18
19
|
/**
|
|
19
20
|
* The main Kysely class.
|
|
20
21
|
*
|
|
@@ -60,15 +61,16 @@ class Kysely extends query_creator_js_1.QueryCreator {
|
|
|
60
61
|
constructor(args) {
|
|
61
62
|
if (isKyselyProps(args)) {
|
|
62
63
|
super({ executor: args.executor, parseContext: args.parseContext });
|
|
63
|
-
this.#props = (0, object_utils_js_1.freeze)(args);
|
|
64
|
+
this.#props = (0, object_utils_js_1.freeze)({ ...args });
|
|
64
65
|
}
|
|
65
66
|
else {
|
|
66
67
|
const dialect = args.dialect;
|
|
67
68
|
const driver = dialect.createDriver();
|
|
68
69
|
const compiler = dialect.createQueryCompiler();
|
|
69
70
|
const adapter = dialect.createAdapter();
|
|
71
|
+
const log = new log_js_1.Log(args.log ?? []);
|
|
70
72
|
const parseContext = new parse_context_js_1.DefaultParseContext(adapter);
|
|
71
|
-
const runtimeDriver = new runtime_driver_js_1.RuntimeDriver(driver);
|
|
73
|
+
const runtimeDriver = new runtime_driver_js_1.RuntimeDriver(driver, log);
|
|
72
74
|
const connectionProvider = new default_connection_provider_js_1.DefaultConnectionProvider(runtimeDriver);
|
|
73
75
|
const executor = new default_query_executor_js_1.DefaultQueryExecutor(compiler, connectionProvider, args.plugins ?? []);
|
|
74
76
|
super({ executor, parseContext });
|
|
@@ -166,15 +168,20 @@ class Kysely extends query_creator_js_1.QueryCreator {
|
|
|
166
168
|
return new function_builder_js_1.FunctionBuilder({ executor: this.#props.executor });
|
|
167
169
|
}
|
|
168
170
|
/**
|
|
169
|
-
*
|
|
170
|
-
* otherwise it's committed.
|
|
171
|
+
* Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
|
|
171
172
|
*
|
|
172
|
-
* @
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
173
|
+
* The returned {@link TransactionBuilder} can be used to configure the transaction. The
|
|
174
|
+
* {@link TransactionBuilder.execute} method can then be called to run the transaction.
|
|
175
|
+
* {@link TransactionBuilder.execute} takes a function that is run inside the
|
|
176
|
+
* transaction. If the function throws, the transaction is rolled back. Otherwise
|
|
177
|
+
* the transaction is committed.
|
|
178
|
+
*
|
|
179
|
+
* The callback function passed to the {@link TransactionBuilder.execute | execute}
|
|
180
|
+
* method gets the transaction object as its only argument. The transaction is
|
|
181
|
+
* of type {@link Transaction} which inherits {@link Kysely}. Any query
|
|
182
|
+
* started through the transaction object is executed inside the transaction.
|
|
177
183
|
*
|
|
184
|
+
* @example
|
|
178
185
|
* ```ts
|
|
179
186
|
* const catto = await db.transaction().execute(async (trx) => {
|
|
180
187
|
* const jennifer = await trx.insertInto('person')
|
|
@@ -24,6 +24,38 @@ export declare class MigrationModule {
|
|
|
24
24
|
* path.join(__dirname, 'migrations')
|
|
25
25
|
* )
|
|
26
26
|
* ```
|
|
27
|
+
*
|
|
28
|
+
* In the next example, we use a record of migration objects instead of file folder path.
|
|
29
|
+
* The keys in the object are migration names that can be anything you want. The order of
|
|
30
|
+
* the migrations is determined based on the alphabetical order of the keys in the record.
|
|
31
|
+
* This can be useful if you are using a bundler like webpack or esbuild.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* await db.migration.migrateToLatest({
|
|
36
|
+
* migration1: {
|
|
37
|
+
* async up(db: Kysely<any>): Promise<void> {
|
|
38
|
+
* ...
|
|
39
|
+
* },
|
|
40
|
+
*
|
|
41
|
+
* async down(db: Kysely<any>): Promise<void> {
|
|
42
|
+
* ...
|
|
43
|
+
* }
|
|
44
|
+
* },
|
|
45
|
+
*
|
|
46
|
+
* migration2: {
|
|
47
|
+
* async up(db: Kysely<any>): Promise<void> {
|
|
48
|
+
* ...
|
|
49
|
+
* },
|
|
50
|
+
*
|
|
51
|
+
* async down(db: Kysely<any>): Promise<void> {
|
|
52
|
+
* ...
|
|
53
|
+
* }
|
|
54
|
+
* },
|
|
55
|
+
*
|
|
56
|
+
* ...
|
|
57
|
+
* })
|
|
58
|
+
* ```
|
|
27
59
|
*/
|
|
28
60
|
migrateToLatest(migrationsFolderPath: string): Promise<void>;
|
|
29
61
|
migrateToLatest(allMigrations: Record<string, Migration>): Promise<void>;
|
|
@@ -3,9 +3,9 @@ import { AnyColumn, AnyColumnWithTable } from '../util/type-utils.js';
|
|
|
3
3
|
import { ExtractAliasFromTableExpression, TableExpressionDatabase } from './table-parser.js';
|
|
4
4
|
import { JoinBuilder } from '../query-builder/join-builder.js';
|
|
5
5
|
import { ParseContext } from './parse-context.js';
|
|
6
|
-
export declare type JoinReferenceExpression<DB, TB extends keyof DB,
|
|
7
|
-
export declare type JoinCallbackExpression<DB, TB extends keyof DB,
|
|
8
|
-
declare type AnyJoinColumn<DB, TB extends keyof DB,
|
|
9
|
-
declare type AnyJoinColumnWithTable<DB, TB extends keyof DB,
|
|
6
|
+
export declare type JoinReferenceExpression<DB, TB extends keyof DB, TE> = AnyJoinColumn<DB, TB, TE> | AnyJoinColumnWithTable<DB, TB, TE>;
|
|
7
|
+
export declare type JoinCallbackExpression<DB, TB extends keyof DB, TE> = (join: JoinBuilder<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>>) => JoinBuilder<any, any>;
|
|
8
|
+
declare type AnyJoinColumn<DB, TB extends keyof DB, TE> = AnyColumn<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>>;
|
|
9
|
+
declare type AnyJoinColumnWithTable<DB, TB extends keyof DB, TE> = AnyColumnWithTable<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>>;
|
|
10
10
|
export declare function parseJoin(ctx: ParseContext, joinType: JoinType, args: any[]): JoinNode;
|
|
11
11
|
export {};
|
|
@@ -1582,6 +1582,41 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
1582
1582
|
* ```
|
|
1583
1583
|
*/
|
|
1584
1584
|
unionAll(expression: UnionExpression<DB, O>): QueryBuilder<DB, TB, O>;
|
|
1585
|
+
/**
|
|
1586
|
+
* Simply calls the given method passing `this` as the only argument.
|
|
1587
|
+
*
|
|
1588
|
+
* This method can be useful when adding optional method calls:
|
|
1589
|
+
*
|
|
1590
|
+
* @example
|
|
1591
|
+
* ```ts
|
|
1592
|
+
* db.selectFrom('person')
|
|
1593
|
+
* .selectAll()
|
|
1594
|
+
* .call((qb) => {
|
|
1595
|
+
* if (something) {
|
|
1596
|
+
* return qb.where('something', '=', something)
|
|
1597
|
+
* } else {
|
|
1598
|
+
* return qb.where('somethingElse', '=', somethingElse)
|
|
1599
|
+
* }
|
|
1600
|
+
* })
|
|
1601
|
+
* .execute()
|
|
1602
|
+
* ```
|
|
1603
|
+
*
|
|
1604
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
1605
|
+
*
|
|
1606
|
+
* @example
|
|
1607
|
+
* ```ts
|
|
1608
|
+
* function log<T extends AnyQueryBuilder>(qb: T): T {
|
|
1609
|
+
* console.log(qb.compile())
|
|
1610
|
+
* return qb
|
|
1611
|
+
* }
|
|
1612
|
+
*
|
|
1613
|
+
* db.selectFrom('person')
|
|
1614
|
+
* .selectAll()
|
|
1615
|
+
* .call(log)
|
|
1616
|
+
* .execute()
|
|
1617
|
+
* ```
|
|
1618
|
+
*/
|
|
1619
|
+
call<T>(func: (qb: this) => T): T;
|
|
1585
1620
|
/**
|
|
1586
1621
|
* Gives an alias for the query. This method is only useful for sub queries.
|
|
1587
1622
|
*
|
|
@@ -818,6 +818,43 @@ class QueryBuilder {
|
|
|
818
818
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithUnion(this.#props.queryNode, (0, union_parser_js_1.parseUnion)(expression, true)),
|
|
819
819
|
});
|
|
820
820
|
}
|
|
821
|
+
/**
|
|
822
|
+
* Simply calls the given method passing `this` as the only argument.
|
|
823
|
+
*
|
|
824
|
+
* This method can be useful when adding optional method calls:
|
|
825
|
+
*
|
|
826
|
+
* @example
|
|
827
|
+
* ```ts
|
|
828
|
+
* db.selectFrom('person')
|
|
829
|
+
* .selectAll()
|
|
830
|
+
* .call((qb) => {
|
|
831
|
+
* if (something) {
|
|
832
|
+
* return qb.where('something', '=', something)
|
|
833
|
+
* } else {
|
|
834
|
+
* return qb.where('somethingElse', '=', somethingElse)
|
|
835
|
+
* }
|
|
836
|
+
* })
|
|
837
|
+
* .execute()
|
|
838
|
+
* ```
|
|
839
|
+
*
|
|
840
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
841
|
+
*
|
|
842
|
+
* @example
|
|
843
|
+
* ```ts
|
|
844
|
+
* function log<T extends AnyQueryBuilder>(qb: T): T {
|
|
845
|
+
* console.log(qb.compile())
|
|
846
|
+
* return qb
|
|
847
|
+
* }
|
|
848
|
+
*
|
|
849
|
+
* db.selectFrom('person')
|
|
850
|
+
* .selectAll()
|
|
851
|
+
* .call(log)
|
|
852
|
+
* .execute()
|
|
853
|
+
* ```
|
|
854
|
+
*/
|
|
855
|
+
call(func) {
|
|
856
|
+
return func(this);
|
|
857
|
+
}
|
|
821
858
|
/**
|
|
822
859
|
* Gives an alias for the query. This method is only useful for sub queries.
|
|
823
860
|
*
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ArrayItemType } from './type-utils.js';
|
|
2
|
+
export declare const LOG_LEVELS: readonly ["query"];
|
|
3
|
+
export declare type LogLevel = ArrayItemType<typeof LOG_LEVELS>;
|
|
4
|
+
export declare class Log {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(levels: ReadonlyArray<LogLevel>);
|
|
7
|
+
query(callback: (log: (message: string) => void) => void): void;
|
|
8
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Log = exports.LOG_LEVELS = void 0;
|
|
4
|
+
exports.LOG_LEVELS = ['query'];
|
|
5
|
+
class Log {
|
|
6
|
+
#levels;
|
|
7
|
+
constructor(levels) {
|
|
8
|
+
this.#levels = {
|
|
9
|
+
query: levels.includes('query'),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
query(callback) {
|
|
13
|
+
if (this.#levels.query) {
|
|
14
|
+
callback(this.#query);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
#query(message) {
|
|
18
|
+
console.log(`kysely:query: ${message}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.Log = Log;
|
|
@@ -3,10 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.randomString = void 0;
|
|
4
4
|
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
5
5
|
function randomString(length) {
|
|
6
|
-
let chars =
|
|
6
|
+
let chars = '';
|
|
7
7
|
for (let i = 0; i < length; ++i) {
|
|
8
|
-
chars
|
|
8
|
+
chars += randomChar();
|
|
9
9
|
}
|
|
10
|
-
return chars
|
|
10
|
+
return chars;
|
|
11
11
|
}
|
|
12
12
|
exports.randomString = randomString;
|
|
13
|
+
function randomChar() {
|
|
14
|
+
return CHARS[Math.floor(Math.random() * CHARS.length)];
|
|
15
|
+
}
|
|
@@ -136,19 +136,19 @@ export declare type RawBuilderFactory<DB, TB extends keyof DB> = (qb: Expression
|
|
|
136
136
|
export declare type AliasedRawBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyAliasedRawBuilder;
|
|
137
137
|
export interface InsertResultTypeTag {
|
|
138
138
|
/** @internal */
|
|
139
|
-
__isInsertResultTypeTag__: true;
|
|
139
|
+
readonly __isInsertResultTypeTag__: true;
|
|
140
140
|
}
|
|
141
141
|
export interface DeleteResultTypeTag {
|
|
142
142
|
/** @internal */
|
|
143
|
-
__isDeleteResultTypeTag__: true;
|
|
143
|
+
readonly __isDeleteResultTypeTag__: true;
|
|
144
144
|
}
|
|
145
145
|
export interface UpdateResultTypeTag {
|
|
146
146
|
/** @internal */
|
|
147
|
-
__isUpdateResultTypeTag__: true;
|
|
147
|
+
readonly __isUpdateResultTypeTag__: true;
|
|
148
148
|
}
|
|
149
149
|
export interface GeneratedPlaceholder {
|
|
150
150
|
/** @internal */
|
|
151
|
-
__isGeneratedPlaceholder__: true;
|
|
151
|
+
readonly __isGeneratedPlaceholder__: true;
|
|
152
152
|
}
|
|
153
153
|
export declare type ManyResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O;
|
|
154
154
|
export declare type SingleResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O | undefined;
|
|
@@ -5,6 +5,13 @@ import { Dialect } from '../dialect.js';
|
|
|
5
5
|
import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
|
|
6
6
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
7
7
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
8
|
+
/**
|
|
9
|
+
* MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
|
|
10
|
+
*
|
|
11
|
+
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
12
|
+
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
13
|
+
* method.
|
|
14
|
+
*/
|
|
8
15
|
export declare class MysqlDialect implements Dialect {
|
|
9
16
|
#private;
|
|
10
17
|
constructor(config: MysqlDialectConfig);
|
|
@@ -17,6 +24,8 @@ export declare class MysqlDialect implements Dialect {
|
|
|
17
24
|
* Config for the mysql dialect.
|
|
18
25
|
*
|
|
19
26
|
* This interface is equal to `mysql2` library's pool config.
|
|
27
|
+
*
|
|
28
|
+
* https://github.com/sidorares/node-mysql2#using-connection-pools
|
|
20
29
|
*/
|
|
21
30
|
export interface MysqlDialectConfig {
|
|
22
31
|
/**
|
|
@@ -2,6 +2,13 @@ import { MysqlDriver } from './mysql-driver.js';
|
|
|
2
2
|
import { MysqlQueryCompiler } from './mysql-query-compiler.js';
|
|
3
3
|
import { MysqlIntrospector } from './mysql-introspector.js';
|
|
4
4
|
import { MysqlAdapter } from './mysql-adapter.js';
|
|
5
|
+
/**
|
|
6
|
+
* MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
|
|
7
|
+
*
|
|
8
|
+
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
9
|
+
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
10
|
+
* method.
|
|
11
|
+
*/
|
|
5
12
|
export class MysqlDialect {
|
|
6
13
|
#config;
|
|
7
14
|
constructor(config) {
|
|
@@ -8,6 +8,16 @@ import { Dialect } from '../dialect.js';
|
|
|
8
8
|
import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
|
|
9
9
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
10
10
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
11
|
+
/**
|
|
12
|
+
* PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
|
|
13
|
+
*
|
|
14
|
+
* The {@link PostgresDialectConfig | configuration} passed to the constructor
|
|
15
|
+
* is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
|
|
16
|
+
* constructor. See the following two links for more documentation:
|
|
17
|
+
*
|
|
18
|
+
* https://node-postgres.com/api/pool
|
|
19
|
+
* https://node-postgres.com/api/client
|
|
20
|
+
*/
|
|
11
21
|
export declare class PostgresDialect implements Dialect {
|
|
12
22
|
#private;
|
|
13
23
|
constructor(config: PostgresDialectConfig);
|
|
@@ -19,7 +29,7 @@ export declare class PostgresDialect implements Dialect {
|
|
|
19
29
|
/**
|
|
20
30
|
* Config for the postgres dialect.
|
|
21
31
|
*
|
|
22
|
-
* This interface is equal to `pg` library's
|
|
32
|
+
* This interface is equal to `pg` library's `Pool` config:
|
|
23
33
|
*
|
|
24
34
|
* https://node-postgres.com/api/pool
|
|
25
35
|
* https://node-postgres.com/api/client
|
|
@@ -2,6 +2,16 @@ import { PostgresDriver } from './postgres-driver.js';
|
|
|
2
2
|
import { PostgresIntrospector } from './postgres-introspector.js';
|
|
3
3
|
import { PostgresQueryCompiler } from './postgres-query-compiler.js';
|
|
4
4
|
import { PostgresAdapter } from './postgres-adapter.js';
|
|
5
|
+
/**
|
|
6
|
+
* PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
|
|
7
|
+
*
|
|
8
|
+
* The {@link PostgresDialectConfig | configuration} passed to the constructor
|
|
9
|
+
* is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
|
|
10
|
+
* constructor. See the following two links for more documentation:
|
|
11
|
+
*
|
|
12
|
+
* https://node-postgres.com/api/pool
|
|
13
|
+
* https://node-postgres.com/api/client
|
|
14
|
+
*/
|
|
5
15
|
export class PostgresDialect {
|
|
6
16
|
#config;
|
|
7
17
|
constructor(config) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Log } from '../util/log.js';
|
|
1
2
|
import { DatabaseConnection } from './database-connection.js';
|
|
2
3
|
import { Driver, TransactionSettings } from './driver.js';
|
|
3
4
|
/**
|
|
@@ -7,10 +8,10 @@ import { Driver, TransactionSettings } from './driver.js';
|
|
|
7
8
|
*/
|
|
8
9
|
export declare class RuntimeDriver implements Driver {
|
|
9
10
|
#private;
|
|
10
|
-
constructor(driver: Driver);
|
|
11
|
+
constructor(driver: Driver, log: Log);
|
|
11
12
|
init(): Promise<void>;
|
|
12
13
|
acquireConnection(): Promise<DatabaseConnection>;
|
|
13
|
-
releaseConnection(
|
|
14
|
+
releaseConnection(runtimeConnection: DatabaseConnection): Promise<void>;
|
|
14
15
|
beginTransaction(connection: DatabaseConnection, settings: TransactionSettings): Promise<void>;
|
|
15
16
|
commitTransaction(connection: DatabaseConnection): Promise<void>;
|
|
16
17
|
rollbackTransaction(connection: DatabaseConnection): Promise<void>;
|
|
@@ -5,10 +5,13 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export class RuntimeDriver {
|
|
7
7
|
#driver;
|
|
8
|
+
#log;
|
|
8
9
|
#initPromise;
|
|
9
10
|
#destroyPromise;
|
|
10
|
-
|
|
11
|
+
#connections = new WeakMap();
|
|
12
|
+
constructor(driver, log) {
|
|
11
13
|
this.#driver = driver;
|
|
14
|
+
this.#log = log;
|
|
12
15
|
}
|
|
13
16
|
async init() {
|
|
14
17
|
if (!this.#initPromise) {
|
|
@@ -21,10 +24,18 @@ export class RuntimeDriver {
|
|
|
21
24
|
}
|
|
22
25
|
async acquireConnection() {
|
|
23
26
|
await this.init();
|
|
24
|
-
|
|
27
|
+
const connection = await this.#driver.acquireConnection();
|
|
28
|
+
let runtimeConnection = this.#connections.get(connection);
|
|
29
|
+
if (!runtimeConnection) {
|
|
30
|
+
runtimeConnection = new RuntimeConnection(connection, this.#log);
|
|
31
|
+
this.#connections.set(connection, runtimeConnection);
|
|
32
|
+
}
|
|
33
|
+
return runtimeConnection;
|
|
25
34
|
}
|
|
26
|
-
releaseConnection(
|
|
27
|
-
|
|
35
|
+
async releaseConnection(runtimeConnection) {
|
|
36
|
+
if (runtimeConnection instanceof RuntimeConnection) {
|
|
37
|
+
await this.#driver.releaseConnection(runtimeConnection.connection);
|
|
38
|
+
}
|
|
28
39
|
}
|
|
29
40
|
beginTransaction(connection, settings) {
|
|
30
41
|
return this.#driver.beginTransaction(connection, settings);
|
|
@@ -49,3 +60,31 @@ export class RuntimeDriver {
|
|
|
49
60
|
await this.#destroyPromise;
|
|
50
61
|
}
|
|
51
62
|
}
|
|
63
|
+
class RuntimeConnection {
|
|
64
|
+
#connection;
|
|
65
|
+
#log;
|
|
66
|
+
get connection() {
|
|
67
|
+
return this.#connection;
|
|
68
|
+
}
|
|
69
|
+
constructor(connection, log) {
|
|
70
|
+
this.#connection = connection;
|
|
71
|
+
this.#log = log;
|
|
72
|
+
}
|
|
73
|
+
async executeQuery(compiledQuery) {
|
|
74
|
+
const startTime = process.hrtime.bigint();
|
|
75
|
+
try {
|
|
76
|
+
return await this.#connection.executeQuery(compiledQuery);
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
this.#log.query((log) => {
|
|
80
|
+
log(compiledQuery.sql);
|
|
81
|
+
log(`duration: ${this.#calculateDurationMillis(startTime)}ms`);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
#calculateDurationMillis(startTime) {
|
|
86
|
+
const endTime = process.hrtime.bigint();
|
|
87
|
+
const durationTensMillis = Number((endTime - startTime) / 100000n);
|
|
88
|
+
return durationTensMillis / 10;
|
|
89
|
+
}
|
|
90
|
+
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -110,7 +110,7 @@ export * from './operation-node/operation-node-source.js';
|
|
|
110
110
|
export * from './operation-node/order-by-node.js';
|
|
111
111
|
export * from './introspection/database-introspector.js';
|
|
112
112
|
export * from './util/compilable.js';
|
|
113
|
-
export { AnyColumn, UnknownRow
|
|
113
|
+
export { AnyColumn, UnknownRow, AnyQueryBuilder } from './util/type-utils.js';
|
|
114
114
|
export { SelectExpression, SelectExpressionOrList, } from './parser/select-parser.js';
|
|
115
115
|
export { ReferenceExpression, ReferenceExpressionOrList, } from './parser/reference-parser.js';
|
|
116
116
|
export { ValueExpression, ValueExpressionOrList, } from './parser/value-parser.js';
|
package/dist/esm/kysely.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { DatabaseIntrospector } from './introspection/database-introspector.js';
|
|
|
10
10
|
import { Driver, IsolationLevel } from './driver/driver.js';
|
|
11
11
|
import { ParseContext } from './parser/parse-context.js';
|
|
12
12
|
import { FunctionBuilder } from './query-builder/function-builder.js';
|
|
13
|
+
import { LogLevel } from './util/log.js';
|
|
13
14
|
/**
|
|
14
15
|
* The main Kysely class.
|
|
15
16
|
*
|
|
@@ -127,15 +128,20 @@ export declare class Kysely<DB> extends QueryCreator<DB> {
|
|
|
127
128
|
*/
|
|
128
129
|
get fn(): FunctionBuilder<DB, keyof DB>;
|
|
129
130
|
/**
|
|
130
|
-
*
|
|
131
|
-
* otherwise it's committed.
|
|
131
|
+
* Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
|
|
132
132
|
*
|
|
133
|
-
* @
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
133
|
+
* The returned {@link TransactionBuilder} can be used to configure the transaction. The
|
|
134
|
+
* {@link TransactionBuilder.execute} method can then be called to run the transaction.
|
|
135
|
+
* {@link TransactionBuilder.execute} takes a function that is run inside the
|
|
136
|
+
* transaction. If the function throws, the transaction is rolled back. Otherwise
|
|
137
|
+
* the transaction is committed.
|
|
138
|
+
*
|
|
139
|
+
* The callback function passed to the {@link TransactionBuilder.execute | execute}
|
|
140
|
+
* method gets the transaction object as its only argument. The transaction is
|
|
141
|
+
* of type {@link Transaction} which inherits {@link Kysely}. Any query
|
|
142
|
+
* started through the transaction object is executed inside the transaction.
|
|
138
143
|
*
|
|
144
|
+
* @example
|
|
139
145
|
* ```ts
|
|
140
146
|
* const catto = await db.transaction().execute(async (trx) => {
|
|
141
147
|
* const jennifer = await trx.insertInto('person')
|
|
@@ -220,8 +226,19 @@ export interface KyselyProps {
|
|
|
220
226
|
}
|
|
221
227
|
export declare function isKyselyProps(obj: unknown): obj is KyselyProps;
|
|
222
228
|
export interface KyselyConfig {
|
|
223
|
-
dialect: Dialect;
|
|
224
|
-
plugins?: KyselyPlugin[];
|
|
229
|
+
readonly dialect: Dialect;
|
|
230
|
+
readonly plugins?: KyselyPlugin[];
|
|
231
|
+
/**
|
|
232
|
+
* A list of log levels to log.
|
|
233
|
+
*
|
|
234
|
+
* Currently there's only one level: `query` and it's logged using
|
|
235
|
+
* `console.log`. This will be expanded based on user request later.
|
|
236
|
+
*
|
|
237
|
+
* Log levels:
|
|
238
|
+
*
|
|
239
|
+
* - query: Log each query's SQL and duration.
|
|
240
|
+
*/
|
|
241
|
+
readonly log?: ReadonlyArray<LogLevel>;
|
|
225
242
|
}
|
|
226
243
|
export declare class ConnectionBuilder<DB> {
|
|
227
244
|
#private;
|
package/dist/esm/kysely.js
CHANGED
|
@@ -12,6 +12,7 @@ import { TRANSACTION_ISOLATION_LEVELS, } from './driver/driver.js';
|
|
|
12
12
|
import { preventAwait } from './util/prevent-await.js';
|
|
13
13
|
import { DefaultParseContext } from './parser/parse-context.js';
|
|
14
14
|
import { FunctionBuilder } from './query-builder/function-builder.js';
|
|
15
|
+
import { Log } from './util/log.js';
|
|
15
16
|
/**
|
|
16
17
|
* The main Kysely class.
|
|
17
18
|
*
|
|
@@ -57,15 +58,16 @@ export class Kysely extends QueryCreator {
|
|
|
57
58
|
constructor(args) {
|
|
58
59
|
if (isKyselyProps(args)) {
|
|
59
60
|
super({ executor: args.executor, parseContext: args.parseContext });
|
|
60
|
-
this.#props = freeze(args);
|
|
61
|
+
this.#props = freeze({ ...args });
|
|
61
62
|
}
|
|
62
63
|
else {
|
|
63
64
|
const dialect = args.dialect;
|
|
64
65
|
const driver = dialect.createDriver();
|
|
65
66
|
const compiler = dialect.createQueryCompiler();
|
|
66
67
|
const adapter = dialect.createAdapter();
|
|
68
|
+
const log = new Log(args.log ?? []);
|
|
67
69
|
const parseContext = new DefaultParseContext(adapter);
|
|
68
|
-
const runtimeDriver = new RuntimeDriver(driver);
|
|
70
|
+
const runtimeDriver = new RuntimeDriver(driver, log);
|
|
69
71
|
const connectionProvider = new DefaultConnectionProvider(runtimeDriver);
|
|
70
72
|
const executor = new DefaultQueryExecutor(compiler, connectionProvider, args.plugins ?? []);
|
|
71
73
|
super({ executor, parseContext });
|
|
@@ -163,15 +165,20 @@ export class Kysely extends QueryCreator {
|
|
|
163
165
|
return new FunctionBuilder({ executor: this.#props.executor });
|
|
164
166
|
}
|
|
165
167
|
/**
|
|
166
|
-
*
|
|
167
|
-
* otherwise it's committed.
|
|
168
|
+
* Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.
|
|
168
169
|
*
|
|
169
|
-
* @
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
170
|
+
* The returned {@link TransactionBuilder} can be used to configure the transaction. The
|
|
171
|
+
* {@link TransactionBuilder.execute} method can then be called to run the transaction.
|
|
172
|
+
* {@link TransactionBuilder.execute} takes a function that is run inside the
|
|
173
|
+
* transaction. If the function throws, the transaction is rolled back. Otherwise
|
|
174
|
+
* the transaction is committed.
|
|
175
|
+
*
|
|
176
|
+
* The callback function passed to the {@link TransactionBuilder.execute | execute}
|
|
177
|
+
* method gets the transaction object as its only argument. The transaction is
|
|
178
|
+
* of type {@link Transaction} which inherits {@link Kysely}. Any query
|
|
179
|
+
* started through the transaction object is executed inside the transaction.
|
|
174
180
|
*
|
|
181
|
+
* @example
|
|
175
182
|
* ```ts
|
|
176
183
|
* const catto = await db.transaction().execute(async (trx) => {
|
|
177
184
|
* const jennifer = await trx.insertInto('person')
|
|
@@ -24,6 +24,38 @@ export declare class MigrationModule {
|
|
|
24
24
|
* path.join(__dirname, 'migrations')
|
|
25
25
|
* )
|
|
26
26
|
* ```
|
|
27
|
+
*
|
|
28
|
+
* In the next example, we use a record of migration objects instead of file folder path.
|
|
29
|
+
* The keys in the object are migration names that can be anything you want. The order of
|
|
30
|
+
* the migrations is determined based on the alphabetical order of the keys in the record.
|
|
31
|
+
* This can be useful if you are using a bundler like webpack or esbuild.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* await db.migration.migrateToLatest({
|
|
36
|
+
* migration1: {
|
|
37
|
+
* async up(db: Kysely<any>): Promise<void> {
|
|
38
|
+
* ...
|
|
39
|
+
* },
|
|
40
|
+
*
|
|
41
|
+
* async down(db: Kysely<any>): Promise<void> {
|
|
42
|
+
* ...
|
|
43
|
+
* }
|
|
44
|
+
* },
|
|
45
|
+
*
|
|
46
|
+
* migration2: {
|
|
47
|
+
* async up(db: Kysely<any>): Promise<void> {
|
|
48
|
+
* ...
|
|
49
|
+
* },
|
|
50
|
+
*
|
|
51
|
+
* async down(db: Kysely<any>): Promise<void> {
|
|
52
|
+
* ...
|
|
53
|
+
* }
|
|
54
|
+
* },
|
|
55
|
+
*
|
|
56
|
+
* ...
|
|
57
|
+
* })
|
|
58
|
+
* ```
|
|
27
59
|
*/
|
|
28
60
|
migrateToLatest(migrationsFolderPath: string): Promise<void>;
|
|
29
61
|
migrateToLatest(allMigrations: Record<string, Migration>): Promise<void>;
|
|
@@ -3,9 +3,9 @@ import { AnyColumn, AnyColumnWithTable } from '../util/type-utils.js';
|
|
|
3
3
|
import { ExtractAliasFromTableExpression, TableExpressionDatabase } from './table-parser.js';
|
|
4
4
|
import { JoinBuilder } from '../query-builder/join-builder.js';
|
|
5
5
|
import { ParseContext } from './parse-context.js';
|
|
6
|
-
export declare type JoinReferenceExpression<DB, TB extends keyof DB,
|
|
7
|
-
export declare type JoinCallbackExpression<DB, TB extends keyof DB,
|
|
8
|
-
declare type AnyJoinColumn<DB, TB extends keyof DB,
|
|
9
|
-
declare type AnyJoinColumnWithTable<DB, TB extends keyof DB,
|
|
6
|
+
export declare type JoinReferenceExpression<DB, TB extends keyof DB, TE> = AnyJoinColumn<DB, TB, TE> | AnyJoinColumnWithTable<DB, TB, TE>;
|
|
7
|
+
export declare type JoinCallbackExpression<DB, TB extends keyof DB, TE> = (join: JoinBuilder<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>>) => JoinBuilder<any, any>;
|
|
8
|
+
declare type AnyJoinColumn<DB, TB extends keyof DB, TE> = AnyColumn<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>>;
|
|
9
|
+
declare type AnyJoinColumnWithTable<DB, TB extends keyof DB, TE> = AnyColumnWithTable<TableExpressionDatabase<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>>;
|
|
10
10
|
export declare function parseJoin(ctx: ParseContext, joinType: JoinType, args: any[]): JoinNode;
|
|
11
11
|
export {};
|
|
@@ -1582,6 +1582,41 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
1582
1582
|
* ```
|
|
1583
1583
|
*/
|
|
1584
1584
|
unionAll(expression: UnionExpression<DB, O>): QueryBuilder<DB, TB, O>;
|
|
1585
|
+
/**
|
|
1586
|
+
* Simply calls the given method passing `this` as the only argument.
|
|
1587
|
+
*
|
|
1588
|
+
* This method can be useful when adding optional method calls:
|
|
1589
|
+
*
|
|
1590
|
+
* @example
|
|
1591
|
+
* ```ts
|
|
1592
|
+
* db.selectFrom('person')
|
|
1593
|
+
* .selectAll()
|
|
1594
|
+
* .call((qb) => {
|
|
1595
|
+
* if (something) {
|
|
1596
|
+
* return qb.where('something', '=', something)
|
|
1597
|
+
* } else {
|
|
1598
|
+
* return qb.where('somethingElse', '=', somethingElse)
|
|
1599
|
+
* }
|
|
1600
|
+
* })
|
|
1601
|
+
* .execute()
|
|
1602
|
+
* ```
|
|
1603
|
+
*
|
|
1604
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
1605
|
+
*
|
|
1606
|
+
* @example
|
|
1607
|
+
* ```ts
|
|
1608
|
+
* function log<T extends AnyQueryBuilder>(qb: T): T {
|
|
1609
|
+
* console.log(qb.compile())
|
|
1610
|
+
* return qb
|
|
1611
|
+
* }
|
|
1612
|
+
*
|
|
1613
|
+
* db.selectFrom('person')
|
|
1614
|
+
* .selectAll()
|
|
1615
|
+
* .call(log)
|
|
1616
|
+
* .execute()
|
|
1617
|
+
* ```
|
|
1618
|
+
*/
|
|
1619
|
+
call<T>(func: (qb: this) => T): T;
|
|
1585
1620
|
/**
|
|
1586
1621
|
* Gives an alias for the query. This method is only useful for sub queries.
|
|
1587
1622
|
*
|
|
@@ -815,6 +815,43 @@ export class QueryBuilder {
|
|
|
815
815
|
queryNode: SelectQueryNode.cloneWithUnion(this.#props.queryNode, parseUnion(expression, true)),
|
|
816
816
|
});
|
|
817
817
|
}
|
|
818
|
+
/**
|
|
819
|
+
* Simply calls the given method passing `this` as the only argument.
|
|
820
|
+
*
|
|
821
|
+
* This method can be useful when adding optional method calls:
|
|
822
|
+
*
|
|
823
|
+
* @example
|
|
824
|
+
* ```ts
|
|
825
|
+
* db.selectFrom('person')
|
|
826
|
+
* .selectAll()
|
|
827
|
+
* .call((qb) => {
|
|
828
|
+
* if (something) {
|
|
829
|
+
* return qb.where('something', '=', something)
|
|
830
|
+
* } else {
|
|
831
|
+
* return qb.where('somethingElse', '=', somethingElse)
|
|
832
|
+
* }
|
|
833
|
+
* })
|
|
834
|
+
* .execute()
|
|
835
|
+
* ```
|
|
836
|
+
*
|
|
837
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
838
|
+
*
|
|
839
|
+
* @example
|
|
840
|
+
* ```ts
|
|
841
|
+
* function log<T extends AnyQueryBuilder>(qb: T): T {
|
|
842
|
+
* console.log(qb.compile())
|
|
843
|
+
* return qb
|
|
844
|
+
* }
|
|
845
|
+
*
|
|
846
|
+
* db.selectFrom('person')
|
|
847
|
+
* .selectAll()
|
|
848
|
+
* .call(log)
|
|
849
|
+
* .execute()
|
|
850
|
+
* ```
|
|
851
|
+
*/
|
|
852
|
+
call(func) {
|
|
853
|
+
return func(this);
|
|
854
|
+
}
|
|
818
855
|
/**
|
|
819
856
|
* Gives an alias for the query. This method is only useful for sub queries.
|
|
820
857
|
*
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ArrayItemType } from './type-utils.js';
|
|
2
|
+
export declare const LOG_LEVELS: readonly ["query"];
|
|
3
|
+
export declare type LogLevel = ArrayItemType<typeof LOG_LEVELS>;
|
|
4
|
+
export declare class Log {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(levels: ReadonlyArray<LogLevel>);
|
|
7
|
+
query(callback: (log: (message: string) => void) => void): void;
|
|
8
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const LOG_LEVELS = ['query'];
|
|
2
|
+
export class Log {
|
|
3
|
+
#levels;
|
|
4
|
+
constructor(levels) {
|
|
5
|
+
this.#levels = {
|
|
6
|
+
query: levels.includes('query'),
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
query(callback) {
|
|
10
|
+
if (this.#levels.query) {
|
|
11
|
+
callback(this.#query);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
#query(message) {
|
|
15
|
+
console.log(`kysely:query: ${message}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
2
2
|
export function randomString(length) {
|
|
3
|
-
let chars =
|
|
3
|
+
let chars = '';
|
|
4
4
|
for (let i = 0; i < length; ++i) {
|
|
5
|
-
chars
|
|
5
|
+
chars += randomChar();
|
|
6
6
|
}
|
|
7
|
-
return chars
|
|
7
|
+
return chars;
|
|
8
|
+
}
|
|
9
|
+
function randomChar() {
|
|
10
|
+
return CHARS[Math.floor(Math.random() * CHARS.length)];
|
|
8
11
|
}
|
|
@@ -136,19 +136,19 @@ export declare type RawBuilderFactory<DB, TB extends keyof DB> = (qb: Expression
|
|
|
136
136
|
export declare type AliasedRawBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyAliasedRawBuilder;
|
|
137
137
|
export interface InsertResultTypeTag {
|
|
138
138
|
/** @internal */
|
|
139
|
-
__isInsertResultTypeTag__: true;
|
|
139
|
+
readonly __isInsertResultTypeTag__: true;
|
|
140
140
|
}
|
|
141
141
|
export interface DeleteResultTypeTag {
|
|
142
142
|
/** @internal */
|
|
143
|
-
__isDeleteResultTypeTag__: true;
|
|
143
|
+
readonly __isDeleteResultTypeTag__: true;
|
|
144
144
|
}
|
|
145
145
|
export interface UpdateResultTypeTag {
|
|
146
146
|
/** @internal */
|
|
147
|
-
__isUpdateResultTypeTag__: true;
|
|
147
|
+
readonly __isUpdateResultTypeTag__: true;
|
|
148
148
|
}
|
|
149
149
|
export interface GeneratedPlaceholder {
|
|
150
150
|
/** @internal */
|
|
151
|
-
__isGeneratedPlaceholder__: true;
|
|
151
|
+
readonly __isGeneratedPlaceholder__: true;
|
|
152
152
|
}
|
|
153
153
|
export declare type ManyResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O;
|
|
154
154
|
export declare type SingleResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O | undefined;
|