@zudojs/database 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +223 -17
- package/dist/cache/cache.memory.d.ts +167 -0
- package/dist/cache/cache.memory.js +186 -40
- package/dist/cache/index.d.ts +2 -2
- package/dist/cache/index.js +2 -2
- package/dist/database/database.core.d.ts +91 -0
- package/dist/database/database.core.js +150 -0
- package/dist/databaseClient/databaseClient.core.d.ts +165 -14
- package/dist/databaseClient/databaseClient.core.js +304 -70
- package/dist/databaseClient/databaseClient.errors.d.ts +112 -0
- package/dist/databaseClient/databaseClient.errors.js +425 -0
- package/dist/databaseClient/index.d.ts +2 -1
- package/dist/databaseClient/index.js +2 -1
- package/dist/databaseConnection/databaseConnection.manager.d.ts +163 -0
- package/dist/databaseConnection/databaseConnection.manager.js +138 -76
- package/dist/databaseConnection/index.d.ts +1 -1
- package/dist/databaseType/databaseType.type.d.ts +224 -0
- package/dist/databaseType/databaseType.type.js +10 -0
- package/dist/health/health.check.d.ts +74 -0
- package/dist/health/health.check.js +78 -47
- package/dist/health/index.d.ts +1 -1
- package/dist/health/index.js +1 -1
- package/dist/index.d.ts +14 -14
- package/dist/index.js +11 -11
- package/dist/locks/index.d.ts +2 -2
- package/dist/locks/index.js +2 -2
- package/dist/locks/locks.core.d.ts +128 -0
- package/dist/locks/locks.core.js +144 -85
- package/dist/migration/index.d.ts +3 -1
- package/dist/migration/index.js +2 -0
- package/dist/migration/migration.dialect.d.ts +65 -0
- package/dist/migration/migration.dialect.js +63 -0
- package/dist/migration/migration.helpers.d.ts +65 -0
- package/dist/migration/migration.helpers.js +141 -0
- package/dist/migration/migration.runner.d.ts +75 -0
- package/dist/migration/migration.runner.js +195 -107
- package/dist/migration/migration.types.d.ts +85 -0
- package/dist/migration/migration.types.js +2 -0
- package/dist/pagination/index.d.ts +2 -1
- package/dist/pagination/index.js +2 -1
- package/dist/pagination/pagination.core.d.ts +163 -0
- package/dist/pagination/pagination.core.js +279 -0
- package/dist/pagination/pagination.keyset.d.ts +55 -0
- package/dist/pagination/pagination.keyset.js +108 -0
- package/dist/queryBuilder/index.d.ts +4 -3
- package/dist/queryBuilder/index.js +3 -2
- package/dist/queryBuilder/queryBuilder.core.d.ts +178 -0
- package/dist/queryBuilder/queryBuilder.core.js +420 -0
- package/dist/queryBuilder/queryBuilder.factory.d.ts +6 -0
- package/dist/queryBuilder/queryBuilder.factory.js +8 -0
- package/dist/queryBuilder/queryBuilder.filter.d.ts +170 -0
- package/dist/queryBuilder/queryBuilder.filter.js +497 -0
- package/dist/queryBuilder/queryBuilder.prisma.d.ts +60 -0
- package/dist/queryBuilder/queryBuilder.prisma.js +233 -0
- package/dist/queryBuilder/queryBuilder.type.d.ts +48 -0
- package/dist/queryBuilder/queryBuilder.type.js +2 -0
- package/dist/relations/index.d.ts +1 -1
- package/dist/relations/index.js +1 -1
- package/dist/relations/relations.definition.d.ts +184 -0
- package/dist/relations/relations.definition.js +362 -0
- package/dist/repository/index.d.ts +2 -1
- package/dist/repository/index.js +1 -0
- package/dist/repository/repository.base.d.ts +270 -0
- package/dist/repository/repository.base.js +350 -84
- package/dist/repository/repository.errors.d.ts +60 -0
- package/dist/repository/repository.errors.js +235 -0
- package/dist/seed/index.d.ts +2 -2
- package/dist/seed/index.js +1 -1
- package/dist/seed/seed.runner.d.ts +160 -0
- package/dist/seed/seed.runner.js +194 -225
- package/dist/transaction/index.d.ts +1 -1
- package/dist/transaction/index.js +1 -1
- package/dist/transaction/transaction.core.d.ts +117 -0
- package/dist/transaction/transaction.core.js +83 -56
- package/dist/unitOfWork/unitOfWork.core.d.ts +44 -0
- package/dist/unitOfWork/unitOfWork.core.js +9 -20
- package/package.json +35 -19
- package/dist/cache/cache.memory.d.ts.map +0 -1
- package/dist/cache/cache.memory.js.map +0 -1
- package/dist/cache/index.d.ts.map +0 -1
- package/dist/cache/index.js.map +0 -1
- package/dist/database/database.core.d.ts.map +0 -1
- package/dist/database/index.d.ts.map +0 -1
- package/dist/database/index.js.map +0 -1
- package/dist/databaseClient/databaseClient.core.d.ts.map +0 -1
- package/dist/databaseClient/databaseClient.core.js.map +0 -1
- package/dist/databaseClient/databaseClient.logger.d.ts.map +0 -1
- package/dist/databaseClient/databaseClient.logger.js.map +0 -1
- package/dist/databaseClient/index.d.ts.map +0 -1
- package/dist/databaseClient/index.js.map +0 -1
- package/dist/databaseConnection/databaseConnection.manager.d.ts.map +0 -1
- package/dist/databaseConnection/databaseConnection.manager.js.map +0 -1
- package/dist/databaseConnection/index.d.ts.map +0 -1
- package/dist/databaseConnection/index.js.map +0 -1
- package/dist/databaseType/index.d.ts.map +0 -1
- package/dist/databaseType/index.js.map +0 -1
- package/dist/health/health.check.d.ts.map +0 -1
- package/dist/health/health.check.js.map +0 -1
- package/dist/health/index.d.ts.map +0 -1
- package/dist/health/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/locks/index.d.ts.map +0 -1
- package/dist/locks/index.js.map +0 -1
- package/dist/locks/locks.core.d.ts.map +0 -1
- package/dist/locks/locks.core.js.map +0 -1
- package/dist/migration/index.d.ts.map +0 -1
- package/dist/migration/index.js.map +0 -1
- package/dist/migration/migration.helpers.d.ts.map +0 -1
- package/dist/migration/migration.runner.d.ts.map +0 -1
- package/dist/migration/migration.runner.js.map +0 -1
- package/dist/migration/migration.types.d.ts.map +0 -1
- package/dist/pagination/index.d.ts.map +0 -1
- package/dist/pagination/index.js.map +0 -1
- package/dist/queryBuilder/index.d.ts.map +0 -1
- package/dist/queryBuilder/index.js.map +0 -1
- package/dist/relations/index.d.ts.map +0 -1
- package/dist/relations/index.js.map +0 -1
- package/dist/repository/index.d.ts.map +0 -1
- package/dist/repository/index.js.map +0 -1
- package/dist/repository/repository.base.d.ts.map +0 -1
- package/dist/repository/repository.base.js.map +0 -1
- package/dist/seed/index.d.ts.map +0 -1
- package/dist/seed/index.js.map +0 -1
- package/dist/seed/seed.runner.d.ts.map +0 -1
- package/dist/seed/seed.runner.js.map +0 -1
- package/dist/transaction/index.d.ts.map +0 -1
- package/dist/transaction/index.js.map +0 -1
- package/dist/transaction/transaction.core.d.ts.map +0 -1
- package/dist/transaction/transaction.core.js.map +0 -1
- package/dist/unitOfWork/index.d.ts.map +0 -1
- package/dist/unitOfWork/index.js.map +0 -1
- package/dist/unitOfWork/unitOfWork.core.d.ts.map +0 -1
- package/dist/unitOfWork/unitOfWork.core.js.map +0 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { QueryOperator, QueryFilter, QueryBuilderState, PaginationInput, SortDirection, SortInput, QueryOptions } from "./queryBuilder.type.js";
|
|
2
|
+
import { type PrismaQueryArgs, type ToPrismaArgsOptions } from "./queryBuilder.prisma.js";
|
|
3
|
+
import { type RelationInclude } from "../relations/relations.definition.js";
|
|
4
|
+
/**
|
|
5
|
+
* Query builder used to construct database-neutral query definitions.
|
|
6
|
+
*
|
|
7
|
+
* The builder does not execute queries. It produces a plain query
|
|
8
|
+
* definition that repositories or adapters can translate into their
|
|
9
|
+
* ORM-specific representation.
|
|
10
|
+
*/
|
|
11
|
+
export declare class QueryBuilder<TField extends string = string> {
|
|
12
|
+
private filterState?;
|
|
13
|
+
private paginationState?;
|
|
14
|
+
private sortState;
|
|
15
|
+
private selectState;
|
|
16
|
+
private offsetState?;
|
|
17
|
+
private includeState;
|
|
18
|
+
private _cachedBuild?;
|
|
19
|
+
private invalidateCache;
|
|
20
|
+
/**
|
|
21
|
+
* Adds an equality condition.
|
|
22
|
+
*/
|
|
23
|
+
where(field: TField, value: unknown): this;
|
|
24
|
+
/**
|
|
25
|
+
* Adds a condition using a specific operator.
|
|
26
|
+
*/
|
|
27
|
+
whereOperator(field: TField, operator: QueryOperator, value?: unknown): this;
|
|
28
|
+
/**
|
|
29
|
+
* Adds a not-equal condition.
|
|
30
|
+
*/
|
|
31
|
+
whereNot(field: TField, value: unknown): this;
|
|
32
|
+
/**
|
|
33
|
+
* Adds an IN condition.
|
|
34
|
+
*/
|
|
35
|
+
whereIn(field: TField, values: readonly unknown[]): this;
|
|
36
|
+
/**
|
|
37
|
+
* Adds a NOT IN condition.
|
|
38
|
+
*/
|
|
39
|
+
whereNotIn(field: TField, values: readonly unknown[]): this;
|
|
40
|
+
/**
|
|
41
|
+
* Adds a less-than condition.
|
|
42
|
+
*/
|
|
43
|
+
whereLessThan(field: TField, value: unknown): this;
|
|
44
|
+
/**
|
|
45
|
+
* Adds a less-than-or-equal condition.
|
|
46
|
+
*/
|
|
47
|
+
whereLessThanOrEqual(field: TField, value: unknown): this;
|
|
48
|
+
/**
|
|
49
|
+
* Adds a greater-than condition.
|
|
50
|
+
*/
|
|
51
|
+
whereGreaterThan(field: TField, value: unknown): this;
|
|
52
|
+
/**
|
|
53
|
+
* Adds a greater-than-or-equal condition.
|
|
54
|
+
*/
|
|
55
|
+
whereGreaterThanOrEqual(field: TField, value: unknown): this;
|
|
56
|
+
/**
|
|
57
|
+
* Adds a contains condition.
|
|
58
|
+
*/
|
|
59
|
+
whereContains(field: TField, value: string): this;
|
|
60
|
+
/**
|
|
61
|
+
* Adds a starts-with condition.
|
|
62
|
+
*/
|
|
63
|
+
whereStartsWith(field: TField, value: string): this;
|
|
64
|
+
/**
|
|
65
|
+
* Adds an ends-with condition.
|
|
66
|
+
*/
|
|
67
|
+
whereEndsWith(field: TField, value: string): this;
|
|
68
|
+
/**
|
|
69
|
+
* Adds an IS NULL condition.
|
|
70
|
+
*/
|
|
71
|
+
whereNull(field: TField): this;
|
|
72
|
+
/**
|
|
73
|
+
* Adds an IS NOT NULL condition.
|
|
74
|
+
*/
|
|
75
|
+
whereNotNull(field: TField): this;
|
|
76
|
+
/**
|
|
77
|
+
* Adds an AND group.
|
|
78
|
+
*/
|
|
79
|
+
and(...filters: QueryFilter[]): this;
|
|
80
|
+
/**
|
|
81
|
+
* Adds an OR group.
|
|
82
|
+
*/
|
|
83
|
+
or(...filters: QueryFilter[]): this;
|
|
84
|
+
/**
|
|
85
|
+
* Adds a NOT group.
|
|
86
|
+
*/
|
|
87
|
+
not(filter: QueryFilter): this;
|
|
88
|
+
/**
|
|
89
|
+
* Sets the requested page.
|
|
90
|
+
*/
|
|
91
|
+
page(page: number): this;
|
|
92
|
+
/**
|
|
93
|
+
* Sets the requested page size.
|
|
94
|
+
*/
|
|
95
|
+
limit(limit: number): this;
|
|
96
|
+
/**
|
|
97
|
+
* Sets pagination.
|
|
98
|
+
*/
|
|
99
|
+
paginate(pagination: PaginationInput): this;
|
|
100
|
+
/**
|
|
101
|
+
* Sets an explicit row offset. Takes precedence over `page()`.
|
|
102
|
+
*/
|
|
103
|
+
offset(offset: number): this;
|
|
104
|
+
/**
|
|
105
|
+
* Adds relations to include. Accepts relation names or include
|
|
106
|
+
* definitions created with `includeRelation`.
|
|
107
|
+
*/
|
|
108
|
+
include(...relations: readonly (string | RelationInclude)[]): this;
|
|
109
|
+
/**
|
|
110
|
+
* Sorts ascending by a field.
|
|
111
|
+
*/
|
|
112
|
+
orderByAsc(field: TField): this;
|
|
113
|
+
/**
|
|
114
|
+
* Sorts descending by a field.
|
|
115
|
+
*/
|
|
116
|
+
orderByDesc(field: TField): this;
|
|
117
|
+
/**
|
|
118
|
+
* Adds a sort definition.
|
|
119
|
+
*/
|
|
120
|
+
orderBy(field: TField, direction?: SortDirection): this;
|
|
121
|
+
/**
|
|
122
|
+
* Replaces all sort definitions.
|
|
123
|
+
*/
|
|
124
|
+
sort(sort: readonly SortInput<TField>[]): this;
|
|
125
|
+
/**
|
|
126
|
+
* Selects specific fields.
|
|
127
|
+
*/
|
|
128
|
+
select(...fields: TField[]): this;
|
|
129
|
+
/**
|
|
130
|
+
* Clears all filters.
|
|
131
|
+
*/
|
|
132
|
+
clearFilters(): this;
|
|
133
|
+
/**
|
|
134
|
+
* Clears pagination.
|
|
135
|
+
*/
|
|
136
|
+
clearPagination(): this;
|
|
137
|
+
/**
|
|
138
|
+
* Clears sorting.
|
|
139
|
+
*/
|
|
140
|
+
clearSort(): this;
|
|
141
|
+
/**
|
|
142
|
+
* Clears selected fields.
|
|
143
|
+
*/
|
|
144
|
+
clearSelect(): this;
|
|
145
|
+
/**
|
|
146
|
+
* Clears included relations.
|
|
147
|
+
*/
|
|
148
|
+
clearInclude(): this;
|
|
149
|
+
/**
|
|
150
|
+
* Resets the builder to its initial empty state.
|
|
151
|
+
*/
|
|
152
|
+
reset(): this;
|
|
153
|
+
/**
|
|
154
|
+
* Returns the immutable query definition.
|
|
155
|
+
*/
|
|
156
|
+
build(): QueryBuilderState<TField>;
|
|
157
|
+
/**
|
|
158
|
+
* Converts the builder into generic query options (pagination, sort and
|
|
159
|
+
* the built filter).
|
|
160
|
+
*/
|
|
161
|
+
toQueryOptions(): QueryOptions<TField> & {
|
|
162
|
+
readonly filter?: QueryFilter;
|
|
163
|
+
readonly offset?: number;
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Translates the builder into Prisma `findMany`-style arguments.
|
|
167
|
+
*/
|
|
168
|
+
toPrismaArgs(options?: ToPrismaArgsOptions): PrismaQueryArgs;
|
|
169
|
+
/**
|
|
170
|
+
* Creates an independent copy of the builder.
|
|
171
|
+
*/
|
|
172
|
+
clone(): QueryBuilder<TField>;
|
|
173
|
+
/**
|
|
174
|
+
* Adds a condition to the current filter.
|
|
175
|
+
*/
|
|
176
|
+
private addCondition;
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=queryBuilder.core.d.ts.map
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import { cloneFilter } from "./queryBuilder.filter.js";
|
|
2
|
+
import { toPrismaArgs, } from "./queryBuilder.prisma.js";
|
|
3
|
+
import { includeRelation, } from "../relations/relations.definition.js";
|
|
4
|
+
/**
|
|
5
|
+
* Query builder used to construct database-neutral query definitions.
|
|
6
|
+
*
|
|
7
|
+
* The builder does not execute queries. It produces a plain query
|
|
8
|
+
* definition that repositories or adapters can translate into their
|
|
9
|
+
* ORM-specific representation.
|
|
10
|
+
*/
|
|
11
|
+
export class QueryBuilder {
|
|
12
|
+
filterState;
|
|
13
|
+
paginationState;
|
|
14
|
+
sortState = [];
|
|
15
|
+
selectState = [];
|
|
16
|
+
offsetState;
|
|
17
|
+
includeState = [];
|
|
18
|
+
_cachedBuild;
|
|
19
|
+
invalidateCache() {
|
|
20
|
+
this._cachedBuild = undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Adds an equality condition.
|
|
24
|
+
*/
|
|
25
|
+
where(field, value) {
|
|
26
|
+
return this.addCondition({
|
|
27
|
+
field,
|
|
28
|
+
operator: "equals",
|
|
29
|
+
value,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Adds a condition using a specific operator.
|
|
34
|
+
*/
|
|
35
|
+
whereOperator(field, operator, value) {
|
|
36
|
+
return this.addCondition({
|
|
37
|
+
field,
|
|
38
|
+
operator,
|
|
39
|
+
value,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Adds a not-equal condition.
|
|
44
|
+
*/
|
|
45
|
+
whereNot(field, value) {
|
|
46
|
+
return this.whereOperator(field, "not", value);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Adds an IN condition.
|
|
50
|
+
*/
|
|
51
|
+
whereIn(field, values) {
|
|
52
|
+
return this.whereOperator(field, "in", [...values]);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Adds a NOT IN condition.
|
|
56
|
+
*/
|
|
57
|
+
whereNotIn(field, values) {
|
|
58
|
+
return this.whereOperator(field, "notIn", [...values]);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Adds a less-than condition.
|
|
62
|
+
*/
|
|
63
|
+
whereLessThan(field, value) {
|
|
64
|
+
return this.whereOperator(field, "lt", value);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Adds a less-than-or-equal condition.
|
|
68
|
+
*/
|
|
69
|
+
whereLessThanOrEqual(field, value) {
|
|
70
|
+
return this.whereOperator(field, "lte", value);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Adds a greater-than condition.
|
|
74
|
+
*/
|
|
75
|
+
whereGreaterThan(field, value) {
|
|
76
|
+
return this.whereOperator(field, "gt", value);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Adds a greater-than-or-equal condition.
|
|
80
|
+
*/
|
|
81
|
+
whereGreaterThanOrEqual(field, value) {
|
|
82
|
+
return this.whereOperator(field, "gte", value);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Adds a contains condition.
|
|
86
|
+
*/
|
|
87
|
+
whereContains(field, value) {
|
|
88
|
+
return this.whereOperator(field, "contains", value);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Adds a starts-with condition.
|
|
92
|
+
*/
|
|
93
|
+
whereStartsWith(field, value) {
|
|
94
|
+
return this.whereOperator(field, "startsWith", value);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Adds an ends-with condition.
|
|
98
|
+
*/
|
|
99
|
+
whereEndsWith(field, value) {
|
|
100
|
+
return this.whereOperator(field, "endsWith", value);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Adds an IS NULL condition.
|
|
104
|
+
*/
|
|
105
|
+
whereNull(field) {
|
|
106
|
+
return this.whereOperator(field, "isNull");
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Adds an IS NOT NULL condition.
|
|
110
|
+
*/
|
|
111
|
+
whereNotNull(field) {
|
|
112
|
+
return this.whereOperator(field, "isNotNull");
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Adds an AND group.
|
|
116
|
+
*/
|
|
117
|
+
and(...filters) {
|
|
118
|
+
const existing = this.filterState;
|
|
119
|
+
const group = {
|
|
120
|
+
and: filters,
|
|
121
|
+
};
|
|
122
|
+
this.filterState = existing
|
|
123
|
+
? {
|
|
124
|
+
and: [existing, group],
|
|
125
|
+
}
|
|
126
|
+
: group;
|
|
127
|
+
this.invalidateCache();
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Adds an OR group.
|
|
132
|
+
*/
|
|
133
|
+
or(...filters) {
|
|
134
|
+
const existing = this.filterState;
|
|
135
|
+
const group = {
|
|
136
|
+
or: filters,
|
|
137
|
+
};
|
|
138
|
+
this.filterState = existing
|
|
139
|
+
? {
|
|
140
|
+
and: [existing, group],
|
|
141
|
+
}
|
|
142
|
+
: group;
|
|
143
|
+
this.invalidateCache();
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Adds a NOT group.
|
|
148
|
+
*/
|
|
149
|
+
not(filter) {
|
|
150
|
+
const existing = this.filterState;
|
|
151
|
+
const group = {
|
|
152
|
+
not: filter,
|
|
153
|
+
};
|
|
154
|
+
this.filterState = existing
|
|
155
|
+
? {
|
|
156
|
+
and: [existing, group],
|
|
157
|
+
}
|
|
158
|
+
: group;
|
|
159
|
+
this.invalidateCache();
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Sets the requested page.
|
|
164
|
+
*/
|
|
165
|
+
page(page) {
|
|
166
|
+
this.paginationState = {
|
|
167
|
+
...this.paginationState,
|
|
168
|
+
page,
|
|
169
|
+
};
|
|
170
|
+
this.invalidateCache();
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Sets the requested page size.
|
|
175
|
+
*/
|
|
176
|
+
limit(limit) {
|
|
177
|
+
this.paginationState = {
|
|
178
|
+
...this.paginationState,
|
|
179
|
+
limit,
|
|
180
|
+
};
|
|
181
|
+
this.invalidateCache();
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Sets pagination.
|
|
186
|
+
*/
|
|
187
|
+
paginate(pagination) {
|
|
188
|
+
this.paginationState = {
|
|
189
|
+
...pagination,
|
|
190
|
+
};
|
|
191
|
+
this.invalidateCache();
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Sets an explicit row offset. Takes precedence over `page()`.
|
|
196
|
+
*/
|
|
197
|
+
offset(offset) {
|
|
198
|
+
if (!Number.isFinite(offset) || offset < 0) {
|
|
199
|
+
throw new TypeError("Query offset must be a non-negative number.");
|
|
200
|
+
}
|
|
201
|
+
this.offsetState = Math.floor(offset);
|
|
202
|
+
this.invalidateCache();
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Adds relations to include. Accepts relation names or include
|
|
207
|
+
* definitions created with `includeRelation`.
|
|
208
|
+
*/
|
|
209
|
+
include(...relations) {
|
|
210
|
+
for (const relation of relations) {
|
|
211
|
+
const include = typeof relation === "string" ? includeRelation(relation) : relation;
|
|
212
|
+
if (!include || typeof include.relation !== "string") {
|
|
213
|
+
throw new TypeError("A relation include is required.");
|
|
214
|
+
}
|
|
215
|
+
if (!this.includeState.some((e) => e.relation === include.relation)) {
|
|
216
|
+
this.includeState.push(include);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
this.invalidateCache();
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Sorts ascending by a field.
|
|
224
|
+
*/
|
|
225
|
+
orderByAsc(field) {
|
|
226
|
+
return this.orderBy(field, "asc");
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Sorts descending by a field.
|
|
230
|
+
*/
|
|
231
|
+
orderByDesc(field) {
|
|
232
|
+
return this.orderBy(field, "desc");
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Adds a sort definition.
|
|
236
|
+
*/
|
|
237
|
+
orderBy(field, direction = "asc") {
|
|
238
|
+
this.sortState.push({
|
|
239
|
+
field,
|
|
240
|
+
direction,
|
|
241
|
+
});
|
|
242
|
+
this.invalidateCache();
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Replaces all sort definitions.
|
|
247
|
+
*/
|
|
248
|
+
sort(sort) {
|
|
249
|
+
this.sortState = [...sort];
|
|
250
|
+
this.invalidateCache();
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Selects specific fields.
|
|
255
|
+
*/
|
|
256
|
+
select(...fields) {
|
|
257
|
+
this.selectState = [...new Set(fields)];
|
|
258
|
+
this.invalidateCache();
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Clears all filters.
|
|
263
|
+
*/
|
|
264
|
+
clearFilters() {
|
|
265
|
+
this.filterState = undefined;
|
|
266
|
+
this.invalidateCache();
|
|
267
|
+
return this;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Clears pagination.
|
|
271
|
+
*/
|
|
272
|
+
clearPagination() {
|
|
273
|
+
this.paginationState = undefined;
|
|
274
|
+
this.invalidateCache();
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Clears sorting.
|
|
279
|
+
*/
|
|
280
|
+
clearSort() {
|
|
281
|
+
this.sortState = [];
|
|
282
|
+
this.invalidateCache();
|
|
283
|
+
return this;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Clears selected fields.
|
|
287
|
+
*/
|
|
288
|
+
clearSelect() {
|
|
289
|
+
this.selectState = [];
|
|
290
|
+
this.invalidateCache();
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Clears included relations.
|
|
295
|
+
*/
|
|
296
|
+
clearInclude() {
|
|
297
|
+
this.includeState = [];
|
|
298
|
+
this.invalidateCache();
|
|
299
|
+
return this;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Resets the builder to its initial empty state.
|
|
303
|
+
*/
|
|
304
|
+
reset() {
|
|
305
|
+
this.filterState = undefined;
|
|
306
|
+
this.paginationState = undefined;
|
|
307
|
+
this.offsetState = undefined;
|
|
308
|
+
this.sortState = [];
|
|
309
|
+
this.selectState = [];
|
|
310
|
+
this.includeState = [];
|
|
311
|
+
this.invalidateCache();
|
|
312
|
+
return this;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Returns the immutable query definition.
|
|
316
|
+
*/
|
|
317
|
+
build() {
|
|
318
|
+
if (this._cachedBuild) {
|
|
319
|
+
return this._cachedBuild;
|
|
320
|
+
}
|
|
321
|
+
const result = deepFreeze({
|
|
322
|
+
filter: cloneFilter(this.filterState),
|
|
323
|
+
pagination: this.paginationState
|
|
324
|
+
? { ...this.paginationState }
|
|
325
|
+
: undefined,
|
|
326
|
+
offset: this.offsetState,
|
|
327
|
+
sort: this.sortState.length > 0
|
|
328
|
+
? this.sortState.map((entry) => ({ ...entry }))
|
|
329
|
+
: undefined,
|
|
330
|
+
select: this.selectState.length > 0 ? [...this.selectState] : undefined,
|
|
331
|
+
include: this.includeState.length > 0 ? [...this.includeState] : undefined,
|
|
332
|
+
});
|
|
333
|
+
this._cachedBuild = result;
|
|
334
|
+
return result;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Converts the builder into generic query options (pagination, sort and
|
|
338
|
+
* the built filter).
|
|
339
|
+
*/
|
|
340
|
+
toQueryOptions() {
|
|
341
|
+
return {
|
|
342
|
+
filter: cloneFilter(this.filterState),
|
|
343
|
+
pagination: this.paginationState
|
|
344
|
+
? { ...this.paginationState }
|
|
345
|
+
: undefined,
|
|
346
|
+
offset: this.offsetState,
|
|
347
|
+
sort: this.sortState.length > 0 ? [...this.sortState] : undefined,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Translates the builder into Prisma `findMany`-style arguments.
|
|
352
|
+
*/
|
|
353
|
+
toPrismaArgs(options) {
|
|
354
|
+
return toPrismaArgs(this.build(), options);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Creates an independent copy of the builder.
|
|
358
|
+
*/
|
|
359
|
+
clone() {
|
|
360
|
+
const builder = new QueryBuilder();
|
|
361
|
+
builder.filterState = cloneFilter(this.filterState);
|
|
362
|
+
builder.paginationState = this.paginationState
|
|
363
|
+
? {
|
|
364
|
+
...this.paginationState,
|
|
365
|
+
}
|
|
366
|
+
: undefined;
|
|
367
|
+
builder.offsetState = this.offsetState;
|
|
368
|
+
builder.sortState = this.sortState.map((entry) => ({ ...entry }));
|
|
369
|
+
builder.selectState = [...this.selectState];
|
|
370
|
+
builder.includeState = [...this.includeState];
|
|
371
|
+
return builder;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Adds a condition to the current filter.
|
|
375
|
+
*/
|
|
376
|
+
addCondition(condition) {
|
|
377
|
+
const existing = this.filterState;
|
|
378
|
+
if (!existing) {
|
|
379
|
+
this.filterState = {
|
|
380
|
+
conditions: [condition],
|
|
381
|
+
};
|
|
382
|
+
this.invalidateCache();
|
|
383
|
+
return this;
|
|
384
|
+
}
|
|
385
|
+
if (existing.conditions && !existing.and && !existing.or && !existing.not) {
|
|
386
|
+
this.filterState = {
|
|
387
|
+
conditions: [...existing.conditions, condition],
|
|
388
|
+
};
|
|
389
|
+
this.invalidateCache();
|
|
390
|
+
return this;
|
|
391
|
+
}
|
|
392
|
+
this.filterState = {
|
|
393
|
+
and: [
|
|
394
|
+
existing,
|
|
395
|
+
{
|
|
396
|
+
conditions: [condition],
|
|
397
|
+
},
|
|
398
|
+
],
|
|
399
|
+
};
|
|
400
|
+
this.invalidateCache();
|
|
401
|
+
return this;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Recursively freezes plain objects and arrays.
|
|
406
|
+
*/
|
|
407
|
+
function deepFreeze(value) {
|
|
408
|
+
if (value === null || typeof value !== "object" || Object.isFrozen(value)) {
|
|
409
|
+
return value;
|
|
410
|
+
}
|
|
411
|
+
if (value instanceof Date) {
|
|
412
|
+
return value;
|
|
413
|
+
}
|
|
414
|
+
Object.freeze(value);
|
|
415
|
+
for (const entry of Object.values(value)) {
|
|
416
|
+
deepFreeze(entry);
|
|
417
|
+
}
|
|
418
|
+
return value;
|
|
419
|
+
}
|
|
420
|
+
//# sourceMappingURL=queryBuilder.core.js.map
|