@taylordb/query-builder 0.8.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/cjs/@types/insert.d.ts +13 -4
- package/dist/cjs/@types/query-builder.d.ts +2 -2
- package/dist/cjs/@types/type-helpers.d.ts +3 -3
- package/dist/cjs/@types/update.d.ts +8 -3
- package/dist/cjs/__tests__/query-builder.spec.js +1 -1
- package/dist/cjs/__tests__/query-builder.spec.js.map +1 -1
- package/dist/cjs/__tests__/taylorclient.types.d.ts +57 -51
- package/dist/cjs/aggregation-query-builder.d.ts +92 -0
- package/dist/cjs/aggregation-query-builder.js +92 -0
- package/dist/cjs/aggregation-query-builder.js.map +1 -1
- package/dist/cjs/batch-query-builder.d.ts +36 -0
- package/dist/cjs/batch-query-builder.js +36 -0
- package/dist/cjs/batch-query-builder.js.map +1 -1
- package/dist/cjs/delete-query-builder.d.ts +17 -0
- package/dist/cjs/delete-query-builder.js +17 -0
- package/dist/cjs/delete-query-builder.js.map +1 -1
- package/dist/cjs/executor.js +1 -1
- package/dist/cjs/executor.js.map +1 -1
- package/dist/cjs/insert-query-builder.d.ts +68 -0
- package/dist/cjs/insert-query-builder.js +71 -0
- package/dist/cjs/insert-query-builder.js.map +1 -1
- package/dist/cjs/query-builder.d.ts +218 -1
- package/dist/cjs/query-builder.js +188 -0
- package/dist/cjs/query-builder.js.map +1 -1
- package/dist/cjs/selection-builder.d.ts +12 -1
- package/dist/cjs/selection-builder.js +11 -0
- package/dist/cjs/selection-builder.js.map +1 -1
- package/dist/cjs/update-query-builder.d.ts +32 -0
- package/dist/cjs/update-query-builder.js +32 -0
- package/dist/cjs/update-query-builder.js.map +1 -1
- package/dist/cjs/where-query-builder.d.ts +61 -2
- package/dist/cjs/where-query-builder.js +6 -0
- package/dist/cjs/where-query-builder.js.map +1 -1
- package/dist/esm/@types/insert.d.ts +13 -4
- package/dist/esm/@types/query-builder.d.ts +2 -2
- package/dist/esm/@types/type-helpers.d.ts +3 -3
- package/dist/esm/@types/update.d.ts +8 -3
- package/dist/esm/__tests__/query-builder.spec.js +1 -1
- package/dist/esm/__tests__/query-builder.spec.js.map +1 -1
- package/dist/esm/__tests__/taylorclient.types.d.ts +57 -51
- package/dist/esm/aggregation-query-builder.d.ts +92 -0
- package/dist/esm/aggregation-query-builder.js +92 -0
- package/dist/esm/aggregation-query-builder.js.map +1 -1
- package/dist/esm/batch-query-builder.d.ts +36 -0
- package/dist/esm/batch-query-builder.js +36 -0
- package/dist/esm/batch-query-builder.js.map +1 -1
- package/dist/esm/delete-query-builder.d.ts +17 -0
- package/dist/esm/delete-query-builder.js +17 -0
- package/dist/esm/delete-query-builder.js.map +1 -1
- package/dist/esm/executor.js +1 -1
- package/dist/esm/executor.js.map +1 -1
- package/dist/esm/insert-query-builder.d.ts +68 -0
- package/dist/esm/insert-query-builder.js +71 -0
- package/dist/esm/insert-query-builder.js.map +1 -1
- package/dist/esm/query-builder.d.ts +218 -1
- package/dist/esm/query-builder.js +188 -0
- package/dist/esm/query-builder.js.map +1 -1
- package/dist/esm/selection-builder.d.ts +12 -1
- package/dist/esm/selection-builder.js +11 -0
- package/dist/esm/selection-builder.js.map +1 -1
- package/dist/esm/update-query-builder.d.ts +32 -0
- package/dist/esm/update-query-builder.js +32 -0
- package/dist/esm/update-query-builder.js.map +1 -1
- package/dist/esm/where-query-builder.d.ts +61 -2
- package/dist/esm/where-query-builder.js +6 -0
- package/dist/esm/where-query-builder.js.map +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +3 -3
|
@@ -3,14 +3,82 @@ import type { AnyDB } from './@types/internal-types.js';
|
|
|
3
3
|
import { NonLinkColumnNames } from './@types/query-builder.js';
|
|
4
4
|
import { ResolveSelection } from './@types/type-helpers.js';
|
|
5
5
|
import { Executor } from './executor.js';
|
|
6
|
+
/**
|
|
7
|
+
* A query builder for creating new records in the database.
|
|
8
|
+
* @template DB - The database type.
|
|
9
|
+
* @template TableName - The name of the table to insert into.
|
|
10
|
+
* @template Selection - The type of the selected fields.
|
|
11
|
+
*/
|
|
6
12
|
export declare class InsertQueryBuilder<DB extends AnyDB, TableName extends keyof DB, Selection = {
|
|
7
13
|
id: number;
|
|
8
14
|
}> {
|
|
9
15
|
#private;
|
|
10
16
|
constructor(node: InsertNode, executor: Executor);
|
|
17
|
+
/**
|
|
18
|
+
* The values to insert into the table.
|
|
19
|
+
* @param values - An object or array of objects representing the records to create.
|
|
20
|
+
* @returns The `InsertQueryBuilder` instance for chaining.
|
|
21
|
+
*
|
|
22
|
+
* @example Single Record Insertion
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const newUser = await qb
|
|
25
|
+
* .insertInto('users')
|
|
26
|
+
* .values({ name: 'John Doe', email: 'john.doe@example.com' })
|
|
27
|
+
* .executeTakeFirst();
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example Multiple Record Insertion
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const newUsers = await qb
|
|
33
|
+
* .insertInto('users')
|
|
34
|
+
* .values([{ name: 'John Doe' }, { name: 'Jane Doe' }])
|
|
35
|
+
* .execute();
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
11
38
|
values(values: Insertable<DB[TableName]> | Insertable<DB[TableName]>[]): InsertQueryBuilder<DB, TableName, Selection>;
|
|
39
|
+
/**
|
|
40
|
+
* The fields to return after the insert operation.
|
|
41
|
+
* @param fields - An array of field names to return.
|
|
42
|
+
* @returns The `InsertQueryBuilder` instance for chaining.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const newUser = await qb
|
|
47
|
+
* .insertInto('users')
|
|
48
|
+
* .values({ name: 'John Doe' })
|
|
49
|
+
* .returning(['id', 'name'])
|
|
50
|
+
* .executeTakeFirst();
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
12
53
|
returning<const TFields extends readonly NonLinkColumnNames<DB[TableName]>[]>(fields: TFields): InsertQueryBuilder<DB, TableName, ResolveSelection<DB, TableName, TFields, object>>;
|
|
54
|
+
/**
|
|
55
|
+
* Executes the insert query.
|
|
56
|
+
* @returns A promise that resolves with an array of the selected fields from the inserted records.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const newUsers = await qb
|
|
61
|
+
* .insertInto('users')
|
|
62
|
+
* .values([{ name: 'John Doe' }, { name: 'Jane Doe' }])
|
|
63
|
+
* .returning(['id', 'name'])
|
|
64
|
+
* .execute();
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
13
67
|
execute(): Promise<Selection[]>;
|
|
68
|
+
/**
|
|
69
|
+
* Executes the insert query and returns the first result.
|
|
70
|
+
* @returns A promise that resolves with the first inserted record, or `null` if no records were inserted.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const newUser = await qb
|
|
75
|
+
* .insertInto('users')
|
|
76
|
+
* .values({ name: 'John Doe' })
|
|
77
|
+
* .returning(['id', 'name'])
|
|
78
|
+
* .executeTakeFirst();
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
executeTakeFirst(): Promise<Selection | null>;
|
|
14
82
|
compile(): {
|
|
15
83
|
query: string;
|
|
16
84
|
variables: Record<string, any>;
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { QueryBuilder } from './query-builder.js';
|
|
2
2
|
import { SelectionBuilder } from './selection-builder.js';
|
|
3
|
+
/**
|
|
4
|
+
* A query builder for creating new records in the database.
|
|
5
|
+
* @template DB - The database type.
|
|
6
|
+
* @template TableName - The name of the table to insert into.
|
|
7
|
+
* @template Selection - The type of the selected fields.
|
|
8
|
+
*/
|
|
3
9
|
export class InsertQueryBuilder {
|
|
4
10
|
#node;
|
|
5
11
|
#executor;
|
|
@@ -7,12 +13,47 @@ export class InsertQueryBuilder {
|
|
|
7
13
|
this.#node = node;
|
|
8
14
|
this.#executor = executor;
|
|
9
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* The values to insert into the table.
|
|
18
|
+
* @param values - An object or array of objects representing the records to create.
|
|
19
|
+
* @returns The `InsertQueryBuilder` instance for chaining.
|
|
20
|
+
*
|
|
21
|
+
* @example Single Record Insertion
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const newUser = await qb
|
|
24
|
+
* .insertInto('users')
|
|
25
|
+
* .values({ name: 'John Doe', email: 'john.doe@example.com' })
|
|
26
|
+
* .executeTakeFirst();
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Multiple Record Insertion
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const newUsers = await qb
|
|
32
|
+
* .insertInto('users')
|
|
33
|
+
* .values([{ name: 'John Doe' }, { name: 'Jane Doe' }])
|
|
34
|
+
* .execute();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
10
37
|
values(values) {
|
|
11
38
|
return new InsertQueryBuilder({
|
|
12
39
|
...this.#node,
|
|
13
40
|
createdRecords: Array.isArray(values) ? values : [values],
|
|
14
41
|
}, this.#executor);
|
|
15
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* The fields to return after the insert operation.
|
|
45
|
+
* @param fields - An array of field names to return.
|
|
46
|
+
* @returns The `InsertQueryBuilder` instance for chaining.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const newUser = await qb
|
|
51
|
+
* .insertInto('users')
|
|
52
|
+
* .values({ name: 'John Doe' })
|
|
53
|
+
* .returning(['id', 'name'])
|
|
54
|
+
* .executeTakeFirst();
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
16
57
|
returning(fields) {
|
|
17
58
|
const newSelects = fields.map(field => {
|
|
18
59
|
if (typeof field === 'function') {
|
|
@@ -28,9 +69,39 @@ export class InsertQueryBuilder {
|
|
|
28
69
|
returning: [...this.#node.returning, ...newSelects],
|
|
29
70
|
}, this.#executor);
|
|
30
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Executes the insert query.
|
|
74
|
+
* @returns A promise that resolves with an array of the selected fields from the inserted records.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const newUsers = await qb
|
|
79
|
+
* .insertInto('users')
|
|
80
|
+
* .values([{ name: 'John Doe' }, { name: 'Jane Doe' }])
|
|
81
|
+
* .returning(['id', 'name'])
|
|
82
|
+
* .execute();
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
31
85
|
async execute() {
|
|
32
86
|
return (await this.#executor.execute(this))[0];
|
|
33
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Executes the insert query and returns the first result.
|
|
90
|
+
* @returns A promise that resolves with the first inserted record, or `null` if no records were inserted.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const newUser = await qb
|
|
95
|
+
* .insertInto('users')
|
|
96
|
+
* .values({ name: 'John Doe' })
|
|
97
|
+
* .returning(['id', 'name'])
|
|
98
|
+
* .executeTakeFirst();
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
async executeTakeFirst() {
|
|
102
|
+
const response = await this.execute();
|
|
103
|
+
return response[0] ?? null;
|
|
104
|
+
}
|
|
34
105
|
compile() {
|
|
35
106
|
const query = 'mutation ($metadata: JSON) { execute(metadata: $metadata) }';
|
|
36
107
|
const metadata = [this._prepareMetadata()];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insert-query-builder.js","sourceRoot":"","sources":["../../src/insert-query-builder.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,MAAM,OAAO,kBAAkB;IAK7B,KAAK,CAAa;IAClB,SAAS,CAAW;IAEpB,YAAY,IAAgB,EAAE,QAAkB;QAC9C,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,QAAQ,GAAG,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC,CAAC;QAE3C,OAAO;YACL,KAAK;YACL,SAAS,EAAE;gBACT,QAAQ;aACT;SACF,CAAC;IACJ,CAAC;IAED,gBAAgB;QACd,MAAM,YAAY,GAAG,CAAC,OAA+B,EAAS,EAAE;YAC9D,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;gBACzB,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;oBAC9B,OAAO,KAAK,CAAC;gBACf,CAAC;gBACD,MAAM,eAAe,GAAG,IAAI,YAAY,CACtC,KAAkB,EAClB,IAAI,CAAC,SAAS,CACf,CAAC;gBACF,OAAO,eAAe,CAAC,gBAAgB,EAAE,CAAC;YAC5C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,MAAM,kBAAkB,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM;YACpD,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;YACpC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QAEX,OAAO;YACL,IAAI,EAAE,QAAQ;YACd,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS;YAC/B,cAAc,EAAE,IAAI,CAAC,KAAK,CAAC,cAAc;YACzC,SAAS,EAAE,kBAAkB;SAC9B,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -9,22 +9,166 @@ import { Executor } from './executor.js';
|
|
|
9
9
|
import { InsertQueryBuilder } from './insert-query-builder.js';
|
|
10
10
|
import { UpdateQueryBuilder } from './update-query-builder.js';
|
|
11
11
|
import { FilterableQueryBuilder } from './where-query-builder.js';
|
|
12
|
+
/**
|
|
13
|
+
* The main query builder class for constructing and executing select queries.
|
|
14
|
+
* @template DB - The database type.
|
|
15
|
+
* @template TableName - The name of the table to query.
|
|
16
|
+
* @template Selection - The type of the selected fields.
|
|
17
|
+
* @template LinkName - The name of the link field if this is a subquery.
|
|
18
|
+
*/
|
|
12
19
|
export declare class QueryBuilder<DB extends AnyDB, TableName extends keyof DB, Selection = object, LinkName = null> extends FilterableQueryBuilder<DB, TableName> {
|
|
13
20
|
_node: QueryNode;
|
|
14
21
|
constructor(node: QueryNode, executor: Executor);
|
|
22
|
+
/**
|
|
23
|
+
* Selects a set of fields from the table.
|
|
24
|
+
* @param fields - An array of field names to select.
|
|
25
|
+
* @returns A new `QueryBuilder` instance with the specified fields selected.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const users = await qb
|
|
30
|
+
* .selectFrom('users')
|
|
31
|
+
* .select(['id', 'name'])
|
|
32
|
+
* .execute();
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
15
35
|
select<const TFields extends readonly NonLinkColumnNames<DB[TableName]>[]>(fields: TFields): QueryBuilder<DB, TableName, ResolveSelection<DB, TableName, TFields, Selection>>;
|
|
36
|
+
/**
|
|
37
|
+
* Selects all fields from the table.
|
|
38
|
+
* @returns A new `QueryBuilder` instance with all fields selected.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const users = await qb
|
|
43
|
+
* .selectFrom('users')
|
|
44
|
+
* .selectAll()
|
|
45
|
+
* .execute();
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
16
48
|
selectAll(): QueryBuilder<DB, TableName, Selection & {
|
|
17
49
|
[K in keyof DB[TableName]]: InferDataType<DB[TableName][K]>;
|
|
18
50
|
}>;
|
|
51
|
+
/**
|
|
52
|
+
* Includes related records from a linked table.
|
|
53
|
+
* This method has two overloads:
|
|
54
|
+
* 1. Pass an array of link field names to include all fields from the related records.
|
|
55
|
+
* 2. Pass an object to specify subqueries for each link field.
|
|
56
|
+
*
|
|
57
|
+
* @param relations - The relations to include.
|
|
58
|
+
* @returns A new `QueryBuilder` instance with the specified relations included.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const usersWithPosts = await qb
|
|
63
|
+
* .selectFrom('users')
|
|
64
|
+
* .select(['id', 'name'])
|
|
65
|
+
* .with(['posts']) // Assuming 'posts' is a link field on the 'users' table
|
|
66
|
+
* .execute();
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const usersWithPublishedPosts = await qb
|
|
72
|
+
* .selectFrom('users')
|
|
73
|
+
* .select(['id', 'name'])
|
|
74
|
+
* .with({
|
|
75
|
+
* posts: (qb) => qb.select(['title', 'content']).where('isPublished', '=', true),
|
|
76
|
+
* })
|
|
77
|
+
* .execute();
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
19
80
|
with<const TArg extends (LinkColumnNames<DB[TableName]> & string) | readonly (LinkColumnNames<DB[TableName]> & string)[]>(relations: TArg): QueryBuilder<DB, TableName, ResolveWithPlain<DB, TableName, TArg, Selection>>;
|
|
20
81
|
with<const TArg extends {
|
|
21
|
-
[K in LinkColumnNames<DB[TableName]>]?: (qb: QueryBuilder<DB, DB[TableName][K] extends LinkColumnType<any> ? DB[TableName][K]['linkedTo'] : never, object, K>) => QueryBuilder<DB, any, any, any>;
|
|
82
|
+
[K in LinkColumnNames<DB[TableName]>]?: (qb: QueryBuilder<DB, DB[TableName][K] extends LinkColumnType<any, boolean> ? DB[TableName][K]['linkedTo'] : never, object, K>) => QueryBuilder<DB, any, any, any>;
|
|
22
83
|
}>(relations: TArg): QueryBuilder<DB, TableName, ResolveWithObject<TArg, Selection>>;
|
|
84
|
+
/**
|
|
85
|
+
* Sets the maximum number of records to return.
|
|
86
|
+
* @param count - The maximum number of records.
|
|
87
|
+
* @returns A new `QueryBuilder` instance with the limit applied.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const users = await qb
|
|
92
|
+
* .selectFrom('users')
|
|
93
|
+
* .select(['id', 'name'])
|
|
94
|
+
* .limit(10)
|
|
95
|
+
* .execute();
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
23
98
|
limit(count: number): QueryBuilder<DB, TableName, Selection, LinkName>;
|
|
99
|
+
/**
|
|
100
|
+
* Sets the number of records to skip.
|
|
101
|
+
* @param count - The number of records to skip.
|
|
102
|
+
* @returns A new `QueryBuilder` instance with the offset applied.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const users = await qb
|
|
107
|
+
* .selectFrom('users')
|
|
108
|
+
* .select(['id', 'name'])
|
|
109
|
+
* .offset(10)
|
|
110
|
+
* .execute();
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
24
113
|
offset(count: number): QueryBuilder<DB, TableName, Selection, LinkName>;
|
|
114
|
+
/**
|
|
115
|
+
* Paginates the results.
|
|
116
|
+
* @param page - The page number to retrieve.
|
|
117
|
+
* @param limit - The number of records per page.
|
|
118
|
+
* @returns A new `QueryBuilder` instance with pagination applied.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const users = await qb
|
|
123
|
+
* .selectFrom('users')
|
|
124
|
+
* .select(['id', 'name'])
|
|
125
|
+
* .paginate(2, 25) // Retrieves page 2 with 25 records per page
|
|
126
|
+
* .execute();
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
25
129
|
paginate(page: number, limit: number): QueryBuilder<DB, TableName, Selection, LinkName>;
|
|
130
|
+
/**
|
|
131
|
+
* Sorts the results by a specified field.
|
|
132
|
+
* @param field - The field to sort by.
|
|
133
|
+
* @param direction - The sort direction ('asc' or 'desc').
|
|
134
|
+
* @returns A new `QueryBuilder` instance with the sorting applied.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const users = await qb
|
|
139
|
+
* .selectFrom('users')
|
|
140
|
+
* .select(['id', 'name'])
|
|
141
|
+
* .orderBy('name', 'asc')
|
|
142
|
+
* .execute();
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
26
145
|
orderBy(field: keyof DB[TableName], direction?: 'asc' | 'desc'): QueryBuilder<DB, TableName, Selection, LinkName>;
|
|
146
|
+
/**
|
|
147
|
+
* Executes the select query.
|
|
148
|
+
* @returns A promise that resolves with an array of the selected records.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const users = await qb
|
|
153
|
+
* .selectFrom('users')
|
|
154
|
+
* .select(['id', 'name'])
|
|
155
|
+
* .execute();
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
27
158
|
execute(): Promise<Selection[]>;
|
|
159
|
+
/**
|
|
160
|
+
* Executes the select query and returns the first result.
|
|
161
|
+
* @returns A promise that resolves with the first record, or `null` if no records were found.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* const user = await qb
|
|
166
|
+
* .selectFrom('users')
|
|
167
|
+
* .where('id', '=', 1)
|
|
168
|
+
* .select(['id', 'name'])
|
|
169
|
+
* .executeTakeFirst();
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
28
172
|
executeTakeFirst(): Promise<Selection | null>;
|
|
29
173
|
subscribe(callback: (result: Selection[]) => void): Promise<{
|
|
30
174
|
unsubscribe: () => Promise<void>;
|
|
@@ -37,17 +181,90 @@ export declare class QueryBuilder<DB extends AnyDB, TableName extends keyof DB,
|
|
|
37
181
|
isSelectionQueryNode(node: QueryNode): node is SelectionQueryNode;
|
|
38
182
|
isRootQueryNode(node: QueryNode): node is RootQueryNode;
|
|
39
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* The root query builder class that provides entry points for all query types.
|
|
186
|
+
*/
|
|
40
187
|
export declare class RootQueryBuilder<DB extends AnyDB> {
|
|
41
188
|
#private;
|
|
42
189
|
constructor(config: {
|
|
43
190
|
baseUrl: string;
|
|
44
191
|
apiKey: string;
|
|
45
192
|
});
|
|
193
|
+
/**
|
|
194
|
+
* Creates a new select query builder for the specified table.
|
|
195
|
+
* @param from - The name of the table to select from.
|
|
196
|
+
* @returns A new `QueryBuilder` instance.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* const qb = createQueryBuilder<MyDatabase>({ ... });
|
|
201
|
+
* const userQuery = qb.selectFrom('users');
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
46
204
|
selectFrom<TableName extends keyof Omit<DB, 'selectTable' | 'attachmentTable' | 'collaboratorsTable'> & string>(from: TableName): QueryBuilder<DB, TableName>;
|
|
205
|
+
/**
|
|
206
|
+
* Creates a new insert query builder for the specified table.
|
|
207
|
+
* @param into - The name of the table to insert into.
|
|
208
|
+
* @returns A new `InsertQueryBuilder` instance.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```typescript
|
|
212
|
+
* const qb = createQueryBuilder<MyDatabase>({ ... });
|
|
213
|
+
* const insertQuery = qb.insertInto('users');
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
47
216
|
insertInto<TableName extends keyof Omit<DB, 'selectTable' | 'attachmentTable' | 'collaboratorsTable'> & string>(into: TableName): InsertQueryBuilder<DB, TableName>;
|
|
217
|
+
/**
|
|
218
|
+
* Creates a new update query builder for the specified table.
|
|
219
|
+
* @param tableName - The name of the table to update.
|
|
220
|
+
* @returns A new `UpdateQueryBuilder` instance.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```typescript
|
|
224
|
+
* const qb = createQueryBuilder<MyDatabase>({ ... });
|
|
225
|
+
* const updateQuery = qb.update('users');
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
48
228
|
update<TableName extends keyof Omit<DB, 'selectTable' | 'attachmentTable' | 'collaboratorsTable'> & string>(tableName: TableName): UpdateQueryBuilder<DB, TableName>;
|
|
229
|
+
/**
|
|
230
|
+
* Creates a new delete query builder for the specified table.
|
|
231
|
+
* @param tableName - The name of the table to delete from.
|
|
232
|
+
* @returns A new `DeleteQueryBuilder` instance.
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```typescript
|
|
236
|
+
* const qb = createQueryBuilder<MyDatabase>({ ... });
|
|
237
|
+
* const deleteQuery = qb.deleteFrom('users');
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
49
240
|
deleteFrom<TableName extends keyof Omit<DB, 'selectTable' | 'attachmentTable' | 'collaboratorsTable'> & string>(tableName: TableName): DeleteQueryBuilder<DB, TableName>;
|
|
241
|
+
/**
|
|
242
|
+
* Creates a new batch query builder.
|
|
243
|
+
* @param builders - An array of query builders to execute in a batch.
|
|
244
|
+
* @returns A new `BatchQueryBuilder` instance.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```typescript
|
|
248
|
+
* const qb = createQueryBuilder<MyDatabase>({ ... });
|
|
249
|
+
* const batchQuery = qb.batch([
|
|
250
|
+
* qb.selectFrom('users').select(['id', 'name']),
|
|
251
|
+
* qb.insertInto('users').values({ name: 'New User' }),
|
|
252
|
+
* ]);
|
|
253
|
+
* const [users, newUser] = await batchQuery.execute();
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
50
256
|
batch<const TBuilders extends readonly AnyQueryBuilder[]>(builders: TBuilders): AreAllBuildersSubscribable<TBuilders> extends true ? BatchQueryBuilder<TBuilders> : Omit<BatchQueryBuilder<TBuilders>, 'subscribe'>;
|
|
257
|
+
/**
|
|
258
|
+
* Creates a new aggregation query builder for the specified table.
|
|
259
|
+
* @param tableName - The name of the table to aggregate from.
|
|
260
|
+
* @returns A new `AggregationQueryBuilder` instance.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* const qb = createQueryBuilder<MyDatabase>({ ... });
|
|
265
|
+
* const aggregateQuery = qb.aggregateFrom('users');
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
51
268
|
aggregateFrom<TableName extends keyof Omit<DB, 'selectTable' | 'attachmentTable' | 'collaboratorsTable'> & string>(tableName: TableName): AggregationQueryBuilder<DB, TableName>;
|
|
52
269
|
}
|
|
53
270
|
export declare function createQueryBuilder<DB extends AnyDB>(config: {
|