@umituz/react-native-firebase 2.6.1 → 2.6.3
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 +35 -8
- 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 +20 -68
- package/src/domains/firestore/domain/value-objects/QueryOptions.ts.bak +6 -135
- package/src/domains/firestore/domain/value-objects/QueryOptionsFactory.ts +95 -0
- package/src/domains/firestore/domain/value-objects/QueryOptionsHelpers.ts +110 -0
- 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_part_aa +150 -0
- package/src/domains/firestore/domain/value-objects/QueryOptions_part_ab +41 -0
- package/src/domains/firestore/domain/value-objects/WhereClause.ts +35 -205
- package/src/domains/firestore/domain/value-objects/WhereClause.ts.bak +299 -0
- package/src/domains/firestore/domain/value-objects/WhereClauseFactory.ts +44 -150
- package/src/domains/firestore/domain/value-objects/WhereClauseFactory.ts.bak +207 -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/WhereClauseHelpers.ts +123 -0
- package/src/domains/firestore/domain/value-objects/WhereClauseValidation.ts +83 -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/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
|
+
* Document Entity
|
|
3
|
+
* Single Responsibility: Represent a Firestore document with metadata
|
|
4
|
+
*
|
|
5
|
+
* Domain entity that encapsulates document data and metadata.
|
|
6
|
+
* Provides business logic for document operations.
|
|
7
|
+
*
|
|
8
|
+
* Max lines: 150 (enforced for maintainability)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { DocumentData, DocumentSnapshot, Timestamp } from 'firebase/firestore';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Document metadata
|
|
15
|
+
*/
|
|
16
|
+
export interface DocumentMetadata {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly createdAt: Timestamp | null;
|
|
19
|
+
readonly updatedAt: Timestamp | null;
|
|
20
|
+
readonly exists: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Document entity
|
|
25
|
+
* Represents a Firestore document with data and metadata
|
|
26
|
+
*/
|
|
27
|
+
export class Document<T extends DocumentData = DocumentData> {
|
|
28
|
+
readonly id: string;
|
|
29
|
+
readonly data: T | null;
|
|
30
|
+
readonly metadata: DocumentMetadata;
|
|
31
|
+
readonly path: string;
|
|
32
|
+
|
|
33
|
+
constructor(snapshot: DocumentSnapshot<T>, path: string) {
|
|
34
|
+
this.id = snapshot.id;
|
|
35
|
+
this.data = snapshot.data() || null;
|
|
36
|
+
this.path = path;
|
|
37
|
+
this.metadata = {
|
|
38
|
+
id: snapshot.id,
|
|
39
|
+
createdAt: this.extractTimestamp(snapshot, 'createdAt'),
|
|
40
|
+
updatedAt: this.extractTimestamp(snapshot, 'updatedAt'),
|
|
41
|
+
exists: snapshot.exists(),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Create document from snapshot with custom path
|
|
47
|
+
*/
|
|
48
|
+
static fromSnapshot<T extends DocumentData>(
|
|
49
|
+
snapshot: DocumentSnapshot<T>,
|
|
50
|
+
path: string
|
|
51
|
+
): Document<T> {
|
|
52
|
+
return new Document(snapshot, path);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Check if document exists
|
|
57
|
+
*/
|
|
58
|
+
exists(): boolean {
|
|
59
|
+
return this.metadata.exists && this.data !== null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get field value from document data
|
|
64
|
+
*/
|
|
65
|
+
getField<K extends keyof T>(field: K): T[K] | null {
|
|
66
|
+
return this.data?.[field] ?? null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Check if document has a specific field
|
|
71
|
+
*/
|
|
72
|
+
hasField<K extends keyof T>(field: K): boolean {
|
|
73
|
+
return this.data !== null && field in this.data;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Get document age in milliseconds
|
|
78
|
+
* Returns null if createdAt is not set
|
|
79
|
+
*/
|
|
80
|
+
getAge(): number | null {
|
|
81
|
+
if (!this.metadata.createdAt) return null;
|
|
82
|
+
return Date.now() - this.metadata.createdAt.toMillis();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Check if document is recent (created within specified milliseconds)
|
|
87
|
+
*/
|
|
88
|
+
isRecent(maxAgeMs: number): boolean {
|
|
89
|
+
const age = this.getAge();
|
|
90
|
+
return age !== null && age <= maxAgeMs;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Check if document was updated after creation (modified)
|
|
95
|
+
*/
|
|
96
|
+
isModified(): boolean {
|
|
97
|
+
if (!this.metadata.createdAt || !this.metadata.updatedAt) return false;
|
|
98
|
+
return this.metadata.updatedAt.toMillis() > this.metadata.createdAt.toMillis();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Get time since last update in milliseconds
|
|
103
|
+
* Returns null if updatedAt is not set
|
|
104
|
+
*/
|
|
105
|
+
getTimeSinceUpdate(): number | null {
|
|
106
|
+
if (!this.metadata.updatedAt) return null;
|
|
107
|
+
return Date.now() - this.metadata.updatedAt.toMillis();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Check if document is stale (not updated within specified milliseconds)
|
|
112
|
+
*/
|
|
113
|
+
isStale(maxStaleMs: number): boolean {
|
|
114
|
+
const timeSinceUpdate = this.getTimeSinceUpdate();
|
|
115
|
+
return timeSinceUpdate !== null && timeSinceUpdate > maxStaleMs;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Convert to plain object (for serialization)
|
|
120
|
+
*/
|
|
121
|
+
toObject(): { id: string; data: T | null; metadata: DocumentMetadata } {
|
|
122
|
+
return {
|
|
123
|
+
id: this.id,
|
|
124
|
+
data: this.data,
|
|
125
|
+
metadata: this.metadata,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Extract timestamp field from document snapshot
|
|
131
|
+
*/
|
|
132
|
+
private extractTimestamp(
|
|
133
|
+
snapshot: DocumentSnapshot<T>,
|
|
134
|
+
field: string
|
|
135
|
+
): Timestamp | null {
|
|
136
|
+
const data = snapshot.data();
|
|
137
|
+
if (!data) return null;
|
|
138
|
+
|
|
139
|
+
const value = data[field as keyof T];
|
|
140
|
+
if (value && typeof value === 'object' && 'toDate' in value && typeof value.toDate === 'function') {
|
|
141
|
+
return value as unknown as Timestamp;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Create a new document with updated data
|
|
149
|
+
* Useful for immutability patterns
|
|
150
|
+
*/
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
withData<K extends keyof T>(field: K, value: T[K]): Document<T> {
|
|
2
|
+
if (!this.data) {
|
|
3
|
+
throw new Error('Cannot update null document data');
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const newData = { ...this.data, [field]: value };
|
|
7
|
+
return new Document(
|
|
8
|
+
{
|
|
9
|
+
id: this.id,
|
|
10
|
+
exists: true,
|
|
11
|
+
data() {
|
|
12
|
+
return newData;
|
|
13
|
+
},
|
|
14
|
+
} as unknown as DocumentSnapshot<T>,
|
|
15
|
+
this.path
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Validate document data against required fields
|
|
21
|
+
*/
|
|
22
|
+
validateRequiredFields(requiredFields: (keyof T)[]): { valid: boolean; missing: (keyof T)[] } {
|
|
23
|
+
if (!this.data) {
|
|
24
|
+
return { valid: false, missing: requiredFields };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const missing = requiredFields.filter(field => !this.hasField(field));
|
|
28
|
+
return {
|
|
29
|
+
valid: missing.length === 0,
|
|
30
|
+
missing,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Check if document matches filter criteria
|
|
36
|
+
*/
|
|
37
|
+
matches(filter: Partial<T>): boolean {
|
|
38
|
+
if (!this.data) return false;
|
|
39
|
+
|
|
40
|
+
return Object.entries(filter).every(([key, value]) => {
|
|
41
|
+
const fieldValue = this.data![key as keyof T];
|
|
42
|
+
return fieldValue === value;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Check if document matches filter criteria
|
|
48
|
+
*/
|
|
49
|
+
matches(filter: Partial<T>): boolean {
|
|
50
|
+
if (!this.data) return false;
|
|
51
|
+
|
|
52
|
+
return Object.entries(filter).every(([key, value]) => {
|
|
53
|
+
return this.data[key as keyof T] === value;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Get document size in bytes (approximate)
|
|
59
|
+
* Useful for quota management
|
|
60
|
+
*/
|
|
61
|
+
getSize(): number {
|
|
62
|
+
if (!this.data) return 0;
|
|
63
|
+
|
|
64
|
+
return JSON.stringify(this.data).length;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Check if document size exceeds limit
|
|
69
|
+
*/
|
|
70
|
+
exceedsSizeLimit(maxBytes: number): boolean {
|
|
71
|
+
return this.getSize() > maxBytes;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Factory function to create document entity
|
|
77
|
+
*/
|
|
78
|
+
export function createDocument<T extends DocumentData>(
|
|
79
|
+
snapshot: DocumentSnapshot<T>,
|
|
80
|
+
path: string
|
|
81
|
+
): Document<T> {
|
|
82
|
+
return Document.fromSnapshot(snapshot, path);
|
|
83
|
+
}
|
|
@@ -14,17 +14,44 @@ export {
|
|
|
14
14
|
} from './errors/FirebaseFirestoreError';
|
|
15
15
|
|
|
16
16
|
// Domain Entities
|
|
17
|
-
export {
|
|
18
|
-
export { Collection, createCollection } from './entities/Collection';
|
|
19
|
-
export type { DocumentMetadata } from './entities/Document';
|
|
17
|
+
export { Collection, createCollection, fromReference, fromQuery } from './entities/Collection';
|
|
20
18
|
export type { CollectionMetadata } from './entities/Collection';
|
|
21
19
|
|
|
20
|
+
// Collection Entity Helpers
|
|
21
|
+
export {
|
|
22
|
+
isParentOf,
|
|
23
|
+
getCollectionId,
|
|
24
|
+
collectionsEqual,
|
|
25
|
+
getCollectionInfo,
|
|
26
|
+
collectionFromPath,
|
|
27
|
+
getSubCollectionNames,
|
|
28
|
+
buildCollectionPath,
|
|
29
|
+
parseCollectionPath,
|
|
30
|
+
} from './entities/CollectionHelpers';
|
|
31
|
+
|
|
32
|
+
// Collection Utilities
|
|
33
|
+
export {
|
|
34
|
+
getCollectionDepth,
|
|
35
|
+
collectionToObject,
|
|
36
|
+
isQueryReference,
|
|
37
|
+
} from './entities/CollectionUtils';
|
|
38
|
+
|
|
39
|
+
// Collection Validation
|
|
40
|
+
export {
|
|
41
|
+
isValidCollectionName,
|
|
42
|
+
isValidCollectionPath,
|
|
43
|
+
isValidCollection,
|
|
44
|
+
extractCollectionNameFromPath,
|
|
45
|
+
extractParentCollectionPath,
|
|
46
|
+
isDocumentPath,
|
|
47
|
+
isCollectionPath,
|
|
48
|
+
isUserCollectionPath,
|
|
49
|
+
extractUserIdFromPath,
|
|
50
|
+
createSubCollectionPath,
|
|
51
|
+
} from './entities/CollectionValidation';
|
|
52
|
+
|
|
22
53
|
// Domain Value Objects
|
|
23
54
|
export { QueryOptions, createQueryOptions } from './value-objects/QueryOptions';
|
|
24
|
-
export { WhereClause,
|
|
55
|
+
export { WhereClause, equals, where, create as createWhereClause } from './value-objects/WhereClause';
|
|
25
56
|
export type { SortOptions, DateRangeOptions, PaginationOptions } from './value-objects/QueryOptions';
|
|
26
57
|
export type { WhereOperator } from './value-objects/WhereClause';
|
|
27
|
-
|
|
28
|
-
// Domain Services
|
|
29
|
-
export { QueryService, createQueryService } from './services/QueryService';
|
|
30
|
-
export type { QueryBuilderResult } from './services/QueryService';
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query Service Analysis
|
|
3
|
+
* Single Responsibility: Analyze query complexity and metadata
|
|
4
|
+
*
|
|
5
|
+
* Provides analysis functionality for query options.
|
|
6
|
+
* Extracts field information and calculates complexity scores.
|
|
7
|
+
*
|
|
8
|
+
* Max lines: 150 (enforced for maintainability)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { QueryOptions } from '../value-objects/QueryOptions';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Extract field names from query options
|
|
15
|
+
*/
|
|
16
|
+
export function extractFields(options: QueryOptions): string[] {
|
|
17
|
+
const fields = new Set<string>();
|
|
18
|
+
|
|
19
|
+
for (const clause of options.whereClauses) {
|
|
20
|
+
fields.add(clause.field);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
for (const sort of options.sortOptions) {
|
|
24
|
+
fields.add(sort.field);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (options.dateRange) {
|
|
28
|
+
fields.add(options.dateRange.field);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return Array.from(fields);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Check if query uses array operators
|
|
36
|
+
*/
|
|
37
|
+
export function hasArrayOperators(options: QueryOptions): boolean {
|
|
38
|
+
return options.whereClauses.some(clause => clause.isArrayOperator());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Check if query uses membership operators
|
|
43
|
+
*/
|
|
44
|
+
export function hasMembershipOperators(options: QueryOptions): boolean {
|
|
45
|
+
return options.whereClauses.some(clause => clause.isMembership());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Get query complexity score
|
|
50
|
+
* Higher score = more complex query
|
|
51
|
+
*/
|
|
52
|
+
export function getComplexityScore(options: QueryOptions): number {
|
|
53
|
+
let score = 0;
|
|
54
|
+
|
|
55
|
+
score += options.whereClauses.length * 1;
|
|
56
|
+
score += options.sortOptions.length * 2;
|
|
57
|
+
score += options.dateRange ? 3 : 0;
|
|
58
|
+
score += options.pagination?.limit ? 1 : 0;
|
|
59
|
+
|
|
60
|
+
// Array and membership operators add complexity
|
|
61
|
+
if (hasArrayOperators(options)) score += 5;
|
|
62
|
+
if (hasMembershipOperators(options)) score += 5;
|
|
63
|
+
|
|
64
|
+
return score;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Check if query is too complex
|
|
69
|
+
*/
|
|
70
|
+
export function isTooComplex(options: QueryOptions, maxScore: number = 20): boolean {
|
|
71
|
+
return getComplexityScore(options) > maxScore;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Get query statistics
|
|
76
|
+
*/
|
|
77
|
+
export function getQueryStats(options: QueryOptions): {
|
|
78
|
+
readonly whereClauseCount: number;
|
|
79
|
+
readonly sortOptionCount: number;
|
|
80
|
+
readonly hasDateRange: boolean;
|
|
81
|
+
readonly hasPagination: boolean;
|
|
82
|
+
readonly complexityScore: number;
|
|
83
|
+
readonly fieldCount: number;
|
|
84
|
+
} {
|
|
85
|
+
return {
|
|
86
|
+
whereClauseCount: options.whereClauses.length,
|
|
87
|
+
sortOptionCount: options.sortOptions.length,
|
|
88
|
+
hasDateRange: options.hasDateRange(),
|
|
89
|
+
hasPagination: options.hasPagination(),
|
|
90
|
+
complexityScore: getComplexityScore(options),
|
|
91
|
+
fieldCount: extractFields(options).length,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Check if query is simple (low complexity)
|
|
97
|
+
*/
|
|
98
|
+
export function isSimpleQuery(options: QueryOptions, maxScore: number = 5): boolean {
|
|
99
|
+
return getComplexityScore(options) <= maxScore;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Check if query is moderate complexity
|
|
104
|
+
*/
|
|
105
|
+
export function isModerateQuery(options: QueryOptions): boolean {
|
|
106
|
+
const score = getComplexityScore(options);
|
|
107
|
+
return score > 5 && score <= 15;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Check if query is high complexity
|
|
112
|
+
*/
|
|
113
|
+
export function isComplexQuery(options: QueryOptions): boolean {
|
|
114
|
+
return getComplexityScore(options) > 15;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Get query complexity level
|
|
119
|
+
*/
|
|
120
|
+
export function getComplexityLevel(options: QueryOptions): 'simple' | 'moderate' | 'complex' {
|
|
121
|
+
if (isSimpleQuery(options)) {
|
|
122
|
+
return 'simple';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (isModerateQuery(options)) {
|
|
126
|
+
return 'moderate';
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return 'complex';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Compare two queries by complexity
|
|
134
|
+
*/
|
|
135
|
+
export function compareComplexity(options1: QueryOptions, options2: QueryOptions): number {
|
|
136
|
+
const score1 = getComplexityScore(options1);
|
|
137
|
+
const score2 = getComplexityScore(options2);
|
|
138
|
+
|
|
139
|
+
return score1 - score2;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Get query performance hints
|
|
144
|
+
*/
|
|
145
|
+
export function getPerformanceHints(options: QueryOptions): string[] {
|
|
146
|
+
const hints: string[] = [];
|
|
147
|
+
|
|
148
|
+
if (hasArrayOperators(options)) {
|
|
149
|
+
hints.push('Array operators can impact performance');
|
|
150
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
if (hasMembershipOperators(options)) {
|
|
3
|
+
hints.push('Membership operators (in, not-in) are limited to 10 values');
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (options.whereClauses.length > 5) {
|
|
7
|
+
hints.push('Consider reducing number of where clauses');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (!options.pagination?.limit && options.sortOptions.length > 0) {
|
|
11
|
+
hints.push('Add limit to prevent full collection scans');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (options.dateRange && !options.sortOptions.some(s => s.field === options.dateRange?.field)) {
|
|
15
|
+
hints.push('Add sort on date range field for better performance');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return hints;
|
|
19
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query Service Helpers
|
|
3
|
+
* Single Responsibility: Provide helper methods for query operations
|
|
4
|
+
*
|
|
5
|
+
* Helper methods for query options manipulation and creation.
|
|
6
|
+
* Separated from main QueryService for better maintainability.
|
|
7
|
+
*
|
|
8
|
+
* Max lines: 150 (enforced for maintainability)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Query } from 'firebase/firestore';
|
|
12
|
+
import { QueryOptions, createQueryOptions } from '../value-objects/QueryOptions';
|
|
13
|
+
import { WhereClause } from '../value-objects/WhereClause';
|
|
14
|
+
import type { Collection } from '../entities/Collection';
|
|
15
|
+
import type { QueryBuilderResult } from './QueryService';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Create query options from filters object
|
|
19
|
+
*/
|
|
20
|
+
export function createOptionsFromFilters(filters: Record<string, unknown>): QueryOptions {
|
|
21
|
+
const whereClauses = Object.entries(filters).map(([field, value]) =>
|
|
22
|
+
WhereClause.equals(field, value)
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return createQueryOptions({ where: whereClauses });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Merge multiple query options
|
|
30
|
+
* Later options override earlier ones for conflicting settings
|
|
31
|
+
*/
|
|
32
|
+
export function mergeOptions(...options: QueryOptions[]): QueryOptions {
|
|
33
|
+
if (options.length === 0) {
|
|
34
|
+
return createQueryOptions();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (options.length === 1) {
|
|
38
|
+
return options[0];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const merged = options.reduce((acc, opt) => {
|
|
42
|
+
return QueryOptions.create({
|
|
43
|
+
where: [...acc.whereClauses, ...opt.whereClauses] as WhereClause[],
|
|
44
|
+
sort: opt.sortOptions.length > 0 ? [...opt.sortOptions] as any[] : [...acc.sortOptions] as any[],
|
|
45
|
+
dateRange: opt.dateRange ?? acc.dateRange,
|
|
46
|
+
pagination: opt.pagination ?? acc.pagination,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return merged ?? createQueryOptions();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Create paginated query options
|
|
55
|
+
*/
|
|
56
|
+
export function createPaginatedOptions(limit: number, cursor?: number): QueryOptions {
|
|
57
|
+
return createQueryOptions({
|
|
58
|
+
pagination: { limit, cursor },
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Create sorted query options
|
|
64
|
+
*/
|
|
65
|
+
export function createSortedOptions(
|
|
66
|
+
field: string,
|
|
67
|
+
direction: 'asc' | 'desc' = 'asc'
|
|
68
|
+
): QueryOptions {
|
|
69
|
+
return createQueryOptions({
|
|
70
|
+
sort: [{ field, direction }],
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Optimize query options
|
|
76
|
+
* Removes redundant clauses and sorts
|
|
77
|
+
*/
|
|
78
|
+
export function optimizeOptions(options: QueryOptions): QueryOptions {
|
|
79
|
+
// Remove duplicate where clauses
|
|
80
|
+
const uniqueWheres = options.whereClauses.filter((clause, index, self) =>
|
|
81
|
+
index === self.findIndex(c => c.equals(clause))
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
// Remove duplicate sorts
|
|
85
|
+
const uniqueSorts = options.sortOptions.filter((sort, index, self) =>
|
|
86
|
+
index === self.findIndex(s => s.field === sort.field && s.direction === sort.direction)
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return QueryOptions.create({
|
|
90
|
+
where: uniqueWheres,
|
|
91
|
+
sort: uniqueSorts,
|
|
92
|
+
dateRange: options.dateRange,
|
|
93
|
+
pagination: options.pagination,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Build query from collection entity
|
|
99
|
+
*/
|
|
100
|
+
export function buildQueryFromCollection(
|
|
101
|
+
collection: Collection,
|
|
102
|
+
options: QueryOptions,
|
|
103
|
+
queryService: any
|
|
104
|
+
): QueryBuilderResult {
|
|
105
|
+
return queryService.buildQuery(collection.getPath(), options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get query description for logging
|
|
110
|
+
*/
|
|
111
|
+
export function describeQuery(options: QueryOptions): string {
|
|
112
|
+
const parts: string[] = [];
|
|
113
|
+
|
|
114
|
+
if (options.hasWhereClauses()) {
|
|
115
|
+
parts.push(`where: ${options.whereClauses.length} clause(s)`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (options.hasSort()) {
|
|
119
|
+
parts.push(`sort: ${options.sortOptions.length} field(s)`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (options.hasDateRange()) {
|
|
123
|
+
parts.push('date range: yes');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (options.hasPagination()) {
|
|
127
|
+
parts.push(`limit: ${options.pagination?.limit || 'none'}`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return parts.length > 0 ? parts.join(', ') : 'no filters';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Clone query options with modifications
|
|
135
|
+
*/
|
|
136
|
+
export function cloneOptions(
|
|
137
|
+
base: QueryOptions,
|
|
138
|
+
modifications: {
|
|
139
|
+
where?: WhereClause[];
|
|
140
|
+
sort?: Array<{ field: string; direction: 'asc' | 'desc' }>;
|
|
141
|
+
dateRange?: { field: string; startDate?: Date; endDate?: Date };
|
|
142
|
+
pagination?: { limit?: number; cursor?: number };
|
|
143
|
+
}
|
|
144
|
+
): QueryOptions {
|
|
145
|
+
return QueryOptions.create({
|
|
146
|
+
where: modifications.where ?? [...base.whereClauses] as WhereClause[],
|
|
147
|
+
sort: modifications.sort ?? [...base.sortOptions] as any[],
|
|
148
|
+
dateRange: modifications.dateRange ?? base.dateRange ?? undefined,
|
|
149
|
+
pagination: modifications.pagination ?? base.pagination ?? undefined,
|
|
150
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
}
|