@knymbus/firestoredb 1.2.0 → 1.2.4
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/dist/cjs/FirestoreQuery.d.ts +39 -15
- package/dist/cjs/FirestoreQuery.d.ts.map +1 -1
- package/dist/cjs/FirestoreQuery.js +70 -38
- package/dist/cjs/types.d.ts +9 -0
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/esm/FirestoreQuery.d.ts +39 -15
- package/dist/esm/FirestoreQuery.d.ts.map +1 -1
- package/dist/esm/FirestoreQuery.js +70 -38
- package/dist/esm/types.d.ts +9 -0
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
import type { QueryConstraint } from "firebase/firestore";
|
|
2
|
-
import { FirestoreSDK, WithSystemFields } from "./types";
|
|
2
|
+
import { FirestoreSDK, PaginationMetadata, SortDescriptor, WithSystemFields } from "./types";
|
|
3
3
|
import { LRUCache } from './utils';
|
|
4
4
|
import { FirestoreCursor } from "./utils/FirestoreCursor";
|
|
5
|
-
export type
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
export type PaginationConfig = {
|
|
6
|
+
alreadyLoadedCount: number;
|
|
7
|
+
timeComplexity: "oN" | "o1";
|
|
8
|
+
};
|
|
9
|
+
type StandardResponse<T> = {
|
|
10
|
+
data: WithSystemFields<T>[];
|
|
11
|
+
metadata: PaginationMetadata;
|
|
12
|
+
lastId: string | null;
|
|
13
|
+
firstId: string | null;
|
|
14
|
+
};
|
|
15
|
+
type NormalizedResponse<T> = {
|
|
16
|
+
data: {
|
|
17
|
+
ids: string[];
|
|
18
|
+
dataById: Record<string, WithSystemFields<T>>;
|
|
19
|
+
};
|
|
20
|
+
metadata: PaginationMetadata;
|
|
21
|
+
lastId: string | null;
|
|
22
|
+
firstId: string | null;
|
|
23
|
+
};
|
|
13
24
|
export declare class FirestoreQuery<T> {
|
|
14
25
|
private db;
|
|
15
26
|
private collectionName;
|
|
@@ -38,9 +49,9 @@ export declare class FirestoreQuery<T> {
|
|
|
38
49
|
sort(sortObj: Record<keyof T, SortDescriptor>): this;
|
|
39
50
|
limit(n: number): this;
|
|
40
51
|
/** Move to the next page */
|
|
41
|
-
after(id: string): this;
|
|
52
|
+
after(id: string | null | undefined): this;
|
|
42
53
|
/** Move to the previous page */
|
|
43
|
-
before(id: string): this;
|
|
54
|
+
before(id: string | null | undefined): this;
|
|
44
55
|
/**
|
|
45
56
|
* FirestoreDB: db.collection.find({}).withDeleted().execute()
|
|
46
57
|
* Allows viewing soft-deleted documents for this specific query.
|
|
@@ -50,6 +61,7 @@ export declare class FirestoreQuery<T> {
|
|
|
50
61
|
/** Simple execution (Returns Array) */
|
|
51
62
|
execute(): Promise<WithSystemFields<T>[]>;
|
|
52
63
|
/**
|
|
64
|
+
* @deprecated Use the config-based version for more control over complexity.
|
|
53
65
|
* Paginated execution (Returns Data + Metadata)
|
|
54
66
|
*
|
|
55
67
|
* @example
|
|
@@ -67,10 +79,21 @@ export declare class FirestoreQuery<T> {
|
|
|
67
79
|
|
|
68
80
|
console.log(`Viewing page ${metadata.currentPage} of ${metadata.totalPages}`);
|
|
69
81
|
*/
|
|
70
|
-
paginate(): Promise<
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
82
|
+
paginate(alreadyLoadedCount?: number): Promise<StandardResponse<T>>;
|
|
83
|
+
/**
|
|
84
|
+
* Advanced pagination with complexity control.
|
|
85
|
+
* Returns either a flat array (oN) or a normalized object (o1).
|
|
86
|
+
* @param config Configuration object for data shape and loading offset.
|
|
87
|
+
*/
|
|
88
|
+
paginate(config?: PaginationConfig & {
|
|
89
|
+
timeComplexity: "o1";
|
|
90
|
+
}): Promise<NormalizedResponse<T>>;
|
|
91
|
+
/**
|
|
92
|
+
* Overload for O(N) complexity -> returns Standard array
|
|
93
|
+
*/
|
|
94
|
+
paginate(config?: PaginationConfig & {
|
|
95
|
+
timeComplexity: "oN";
|
|
96
|
+
}): Promise<StandardResponse<T>>;
|
|
74
97
|
stream(): ReadableStream<T & {
|
|
75
98
|
_id: string;
|
|
76
99
|
}>;
|
|
@@ -93,4 +116,5 @@ export declare class FirestoreQuery<T> {
|
|
|
93
116
|
*/
|
|
94
117
|
private _sortObjectKeys;
|
|
95
118
|
}
|
|
119
|
+
export {};
|
|
96
120
|
//# sourceMappingURL=FirestoreQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FirestoreQuery.d.ts","sourceRoot":"","sources":["../../src/FirestoreQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"FirestoreQuery.d.ts","sourceRoot":"","sources":["../../src/FirestoreQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAG7F,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE1D,MAAM,MAAM,gBAAgB,GAAG;IAC3B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,cAAc,EAAE,IAAI,GAAG,IAAI,CAAC;CAC/B,CAAC;AAGF,KAAK,gBAAgB,CAAC,CAAC,IAAI;IACvB,IAAI,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAAC;IAC5B,QAAQ,EAAE,kBAAkB,CAAA;IAC5B,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,OAAO,EAAE,MAAM,GAAG,IAAI,CA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IAezB;;GAED;IACC,OAAO,CAAC,eAAe;CAQ1B"}
|
|
@@ -57,14 +57,26 @@ class FirestoreQuery {
|
|
|
57
57
|
}
|
|
58
58
|
/** Move to the next page */
|
|
59
59
|
after(id) {
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
if (!id) {
|
|
61
|
+
this._cursorId = undefined;
|
|
62
|
+
this._cursorType = null;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
this._cursorId = id;
|
|
66
|
+
this._cursorType = 'after';
|
|
67
|
+
}
|
|
62
68
|
return this;
|
|
63
69
|
}
|
|
64
70
|
/** Move to the previous page */
|
|
65
71
|
before(id) {
|
|
66
|
-
|
|
67
|
-
|
|
72
|
+
if (!id) {
|
|
73
|
+
this._cursorId = undefined;
|
|
74
|
+
this._cursorType = null;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this._cursorId = id;
|
|
78
|
+
this._cursorType = 'before';
|
|
79
|
+
}
|
|
68
80
|
return this;
|
|
69
81
|
}
|
|
70
82
|
/**
|
|
@@ -91,26 +103,14 @@ class FirestoreQuery {
|
|
|
91
103
|
});
|
|
92
104
|
});
|
|
93
105
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* // React State example
|
|
99
|
-
const [currentPage, setCurrentPage] = useState(1);
|
|
100
|
-
const [lastId, setLastId] = useState(null);
|
|
101
|
-
|
|
102
|
-
const { data, metadata } = await Products.find({ category: 'electronics' })
|
|
103
|
-
.sort({ price: 'asc' })
|
|
104
|
-
.limit(10)
|
|
105
|
-
.page(currentPage) // Sets the metadata
|
|
106
|
-
.after(lastId) // Sets the cursor
|
|
107
|
-
.cache() // Speeds up the "Back" button
|
|
108
|
-
.paginate();
|
|
109
|
-
|
|
110
|
-
console.log(`Viewing page ${metadata.currentPage} of ${metadata.totalPages}`);
|
|
111
|
-
*/
|
|
112
|
-
paginate() {
|
|
106
|
+
// Implementation
|
|
107
|
+
paginate(arg) {
|
|
113
108
|
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
var _a, _b;
|
|
110
|
+
// Set the Configuration based on the input args
|
|
111
|
+
const config = (typeof arg === 'object' && arg !== null)
|
|
112
|
+
? arg
|
|
113
|
+
: { alreadyLoadedCount: arg !== null && arg !== void 0 ? arg : 0, timeComplexity: "oN" };
|
|
114
114
|
// set the global key for this query
|
|
115
115
|
const key = this._generateCacheKey();
|
|
116
116
|
// 1. Check Cache
|
|
@@ -123,22 +123,54 @@ class FirestoreQuery {
|
|
|
123
123
|
const data = yield this.execute();
|
|
124
124
|
const totalRecords = yield this.countDocs(this.filter);
|
|
125
125
|
const totalPages = this._limit > 0 ? Math.ceil(totalRecords / this._limit) : 1;
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
126
|
+
const currentTotalFetched = config.alreadyLoadedCount + data.length;
|
|
127
|
+
const totalRemaining = Math.max(0, totalRecords - currentTotalFetched);
|
|
128
|
+
let result;
|
|
129
|
+
// Prepare the meta data to be returned no matter the options
|
|
130
|
+
const meta = {
|
|
131
|
+
totalRecords,
|
|
132
|
+
totalPages,
|
|
133
|
+
totalRemaining,
|
|
134
|
+
currentPage: this._pageNumber,
|
|
135
|
+
hasNext: data.length === this._limit && totalRemaining > 0,
|
|
136
|
+
hasPrevious: this._pageNumber > 1 || !!this._cursorId
|
|
135
137
|
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
if (config.timeComplexity === 'oN') {
|
|
139
|
+
result = {
|
|
140
|
+
data,
|
|
141
|
+
metadata: meta,
|
|
142
|
+
firstId: ((_a = data[0]) === null || _a === void 0 ? void 0 : _a._id) || null,
|
|
143
|
+
lastId: ((_b = data[data.length - 1]) === null || _b === void 0 ? void 0 : _b._id) || null
|
|
144
|
+
};
|
|
145
|
+
// 2. Fetch & Store
|
|
146
|
+
if (this._useCache) {
|
|
147
|
+
FirestoreQuery._globalCache.set(key, {
|
|
148
|
+
data: result,
|
|
149
|
+
timestamp: Date.now()
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
if (config.timeComplexity === 'o1') {
|
|
155
|
+
const transformed = { ids: [], dataById: {} };
|
|
156
|
+
data.map(el => {
|
|
157
|
+
transformed.ids.push(el._id);
|
|
158
|
+
transformed.dataById[el._id] = el;
|
|
141
159
|
});
|
|
160
|
+
result = {
|
|
161
|
+
data: transformed,
|
|
162
|
+
metadata: meta,
|
|
163
|
+
firstId: transformed.ids[0] || null,
|
|
164
|
+
lastId: transformed.ids[transformed.ids.length - 1] || null
|
|
165
|
+
};
|
|
166
|
+
// 2. Fetch & Store
|
|
167
|
+
if (this._useCache) {
|
|
168
|
+
FirestoreQuery._globalCache.set(key, {
|
|
169
|
+
data: result,
|
|
170
|
+
timestamp: Date.now()
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return result; // Type assertion required in the implementation body
|
|
142
174
|
}
|
|
143
175
|
return result;
|
|
144
176
|
});
|
|
@@ -196,7 +228,7 @@ class FirestoreQuery {
|
|
|
196
228
|
constraints.push(this._sdk.orderBy(field, dir));
|
|
197
229
|
});
|
|
198
230
|
// 2. Handle Cursors (Validation + Logic)
|
|
199
|
-
if (this._cursorId) {
|
|
231
|
+
if (this._cursorId && typeof this._cursorId === 'string') {
|
|
200
232
|
const docRef = this._sdk.doc(this.db, this.collectionName, this._cursorId);
|
|
201
233
|
const snap = yield this._sdk.getDoc(docRef);
|
|
202
234
|
if (snap.exists()) {
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import type { Firestore, CollectionReference, DocumentReference, QueryConstraint, WriteBatch, Query, DocumentSnapshot, DocumentData, FirestoreError, Unsubscribe, FieldPath, AggregateField, AggregateSpec, AggregateQuerySnapshot, SnapshotListenOptions, QuerySnapshot, Transaction, TransactionOptions } from "firebase/firestore";
|
|
2
|
+
export type SortDescriptor = "asc" | "desc";
|
|
3
|
+
export interface PaginationMetadata {
|
|
4
|
+
totalRecords: number;
|
|
5
|
+
totalPages: number;
|
|
6
|
+
currentPage: number;
|
|
7
|
+
totalRemaining: number;
|
|
8
|
+
hasNext: boolean;
|
|
9
|
+
hasPrevious: boolean;
|
|
10
|
+
}
|
|
2
11
|
export type DocumentInput<T> = Partial<T> & {
|
|
3
12
|
_id?: string;
|
|
4
13
|
};
|
package/dist/cjs/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,SAAS,EAAE,mBAAmB,EAAE,iBAAiB,EACjD,eAAe,EAAE,UAAU,EAAE,KAAK,EAAE,gBAAgB,EACpD,YAAY,EACZ,cAAc,EACd,WAAW,EACX,SAAS,EACT,cAAc,EACd,aAAa,EACb,sBAAsB,EACtB,qBAAqB,EACrB,aAAa,EACb,WAAW,EACX,kBAAkB,EACrB,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,SAAS,EAAE,mBAAmB,EAAE,iBAAiB,EACjD,eAAe,EAAE,UAAU,EAAE,KAAK,EAAE,gBAAgB,EACpD,YAAY,EACZ,cAAc,EACd,WAAW,EACX,SAAS,EACT,cAAc,EACd,aAAa,EACb,sBAAsB,EACtB,qBAAqB,EACrB,aAAa,EACb,WAAW,EACX,kBAAkB,EACrB,MAAM,oBAAoB,CAAC;AAG5B,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,MAAM,CAAA;AAC3C,MAAM,WAAW,kBAAkB;IAC/B,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EA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,cAAc,KAAK,IAAI,EAAE,YAAY,CAAC,EAAE,MAAM,IAAI,GAAG,WAAW,CAAC;QAG7I,CAAC,GAAG,EAAE,iBAAiB,EAAE,OAAO,EAAE,qBAAqB,EAAE,MAAM,EAAE,CAAC,QAAQ,EAAE,gBAAgB,KAAK,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,EAAE,YAAY,CAAC,EAAE,MAAM,IAAI,GAAG,WAAW,CAAC;QAC1L,CAAC,GAAG,EAAE,iBAAiB,EAAE,MAAM,EAAE,CAAC,QAAQ,EAAE,gBAAgB,KAAK,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,EAAE,YAAY,CAAC,EAAE,MAAM,IAAI,GAAG,WAAW,CAAC;KAC7J,CAAC;IACF,cAAc,EAAE,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC,WAAW,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,kBAAkB,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;CAClJ"}
|
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
import type { QueryConstraint } from "firebase/firestore";
|
|
2
|
-
import { FirestoreSDK, WithSystemFields } from "./types";
|
|
2
|
+
import { FirestoreSDK, PaginationMetadata, SortDescriptor, WithSystemFields } from "./types";
|
|
3
3
|
import { LRUCache } from './utils';
|
|
4
4
|
import { FirestoreCursor } from "./utils/FirestoreCursor";
|
|
5
|
-
export type
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
export type PaginationConfig = {
|
|
6
|
+
alreadyLoadedCount: number;
|
|
7
|
+
timeComplexity: "oN" | "o1";
|
|
8
|
+
};
|
|
9
|
+
type StandardResponse<T> = {
|
|
10
|
+
data: WithSystemFields<T>[];
|
|
11
|
+
metadata: PaginationMetadata;
|
|
12
|
+
lastId: string | null;
|
|
13
|
+
firstId: string | null;
|
|
14
|
+
};
|
|
15
|
+
type NormalizedResponse<T> = {
|
|
16
|
+
data: {
|
|
17
|
+
ids: string[];
|
|
18
|
+
dataById: Record<string, WithSystemFields<T>>;
|
|
19
|
+
};
|
|
20
|
+
metadata: PaginationMetadata;
|
|
21
|
+
lastId: string | null;
|
|
22
|
+
firstId: string | null;
|
|
23
|
+
};
|
|
13
24
|
export declare class FirestoreQuery<T> {
|
|
14
25
|
private db;
|
|
15
26
|
private collectionName;
|
|
@@ -38,9 +49,9 @@ export declare class FirestoreQuery<T> {
|
|
|
38
49
|
sort(sortObj: Record<keyof T, SortDescriptor>): this;
|
|
39
50
|
limit(n: number): this;
|
|
40
51
|
/** Move to the next page */
|
|
41
|
-
after(id: string): this;
|
|
52
|
+
after(id: string | null | undefined): this;
|
|
42
53
|
/** Move to the previous page */
|
|
43
|
-
before(id: string): this;
|
|
54
|
+
before(id: string | null | undefined): this;
|
|
44
55
|
/**
|
|
45
56
|
* FirestoreDB: db.collection.find({}).withDeleted().execute()
|
|
46
57
|
* Allows viewing soft-deleted documents for this specific query.
|
|
@@ -50,6 +61,7 @@ export declare class FirestoreQuery<T> {
|
|
|
50
61
|
/** Simple execution (Returns Array) */
|
|
51
62
|
execute(): Promise<WithSystemFields<T>[]>;
|
|
52
63
|
/**
|
|
64
|
+
* @deprecated Use the config-based version for more control over complexity.
|
|
53
65
|
* Paginated execution (Returns Data + Metadata)
|
|
54
66
|
*
|
|
55
67
|
* @example
|
|
@@ -67,10 +79,21 @@ export declare class FirestoreQuery<T> {
|
|
|
67
79
|
|
|
68
80
|
console.log(`Viewing page ${metadata.currentPage} of ${metadata.totalPages}`);
|
|
69
81
|
*/
|
|
70
|
-
paginate(): Promise<
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
82
|
+
paginate(alreadyLoadedCount?: number): Promise<StandardResponse<T>>;
|
|
83
|
+
/**
|
|
84
|
+
* Advanced pagination with complexity control.
|
|
85
|
+
* Returns either a flat array (oN) or a normalized object (o1).
|
|
86
|
+
* @param config Configuration object for data shape and loading offset.
|
|
87
|
+
*/
|
|
88
|
+
paginate(config?: PaginationConfig & {
|
|
89
|
+
timeComplexity: "o1";
|
|
90
|
+
}): Promise<NormalizedResponse<T>>;
|
|
91
|
+
/**
|
|
92
|
+
* Overload for O(N) complexity -> returns Standard array
|
|
93
|
+
*/
|
|
94
|
+
paginate(config?: PaginationConfig & {
|
|
95
|
+
timeComplexity: "oN";
|
|
96
|
+
}): Promise<StandardResponse<T>>;
|
|
74
97
|
stream(): ReadableStream<T & {
|
|
75
98
|
_id: string;
|
|
76
99
|
}>;
|
|
@@ -93,4 +116,5 @@ export declare class FirestoreQuery<T> {
|
|
|
93
116
|
*/
|
|
94
117
|
private _sortObjectKeys;
|
|
95
118
|
}
|
|
119
|
+
export {};
|
|
96
120
|
//# sourceMappingURL=FirestoreQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FirestoreQuery.d.ts","sourceRoot":"","sources":["../../src/FirestoreQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"FirestoreQuery.d.ts","sourceRoot":"","sources":["../../src/FirestoreQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAG7F,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE1D,MAAM,MAAM,gBAAgB,GAAG;IAC3B,kBAA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,EAAE,IAAI,CAAA;KAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAmFlG,MAAM,IAAI,cAAc,CAAC,CAAC,GAAG;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;IAkBpD;;;;;;;;;;OAUG;IACU,MAAM,IAAI,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;YAapC,mBAAmB;IAsCjC,OAAO,CAAC,iBAAiB;IAezB;;GAED;IACC,OAAO,CAAC,eAAe;CAQ1B"}
|
|
@@ -57,14 +57,26 @@ class FirestoreQuery {
|
|
|
57
57
|
}
|
|
58
58
|
/** Move to the next page */
|
|
59
59
|
after(id) {
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
if (!id) {
|
|
61
|
+
this._cursorId = undefined;
|
|
62
|
+
this._cursorType = null;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
this._cursorId = id;
|
|
66
|
+
this._cursorType = 'after';
|
|
67
|
+
}
|
|
62
68
|
return this;
|
|
63
69
|
}
|
|
64
70
|
/** Move to the previous page */
|
|
65
71
|
before(id) {
|
|
66
|
-
|
|
67
|
-
|
|
72
|
+
if (!id) {
|
|
73
|
+
this._cursorId = undefined;
|
|
74
|
+
this._cursorType = null;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this._cursorId = id;
|
|
78
|
+
this._cursorType = 'before';
|
|
79
|
+
}
|
|
68
80
|
return this;
|
|
69
81
|
}
|
|
70
82
|
/**
|
|
@@ -91,26 +103,14 @@ class FirestoreQuery {
|
|
|
91
103
|
});
|
|
92
104
|
});
|
|
93
105
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* // React State example
|
|
99
|
-
const [currentPage, setCurrentPage] = useState(1);
|
|
100
|
-
const [lastId, setLastId] = useState(null);
|
|
101
|
-
|
|
102
|
-
const { data, metadata } = await Products.find({ category: 'electronics' })
|
|
103
|
-
.sort({ price: 'asc' })
|
|
104
|
-
.limit(10)
|
|
105
|
-
.page(currentPage) // Sets the metadata
|
|
106
|
-
.after(lastId) // Sets the cursor
|
|
107
|
-
.cache() // Speeds up the "Back" button
|
|
108
|
-
.paginate();
|
|
109
|
-
|
|
110
|
-
console.log(`Viewing page ${metadata.currentPage} of ${metadata.totalPages}`);
|
|
111
|
-
*/
|
|
112
|
-
paginate() {
|
|
106
|
+
// Implementation
|
|
107
|
+
paginate(arg) {
|
|
113
108
|
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
var _a, _b;
|
|
110
|
+
// Set the Configuration based on the input args
|
|
111
|
+
const config = (typeof arg === 'object' && arg !== null)
|
|
112
|
+
? arg
|
|
113
|
+
: { alreadyLoadedCount: arg !== null && arg !== void 0 ? arg : 0, timeComplexity: "oN" };
|
|
114
114
|
// set the global key for this query
|
|
115
115
|
const key = this._generateCacheKey();
|
|
116
116
|
// 1. Check Cache
|
|
@@ -123,22 +123,54 @@ class FirestoreQuery {
|
|
|
123
123
|
const data = yield this.execute();
|
|
124
124
|
const totalRecords = yield this.countDocs(this.filter);
|
|
125
125
|
const totalPages = this._limit > 0 ? Math.ceil(totalRecords / this._limit) : 1;
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
126
|
+
const currentTotalFetched = config.alreadyLoadedCount + data.length;
|
|
127
|
+
const totalRemaining = Math.max(0, totalRecords - currentTotalFetched);
|
|
128
|
+
let result;
|
|
129
|
+
// Prepare the meta data to be returned no matter the options
|
|
130
|
+
const meta = {
|
|
131
|
+
totalRecords,
|
|
132
|
+
totalPages,
|
|
133
|
+
totalRemaining,
|
|
134
|
+
currentPage: this._pageNumber,
|
|
135
|
+
hasNext: data.length === this._limit && totalRemaining > 0,
|
|
136
|
+
hasPrevious: this._pageNumber > 1 || !!this._cursorId
|
|
135
137
|
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
if (config.timeComplexity === 'oN') {
|
|
139
|
+
result = {
|
|
140
|
+
data,
|
|
141
|
+
metadata: meta,
|
|
142
|
+
firstId: ((_a = data[0]) === null || _a === void 0 ? void 0 : _a._id) || null,
|
|
143
|
+
lastId: ((_b = data[data.length - 1]) === null || _b === void 0 ? void 0 : _b._id) || null
|
|
144
|
+
};
|
|
145
|
+
// 2. Fetch & Store
|
|
146
|
+
if (this._useCache) {
|
|
147
|
+
FirestoreQuery._globalCache.set(key, {
|
|
148
|
+
data: result,
|
|
149
|
+
timestamp: Date.now()
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
if (config.timeComplexity === 'o1') {
|
|
155
|
+
const transformed = { ids: [], dataById: {} };
|
|
156
|
+
data.map(el => {
|
|
157
|
+
transformed.ids.push(el._id);
|
|
158
|
+
transformed.dataById[el._id] = el;
|
|
141
159
|
});
|
|
160
|
+
result = {
|
|
161
|
+
data: transformed,
|
|
162
|
+
metadata: meta,
|
|
163
|
+
firstId: transformed.ids[0] || null,
|
|
164
|
+
lastId: transformed.ids[transformed.ids.length - 1] || null
|
|
165
|
+
};
|
|
166
|
+
// 2. Fetch & Store
|
|
167
|
+
if (this._useCache) {
|
|
168
|
+
FirestoreQuery._globalCache.set(key, {
|
|
169
|
+
data: result,
|
|
170
|
+
timestamp: Date.now()
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return result; // Type assertion required in the implementation body
|
|
142
174
|
}
|
|
143
175
|
return result;
|
|
144
176
|
});
|
|
@@ -196,7 +228,7 @@ class FirestoreQuery {
|
|
|
196
228
|
constraints.push(this._sdk.orderBy(field, dir));
|
|
197
229
|
});
|
|
198
230
|
// 2. Handle Cursors (Validation + Logic)
|
|
199
|
-
if (this._cursorId) {
|
|
231
|
+
if (this._cursorId && typeof this._cursorId === 'string') {
|
|
200
232
|
const docRef = this._sdk.doc(this.db, this.collectionName, this._cursorId);
|
|
201
233
|
const snap = yield this._sdk.getDoc(docRef);
|
|
202
234
|
if (snap.exists()) {
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import type { Firestore, CollectionReference, DocumentReference, QueryConstraint, WriteBatch, Query, DocumentSnapshot, DocumentData, FirestoreError, Unsubscribe, FieldPath, AggregateField, AggregateSpec, AggregateQuerySnapshot, SnapshotListenOptions, QuerySnapshot, Transaction, TransactionOptions } from "firebase/firestore";
|
|
2
|
+
export type SortDescriptor = "asc" | "desc";
|
|
3
|
+
export interface PaginationMetadata {
|
|
4
|
+
totalRecords: number;
|
|
5
|
+
totalPages: number;
|
|
6
|
+
currentPage: number;
|
|
7
|
+
totalRemaining: number;
|
|
8
|
+
hasNext: boolean;
|
|
9
|
+
hasPrevious: boolean;
|
|
10
|
+
}
|
|
2
11
|
export type DocumentInput<T> = Partial<T> & {
|
|
3
12
|
_id?: string;
|
|
4
13
|
};
|
package/dist/esm/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,SAAS,EAAE,mBAAmB,EAAE,iBAAiB,EACjD,eAAe,EAAE,UAAU,EAAE,KAAK,EAAE,gBAAgB,EACpD,YAAY,EACZ,cAAc,EACd,WAAW,EACX,SAAS,EACT,cAAc,EACd,aAAa,EACb,sBAAsB,EACtB,qBAAqB,EACrB,aAAa,EACb,WAAW,EACX,kBAAkB,EACrB,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,SAAS,EAAE,mBAAmB,EAAE,iBAAiB,EACjD,eAAe,EAAE,UAAU,EAAE,KAAK,EAAE,gBAAgB,EACpD,YAAY,EACZ,cAAc,EACd,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