@umituz/react-native-firebase 2.6.1 → 2.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/application/auth/ports/AuthPort_part_aa +150 -0
- package/src/application/auth/ports/AuthPort_part_ab +14 -0
- package/src/application/auth/use-cases/SignInUseCaseHelpers.ts +0 -0
- package/src/application/auth/use-cases/SignInUseCaseMain.ts +0 -0
- package/src/application/auth/use-cases/SignInUseCase_part_aa +150 -0
- package/src/application/auth/use-cases/SignInUseCase_part_ab +103 -0
- package/src/application/auth/use-cases/SignOutUseCaseCleanup.ts +0 -0
- package/src/application/auth/use-cases/SignOutUseCaseMain.ts +0 -0
- package/src/application/auth/use-cases/SignOutUseCase_part_aa +150 -0
- package/src/application/auth/use-cases/SignOutUseCase_part_ab +138 -0
- package/src/domains/account-deletion/domain/services/UserValidationHelpers.ts.bak +181 -0
- package/src/domains/account-deletion/domain/services/UserValidationHelpers_part_aa +150 -0
- package/src/domains/account-deletion/domain/services/UserValidationHelpers_part_ab +31 -0
- package/src/domains/account-deletion/domain/services/{UserValidationService.ts → UserValidationService.ts.bak} +1 -10
- package/src/domains/account-deletion/domain/services/UserValidationService_part_aa +150 -0
- package/src/domains/account-deletion/domain/services/UserValidationService_part_ab +136 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionExecutor_part_aa +150 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionExecutor_part_ab +80 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler_part_aa +150 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionReauthHandler_part_ab +24 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionRepository_part_aa +150 -0
- package/src/domains/account-deletion/infrastructure/services/AccountDeletionRepository_part_ab +116 -0
- package/src/domains/account-deletion/infrastructure/services/reauthentication.service_part_aa +150 -0
- package/src/domains/account-deletion/infrastructure/services/reauthentication.service_part_ab +10 -0
- package/src/domains/auth/infrastructure_part_aa +150 -0
- package/src/domains/auth/infrastructure_part_ab +6 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthHelpers.ts +0 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthHookService_part_aa +150 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthHookService_part_ab +97 -0
- package/src/domains/auth/presentation/hooks/GoogleOAuthService.ts +0 -0
- package/src/domains/firestore/domain/entities/Collection.ts +31 -191
- package/src/domains/firestore/domain/entities/Collection.ts.bak +288 -0
- package/src/domains/firestore/domain/entities/CollectionFactory.ts +55 -0
- package/src/domains/firestore/domain/entities/CollectionHelpers.ts +143 -0
- package/src/domains/firestore/domain/entities/CollectionUtils.ts +72 -0
- package/src/domains/firestore/domain/entities/CollectionValidation.ts +138 -0
- package/src/domains/firestore/domain/entities/Collection_part_aa +150 -0
- package/src/domains/firestore/domain/entities/Collection_part_ab +138 -0
- package/src/domains/firestore/domain/entities/DocumentHelpers.ts +0 -0
- package/src/domains/firestore/domain/entities/DocumentMain.ts +0 -0
- package/src/domains/firestore/domain/entities/Document_part_aa +150 -0
- package/src/domains/firestore/domain/entities/Document_part_ab +83 -0
- package/src/domains/firestore/domain/index.ts +44 -9
- package/src/domains/firestore/domain/services/QueryServiceAnalysis_part_aa +150 -0
- package/src/domains/firestore/domain/services/QueryServiceAnalysis_part_ab +19 -0
- package/src/domains/firestore/domain/services/QueryServiceHelpers_part_aa +150 -0
- package/src/domains/firestore/domain/services/QueryServiceHelpers_part_ab +1 -0
- package/src/domains/firestore/domain/services/QueryService_part_aa +150 -0
- package/src/domains/firestore/domain/services/QueryService_part_ab +32 -0
- package/src/domains/firestore/domain/value-objects/QueryOptions.ts.bak +6 -135
- package/src/domains/firestore/domain/value-objects/QueryOptionsSerialization_part_aa +150 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsSerialization_part_ab +57 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsValidation_part_aa +150 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsValidation_part_ab +32 -0
- package/src/domains/firestore/domain/value-objects/{QueryOptions.ts → QueryOptions_part_aa} +0 -41
- package/src/domains/firestore/domain/value-objects/QueryOptions_part_ab +41 -0
- package/src/domains/firestore/domain/value-objects/WhereClauseFactory_part_aa +150 -0
- package/src/domains/firestore/domain/value-objects/WhereClauseFactory_part_ab +57 -0
- package/src/domains/firestore/domain/value-objects/WhereClause_part_aa +150 -0
- package/src/domains/firestore/domain/value-objects/WhereClause_part_ab +149 -0
- package/src/shared/infrastructure/base/ErrorHandler_part_aa +150 -0
- package/src/shared/infrastructure/base/ErrorHandler_part_ab +39 -0
- package/src/shared/infrastructure/base/ServiceBase_part_aa +150 -0
- package/src/shared/infrastructure/base/ServiceBase_part_ab +70 -0
- package/src/shared/infrastructure/config/base/ServiceClientSingleton_part_aa +150 -0
- package/src/shared/infrastructure/config/base/ServiceClientSingleton_part_ab +5 -0
- /package/src/application/auth/ports/{AuthPort.ts → AuthPort.ts.bak} +0 -0
- /package/src/application/auth/use-cases/{SignInUseCase.ts → SignInUseCase.ts.bak} +0 -0
- /package/src/application/auth/use-cases/{SignOutUseCase.ts → SignOutUseCase.ts.bak} +0 -0
- /package/src/domains/account-deletion/infrastructure/services/{AccountDeletionExecutor.ts → AccountDeletionExecutor.ts.bak} +0 -0
- /package/src/domains/account-deletion/infrastructure/services/{AccountDeletionReauthHandler.ts → AccountDeletionReauthHandler.ts.bak} +0 -0
- /package/src/domains/account-deletion/infrastructure/services/{AccountDeletionRepository.ts → AccountDeletionRepository.ts.bak} +0 -0
- /package/src/domains/account-deletion/infrastructure/services/{reauthentication.service.ts → reauthentication.service.ts.bak} +0 -0
- /package/src/domains/auth/{infrastructure.ts → infrastructure.ts.bak} +0 -0
- /package/src/domains/auth/presentation/hooks/{GoogleOAuthHookService.ts → GoogleOAuthHookService.ts.bak} +0 -0
- /package/src/domains/firestore/domain/entities/{Document.ts → Document.ts.bak} +0 -0
- /package/src/domains/firestore/domain/services/{QueryService.ts → QueryService.ts.bak} +0 -0
- /package/src/domains/firestore/domain/services/{QueryServiceAnalysis.ts → QueryServiceAnalysis.ts.bak} +0 -0
- /package/src/domains/firestore/domain/services/{QueryServiceHelpers.ts → QueryServiceHelpers.ts.bak} +0 -0
- /package/src/domains/firestore/domain/value-objects/{QueryOptionsSerialization.ts → QueryOptionsSerialization.ts.bak} +0 -0
- /package/src/domains/firestore/domain/value-objects/{QueryOptionsValidation.ts → QueryOptionsValidation.ts.bak} +0 -0
- /package/src/domains/firestore/domain/value-objects/{WhereClause.ts → WhereClause.ts.bak} +0 -0
- /package/src/domains/firestore/domain/value-objects/{WhereClauseFactory.ts → WhereClauseFactory.ts.bak} +0 -0
- /package/src/shared/infrastructure/base/{ErrorHandler.ts → ErrorHandler.ts.bak} +0 -0
- /package/src/shared/infrastructure/base/{ServiceBase.ts → ServiceBase.ts.bak} +0 -0
- /package/src/shared/infrastructure/config/base/{ServiceClientSingleton.ts → ServiceClientSingleton.ts.bak} +0 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query Service (Main Builder)
|
|
3
|
+
* Single Responsibility: Build and validate Firestore queries
|
|
4
|
+
*
|
|
5
|
+
* Domain service that encapsulates query building logic.
|
|
6
|
+
* Moves business logic from infrastructure to domain layer.
|
|
7
|
+
*
|
|
8
|
+
* Max lines: 150 (enforced for maintainability)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Query, Firestore, WhereFilterOp } from 'firebase/firestore';
|
|
12
|
+
import { collection, query, where, orderBy, limit, startAfter, startAt } from 'firebase/firestore';
|
|
13
|
+
import { QueryOptions, createQueryOptions } from '../value-objects/QueryOptions';
|
|
14
|
+
import { WhereClause } from '../value-objects/WhereClause';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Query builder result
|
|
18
|
+
*/
|
|
19
|
+
export interface QueryBuilderResult {
|
|
20
|
+
readonly query: Query;
|
|
21
|
+
readonly options: QueryOptions;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Query service
|
|
26
|
+
* Provides query building and validation functionality
|
|
27
|
+
*/
|
|
28
|
+
export class QueryService {
|
|
29
|
+
private readonly db: Firestore;
|
|
30
|
+
|
|
31
|
+
constructor(db: Firestore) {
|
|
32
|
+
this.db = db;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Build a query from options
|
|
37
|
+
* Main method for query construction
|
|
38
|
+
*/
|
|
39
|
+
buildQuery(collectionPath: string, options: QueryOptions): QueryBuilderResult {
|
|
40
|
+
const validation = options.validate();
|
|
41
|
+
if (!validation.valid) {
|
|
42
|
+
throw new Error(`Invalid query options: ${validation.errors.join(', ')}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const collectionRef = collection(this.db, collectionPath);
|
|
46
|
+
let q: Query = collectionRef;
|
|
47
|
+
|
|
48
|
+
// Apply where clauses
|
|
49
|
+
for (const clause of options.whereClauses) {
|
|
50
|
+
q = query(q, where(clause.field, clause.operator, clause.value));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Apply sort options
|
|
54
|
+
for (const sort of options.sortOptions) {
|
|
55
|
+
q = query(q, orderBy(sort.field, sort.direction));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Apply date range
|
|
59
|
+
if (options.dateRange) {
|
|
60
|
+
const { field, startDate, endDate } = options.dateRange;
|
|
61
|
+
if (startDate) {
|
|
62
|
+
q = query(q, where(field, '>=', startDate));
|
|
63
|
+
}
|
|
64
|
+
if (endDate) {
|
|
65
|
+
q = query(q, where(field, '<=', endDate));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Apply pagination
|
|
70
|
+
if (options.pagination) {
|
|
71
|
+
const { cursor, limit: limitValue, startAfter: startAfterValue, startAt: startAtValue } = options.pagination;
|
|
72
|
+
|
|
73
|
+
if (startAfterValue !== undefined) {
|
|
74
|
+
q = query(q, startAfter(startAfterValue));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (startAtValue !== undefined) {
|
|
78
|
+
q = query(q, startAt(startAtValue));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (limitValue !== undefined) {
|
|
82
|
+
q = query(q, limit(limitValue));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return { query: q, options };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Build simple query with single where clause
|
|
91
|
+
*/
|
|
92
|
+
buildSimpleQuery(
|
|
93
|
+
collectionPath: string,
|
|
94
|
+
field: string,
|
|
95
|
+
operator: WhereFilterOp,
|
|
96
|
+
value: unknown
|
|
97
|
+
): Query {
|
|
98
|
+
const options = createQueryOptions({
|
|
99
|
+
where: [WhereClause.create(field, operator, value)],
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return this.buildQuery(collectionPath, options).query;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Build query with equality filter
|
|
107
|
+
*/
|
|
108
|
+
buildEqualsQuery(collectionPath: string, field: string, value: unknown): Query {
|
|
109
|
+
return this.buildSimpleQuery(collectionPath, field, '==', value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Build query with multiple equality filters
|
|
114
|
+
*/
|
|
115
|
+
buildMultiEqualsQuery(
|
|
116
|
+
collectionPath: string,
|
|
117
|
+
filters: Record<string, unknown>
|
|
118
|
+
): Query {
|
|
119
|
+
const whereClauses = Object.entries(filters).map(([field, value]) =>
|
|
120
|
+
WhereClause.equals(field, value)
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const options = createQueryOptions({ where: whereClauses });
|
|
124
|
+
return this.buildQuery(collectionPath, options).query;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Build query with sorting
|
|
129
|
+
*/
|
|
130
|
+
buildSortedQuery(
|
|
131
|
+
collectionPath: string,
|
|
132
|
+
sortField: string,
|
|
133
|
+
direction: 'asc' | 'desc' = 'asc'
|
|
134
|
+
): Query {
|
|
135
|
+
const options = createQueryOptions({
|
|
136
|
+
sort: [{ field: sortField, direction }],
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
return this.buildQuery(collectionPath, options).query;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Build query with limit
|
|
144
|
+
*/
|
|
145
|
+
buildLimitedQuery(collectionPath: string, limitValue: number): Query {
|
|
146
|
+
const options = createQueryOptions({
|
|
147
|
+
pagination: { limit: limitValue },
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
return this.buildQuery(collectionPath, options).query;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
}
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Build query with date range
|
|
5
|
+
*/
|
|
6
|
+
buildDateRangeQuery(
|
|
7
|
+
collectionPath: string,
|
|
8
|
+
field: string,
|
|
9
|
+
startDate?: Date,
|
|
10
|
+
endDate?: Date
|
|
11
|
+
): Query {
|
|
12
|
+
const options = createQueryOptions({
|
|
13
|
+
dateRange: { field, startDate, endDate },
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
return this.buildQuery(collectionPath, options).query;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Validate query options
|
|
21
|
+
*/
|
|
22
|
+
validateOptions(options: QueryOptions): { valid: boolean; errors: string[] } {
|
|
23
|
+
return options.validate();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Check if query is empty (no filters)
|
|
28
|
+
*/
|
|
29
|
+
isEmptyQuery(options: QueryOptions): boolean {
|
|
30
|
+
return options.isEmpty();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Query Options Value Object
|
|
2
|
+
* Query Options Value Object (Main)
|
|
3
3
|
* Single Responsibility: Encapsulate query configuration options
|
|
4
4
|
*
|
|
5
5
|
* Value object that represents query options in a type-safe way.
|
|
@@ -89,7 +89,7 @@ export class QueryOptions {
|
|
|
89
89
|
*/
|
|
90
90
|
withWhere(clause: WhereClause): QueryOptions {
|
|
91
91
|
return new QueryOptions(
|
|
92
|
-
[...this.whereClauses, clause] as WhereClause[]
|
|
92
|
+
[...this.whereClauses, clause] as WhereClause[],
|
|
93
93
|
this.sortOptions,
|
|
94
94
|
this.dateRange,
|
|
95
95
|
this.pagination
|
|
@@ -160,135 +160,6 @@ export class QueryOptions {
|
|
|
160
160
|
return new QueryOptions(this.whereClauses, this.sortOptions, this.dateRange, null);
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
/**
|
|
164
|
-
* Check if has any where clauses
|
|
165
|
-
*/
|
|
166
|
-
hasWhereClauses(): boolean {
|
|
167
|
-
return this.whereClauses.length > 0;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Check if has any sort options
|
|
172
|
-
*/
|
|
173
|
-
hasSort(): boolean {
|
|
174
|
-
return this.sortOptions.length > 0;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Check if has date range
|
|
179
|
-
*/
|
|
180
|
-
hasDateRange(): boolean {
|
|
181
|
-
return this.dateRange !== null;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Check if has pagination
|
|
186
|
-
*/
|
|
187
|
-
hasPagination(): boolean {
|
|
188
|
-
return this.pagination !== null;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Check if is empty (no options set)
|
|
193
|
-
*/
|
|
194
|
-
isEmpty(): boolean {
|
|
195
|
-
return !this.hasWhereClauses() &&
|
|
196
|
-
!this.hasSort() &&
|
|
197
|
-
!this.hasDateRange() &&
|
|
198
|
-
!this.hasPagination();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Validate query options
|
|
203
|
-
*/
|
|
204
|
-
validate(): { valid: boolean; errors: string[] } {
|
|
205
|
-
const errors: string[] = [];
|
|
206
|
-
|
|
207
|
-
// Validate where clauses
|
|
208
|
-
for (const clause of this.whereClauses) {
|
|
209
|
-
const clauseValidation = clause.validate();
|
|
210
|
-
if (!clauseValidation.valid) {
|
|
211
|
-
errors.push(...clauseValidation.errors);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Validate sort options
|
|
216
|
-
for (const sort of this.sortOptions) {
|
|
217
|
-
if (!sort.field || typeof sort.field !== 'string') {
|
|
218
|
-
errors.push('Sort field must be a non-empty string');
|
|
219
|
-
}
|
|
220
|
-
if (sort.direction !== 'asc' && sort.direction !== 'desc') {
|
|
221
|
-
errors.push('Sort direction must be "asc" or "desc"');
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// Validate date range
|
|
226
|
-
if (this.dateRange) {
|
|
227
|
-
if (!this.dateRange.field || typeof this.dateRange.field !== 'string') {
|
|
228
|
-
errors.push('Date range field must be a non-empty string');
|
|
229
|
-
}
|
|
230
|
-
if (this.dateRange.startDate && !(this.dateRange.startDate instanceof Date)) {
|
|
231
|
-
errors.push('Start date must be a Date instance');
|
|
232
|
-
}
|
|
233
|
-
if (this.dateRange.endDate && !(this.dateRange.endDate instanceof Date)) {
|
|
234
|
-
errors.push('End date must be a Date instance');
|
|
235
|
-
}
|
|
236
|
-
if (
|
|
237
|
-
this.dateRange.startDate &&
|
|
238
|
-
this.dateRange.endDate &&
|
|
239
|
-
this.dateRange.startDate > this.dateRange.endDate
|
|
240
|
-
) {
|
|
241
|
-
errors.push('Start date must be before end date');
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Validate pagination
|
|
246
|
-
if (this.pagination) {
|
|
247
|
-
if (this.pagination.limit !== undefined && (typeof this.pagination.limit !== 'number' || this.pagination.limit <= 0)) {
|
|
248
|
-
errors.push('Pagination limit must be a positive number');
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return {
|
|
253
|
-
valid: errors.length === 0,
|
|
254
|
-
errors,
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Convert to plain object (for serialization)
|
|
260
|
-
*/
|
|
261
|
-
toObject(): {
|
|
262
|
-
where: WhereClause[];
|
|
263
|
-
sort: SortOptions[];
|
|
264
|
-
dateRange: DateRangeOptions | null;
|
|
265
|
-
pagination: PaginationOptions | null;
|
|
266
|
-
} {
|
|
267
|
-
return {
|
|
268
|
-
where: [...this.whereClauses] as WhereClause[],
|
|
269
|
-
sort: [...this.sortOptions] as SortOptions[],
|
|
270
|
-
dateRange: this.dateRange,
|
|
271
|
-
pagination: this.pagination,
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Create from plain object
|
|
277
|
-
*/
|
|
278
|
-
static fromObject(obj: {
|
|
279
|
-
where?: Array<{ field: string; operator: WhereFilterOp; value: unknown }>;
|
|
280
|
-
sort?: SortOptions[];
|
|
281
|
-
dateRange?: DateRangeOptions;
|
|
282
|
-
pagination?: PaginationOptions;
|
|
283
|
-
}): QueryOptions {
|
|
284
|
-
return QueryOptions.create({
|
|
285
|
-
where: obj.where?.map(w => WhereClause.create(w.field, w.operator, w.value)) || [],
|
|
286
|
-
sort: obj.sort || [],
|
|
287
|
-
dateRange: obj.dateRange || null,
|
|
288
|
-
pagination: obj.pagination || null,
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
|
|
292
163
|
/**
|
|
293
164
|
* Clone with modifications
|
|
294
165
|
*/
|
|
@@ -299,10 +170,10 @@ export class QueryOptions {
|
|
|
299
170
|
pagination?: PaginationOptions | null;
|
|
300
171
|
}): QueryOptions {
|
|
301
172
|
return QueryOptions.create({
|
|
302
|
-
where: modifications.where ?? this.whereClauses,
|
|
303
|
-
sort: modifications.sort ?? this.sortOptions,
|
|
304
|
-
dateRange: modifications.dateRange ?? this.dateRange,
|
|
305
|
-
pagination: modifications.pagination ?? this.pagination,
|
|
173
|
+
where: modifications.where ?? [...this.whereClauses] as WhereClause[],
|
|
174
|
+
sort: modifications.sort ?? [...this.sortOptions] as SortOptions[],
|
|
175
|
+
dateRange: modifications.dateRange ?? this.dateRange ?? null,
|
|
176
|
+
pagination: modifications.pagination ?? this.pagination ?? null,
|
|
306
177
|
});
|
|
307
178
|
}
|
|
308
179
|
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query Options Serialization
|
|
3
|
+
* Single Responsibility: Handle serialization and conversion
|
|
4
|
+
*
|
|
5
|
+
* Serialization and conversion utilities for QueryOptions.
|
|
6
|
+
* Separated for better maintainability.
|
|
7
|
+
*
|
|
8
|
+
* Max lines: 150 (enforced for maintainability)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { QueryOptions, createQueryOptions } from './QueryOptions';
|
|
12
|
+
import { WhereClause } from './WhereClause';
|
|
13
|
+
import type { SortOptions, DateRangeOptions, PaginationOptions } from './QueryOptions';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Convert to plain object (for serialization)
|
|
17
|
+
*/
|
|
18
|
+
export function toObject(options: QueryOptions): {
|
|
19
|
+
where: WhereClause[];
|
|
20
|
+
sort: SortOptions[];
|
|
21
|
+
dateRange: DateRangeOptions | null;
|
|
22
|
+
pagination: PaginationOptions | null;
|
|
23
|
+
} {
|
|
24
|
+
return {
|
|
25
|
+
where: [...options.whereClauses],
|
|
26
|
+
sort: [...options.sortOptions],
|
|
27
|
+
dateRange: options.dateRange,
|
|
28
|
+
pagination: options.pagination,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create from plain object
|
|
34
|
+
*/
|
|
35
|
+
export function fromObject(obj: {
|
|
36
|
+
where?: Array<{ field: string; operator: string; value: unknown }>;
|
|
37
|
+
sort?: SortOptions[];
|
|
38
|
+
dateRange?: DateRangeOptions;
|
|
39
|
+
pagination?: PaginationOptions;
|
|
40
|
+
}): QueryOptions {
|
|
41
|
+
return QueryOptions.create({
|
|
42
|
+
where: obj.where?.map(w => WhereClause.create(w.field, w.operator as any, w.value)) || [],
|
|
43
|
+
sort: obj.sort || [],
|
|
44
|
+
dateRange: obj.dateRange || null,
|
|
45
|
+
pagination: obj.pagination || null,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Create from filters object (simplified)
|
|
51
|
+
*/
|
|
52
|
+
export function fromFilters(filters: Record<string, unknown>): QueryOptions {
|
|
53
|
+
const whereClauses = Object.entries(filters).map(([field, value]) =>
|
|
54
|
+
WhereClause.equals(field, value)
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
return QueryOptions.create({ where: whereClauses });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Merge multiple query options
|
|
62
|
+
*/
|
|
63
|
+
export function mergeOptions(...options: QueryOptions[]): QueryOptions {
|
|
64
|
+
if (options.length === 0) {
|
|
65
|
+
return createQueryOptions();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (options.length === 1) {
|
|
69
|
+
return options[0];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const merged = options.reduce((acc, opt) => {
|
|
73
|
+
return QueryOptions.create({
|
|
74
|
+
where: [...acc.whereClauses, ...opt.whereClauses] as WhereClause[],
|
|
75
|
+
sort: opt.sortOptions.length > 0 ? [...opt.sortOptions] as any[] : [...acc.sortOptions] as any[],
|
|
76
|
+
dateRange: opt.dateRange ?? acc.dateRange,
|
|
77
|
+
pagination: opt.pagination ?? acc.pagination,
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return merged ?? createQueryOptions();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Create paginated query options
|
|
86
|
+
*/
|
|
87
|
+
export function createPaginatedOptions(limit: number, cursor?: number): QueryOptions {
|
|
88
|
+
return createQueryOptions({
|
|
89
|
+
pagination: { limit, cursor },
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Create sorted query options
|
|
95
|
+
*/
|
|
96
|
+
export function createSortedOptions(
|
|
97
|
+
field: string,
|
|
98
|
+
direction: 'asc' | 'desc' = 'asc'
|
|
99
|
+
): QueryOptions {
|
|
100
|
+
return createQueryOptions({
|
|
101
|
+
sort: [{ field, direction }],
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Create date range query options
|
|
107
|
+
*/
|
|
108
|
+
export function createDateRangeOptions(
|
|
109
|
+
field: string,
|
|
110
|
+
startDate?: Date,
|
|
111
|
+
endDate?: Date
|
|
112
|
+
): QueryOptions {
|
|
113
|
+
return createQueryOptions({
|
|
114
|
+
dateRange: { field, startDate, endDate },
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Clone query options with safety checks
|
|
120
|
+
*/
|
|
121
|
+
export function cloneSafe(options: QueryOptions, defaults?: Partial<{
|
|
122
|
+
where: WhereClause[];
|
|
123
|
+
sort: SortOptions[];
|
|
124
|
+
dateRange: DateRangeOptions;
|
|
125
|
+
pagination: PaginationOptions;
|
|
126
|
+
}>): QueryOptions {
|
|
127
|
+
return QueryOptions.create({
|
|
128
|
+
where: options.whereClauses.length > 0 ? [...options.whereClauses] as WhereClause[] : defaults?.where || [],
|
|
129
|
+
sort: options.sortOptions.length > 0 ? [...options.sortOptions] as SortOptions[] : defaults?.sort || [],
|
|
130
|
+
dateRange: options.dateRange ?? defaults?.dateRange ?? null,
|
|
131
|
+
pagination: options.pagination ?? defaults?.pagination ?? null,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Strip pagination from options
|
|
137
|
+
*/
|
|
138
|
+
export function withoutPagination(options: QueryOptions): QueryOptions {
|
|
139
|
+
return QueryOptions.create({
|
|
140
|
+
where: [...options.whereClauses] as WhereClause[],
|
|
141
|
+
sort: [...options.sortOptions] as SortOptions[],
|
|
142
|
+
dateRange: options.dateRange,
|
|
143
|
+
pagination: null,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Strip sort from options
|
|
149
|
+
*/
|
|
150
|
+
export function withoutSort(options: QueryOptions): QueryOptions {
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
return QueryOptions.create({
|
|
2
|
+
where: [...options.whereClauses] as WhereClause[],
|
|
3
|
+
sort: [],
|
|
4
|
+
dateRange: options.dateRange,
|
|
5
|
+
pagination: options.pagination,
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Strip date range from options
|
|
11
|
+
*/
|
|
12
|
+
export function withoutDateRange(options: QueryOptions): QueryOptions {
|
|
13
|
+
return QueryOptions.create({
|
|
14
|
+
where: [...options.whereClauses] as WhereClause[],
|
|
15
|
+
sort: [...options.sortOptions] as SortOptions[],
|
|
16
|
+
dateRange: null,
|
|
17
|
+
pagination: options.pagination,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Strip where clauses from options
|
|
23
|
+
*/
|
|
24
|
+
export function withoutWhere(options: QueryOptions): QueryOptions {
|
|
25
|
+
return QueryOptions.create({
|
|
26
|
+
where: [],
|
|
27
|
+
sort: [...options.sortOptions] as SortOptions[],
|
|
28
|
+
dateRange: options.dateRange,
|
|
29
|
+
pagination: options.pagination,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Add limit to existing options
|
|
35
|
+
*/
|
|
36
|
+
export function withLimit(options: QueryOptions, limit: number): QueryOptions {
|
|
37
|
+
const currentPagination = options.pagination || {};
|
|
38
|
+
return QueryOptions.create({
|
|
39
|
+
where: [...options.whereClauses] as WhereClause[],
|
|
40
|
+
sort: [...options.sortOptions] as SortOptions[],
|
|
41
|
+
dateRange: options.dateRange,
|
|
42
|
+
pagination: { ...currentPagination, limit },
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Add cursor to existing options
|
|
48
|
+
*/
|
|
49
|
+
export function withCursor(options: QueryOptions, cursor: number): QueryOptions {
|
|
50
|
+
const currentPagination = options.pagination || {};
|
|
51
|
+
return QueryOptions.create({
|
|
52
|
+
where: [...options.whereClauses] as WhereClause[],
|
|
53
|
+
sort: [...options.sortOptions] as SortOptions[],
|
|
54
|
+
dateRange: options.dateRange,
|
|
55
|
+
pagination: { ...currentPagination, cursor },
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query Options Validation
|
|
3
|
+
* Single Responsibility: Validate and check query options
|
|
4
|
+
*
|
|
5
|
+
* Validation and type checking functionality for QueryOptions.
|
|
6
|
+
* Separated for better maintainability.
|
|
7
|
+
*
|
|
8
|
+
* Max lines: 150 (enforced for maintainability)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { QueryOptions } from './QueryOptions';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Validate query options
|
|
15
|
+
*/
|
|
16
|
+
export function validateOptions(options: QueryOptions): { valid: boolean; errors: string[] } {
|
|
17
|
+
const errors: string[] = [];
|
|
18
|
+
|
|
19
|
+
// Validate where clauses
|
|
20
|
+
for (const clause of options.whereClauses) {
|
|
21
|
+
const clauseValidation = clause.validate();
|
|
22
|
+
if (!clauseValidation.valid) {
|
|
23
|
+
errors.push(...clauseValidation.errors);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Validate sort options
|
|
28
|
+
for (const sort of options.sortOptions) {
|
|
29
|
+
if (!sort.field || typeof sort.field !== 'string') {
|
|
30
|
+
errors.push('Sort field must be a non-empty string');
|
|
31
|
+
}
|
|
32
|
+
if (sort.direction !== 'asc' && sort.direction !== 'desc') {
|
|
33
|
+
errors.push('Sort direction must be "asc" or "desc"');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Validate date range
|
|
38
|
+
if (options.dateRange) {
|
|
39
|
+
if (!options.dateRange.field || typeof options.dateRange.field !== 'string') {
|
|
40
|
+
errors.push('Date range field must be a non-empty string');
|
|
41
|
+
}
|
|
42
|
+
if (options.dateRange.startDate && !(options.dateRange.startDate instanceof Date)) {
|
|
43
|
+
errors.push('Start date must be a Date instance');
|
|
44
|
+
}
|
|
45
|
+
if (options.dateRange.endDate && !(options.dateRange.endDate instanceof Date)) {
|
|
46
|
+
errors.push('End date must be a Date instance');
|
|
47
|
+
}
|
|
48
|
+
if (
|
|
49
|
+
options.dateRange.startDate &&
|
|
50
|
+
options.dateRange.endDate &&
|
|
51
|
+
options.dateRange.startDate > options.dateRange.endDate
|
|
52
|
+
) {
|
|
53
|
+
errors.push('Start date must be before end date');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Validate pagination
|
|
58
|
+
if (options.pagination) {
|
|
59
|
+
if (options.pagination.limit !== undefined && (typeof options.pagination.limit !== 'number' || options.pagination.limit <= 0)) {
|
|
60
|
+
errors.push('Pagination limit must be a positive number');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
valid: errors.length === 0,
|
|
66
|
+
errors,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Check if has any where clauses
|
|
72
|
+
*/
|
|
73
|
+
export function hasWhereClauses(options: QueryOptions): boolean {
|
|
74
|
+
return options.whereClauses.length > 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Check if has any sort options
|
|
79
|
+
*/
|
|
80
|
+
export function hasSort(options: QueryOptions): boolean {
|
|
81
|
+
return options.sortOptions.length > 0;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Check if has date range
|
|
86
|
+
*/
|
|
87
|
+
export function hasDateRange(options: QueryOptions): boolean {
|
|
88
|
+
return options.dateRange !== null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Check if has pagination
|
|
93
|
+
*/
|
|
94
|
+
export function hasPagination(options: QueryOptions): boolean {
|
|
95
|
+
return options.pagination !== null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Check if is empty (no options set)
|
|
100
|
+
*/
|
|
101
|
+
export function isEmpty(options: QueryOptions): boolean {
|
|
102
|
+
return !hasWhereClauses(options) &&
|
|
103
|
+
!hasSort(options) &&
|
|
104
|
+
!hasDateRange(options) &&
|
|
105
|
+
!hasPagination(options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Check if options have date range filter
|
|
110
|
+
*/
|
|
111
|
+
export function hasDateFilter(options: QueryOptions): boolean {
|
|
112
|
+
return hasDateRange(options);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Check if options have limit set
|
|
117
|
+
*/
|
|
118
|
+
export function hasLimit(options: QueryOptions): boolean {
|
|
119
|
+
return options.pagination?.limit !== undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Check if options have cursor set
|
|
124
|
+
*/
|
|
125
|
+
export function hasCursor(options: QueryOptions): boolean {
|
|
126
|
+
return options.pagination?.cursor !== undefined;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Check if options are valid for compound queries
|
|
131
|
+
*/
|
|
132
|
+
export function isValidForCompoundQuery(options: QueryOptions): boolean {
|
|
133
|
+
// Check for array/membership operator conflicts
|
|
134
|
+
const arrayCount = options.whereClauses.filter(c => c.isArrayOperator() || c.isMembership()).length;
|
|
135
|
+
|
|
136
|
+
if (arrayCount > 1) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return validateOptions(options).valid;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Check if options are ready for execution
|
|
145
|
+
*/
|
|
146
|
+
export function isReadyToExecute(options: QueryOptions): boolean {
|
|
147
|
+
if (!validateOptions(options).valid) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
|