kysely 0.19.8 → 0.19.11
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/README.md +67 -13
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +10 -0
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +1 -0
- package/dist/cjs/dialect/mysql/mysql-driver.js +25 -0
- package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +29 -0
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +6 -2
- package/dist/cjs/dialect/postgres/postgres-driver.js +24 -2
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +3 -0
- package/dist/cjs/driver/database-connection.d.ts +1 -0
- package/dist/cjs/driver/dummy-driver.js +3 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +4 -0
- package/dist/cjs/operation-node/create-type-node.d.ts +17 -0
- package/dist/cjs/operation-node/create-type-node.js +27 -0
- package/dist/cjs/operation-node/data-type-node.d.ts +1 -1
- package/dist/cjs/operation-node/drop-type-node.d.ts +16 -0
- package/dist/cjs/operation-node/drop-type-node.js +25 -0
- package/dist/cjs/operation-node/insert-query-node.d.ts +2 -1
- package/dist/cjs/operation-node/insert-query-node.js +2 -1
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +9 -2
- package/dist/cjs/operation-node/operation-node-transformer.js +148 -122
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +6 -0
- package/dist/cjs/operation-node/operation-node-visitor.js +3 -0
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/select-modifier-node.d.ts +16 -0
- package/dist/cjs/operation-node/select-modifier-node.js +24 -0
- package/dist/cjs/operation-node/select-query-node.d.ts +5 -5
- package/dist/cjs/operation-node/select-query-node.js +11 -9
- package/dist/cjs/plugin/with-schema/with-schema-transformer.d.ts +4 -0
- package/dist/cjs/plugin/with-schema/with-schema-transformer.js +6 -0
- package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -1
- package/dist/cjs/query-builder/select-query-builder.d.ts +77 -6
- package/dist/cjs/query-builder/select-query-builder.js +101 -14
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +6 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +30 -8
- package/dist/cjs/query-compiler/query-compiler.d.ts +3 -1
- package/dist/cjs/query-creator.d.ts +27 -0
- package/dist/cjs/query-creator.js +33 -0
- package/dist/cjs/query-executor/query-executor-base.d.ts +6 -0
- package/dist/cjs/query-executor/query-executor-base.js +19 -0
- package/dist/cjs/query-executor/query-executor.d.ts +11 -0
- package/dist/cjs/schema/create-table-builder.d.ts +6 -6
- package/dist/cjs/schema/create-table-builder.js +6 -6
- package/dist/cjs/schema/create-type-builder.d.ts +28 -0
- package/dist/cjs/schema/create-type-builder.js +38 -0
- package/dist/cjs/schema/drop-type-builder.d.ts +19 -0
- package/dist/cjs/schema/drop-type-builder.js +31 -0
- package/dist/cjs/schema/schema.d.ts +32 -0
- package/dist/cjs/schema/schema.js +46 -0
- package/dist/cjs/util/deferred.d.ts +7 -0
- package/dist/cjs/util/deferred.js +28 -0
- package/dist/cjs/util/require-all-props.d.ts +43 -0
- package/dist/cjs/util/require-all-props.js +40 -0
- package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +10 -0
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +1 -0
- package/dist/esm/dialect/mysql/mysql-driver.js +25 -0
- package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +29 -0
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +6 -2
- package/dist/esm/dialect/postgres/postgres-driver.js +24 -2
- package/dist/esm/dialect/sqlite/sqlite-driver.js +3 -0
- package/dist/esm/driver/database-connection.d.ts +1 -0
- package/dist/esm/driver/dummy-driver.js +3 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/operation-node/create-type-node.d.ts +17 -0
- package/dist/esm/operation-node/create-type-node.js +25 -0
- package/dist/esm/operation-node/data-type-node.d.ts +1 -1
- package/dist/esm/operation-node/drop-type-node.d.ts +16 -0
- package/dist/esm/operation-node/drop-type-node.js +23 -0
- package/dist/esm/operation-node/insert-query-node.d.ts +2 -1
- package/dist/esm/operation-node/insert-query-node.js +2 -1
- package/dist/esm/operation-node/operation-node-transformer.d.ts +9 -2
- package/dist/esm/operation-node/operation-node-transformer.js +148 -122
- package/dist/esm/operation-node/operation-node-visitor.d.ts +6 -0
- package/dist/esm/operation-node/operation-node-visitor.js +3 -0
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/select-modifier-node.d.ts +16 -0
- package/dist/esm/operation-node/select-modifier-node.js +22 -0
- package/dist/esm/operation-node/select-query-node.d.ts +5 -5
- package/dist/esm/operation-node/select-query-node.js +11 -9
- package/dist/esm/plugin/with-schema/with-schema-transformer.d.ts +4 -0
- package/dist/esm/plugin/with-schema/with-schema-transformer.js +6 -0
- package/dist/esm/query-builder/insert-query-builder.d.ts +1 -1
- package/dist/esm/query-builder/select-query-builder.d.ts +77 -6
- package/dist/esm/query-builder/select-query-builder.js +101 -14
- package/dist/esm/query-compiler/default-query-compiler.d.ts +6 -0
- package/dist/esm/query-compiler/default-query-compiler.js +30 -8
- package/dist/esm/query-compiler/query-compiler.d.ts +3 -1
- package/dist/esm/query-creator.d.ts +27 -0
- package/dist/esm/query-creator.js +33 -0
- package/dist/esm/query-executor/query-executor-base.d.ts +6 -0
- package/dist/esm/query-executor/query-executor-base.js +19 -0
- package/dist/esm/query-executor/query-executor.d.ts +11 -0
- package/dist/esm/schema/create-table-builder.d.ts +6 -6
- package/dist/esm/schema/create-table-builder.js +6 -6
- package/dist/esm/schema/create-type-builder.d.ts +28 -0
- package/dist/esm/schema/create-type-builder.js +35 -0
- package/dist/esm/schema/drop-type-builder.d.ts +19 -0
- package/dist/esm/schema/drop-type-builder.js +28 -0
- package/dist/esm/schema/schema.d.ts +32 -0
- package/dist/esm/schema/schema.js +46 -0
- package/dist/esm/util/deferred.d.ts +7 -0
- package/dist/esm/util/deferred.js +25 -0
- package/dist/esm/util/require-all-props.d.ts +43 -0
- package/dist/esm/util/require-all-props.js +37 -0
- package/package.json +5 -2
|
@@ -36,6 +36,12 @@ export declare abstract class QueryExecutorBase implements QueryExecutor {
|
|
|
36
36
|
* `transformResult` method.
|
|
37
37
|
*/
|
|
38
38
|
executeQuery<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
|
|
39
|
+
/**
|
|
40
|
+
* Executes a compiled query and runs the result through all plugins'
|
|
41
|
+
* `transformResult` method. Results are streamead instead of loaded
|
|
42
|
+
* at once.
|
|
43
|
+
*/
|
|
44
|
+
stream<R>(compiledQuery: CompiledQuery, chunkSize: number, queryId: QueryId): AsyncIterableIterator<QueryResult<R>>;
|
|
39
45
|
/**
|
|
40
46
|
* Returns a copy of this executor with a new connection provider.
|
|
41
47
|
*/
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.QueryExecutorBase = void 0;
|
|
4
4
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
+
const deferred_js_1 = require("../util/deferred.js");
|
|
5
6
|
const NO_PLUGINS = (0, object_utils_js_1.freeze)([]);
|
|
6
7
|
class QueryExecutorBase {
|
|
7
8
|
#plugins;
|
|
@@ -36,6 +37,24 @@ class QueryExecutorBase {
|
|
|
36
37
|
return this.#transformResult(result, queryId);
|
|
37
38
|
});
|
|
38
39
|
}
|
|
40
|
+
async *stream(compiledQuery, chunkSize, queryId) {
|
|
41
|
+
const connectionDefer = new deferred_js_1.Deferred();
|
|
42
|
+
const connectionReleaseDefer = new deferred_js_1.Deferred();
|
|
43
|
+
this.provideConnection(async (connection) => {
|
|
44
|
+
connectionDefer.resolve(connection);
|
|
45
|
+
// Lets wait until we don't need connection before returning here (returning releases connection)
|
|
46
|
+
return await connectionReleaseDefer.promise;
|
|
47
|
+
}).catch((ex) => connectionDefer.reject(ex));
|
|
48
|
+
const connection = await connectionDefer.promise;
|
|
49
|
+
try {
|
|
50
|
+
for await (const result of connection.streamQuery(compiledQuery, chunkSize)) {
|
|
51
|
+
yield await this.#transformResult(result, queryId);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
connectionReleaseDefer.resolve();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
39
58
|
async #transformResult(result, queryId) {
|
|
40
59
|
for (const plugin of this.#plugins) {
|
|
41
60
|
result = await plugin.transformResult({ result, queryId });
|
|
@@ -37,6 +37,17 @@ export interface QueryExecutor extends ConnectionProvider {
|
|
|
37
37
|
* `transformResult` method.
|
|
38
38
|
*/
|
|
39
39
|
executeQuery<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
|
|
40
|
+
/**
|
|
41
|
+
* Executes a compiled query and runs the result through all plugins'
|
|
42
|
+
* `transformResult` method. Results are streamead instead of loaded
|
|
43
|
+
* at once.
|
|
44
|
+
*/
|
|
45
|
+
stream<R>(compiledQuery: CompiledQuery,
|
|
46
|
+
/**
|
|
47
|
+
* How many rows should be pulled from the database at once. Supported
|
|
48
|
+
* only by the postgres driver.
|
|
49
|
+
*/
|
|
50
|
+
chunkSize: number, queryId: QueryId): AsyncIterableIterator<QueryResult<R>>;
|
|
40
51
|
/**
|
|
41
52
|
* Returns a copy of this executor with a new connection provider.
|
|
42
53
|
*/
|
|
@@ -47,17 +47,17 @@ export declare class CreateTableBuilder<TB extends string, C extends string = ne
|
|
|
47
47
|
* .addColumn('first_name', 'varchar(50)', (col) => col.notNull())
|
|
48
48
|
* .addColumn('last_name', 'varchar(255)')
|
|
49
49
|
* .addColumn('bank_balance', 'numeric(8, 2)')
|
|
50
|
-
* .addColumn('data', sql`
|
|
50
|
+
* .addColumn('data', sql`some_type`)
|
|
51
51
|
* .addColumn('parent_id', 'integer', (col) =>
|
|
52
52
|
* col.references('person.id').onDelete('cascade'))
|
|
53
53
|
* )
|
|
54
54
|
* ```
|
|
55
55
|
*
|
|
56
|
-
* With this method, it's once again good to remember that Kysely just builds the
|
|
57
|
-
* and doesn't provide the same API for all databses. For example, some
|
|
58
|
-
* older MySQL don't support `references` statement in the
|
|
59
|
-
* foreign key constraints need to be defined in
|
|
60
|
-
* query. See the next example:
|
|
56
|
+
* With this method, it's once again good to remember that Kysely just builds the
|
|
57
|
+
* query and doesn't provide the same API for all databses. For example, some
|
|
58
|
+
* databases like older MySQL don't support the `references` statement in the
|
|
59
|
+
* column definition. Instead foreign key constraints need to be defined in the
|
|
60
|
+
* `create table` query. See the next example:
|
|
61
61
|
*
|
|
62
62
|
* ```ts
|
|
63
63
|
* .addColumn('parent_id', 'integer')
|
|
@@ -76,17 +76,17 @@ class CreateTableBuilder {
|
|
|
76
76
|
* .addColumn('first_name', 'varchar(50)', (col) => col.notNull())
|
|
77
77
|
* .addColumn('last_name', 'varchar(255)')
|
|
78
78
|
* .addColumn('bank_balance', 'numeric(8, 2)')
|
|
79
|
-
* .addColumn('data', sql`
|
|
79
|
+
* .addColumn('data', sql`some_type`)
|
|
80
80
|
* .addColumn('parent_id', 'integer', (col) =>
|
|
81
81
|
* col.references('person.id').onDelete('cascade'))
|
|
82
82
|
* )
|
|
83
83
|
* ```
|
|
84
84
|
*
|
|
85
|
-
* With this method, it's once again good to remember that Kysely just builds the
|
|
86
|
-
* and doesn't provide the same API for all databses. For example, some
|
|
87
|
-
* older MySQL don't support `references` statement in the
|
|
88
|
-
* foreign key constraints need to be defined in
|
|
89
|
-
* query. See the next example:
|
|
85
|
+
* With this method, it's once again good to remember that Kysely just builds the
|
|
86
|
+
* query and doesn't provide the same API for all databses. For example, some
|
|
87
|
+
* databases like older MySQL don't support the `references` statement in the
|
|
88
|
+
* column definition. Instead foreign key constraints need to be defined in the
|
|
89
|
+
* `create table` query. See the next example:
|
|
90
90
|
*
|
|
91
91
|
* ```ts
|
|
92
92
|
* .addColumn('parent_id', 'integer')
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
2
|
+
import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
3
|
+
import { Compilable } from '../util/compilable.js';
|
|
4
|
+
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
5
|
+
import { QueryId } from '../util/query-id.js';
|
|
6
|
+
import { CreateTypeNode } from '../operation-node/create-type-node.js';
|
|
7
|
+
export declare class CreateTypeBuilder implements OperationNodeSource, Compilable {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(props: CreateTypeBuilderProps);
|
|
10
|
+
toOperationNode(): CreateTypeNode;
|
|
11
|
+
/**
|
|
12
|
+
* Creates an anum type.
|
|
13
|
+
*
|
|
14
|
+
* ### Examples
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* db.schema.createType('species').asEnum(['cat', 'dog', 'frog'])
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
asEnum(values: string[]): CreateTypeBuilder;
|
|
21
|
+
compile(): CompiledQuery;
|
|
22
|
+
execute(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export interface CreateTypeBuilderProps {
|
|
25
|
+
readonly queryId: QueryId;
|
|
26
|
+
readonly executor: QueryExecutor;
|
|
27
|
+
readonly createTypeNode: CreateTypeNode;
|
|
28
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CreateTypeBuilder = void 0;
|
|
4
|
+
const prevent_await_js_1 = require("../util/prevent-await.js");
|
|
5
|
+
const object_utils_js_1 = require("../util/object-utils.js");
|
|
6
|
+
const create_type_node_js_1 = require("../operation-node/create-type-node.js");
|
|
7
|
+
class CreateTypeBuilder {
|
|
8
|
+
#props;
|
|
9
|
+
constructor(props) {
|
|
10
|
+
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
11
|
+
}
|
|
12
|
+
toOperationNode() {
|
|
13
|
+
return this.#props.executor.transformQuery(this.#props.createTypeNode, this.#props.queryId);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates an anum type.
|
|
17
|
+
*
|
|
18
|
+
* ### Examples
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* db.schema.createType('species').asEnum(['cat', 'dog', 'frog'])
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
asEnum(values) {
|
|
25
|
+
return new CreateTypeBuilder({
|
|
26
|
+
...this.#props,
|
|
27
|
+
createTypeNode: create_type_node_js_1.CreateTypeNode.cloneWithEnum(this.#props.createTypeNode, values),
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
compile() {
|
|
31
|
+
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
|
|
32
|
+
}
|
|
33
|
+
async execute() {
|
|
34
|
+
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.CreateTypeBuilder = CreateTypeBuilder;
|
|
38
|
+
(0, prevent_await_js_1.preventAwait)(CreateTypeBuilder, "don't await CreateTypeBuilder instances directly. To execute the query you need to call `execute`");
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DropTypeNode } from '../operation-node/drop-type-node.js';
|
|
2
|
+
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
3
|
+
import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
4
|
+
import { Compilable } from '../util/compilable.js';
|
|
5
|
+
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
6
|
+
import { QueryId } from '../util/query-id.js';
|
|
7
|
+
export declare class DropTypeBuilder implements OperationNodeSource, Compilable {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(props: DropTypeBuilderProps);
|
|
10
|
+
ifExists(): DropTypeBuilder;
|
|
11
|
+
toOperationNode(): DropTypeNode;
|
|
12
|
+
compile(): CompiledQuery;
|
|
13
|
+
execute(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export interface DropTypeBuilderProps {
|
|
16
|
+
readonly queryId: QueryId;
|
|
17
|
+
readonly executor: QueryExecutor;
|
|
18
|
+
readonly dropTypeNode: DropTypeNode;
|
|
19
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DropTypeBuilder = void 0;
|
|
4
|
+
const drop_type_node_js_1 = require("../operation-node/drop-type-node.js");
|
|
5
|
+
const prevent_await_js_1 = require("../util/prevent-await.js");
|
|
6
|
+
const object_utils_js_1 = require("../util/object-utils.js");
|
|
7
|
+
class DropTypeBuilder {
|
|
8
|
+
#props;
|
|
9
|
+
constructor(props) {
|
|
10
|
+
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
11
|
+
}
|
|
12
|
+
ifExists() {
|
|
13
|
+
return new DropTypeBuilder({
|
|
14
|
+
...this.#props,
|
|
15
|
+
dropTypeNode: drop_type_node_js_1.DropTypeNode.cloneWith(this.#props.dropTypeNode, {
|
|
16
|
+
ifExists: true,
|
|
17
|
+
}),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
toOperationNode() {
|
|
21
|
+
return this.#props.executor.transformQuery(this.#props.dropTypeNode, this.#props.queryId);
|
|
22
|
+
}
|
|
23
|
+
compile() {
|
|
24
|
+
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
|
|
25
|
+
}
|
|
26
|
+
async execute() {
|
|
27
|
+
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.DropTypeBuilder = DropTypeBuilder;
|
|
31
|
+
(0, prevent_await_js_1.preventAwait)(DropTypeBuilder, "don't await DropTypeBuilder instances directly. To execute the query you need to call `execute`");
|
|
@@ -9,6 +9,8 @@ import { DropTableBuilder } from './drop-table-builder.js';
|
|
|
9
9
|
import { CreateViewBuilder } from './create-view-builder.js';
|
|
10
10
|
import { DropViewBuilder } from './drop-view-builder.js';
|
|
11
11
|
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
12
|
+
import { CreateTypeBuilder } from './create-type-builder.js';
|
|
13
|
+
import { DropTypeBuilder } from './drop-type-builder.js';
|
|
12
14
|
/**
|
|
13
15
|
* Provides methods for building database schema.
|
|
14
16
|
*/
|
|
@@ -170,6 +172,36 @@ export declare class SchemaModule {
|
|
|
170
172
|
* ```
|
|
171
173
|
*/
|
|
172
174
|
dropView(viewName: string): DropViewBuilder;
|
|
175
|
+
/**
|
|
176
|
+
* Create a new type.
|
|
177
|
+
*
|
|
178
|
+
* Only some dialects like PostgreSQL have user-defined types.
|
|
179
|
+
*
|
|
180
|
+
* ### Examples
|
|
181
|
+
*
|
|
182
|
+
* ```ts
|
|
183
|
+
* await db.schema
|
|
184
|
+
* .createType('species')
|
|
185
|
+
* .asEnum(['dog', 'cat', 'frog'])
|
|
186
|
+
* .execute()
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
createType(viewName: string): CreateTypeBuilder;
|
|
190
|
+
/**
|
|
191
|
+
* Drop a type.
|
|
192
|
+
*
|
|
193
|
+
* Only some dialects like PostgreSQL have user-defined types.
|
|
194
|
+
*
|
|
195
|
+
* ### Examples
|
|
196
|
+
*
|
|
197
|
+
* ```ts
|
|
198
|
+
* await db.schema
|
|
199
|
+
* .dropType('species')
|
|
200
|
+
* .ifExists()
|
|
201
|
+
* .execute()
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
dropType(viewName: string): DropTypeBuilder;
|
|
173
205
|
/**
|
|
174
206
|
* Returns a copy of this schema module with the given plugin installed.
|
|
175
207
|
*/
|
|
@@ -22,6 +22,10 @@ const create_view_builder_js_1 = require("./create-view-builder.js");
|
|
|
22
22
|
const create_view_node_js_1 = require("../operation-node/create-view-node.js");
|
|
23
23
|
const drop_view_builder_js_1 = require("./drop-view-builder.js");
|
|
24
24
|
const drop_view_node_js_1 = require("../operation-node/drop-view-node.js");
|
|
25
|
+
const create_type_builder_js_1 = require("./create-type-builder.js");
|
|
26
|
+
const drop_type_builder_js_1 = require("./drop-type-builder.js");
|
|
27
|
+
const create_type_node_js_1 = require("../operation-node/create-type-node.js");
|
|
28
|
+
const drop_type_node_js_1 = require("../operation-node/drop-type-node.js");
|
|
25
29
|
/**
|
|
26
30
|
* Provides methods for building database schema.
|
|
27
31
|
*/
|
|
@@ -239,6 +243,48 @@ class SchemaModule {
|
|
|
239
243
|
dropViewNode: drop_view_node_js_1.DropViewNode.create(viewName),
|
|
240
244
|
});
|
|
241
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* Create a new type.
|
|
248
|
+
*
|
|
249
|
+
* Only some dialects like PostgreSQL have user-defined types.
|
|
250
|
+
*
|
|
251
|
+
* ### Examples
|
|
252
|
+
*
|
|
253
|
+
* ```ts
|
|
254
|
+
* await db.schema
|
|
255
|
+
* .createType('species')
|
|
256
|
+
* .asEnum(['dog', 'cat', 'frog'])
|
|
257
|
+
* .execute()
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
createType(viewName) {
|
|
261
|
+
return new create_type_builder_js_1.CreateTypeBuilder({
|
|
262
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
263
|
+
executor: this.#executor,
|
|
264
|
+
createTypeNode: create_type_node_js_1.CreateTypeNode.create(viewName),
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Drop a type.
|
|
269
|
+
*
|
|
270
|
+
* Only some dialects like PostgreSQL have user-defined types.
|
|
271
|
+
*
|
|
272
|
+
* ### Examples
|
|
273
|
+
*
|
|
274
|
+
* ```ts
|
|
275
|
+
* await db.schema
|
|
276
|
+
* .dropType('species')
|
|
277
|
+
* .ifExists()
|
|
278
|
+
* .execute()
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
dropType(viewName) {
|
|
282
|
+
return new drop_type_builder_js_1.DropTypeBuilder({
|
|
283
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
284
|
+
executor: this.#executor,
|
|
285
|
+
dropTypeNode: drop_type_node_js_1.DropTypeNode.create(viewName),
|
|
286
|
+
});
|
|
287
|
+
}
|
|
242
288
|
/**
|
|
243
289
|
* Returns a copy of this schema module with the given plugin installed.
|
|
244
290
|
*/
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Deferred = void 0;
|
|
4
|
+
class Deferred {
|
|
5
|
+
#promise;
|
|
6
|
+
#resolve;
|
|
7
|
+
#reject;
|
|
8
|
+
constructor() {
|
|
9
|
+
this.#promise = new Promise((resolve, reject) => {
|
|
10
|
+
this.#reject = reject;
|
|
11
|
+
this.#resolve = resolve;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
get promise() {
|
|
15
|
+
return this.#promise;
|
|
16
|
+
}
|
|
17
|
+
resolve = (value) => {
|
|
18
|
+
if (this.#resolve) {
|
|
19
|
+
this.#resolve(value);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
reject = (reason) => {
|
|
23
|
+
if (this.#reject) {
|
|
24
|
+
this.#reject(reason);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
exports.Deferred = Deferred;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility type to force use of all properties of T.
|
|
3
|
+
*
|
|
4
|
+
* Similar to Required<T> build-in type, but allows `undefined` values.
|
|
5
|
+
*/
|
|
6
|
+
declare type AllProps<T> = T & {
|
|
7
|
+
[P in keyof T]-?: unknown;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Helper function to check listed properties according to given type. Check if all properties has been used when object is initialised.
|
|
11
|
+
*
|
|
12
|
+
* Example use:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* type SomeType = { propA: string; propB?: number; }
|
|
16
|
+
*
|
|
17
|
+
* // propB has to be mentioned even it is optional. It still should be initialized with undefined.
|
|
18
|
+
* const a: SomeType = requireAllProps<SomeType>({ propA: "value A", propB: undefined });
|
|
19
|
+
*
|
|
20
|
+
* // checked type is implicit for variable.
|
|
21
|
+
* const b = requireAllProps<SomeType>({ propA: "value A", propB: undefined });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* Wrong use of this helper:
|
|
25
|
+
*
|
|
26
|
+
* 1. Omit checked type - all checked properties will be expect as of type never
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* const z: SomeType = requireAllProps({ propC: "no type will work" });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* 2. Apply to spreaded object - there is no way how to check in compile time if spreaded object contains all properties
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* const y: SomeType = { propA: "" }; // valid object according to SomeType declaration
|
|
36
|
+
* const x = requireAllProps<SomeType>( { ... y } );
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @param obj object to check if all properties has been used
|
|
40
|
+
* @returns untouched obj parameter is returned
|
|
41
|
+
*/
|
|
42
|
+
export declare function requireAllProps<T>(obj: AllProps<T>): T;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireAllProps = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Helper function to check listed properties according to given type. Check if all properties has been used when object is initialised.
|
|
6
|
+
*
|
|
7
|
+
* Example use:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* type SomeType = { propA: string; propB?: number; }
|
|
11
|
+
*
|
|
12
|
+
* // propB has to be mentioned even it is optional. It still should be initialized with undefined.
|
|
13
|
+
* const a: SomeType = requireAllProps<SomeType>({ propA: "value A", propB: undefined });
|
|
14
|
+
*
|
|
15
|
+
* // checked type is implicit for variable.
|
|
16
|
+
* const b = requireAllProps<SomeType>({ propA: "value A", propB: undefined });
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Wrong use of this helper:
|
|
20
|
+
*
|
|
21
|
+
* 1. Omit checked type - all checked properties will be expect as of type never
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const z: SomeType = requireAllProps({ propC: "no type will work" });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* 2. Apply to spreaded object - there is no way how to check in compile time if spreaded object contains all properties
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* const y: SomeType = { propA: "" }; // valid object according to SomeType declaration
|
|
31
|
+
* const x = requireAllProps<SomeType>( { ... y } );
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @param obj object to check if all properties has been used
|
|
35
|
+
* @returns untouched obj parameter is returned
|
|
36
|
+
*/
|
|
37
|
+
function requireAllProps(obj) {
|
|
38
|
+
return obj;
|
|
39
|
+
}
|
|
40
|
+
exports.requireAllProps = requireAllProps;
|
|
@@ -31,9 +31,19 @@ export interface MysqlPool {
|
|
|
31
31
|
end(callback: (error: unknown) => void): void;
|
|
32
32
|
}
|
|
33
33
|
export interface MysqlPoolConnection {
|
|
34
|
+
query(sql: string, parameters: ReadonlyArray<unknown>): {
|
|
35
|
+
stream: <T>(options: MysqlStreamOptions) => MysqlStream<T>;
|
|
36
|
+
};
|
|
34
37
|
query(sql: string, parameters: ReadonlyArray<unknown>, callback: (error: unknown, result: MysqlQueryResult) => void): void;
|
|
35
38
|
release(): void;
|
|
36
39
|
}
|
|
40
|
+
export interface MysqlStreamOptions {
|
|
41
|
+
highWaterMark?: number;
|
|
42
|
+
objectMode?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export interface MysqlStream<T> {
|
|
45
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
|
|
46
|
+
}
|
|
37
47
|
export interface MysqlOkPacket {
|
|
38
48
|
affectedRows: number;
|
|
39
49
|
insertId: number;
|
|
@@ -42,6 +42,7 @@ declare class MysqlConnection implements DatabaseConnection {
|
|
|
42
42
|
#private;
|
|
43
43
|
constructor(rawConnection: MysqlPoolConnection);
|
|
44
44
|
executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
|
|
45
|
+
streamQuery<O>(compiledQuery: CompiledQuery, chunkSize: number): AsyncIterableIterator<QueryResult<O>>;
|
|
45
46
|
[PRIVATE_RELEASE_METHOD](): void;
|
|
46
47
|
}
|
|
47
48
|
export {};
|
|
@@ -121,6 +121,31 @@ class MysqlConnection {
|
|
|
121
121
|
});
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
|
+
async *streamQuery(compiledQuery, chunkSize) {
|
|
125
|
+
const stream = this.#rawConnection
|
|
126
|
+
.query(compiledQuery.sql, compiledQuery.parameters)
|
|
127
|
+
.stream({
|
|
128
|
+
objectMode: true,
|
|
129
|
+
});
|
|
130
|
+
try {
|
|
131
|
+
for await (const row of stream) {
|
|
132
|
+
yield {
|
|
133
|
+
rows: [row],
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch (ex) {
|
|
138
|
+
if (ex &&
|
|
139
|
+
typeof ex === 'object' &&
|
|
140
|
+
'code' in ex &&
|
|
141
|
+
// @ts-ignore
|
|
142
|
+
ex.code === 'ERR_STREAM_PREMATURE_CLOSE') {
|
|
143
|
+
// Most likely because of https://github.com/mysqljs/mysql/blob/master/lib/protocol/sequences/Query.js#L220
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
throw ex;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
124
149
|
[PRIVATE_RELEASE_METHOD]() {
|
|
125
150
|
this.#rawConnection.release();
|
|
126
151
|
}
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
2
|
+
export declare type PostgresCursorConstructor = new <T>(sql: string, parameters: readonly unknown[]) => PostgresCursor<T>;
|
|
3
|
+
export declare type PostgresCursor<T> = {
|
|
4
|
+
state: 'initialized' | 'idle' | 'submitted' | 'busy';
|
|
5
|
+
read: (rowsCount: number) => Promise<T[]>;
|
|
6
|
+
} | {
|
|
7
|
+
state: 'done';
|
|
8
|
+
} | {
|
|
9
|
+
state: 'error';
|
|
10
|
+
_error: Error;
|
|
11
|
+
};
|
|
2
12
|
/**
|
|
3
13
|
* Config for the PostgreSQL dialect.
|
|
4
14
|
*/
|
|
@@ -11,6 +21,19 @@ export interface PostgresDialectConfig {
|
|
|
11
21
|
* https://node-postgres.com/api/pool
|
|
12
22
|
*/
|
|
13
23
|
pool: PostgresPool | (() => Promise<PostgresPool>);
|
|
24
|
+
/**
|
|
25
|
+
* https://github.com/brianc/node-postgres/tree/master/packages/pg-cursor
|
|
26
|
+
* ```ts
|
|
27
|
+
* import Cursor from 'pg-cursor'
|
|
28
|
+
* // or
|
|
29
|
+
* import * as Cursor from 'pg-cursor'
|
|
30
|
+
*
|
|
31
|
+
* new PostgresDialect({
|
|
32
|
+
* cursor: Cursor
|
|
33
|
+
* })
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
cursor?: PostgresCursorConstructor;
|
|
14
37
|
/**
|
|
15
38
|
* Called once for each created connection.
|
|
16
39
|
*/
|
|
@@ -30,6 +53,7 @@ export interface PostgresPool {
|
|
|
30
53
|
}
|
|
31
54
|
export interface PostgresPoolClient {
|
|
32
55
|
query<R>(sql: string, parameters: ReadonlyArray<unknown>): Promise<PostgresQueryResult<R>>;
|
|
56
|
+
query<R>(cursor: PostgresCursor<R>): PostgresCursor<R>;
|
|
33
57
|
release(): void;
|
|
34
58
|
}
|
|
35
59
|
export interface PostgresQueryResult<R> {
|
|
@@ -37,3 +61,8 @@ export interface PostgresQueryResult<R> {
|
|
|
37
61
|
rowCount: number;
|
|
38
62
|
rows: R[];
|
|
39
63
|
}
|
|
64
|
+
export interface PostgresStreamOptions {
|
|
65
|
+
}
|
|
66
|
+
export interface PostgresStream<T> {
|
|
67
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
|
|
68
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
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, PostgresPoolClient } from './postgres-dialect-config.js';
|
|
4
|
+
import { PostgresCursorConstructor, 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;
|
|
@@ -38,10 +38,14 @@ export declare class PostgresDriver implements Driver {
|
|
|
38
38
|
*/
|
|
39
39
|
destroy(): Promise<void>;
|
|
40
40
|
}
|
|
41
|
+
interface PostgresConnectionOptions {
|
|
42
|
+
cursor: PostgresCursorConstructor | null;
|
|
43
|
+
}
|
|
41
44
|
declare class PostgresConnection implements DatabaseConnection {
|
|
42
45
|
#private;
|
|
43
|
-
constructor(client: PostgresPoolClient);
|
|
46
|
+
constructor(client: PostgresPoolClient, options: PostgresConnectionOptions);
|
|
44
47
|
executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
|
|
48
|
+
streamQuery<O>(compiledQuery: CompiledQuery, chunkSize: number): AsyncIterableIterator<QueryResult<O>>;
|
|
45
49
|
[PRIVATE_RELEASE_METHOD](): void;
|
|
46
50
|
}
|
|
47
51
|
export {};
|
|
@@ -19,7 +19,9 @@ export class PostgresDriver {
|
|
|
19
19
|
const client = await this.#pool.connect();
|
|
20
20
|
let connection = this.#connections.get(client);
|
|
21
21
|
if (!connection) {
|
|
22
|
-
connection = new PostgresConnection(client
|
|
22
|
+
connection = new PostgresConnection(client, {
|
|
23
|
+
cursor: this.#config.cursor ?? null,
|
|
24
|
+
});
|
|
23
25
|
this.#connections.set(client, connection);
|
|
24
26
|
// The driver must take care of calling `onCreateConnection` when a new
|
|
25
27
|
// connection is created. The `pg` module doesn't provide an async hook
|
|
@@ -57,8 +59,10 @@ export class PostgresDriver {
|
|
|
57
59
|
}
|
|
58
60
|
class PostgresConnection {
|
|
59
61
|
#client;
|
|
60
|
-
|
|
62
|
+
#options;
|
|
63
|
+
constructor(client, options) {
|
|
61
64
|
this.#client = client;
|
|
65
|
+
this.#options = options;
|
|
62
66
|
}
|
|
63
67
|
async executeQuery(compiledQuery) {
|
|
64
68
|
try {
|
|
@@ -79,6 +83,24 @@ class PostgresConnection {
|
|
|
79
83
|
throw extendStackTrace(err, new Error());
|
|
80
84
|
}
|
|
81
85
|
}
|
|
86
|
+
async *streamQuery(compiledQuery, chunkSize) {
|
|
87
|
+
if (!this.#options.cursor) {
|
|
88
|
+
throw new Error("'cursor' is not present in your postgres dialect config. It's required to make streaming work in postgres.");
|
|
89
|
+
}
|
|
90
|
+
const cursor = this.#client.query(new this.#options.cursor(compiledQuery.sql, compiledQuery.parameters));
|
|
91
|
+
while (true) {
|
|
92
|
+
if (cursor.state === 'done') {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
if (cursor.state === 'error') {
|
|
96
|
+
throw cursor._error;
|
|
97
|
+
}
|
|
98
|
+
const rows = await cursor.read(chunkSize);
|
|
99
|
+
yield {
|
|
100
|
+
rows,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
82
104
|
[PRIVATE_RELEASE_METHOD]() {
|
|
83
105
|
this.#client.release();
|
|
84
106
|
}
|