@dereekb/firebase 11.1.8 → 12.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/index.cjs.js +3748 -2018
- package/index.esm.d.ts +1 -0
- package/index.esm.js +3672 -3107
- package/package.json +7 -6
- package/src/lib/common/firestore/accessor/accessor.batch.d.ts +12 -0
- package/src/lib/common/firestore/accessor/accessor.d.ts +90 -30
- package/src/lib/common/firestore/accessor/accessor.default.d.ts +14 -0
- package/src/lib/common/firestore/accessor/accessor.transaction.d.ts +15 -0
- package/src/lib/common/firestore/accessor/accessor.wrap.d.ts +107 -2
- package/src/lib/common/firestore/accessor/context.batch.d.ts +31 -0
- package/src/lib/common/firestore/accessor/context.d.ts +28 -5
- package/src/lib/common/firestore/accessor/context.default.d.ts +21 -0
- package/src/lib/common/firestore/accessor/context.transaction.d.ts +31 -0
- package/src/lib/common/firestore/accessor/converter.d.ts +28 -3
- package/src/lib/common/firestore/accessor/document.rxjs.d.ts +37 -0
- package/src/lib/common/firestore/accessor/document.utility.d.ts +129 -8
- package/src/lib/common/firestore/collection/collection.d.ts +116 -8
- package/src/lib/common/firestore/collection/collection.group.d.ts +38 -3
- package/src/lib/common/firestore/collection/collection.key.d.ts +62 -2
- package/src/lib/common/firestore/collection/collection.query.d.ts +168 -8
- package/src/lib/common/firestore/collection/collection.single.d.ts +33 -0
- package/src/lib/common/firestore/collection/subcollection.d.ts +70 -5
- package/src/lib/common/firestore/collection/subcollection.single.d.ts +40 -0
- package/src/lib/common/firestore/context.d.ts +154 -5
- package/src/lib/common/firestore/query/accumulator.d.ts +78 -5
- package/src/lib/common/firestore/query/constraint.d.ts +401 -15
- package/src/lib/common/firestore/query/constraint.template.d.ts +133 -40
- package/src/lib/common/firestore/query/iterator.d.ts +200 -14
- package/src/lib/common/firestore/query/query.d.ts +120 -11
- package/src/lib/common/firestore/query/query.iterate.array.d.ts +113 -9
- package/src/lib/common/firestore/query/query.iterate.d.ts +136 -13
- package/src/lib/common/firestore/query/query.util.d.ts +74 -7
- package/src/lib/common/firestore/query/watcher.d.ts +133 -2
- package/src/lib/common/firestore/snapshot/snapshot.d.ts +26 -0
- package/src/lib/common/firestore/snapshot/snapshot.field.d.ts +513 -3
- package/src/lib/common/firestore/snapshot/snapshot.type.d.ts +86 -5
- package/src/lib/common/model/function.d.ts +1 -1
- package/src/lib/common/storage/driver/accessor.d.ts +0 -1
- package/src/lib/common/storage/types.d.ts +0 -1
- package/test/CHANGELOG.md +9 -0
- package/test/package.json +3 -2
- package/test/src/lib/client/firebase.authorized.d.ts +2 -2
- package/test/src/lib/client/firebase.authorized.js.map +1 -1
- package/test/src/lib/client/firebase.d.ts +13 -13
- package/test/src/lib/client/firebase.js +4 -4
- package/test/src/lib/client/firebase.js.map +1 -1
- package/test/src/lib/client/firestore.mock.item.fixture.authorized.d.ts +2 -2
- package/test/src/lib/common/firebase.instance.d.ts +6 -6
- package/test/src/lib/common/firebase.instance.js.map +1 -1
- package/test/src/lib/common/firestore/firestore.d.ts +1 -1
- package/test/src/lib/common/firestore/firestore.instance.d.ts +3 -3
- package/test/src/lib/common/firestore/firestore.instance.js.map +1 -1
- package/test/src/lib/common/firestore/firestore.js +3 -4
- package/test/src/lib/common/firestore/firestore.js.map +1 -1
- package/test/src/lib/common/firestore/test.driver.accessor.d.ts +3 -3
- package/test/src/lib/common/firestore/test.driver.accessor.js +5 -6
- package/test/src/lib/common/firestore/test.driver.accessor.js.map +1 -1
- package/test/src/lib/common/firestore/test.driver.query.d.ts +1 -1
- package/test/src/lib/common/firestore/test.driver.query.js +14 -10
- package/test/src/lib/common/firestore/test.driver.query.js.map +1 -1
- package/test/src/lib/common/firestore/test.iterator.d.ts +1 -1
- package/test/src/lib/common/firestore/test.iterator.js +1 -2
- package/test/src/lib/common/firestore/test.iterator.js.map +1 -1
- package/test/src/lib/common/mock/mock.item.collection.fixture.d.ts +5 -5
- package/test/src/lib/common/mock/mock.item.collection.fixture.js +2 -2
- package/test/src/lib/common/mock/mock.item.collection.fixture.js.map +1 -1
- package/test/src/lib/common/mock/mock.item.d.ts +5 -5
- package/test/src/lib/common/mock/mock.item.id.d.ts +1 -1
- package/test/src/lib/common/mock/mock.item.js +19 -19
- package/test/src/lib/common/mock/mock.item.js.map +1 -1
- package/test/src/lib/common/mock/mock.item.query.d.ts +2 -2
- package/test/src/lib/common/mock/mock.item.query.js +3 -4
- package/test/src/lib/common/mock/mock.item.query.js.map +1 -1
- package/test/src/lib/common/mock/mock.item.service.d.ts +3 -3
- package/test/src/lib/common/mock/mock.item.service.js +2 -2
- package/test/src/lib/common/mock/mock.item.service.js.map +1 -1
- package/test/src/lib/common/mock/mock.item.storage.fixture.d.ts +4 -4
- package/test/src/lib/common/mock/mock.item.storage.fixture.js +2 -2
- package/test/src/lib/common/mock/mock.item.storage.fixture.js.map +1 -1
- package/test/src/lib/common/storage/storage.d.ts +1 -1
- package/test/src/lib/common/storage/storage.instance.d.ts +3 -3
- package/test/src/lib/common/storage/storage.instance.js.map +1 -1
- package/test/src/lib/common/storage/storage.js +2 -3
- package/test/src/lib/common/storage/storage.js.map +1 -1
- package/test/src/lib/common/storage/test.driver.accessor.d.ts +1 -1
- package/test/src/lib/common/storage/test.driver.accessor.js +1 -2
- package/test/src/lib/common/storage/test.driver.accessor.js.map +1 -1
|
@@ -1,53 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Firestore Query
|
|
3
|
+
*
|
|
4
|
+
* This module provides core functionality for creating and executing Firestore queries,
|
|
5
|
+
* with a fluent interface for extending queries with additional constraints.
|
|
6
|
+
*/
|
|
1
7
|
import { type Observable } from 'rxjs';
|
|
2
8
|
import { type ArrayOrValue, type Maybe } from '@dereekb/util';
|
|
3
9
|
import { type QueryLikeReferenceRef } from '../reference';
|
|
4
10
|
import { type Query, type QueryDocumentSnapshot, type QuerySnapshot, type Transaction } from '../types';
|
|
5
11
|
import { type FirestoreQueryConstraint } from './constraint';
|
|
6
12
|
import { type FirestoreQueryDriverRef } from '../driver/query';
|
|
13
|
+
/**
|
|
14
|
+
* Context for executing a Firestore query.
|
|
15
|
+
*
|
|
16
|
+
* This interface allows specifying optional parameters when executing a query,
|
|
17
|
+
* such as a transaction to run the query within.
|
|
18
|
+
*/
|
|
7
19
|
export interface FirestoreExecutableQueryGetDocsContext {
|
|
20
|
+
/**
|
|
21
|
+
* Optional transaction to run the query within.
|
|
22
|
+
*
|
|
23
|
+
* When provided, the query will be executed as part of the transaction,
|
|
24
|
+
* ensuring consistency with other operations in the same transaction.
|
|
25
|
+
*/
|
|
8
26
|
readonly transaction?: Transaction;
|
|
9
27
|
}
|
|
10
28
|
/**
|
|
11
|
-
* Immutable wrapper of a query
|
|
29
|
+
* Immutable wrapper of a Firestore query with methods to execute and extend it.
|
|
30
|
+
*
|
|
31
|
+
* This interface provides a fluent API for working with Firestore queries,
|
|
32
|
+
* allowing them to be executed in various ways (count, get, stream) and
|
|
33
|
+
* extended with additional constraints through the filter method.
|
|
34
|
+
*
|
|
35
|
+
* @template T - The document data type in the query results
|
|
12
36
|
*/
|
|
13
37
|
export interface FirestoreExecutableQuery<T> {
|
|
38
|
+
/**
|
|
39
|
+
* The underlying Firestore query object.
|
|
40
|
+
*/
|
|
14
41
|
readonly query: Query<T>;
|
|
15
42
|
/**
|
|
16
|
-
* Returns the
|
|
43
|
+
* Returns the count of documents that match the query.
|
|
44
|
+
*
|
|
45
|
+
* This method efficiently counts matching documents without retrieving their contents.
|
|
46
|
+
*
|
|
47
|
+
* @returns A promise that resolves with the number of matching documents
|
|
17
48
|
*/
|
|
18
49
|
countDocs(): Promise<number>;
|
|
19
50
|
/**
|
|
20
|
-
*
|
|
51
|
+
* Retrieves the first document matching the query, if any exists.
|
|
52
|
+
*
|
|
53
|
+
* This method optimizes the query by adding a limit(1) constraint before execution.
|
|
54
|
+
*
|
|
55
|
+
* @param transaction - Optional transaction to execute the query within
|
|
56
|
+
* @returns A promise that resolves with the first matching document snapshot, or undefined if none exist
|
|
21
57
|
*/
|
|
22
58
|
getFirstDoc(transaction?: Transaction): Promise<Maybe<QueryDocumentSnapshot<T>>>;
|
|
23
59
|
/**
|
|
24
|
-
*
|
|
60
|
+
* Executes the query and returns all matching documents.
|
|
61
|
+
*
|
|
62
|
+
* @param transaction - Optional transaction to execute the query within
|
|
63
|
+
* @returns A promise that resolves with a snapshot containing all matching documents
|
|
25
64
|
*/
|
|
26
65
|
getDocs(transaction?: Transaction): Promise<QuerySnapshot<T>>;
|
|
27
66
|
/**
|
|
28
|
-
*
|
|
67
|
+
* Creates an Observable that streams query results in real-time.
|
|
68
|
+
*
|
|
69
|
+
* This method establishes a listener that emits a new snapshot whenever
|
|
70
|
+
* any of the matching documents change.
|
|
71
|
+
*
|
|
72
|
+
* @returns An Observable that emits query snapshots when documents change
|
|
29
73
|
*/
|
|
30
74
|
streamDocs(): Observable<QuerySnapshot<T>>;
|
|
31
75
|
/**
|
|
32
|
-
*
|
|
76
|
+
* Creates a new query by adding more constraints to this query.
|
|
33
77
|
*
|
|
34
|
-
*
|
|
78
|
+
* This method creates a new FirestoreExecutableQuery with the additional constraints
|
|
79
|
+
* applied, preserving the immutability of the original query.
|
|
80
|
+
*
|
|
81
|
+
* @param queryConstraints - Additional constraints to apply to the query
|
|
82
|
+
* @returns A new FirestoreExecutableQuery with the combined constraints
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* const baseQuery = queryFactory.query();
|
|
86
|
+
* const filteredQuery = baseQuery.filter(
|
|
87
|
+
* where('status', '==', 'active'),
|
|
88
|
+
* orderBy('createdAt', 'desc')
|
|
89
|
+
* );
|
|
35
90
|
*/
|
|
36
91
|
filter(...queryConstraints: ArrayOrValue<FirestoreQueryConstraint>[]): FirestoreExecutableQuery<T>;
|
|
37
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Function that creates a FirestoreExecutableQuery with the specified constraints.
|
|
95
|
+
*
|
|
96
|
+
* This type represents a factory function that creates query instances with a consistent
|
|
97
|
+
* base query (typically a collection or collection group query) and applies the provided
|
|
98
|
+
* constraints.
|
|
99
|
+
*
|
|
100
|
+
* @template T - The document data type in the query results
|
|
101
|
+
*/
|
|
38
102
|
export type FirestoreQueryFactoryFunction<T> = (...queryConstraints: ArrayOrValue<FirestoreQueryConstraint>[]) => FirestoreExecutableQuery<T>;
|
|
103
|
+
/**
|
|
104
|
+
* Factory for creating Firestore queries with consistent base configuration.
|
|
105
|
+
*
|
|
106
|
+
* This interface provides a method for creating executable queries against a specific
|
|
107
|
+
* collection or query reference, with various constraints applied.
|
|
108
|
+
*
|
|
109
|
+
* @template T - The document data type in the query results
|
|
110
|
+
*/
|
|
39
111
|
export interface FirestoreQueryFactory<T> {
|
|
40
112
|
/**
|
|
41
|
-
* Creates a new FirestoreExecutableQuery
|
|
113
|
+
* Creates a new FirestoreExecutableQuery with the specified constraints.
|
|
114
|
+
*
|
|
115
|
+
* This function creates a query against the factory's base query reference
|
|
116
|
+
* (typically a collection or collection group) with the provided constraints applied.
|
|
42
117
|
*/
|
|
43
118
|
readonly query: FirestoreQueryFactoryFunction<T>;
|
|
44
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Configuration for creating a FirestoreQueryFactory.
|
|
122
|
+
*
|
|
123
|
+
* This interface combines the necessary components for creating a query factory:
|
|
124
|
+
* - A query-like reference (collection, collection group, etc.)
|
|
125
|
+
* - A query driver to handle the Firestore operations
|
|
126
|
+
*
|
|
127
|
+
* @template T - The document data type in the query results
|
|
128
|
+
*/
|
|
45
129
|
export interface FirestoreQueryConfig<T> extends FirestoreQueryDriverRef, QueryLikeReferenceRef<T> {
|
|
46
130
|
}
|
|
47
131
|
/**
|
|
48
|
-
* Creates a
|
|
132
|
+
* Creates a factory for building and executing Firestore queries against a specific collection.
|
|
133
|
+
*
|
|
134
|
+
* This function creates a query factory that allows building queries with various constraints
|
|
135
|
+
* and executing them in different ways (getting documents, counting, streaming). The factory
|
|
136
|
+
* maintains the base query reference (typically a collection or collection group) and provides
|
|
137
|
+
* a fluent API for working with it.
|
|
138
|
+
*
|
|
139
|
+
* @template T - The document data type in the query results
|
|
140
|
+
* @param config - Configuration for the query factory, including the base query reference and driver
|
|
141
|
+
* @returns A factory for creating and executing queries against the specified collection
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* // Create a query factory for the 'users' collection
|
|
145
|
+
* const usersQuery = firestoreQueryFactory({
|
|
146
|
+
* queryLike: collection(firestore, 'users'),
|
|
147
|
+
* firestoreQueryDriver: driver
|
|
148
|
+
* });
|
|
149
|
+
*
|
|
150
|
+
* // Use the factory to create and execute queries
|
|
151
|
+
* const activeUsers = await usersQuery.query(
|
|
152
|
+
* where('status', '==', 'active'),
|
|
153
|
+
* orderBy('lastLogin', 'desc'),
|
|
154
|
+
* limit(10)
|
|
155
|
+
* ).getDocs();
|
|
49
156
|
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
157
|
+
* // Queries can be extended with additional constraints
|
|
158
|
+
* const adminUsers = activeUsers.filter(
|
|
159
|
+
* where('role', '==', 'admin')
|
|
160
|
+
* ).getDocs();
|
|
52
161
|
*/
|
|
53
162
|
export declare function firestoreQueryFactory<T>(config: FirestoreQueryConfig<T>): FirestoreQueryFactory<T>;
|
|
@@ -3,37 +3,141 @@ import { type IterateFirestoreDocumentSnapshotCheckpointsConfig, type IterateFir
|
|
|
3
3
|
import { type FirestoreDocument } from '../accessor/document';
|
|
4
4
|
import { type FirestoreDocumentSnapshotDataPairWithData } from '../accessor/document.utility';
|
|
5
5
|
/**
|
|
6
|
-
* Configuration for
|
|
6
|
+
* Configuration for loading all document snapshots with their associated data as pairs.
|
|
7
|
+
*
|
|
8
|
+
* This interface defines the options for retrieving all document snapshots that match
|
|
9
|
+
* specific criteria, along with their associated data, in a single batch operation.
|
|
10
|
+
* It supports pagination via the checkpoint system for efficient handling of large result sets.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The document data type
|
|
13
|
+
* @template D - The FirestoreDocument implementation type (defaults to FirestoreDocument<T>)
|
|
7
14
|
*/
|
|
8
15
|
export interface LoadAllFirestoreDocumentSnapshotPairsConfig<T, D extends FirestoreDocument<T> = FirestoreDocument<T>> extends Pick<IterateFirestoreDocumentSnapshotPairBatchesConfig<T, unknown, D>, 'documentAccessor' | 'queryFactory' | 'constraintsFactory' | 'dynamicConstraints' | 'totalSnapshotsLimit' | 'handleRepeatCursor' | 'filterCheckpointSnapshots' | 'limitPerCheckpoint'> {
|
|
9
16
|
/**
|
|
10
|
-
* Optional
|
|
17
|
+
* Optional callback function to process each batch of snapshot pairs as they are loaded.
|
|
18
|
+
*
|
|
19
|
+
* This function is called once for each batch (checkpoint) of results, allowing for
|
|
20
|
+
* batch processing of documents without waiting for all results to be collected.
|
|
21
|
+
*
|
|
22
|
+
* @param snapshotDataPairs - Array of document snapshots with their associated data
|
|
23
|
+
* @param batchIndex - Zero-based index of the current batch
|
|
24
|
+
* @returns A promise that resolves when batch processing is complete
|
|
11
25
|
*/
|
|
12
26
|
iterateSnapshotPairsBatch?(snapshotDataPairs: FirestoreDocumentSnapshotDataPairWithData<D>[], batchIndex: number): Promise<void>;
|
|
13
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Result of loading all document snapshots with their associated data as pairs.
|
|
30
|
+
*
|
|
31
|
+
* Contains the complete array of document snapshot pairs and metadata about the
|
|
32
|
+
* query execution, such as the number of checkpoints processed and whether any
|
|
33
|
+
* limits were reached.
|
|
34
|
+
*
|
|
35
|
+
* @template T - The document data type
|
|
36
|
+
* @template D - The FirestoreDocument implementation type (defaults to FirestoreDocument<T>)
|
|
37
|
+
*/
|
|
14
38
|
export interface LoadAllFirestoreDocumentSnapshotPairsResult<T, D extends FirestoreDocument<T> = FirestoreDocument<T>> extends Pick<IterateFirestoreDocumentSnapshotCheckpointsResult, 'totalCheckpoints' | 'totalSnapshotsVisited' | 'totalSnapshotsLimitReached'> {
|
|
39
|
+
/**
|
|
40
|
+
* Array of all document snapshots with their associated data.
|
|
41
|
+
*
|
|
42
|
+
* Each item in the array contains both the Firestore document snapshot and
|
|
43
|
+
* the data extracted from it, along with additional metadata.
|
|
44
|
+
*/
|
|
15
45
|
readonly snapshotPairs: FirestoreDocumentSnapshotDataPairWithData<D>[];
|
|
16
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Loads all document snapshots that match the specified criteria, along with their associated data,
|
|
49
|
+
* into a single array.
|
|
50
|
+
*
|
|
51
|
+
* This function uses the paginated checkpoint system to efficiently retrieve potentially large
|
|
52
|
+
* result sets in batches, then combines them into a single result array. It supports optional
|
|
53
|
+
* batch processing while loading.
|
|
54
|
+
*
|
|
55
|
+
* @template T - The document data type
|
|
56
|
+
* @template D - The FirestoreDocument implementation type (defaults to FirestoreDocument<T>)
|
|
57
|
+
* @param config - Configuration options for the loading operation
|
|
58
|
+
* @returns Promise resolving to the result containing all matching document snapshot pairs
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* // Load all active user documents with their data
|
|
62
|
+
* const result = await loadAllFirestoreDocumentSnapshotPairs({
|
|
63
|
+
* queryFactory: () => collection(firestore, 'users'),
|
|
64
|
+
* constraintsFactory: () => [where('status', '==', 'active')],
|
|
65
|
+
* documentAccessor: userAccessorFactory,
|
|
66
|
+
* // Process batches as they load
|
|
67
|
+
* iterateSnapshotPairsBatch: async (pairs, index) => {
|
|
68
|
+
* console.log(`Processing batch ${index} with ${pairs.length} users`);
|
|
69
|
+
* }
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* console.log(`Loaded ${result.snapshotPairs.length} user documents`);
|
|
73
|
+
*/
|
|
17
74
|
export declare function loadAllFirestoreDocumentSnapshotPairs<T, D extends FirestoreDocument<T> = FirestoreDocument<T>>(config: LoadAllFirestoreDocumentSnapshotPairsConfig<T, D>): Promise<LoadAllFirestoreDocumentSnapshotPairsResult<T, D>>;
|
|
18
75
|
/**
|
|
19
|
-
* Configuration for
|
|
76
|
+
* Configuration for loading all document snapshots that match specific criteria.
|
|
77
|
+
*
|
|
78
|
+
* This interface defines the options for retrieving all document snapshots in a single batch
|
|
79
|
+
* operation, without their associated data. It supports pagination via the checkpoint system
|
|
80
|
+
* for efficient handling of large result sets.
|
|
81
|
+
*
|
|
82
|
+
* @template T - The document data type
|
|
20
83
|
*/
|
|
21
84
|
export interface LoadAllFirestoreDocumentSnapshotsConfig<T> extends Pick<IterateFirestoreDocumentSnapshotCheckpointsConfig<T, unknown>, 'queryFactory' | 'constraintsFactory' | 'dynamicConstraints' | 'totalSnapshotsLimit' | 'handleRepeatCursor' | 'filterCheckpointSnapshots' | 'limitPerCheckpoint'> {
|
|
22
85
|
/**
|
|
23
|
-
* Optional
|
|
86
|
+
* Optional callback function to process each batch of snapshots as they are loaded.
|
|
87
|
+
*
|
|
88
|
+
* This function is called once for each batch (checkpoint) of results, allowing for
|
|
89
|
+
* batch processing of documents without waiting for all results to be collected.
|
|
24
90
|
*
|
|
25
|
-
* @param snapshots
|
|
26
|
-
* @param query
|
|
91
|
+
* @param snapshots - Array of document snapshots in the current batch
|
|
92
|
+
* @param query - The complete query snapshot for the current batch
|
|
93
|
+
* @returns A promise that resolves when batch processing is complete
|
|
27
94
|
*/
|
|
28
95
|
iterateSnapshotsForCheckpoint?(snapshots: QueryDocumentSnapshot<T>[], query: QuerySnapshot<T>): Promise<void>;
|
|
29
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Result of loading all document snapshots that match specific criteria.
|
|
99
|
+
*
|
|
100
|
+
* Contains the complete array of document snapshots and metadata about the
|
|
101
|
+
* query execution, such as the number of checkpoints processed and whether any
|
|
102
|
+
* limits were reached.
|
|
103
|
+
*
|
|
104
|
+
* @template T - The document data type
|
|
105
|
+
*/
|
|
30
106
|
export interface LoadAllFirestoreDocumentSnapshotsResult<T> extends Pick<IterateFirestoreDocumentSnapshotCheckpointsResult, 'totalCheckpoints' | 'totalSnapshotsVisited' | 'totalSnapshotsLimitReached'> {
|
|
107
|
+
/**
|
|
108
|
+
* Array of all document snapshots that matched the query criteria.
|
|
109
|
+
*
|
|
110
|
+
* These snapshots can be used to access document data, IDs, and references.
|
|
111
|
+
*/
|
|
31
112
|
readonly snapshots: QueryDocumentSnapshot<T>[];
|
|
32
113
|
}
|
|
33
114
|
/**
|
|
34
|
-
*
|
|
115
|
+
* Loads all document snapshots that match the specified criteria into a single array.
|
|
116
|
+
*
|
|
117
|
+
* This function uses the paginated checkpoint system to efficiently retrieve potentially large
|
|
118
|
+
* result sets in batches, then combines them into a single result array. It supports optional
|
|
119
|
+
* batch processing while loading.
|
|
120
|
+
*
|
|
121
|
+
* Unlike loadAllFirestoreDocumentSnapshotPairs, this function only retrieves the document
|
|
122
|
+
* snapshots without automatically loading their associated data.
|
|
123
|
+
*
|
|
124
|
+
* @template T - The document data type
|
|
125
|
+
* @param config - Configuration options for the loading operation
|
|
126
|
+
* @returns Promise resolving to the result containing all matching document snapshots
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* // Load all documents created in the last 24 hours
|
|
130
|
+
* const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
|
131
|
+
* const result = await loadAllFirestoreDocumentSnapshot({
|
|
132
|
+
* queryFactory: () => collection(firestore, 'events'),
|
|
133
|
+
* constraintsFactory: () => [where('createdAt', '>=', yesterday), orderBy('createdAt')],
|
|
134
|
+
* totalSnapshotsLimit: 1000, // Stop after 1000 documents
|
|
135
|
+
* // Process batches as they load
|
|
136
|
+
* iterateSnapshotsForCheckpoint: async (snapshots, query) => {
|
|
137
|
+
* console.log(`Processing batch with ${snapshots.length} events`);
|
|
138
|
+
* }
|
|
139
|
+
* });
|
|
35
140
|
*
|
|
36
|
-
*
|
|
37
|
-
* @returns
|
|
141
|
+
* console.log(`Loaded ${result.snapshots.length} event documents`);
|
|
38
142
|
*/
|
|
39
143
|
export declare function loadAllFirestoreDocumentSnapshot<T>(config: LoadAllFirestoreDocumentSnapshotsConfig<T>): Promise<LoadAllFirestoreDocumentSnapshotsResult<T>>;
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Firestore Query Iteration
|
|
3
|
+
*
|
|
4
|
+
* This module provides a comprehensive system for iterating through Firestore query results
|
|
5
|
+
* with support for pagination, batching, and parallelism. It enables efficient processing of
|
|
6
|
+
* large result sets by using cursor-based pagination (via "checkpoints") and various iteration
|
|
7
|
+
* strategies.
|
|
8
|
+
*/
|
|
1
9
|
import { type GetterOrValue, type PromiseOrValue, type IndexRef, type Maybe, type PerformAsyncTasksConfig, type IndexNumber, type PerformAsyncTasksResult, type FactoryWithRequiredInput, type Milliseconds, type AllowValueOnceFilter, type ReadKeyFunction } from '@dereekb/util';
|
|
2
10
|
import { type FirestoreDocument, type LimitedFirestoreDocumentAccessor, type FirestoreDocumentSnapshotDataPairWithData } from '../accessor';
|
|
3
11
|
import { type QueryDocumentSnapshot, type QuerySnapshot, type DocumentSnapshot } from '../types';
|
|
@@ -5,7 +13,11 @@ import { type FirestoreQueryConstraint } from './constraint';
|
|
|
5
13
|
import { type FirestoreQueryFactory } from './query';
|
|
6
14
|
import { type FirestoreModelKey } from '../collection/collection';
|
|
7
15
|
/**
|
|
8
|
-
*
|
|
16
|
+
* Configuration for iterating through Firestore document snapshots with their associated data as pairs.
|
|
17
|
+
*
|
|
18
|
+
* @template T - The document data type
|
|
19
|
+
* @template R - The result type of processing each snapshot pair
|
|
20
|
+
* @template D - The FirestoreDocument implementation type (defaults to FirestoreDocument<T>)
|
|
9
21
|
*/
|
|
10
22
|
export interface IterateFirestoreDocumentSnapshotPairsConfig<T, R, D extends FirestoreDocument<T> = FirestoreDocument<T>> extends Omit<IterateFirestoreDocumentSnapshotsConfig<T, R>, 'iterateSnapshot'> {
|
|
11
23
|
/**
|
|
@@ -20,33 +32,78 @@ export interface IterateFirestoreDocumentSnapshotPairsConfig<T, R, D extends Fir
|
|
|
20
32
|
/**
|
|
21
33
|
* Iterates through the results of a Firestore query by each FirestoreDocumentSnapshotDataPairWithData.
|
|
22
34
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
35
|
+
* This function efficiently handles pagination through potentially large result sets by using
|
|
36
|
+
* the checkpoint system to load documents in batches. For each document snapshot, it loads the
|
|
37
|
+
* associated data using the provided document accessor, then passes the combined pair to the
|
|
38
|
+
* processing function.
|
|
39
|
+
*
|
|
40
|
+
* @template T - The document data type
|
|
41
|
+
* @template R - The result type of processing each snapshot pair
|
|
42
|
+
* @template D - The FirestoreDocument implementation type (defaults to FirestoreDocument<T>)
|
|
43
|
+
* @param config - Configuration for the iteration, including the document accessor and processing function
|
|
44
|
+
* @returns A promise that resolves to the result of the iteration, including statistics about checkpoints and snapshots processed
|
|
25
45
|
*/
|
|
26
46
|
export declare function iterateFirestoreDocumentSnapshotPairs<T, R, D extends FirestoreDocument<T> = FirestoreDocument<T>>(config: IterateFirestoreDocumentSnapshotPairsConfig<T, R, D>): Promise<IterateFirestoreDocumentSnapshotCheckpointsResult>;
|
|
27
47
|
/**
|
|
28
|
-
*
|
|
48
|
+
* Configuration for iterating through Firestore document snapshots individually.
|
|
49
|
+
*
|
|
50
|
+
* This interface provides settings for processing each document snapshot one by one,
|
|
51
|
+
* rather than in batches.
|
|
52
|
+
*
|
|
53
|
+
* @template T - The document data type
|
|
54
|
+
* @template R - The result type of processing each snapshot
|
|
29
55
|
*/
|
|
30
56
|
export interface IterateFirestoreDocumentSnapshotsConfig<T, R> extends Omit<IterateFirestoreDocumentSnapshotBatchesConfig<T, IterateFirestoreDocumentSnapshotsResult<T, R>>, 'iterateSnapshotBatch' | 'maxParallelCheckpoints'> {
|
|
31
57
|
/**
|
|
32
|
-
* The iterate function per each snapshot individually
|
|
58
|
+
* The iterate function per each snapshot individually.
|
|
59
|
+
*
|
|
60
|
+
* This function is called once for each document snapshot encountered during iteration.
|
|
61
|
+
*
|
|
62
|
+
* @param snapshot - The document snapshot to process
|
|
63
|
+
* @returns A promise resolving to the result of processing this snapshot
|
|
33
64
|
*/
|
|
34
65
|
iterateSnapshot(snapshot: QueryDocumentSnapshot<T>): Promise<R>;
|
|
35
66
|
/**
|
|
36
|
-
* (Optional) Additional config for the snapshot's PerformAsyncTasks call. By default
|
|
67
|
+
* (Optional) Additional config for the snapshot's PerformAsyncTasks call. By default uses the performTasksConfig value.
|
|
68
|
+
*
|
|
69
|
+
* This allows fine-tuning the parallelism, batching, and error handling when
|
|
70
|
+
* processing the individual snapshots within a checkpoint batch.
|
|
37
71
|
*/
|
|
38
72
|
readonly snapshotsPerformTasksConfig?: Partial<PerformAsyncTasksConfig<QueryDocumentSnapshot<T>>>;
|
|
39
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Result of processing individual document snapshots during iteration.
|
|
76
|
+
*
|
|
77
|
+
* Contains the combined results of all processed snapshots, including successful results,
|
|
78
|
+
* error information, and statistics about the processing operation.
|
|
79
|
+
*
|
|
80
|
+
* @template T - The document data type
|
|
81
|
+
* @template R - The result type of processing each snapshot
|
|
82
|
+
*/
|
|
40
83
|
export type IterateFirestoreDocumentSnapshotsResult<T, R> = PerformAsyncTasksResult<QueryDocumentSnapshot<T>, R>;
|
|
41
84
|
/**
|
|
42
85
|
* Iterates through the results of a Firestore query by each document snapshot by itself.
|
|
43
86
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
87
|
+
* This function efficiently handles pagination through potentially large result sets by using
|
|
88
|
+
* the checkpoint system to load documents in batches. Each document snapshot is then processed
|
|
89
|
+
* individually using the provided processing function.
|
|
90
|
+
*
|
|
91
|
+
* @template T - The document data type
|
|
92
|
+
* @template R - The result type of processing each snapshot
|
|
93
|
+
* @param config - Configuration for the iteration, including the snapshot processing function
|
|
94
|
+
* @returns A promise that resolves to the result of the iteration, including statistics about checkpoints and snapshots processed
|
|
46
95
|
*/
|
|
47
96
|
export declare function iterateFirestoreDocumentSnapshots<T, R>(config: IterateFirestoreDocumentSnapshotsConfig<T, R>): Promise<IterateFirestoreDocumentSnapshotCheckpointsResult>;
|
|
48
97
|
/**
|
|
49
98
|
* Config for iterateFirestoreDocumentSnapshots().
|
|
99
|
+
*
|
|
100
|
+
* This interface defines the configuration for processing batches of document snapshots
|
|
101
|
+
* with their associated data. It supports efficient batch processing operations on related
|
|
102
|
+
* documents.
|
|
103
|
+
*
|
|
104
|
+
* @template T - The document data type
|
|
105
|
+
* @template R - The result type of processing each batch
|
|
106
|
+
* @template D - The FirestoreDocument implementation type
|
|
50
107
|
*/
|
|
51
108
|
export interface IterateFirestoreDocumentSnapshotPairBatchesConfig<T, R, D extends FirestoreDocument<T> = FirestoreDocument<T>> extends Omit<IterateFirestoreDocumentSnapshotBatchesConfig<T, R>, 'iterateSnapshotBatch'> {
|
|
52
109
|
/**
|
|
@@ -69,6 +126,11 @@ export declare function iterateFirestoreDocumentSnapshotPairBatches<T, R, D exte
|
|
|
69
126
|
* Single batch result from iterateFirestoreDocumentSnapshotBatches().
|
|
70
127
|
*
|
|
71
128
|
* Contains the batch index, the snapshots, and the final result for this batch.
|
|
129
|
+
* Used to track the processing results for each batch of documents processed
|
|
130
|
+
* during iteration.
|
|
131
|
+
*
|
|
132
|
+
* @template T - The document data type
|
|
133
|
+
* @template R - The result type from processing the batch
|
|
72
134
|
*/
|
|
73
135
|
export interface IterateFirestoreDocumentSnapshotBatchesResult<T, R> extends IndexRef {
|
|
74
136
|
/***
|
|
@@ -197,61 +259,122 @@ export interface IterateFirestoreDocumentSnapshotCheckpointsConfig<T, R> {
|
|
|
197
259
|
/**
|
|
198
260
|
* Filter function used to filter out snapshots.
|
|
199
261
|
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
262
|
+
* This type defines a function that can filter document snapshots during iteration,
|
|
263
|
+
* allowing for custom filtering logic to be applied to checkpoints beyond what
|
|
264
|
+
* Firestore query constraints can achieve.
|
|
265
|
+
*
|
|
266
|
+
* @param snapshot - Array of document snapshots to filter
|
|
267
|
+
* @returns Filtered array of document snapshots
|
|
202
268
|
*/
|
|
203
269
|
export type IterateFirestoreDocumentSnapshotCheckpointsFilterCheckpointSnapshotsFunction<T> = (snapshot: QueryDocumentSnapshot<T>[]) => PromiseOrValue<QueryDocumentSnapshot<T>[]>;
|
|
204
270
|
/**
|
|
205
271
|
* Creates a IterateFirestoreDocumentSnapshotCheckpointsFilterCheckpointSnapshotsFunction that filters out any repeat documents.
|
|
206
272
|
*
|
|
207
273
|
* Repeat documents can occur in cases where the document is updated and the query matches it again for a different reason.
|
|
274
|
+
* This utility function creates a filter that prevents processing the same document multiple times.
|
|
208
275
|
*
|
|
209
|
-
* @param readKeyFunction
|
|
276
|
+
* @param readKeyFunction - Function that extracts a unique key from a document snapshot, defaults to document ID
|
|
277
|
+
* @returns A filter function that prevents duplicate document processing
|
|
210
278
|
*/
|
|
211
279
|
export declare function filterRepeatCheckpointSnapshots<T>(readKeyFunction?: ReadKeyFunction<QueryDocumentSnapshot<T>>): IterateFirestoreDocumentSnapshotCheckpointsFilterCheckpointSnapshotsFunction<T>;
|
|
280
|
+
/**
|
|
281
|
+
* Result of processing a single pagination checkpoint during the iteration process.
|
|
282
|
+
*
|
|
283
|
+
* This interface contains all data related to one checkpoint/batch in the pagination
|
|
284
|
+
* sequence, including the documents retrieved, the cursor position, and processing results.
|
|
285
|
+
*
|
|
286
|
+
* @template T - The document data type
|
|
287
|
+
* @template R - The result type from processing this checkpoint
|
|
288
|
+
*/
|
|
212
289
|
export interface IterateFirestoreDocumentSnapshotCheckpointsIterationResult<T, R> extends IndexRef {
|
|
213
290
|
/***
|
|
214
291
|
* Checkpoint index number
|
|
292
|
+
*
|
|
293
|
+
* Zero-based index of this checkpoint within the iteration sequence.
|
|
215
294
|
*/
|
|
216
295
|
readonly i: IndexNumber;
|
|
217
296
|
/**
|
|
218
297
|
* The cursor document used in this query.
|
|
298
|
+
*
|
|
299
|
+
* This is typically the last document in the current batch, which will be used
|
|
300
|
+
* as the starting point for the next query. May be undefined for the last batch
|
|
301
|
+
* or if the batch is empty.
|
|
219
302
|
*/
|
|
220
303
|
readonly cursorDocument?: Maybe<QueryDocumentSnapshot<T>>;
|
|
221
304
|
/**
|
|
222
305
|
* Results returned from each checkpoint.
|
|
306
|
+
*
|
|
307
|
+
* These are the values returned by the checkpoint processing function for
|
|
308
|
+
* this specific batch of documents.
|
|
223
309
|
*/
|
|
224
310
|
readonly results: R[];
|
|
225
311
|
/**
|
|
226
312
|
* All non-filtered document snapshots in this checkpoint.
|
|
227
313
|
*
|
|
228
314
|
* If filterCheckpointSnapshot is configured, this does not include the filtered snapshots.
|
|
315
|
+
* These are the raw document snapshots as retrieved from Firestore that passed any
|
|
316
|
+
* configured filtering.
|
|
229
317
|
*/
|
|
230
318
|
readonly docSnapshots: QueryDocumentSnapshot<T>[];
|
|
231
319
|
/**
|
|
232
320
|
* The query snapshot for this checkpoint.
|
|
321
|
+
*
|
|
322
|
+
* Contains the raw query result, including metadata like query time and
|
|
323
|
+
* whether the query includes data from local cache.
|
|
233
324
|
*/
|
|
234
325
|
readonly docQuerySnapshot: QuerySnapshot<T>;
|
|
235
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* Summary results of a complete paginated iteration through Firestore query results.
|
|
329
|
+
*
|
|
330
|
+
* This interface provides statistics about the query execution, including the number
|
|
331
|
+
* of pagination checkpoints processed and the total number of documents visited.
|
|
332
|
+
*/
|
|
236
333
|
export interface IterateFirestoreDocumentSnapshotCheckpointsResult {
|
|
237
334
|
/**
|
|
238
335
|
* The total number of batches visited.
|
|
336
|
+
*
|
|
337
|
+
* Each checkpoint represents one Firestore query execution as part of the
|
|
338
|
+
* paginated iteration process. The number of checkpoints depends on the
|
|
339
|
+
* number of documents matching the query and the limit per checkpoint.
|
|
239
340
|
*/
|
|
240
341
|
readonly totalCheckpoints: number;
|
|
241
342
|
/**
|
|
242
343
|
* The total number of snapshots that were visited.
|
|
344
|
+
*
|
|
345
|
+
* This represents the total number of documents that matched the query constraints
|
|
346
|
+
* and were processed during the iteration, subject to any total limit applied.
|
|
243
347
|
*/
|
|
244
348
|
readonly totalSnapshotsVisited: number;
|
|
245
349
|
/**
|
|
246
350
|
* Whether or not the total snapshots limit was reached.
|
|
351
|
+
*
|
|
352
|
+
* When true, this indicates that the query potentially had more matching documents
|
|
353
|
+
* than were processed, but the iteration stopped after reaching the configured limit.
|
|
354
|
+
* When false, all matching documents were processed.
|
|
247
355
|
*/
|
|
248
356
|
readonly totalSnapshotsLimitReached: boolean;
|
|
249
357
|
}
|
|
250
358
|
/**
|
|
251
359
|
* Iterates through the results of a Firestore query in several batches.
|
|
252
360
|
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
361
|
+
* This is the core pagination function that handles cursor-based iteration through
|
|
362
|
+
* potentially large Firestore query results. It manages cursor documents, checkpoint
|
|
363
|
+
* processing, parallel execution, and various limit controls.
|
|
364
|
+
*
|
|
365
|
+
* @template T - The document data type
|
|
366
|
+
* @template R - The result type of the iteration
|
|
367
|
+
* @param config - Complete configuration for the pagination and processing behavior
|
|
368
|
+
* @returns Promise resolving to statistics about the iteration
|
|
255
369
|
*/
|
|
256
370
|
export declare function iterateFirestoreDocumentSnapshotCheckpoints<T, R>(config: IterateFirestoreDocumentSnapshotCheckpointsConfig<T, R>): Promise<IterateFirestoreDocumentSnapshotCheckpointsResult>;
|
|
371
|
+
/**
|
|
372
|
+
* Creates a filter that allows each document snapshot to be processed only once based on its path.
|
|
373
|
+
*
|
|
374
|
+
* This utility helps prevent duplicate processing of documents by tracking which ones have
|
|
375
|
+
* already been seen based on their path.
|
|
376
|
+
*
|
|
377
|
+
* @template T - The document snapshot type
|
|
378
|
+
* @returns A filter function that only allows each unique document to pass once
|
|
379
|
+
*/
|
|
257
380
|
export declare function allowDocumentSnapshotWithPathOnceFilter<T extends DocumentSnapshot>(): AllowValueOnceFilter<T, FirestoreModelKey>;
|