@knymbus/firestoredb 1.2.2 → 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.
|
@@ -2,6 +2,25 @@ import type { QueryConstraint } from "firebase/firestore";
|
|
|
2
2
|
import { FirestoreSDK, PaginationMetadata, SortDescriptor, WithSystemFields } from "./types";
|
|
3
3
|
import { LRUCache } from './utils';
|
|
4
4
|
import { FirestoreCursor } from "./utils/FirestoreCursor";
|
|
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
|
+
};
|
|
5
24
|
export declare class FirestoreQuery<T> {
|
|
6
25
|
private db;
|
|
7
26
|
private collectionName;
|
|
@@ -42,6 +61,7 @@ export declare class FirestoreQuery<T> {
|
|
|
42
61
|
/** Simple execution (Returns Array) */
|
|
43
62
|
execute(): Promise<WithSystemFields<T>[]>;
|
|
44
63
|
/**
|
|
64
|
+
* @deprecated Use the config-based version for more control over complexity.
|
|
45
65
|
* Paginated execution (Returns Data + Metadata)
|
|
46
66
|
*
|
|
47
67
|
* @example
|
|
@@ -59,10 +79,21 @@ export declare class FirestoreQuery<T> {
|
|
|
59
79
|
|
|
60
80
|
console.log(`Viewing page ${metadata.currentPage} of ${metadata.totalPages}`);
|
|
61
81
|
*/
|
|
62
|
-
paginate(alreadyLoadedCount?: number): Promise<
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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>>;
|
|
66
97
|
stream(): ReadableStream<T & {
|
|
67
98
|
_id: string;
|
|
68
99
|
}>;
|
|
@@ -85,4 +116,5 @@ export declare class FirestoreQuery<T> {
|
|
|
85
116
|
*/
|
|
86
117
|
private _sortObjectKeys;
|
|
87
118
|
}
|
|
119
|
+
export {};
|
|
88
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,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;
|
|
1
|
+
{"version":3,"file":"FirestoreQuery.d.ts","sourceRoot":"","sources":["../../src/FirestoreQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC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AC;IAEhF;;;;MAIE;IACW,QAAQ,CAAC,MAAM,CAAC,EAAE,gBAAgB,GAAG;QAAE,cAAc,EAAE,IAAI,CAAA;KAAE,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC;IAE3G;;OAEG;IACU,QAAQ,CAAC,MAAM,CAAC,EAAE,gBAAgB,GAAG;QAAE,cAAc,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"}
|
|
@@ -103,26 +103,14 @@ class FirestoreQuery {
|
|
|
103
103
|
});
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const { data, metadata } = await Products.find({ category: 'electronics' })
|
|
115
|
-
.sort({ price: 'asc' })
|
|
116
|
-
.limit(10)
|
|
117
|
-
.page(currentPage) // Sets the metadata
|
|
118
|
-
.after(lastId) // Sets the cursor
|
|
119
|
-
.cache() // Speeds up the "Back" button
|
|
120
|
-
.paginate();
|
|
121
|
-
|
|
122
|
-
console.log(`Viewing page ${metadata.currentPage} of ${metadata.totalPages}`);
|
|
123
|
-
*/
|
|
124
|
-
paginate() {
|
|
125
|
-
return __awaiter(this, arguments, void 0, function* (alreadyLoadedCount = 0) {
|
|
106
|
+
// Implementation
|
|
107
|
+
paginate(arg) {
|
|
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" };
|
|
126
114
|
// set the global key for this query
|
|
127
115
|
const key = this._generateCacheKey();
|
|
128
116
|
// 1. Check Cache
|
|
@@ -135,25 +123,54 @@ class FirestoreQuery {
|
|
|
135
123
|
const data = yield this.execute();
|
|
136
124
|
const totalRecords = yield this.countDocs(this.filter);
|
|
137
125
|
const totalPages = this._limit > 0 ? Math.ceil(totalRecords / this._limit) : 1;
|
|
138
|
-
const currentTotalFetched = alreadyLoadedCount + data.length;
|
|
126
|
+
const currentTotalFetched = config.alreadyLoadedCount + data.length;
|
|
139
127
|
const totalRemaining = Math.max(0, totalRecords - currentTotalFetched);
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
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
|
|
150
137
|
};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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;
|
|
156
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
|
|
157
174
|
}
|
|
158
175
|
return result;
|
|
159
176
|
});
|
|
@@ -2,6 +2,25 @@ import type { QueryConstraint } from "firebase/firestore";
|
|
|
2
2
|
import { FirestoreSDK, PaginationMetadata, SortDescriptor, WithSystemFields } from "./types";
|
|
3
3
|
import { LRUCache } from './utils';
|
|
4
4
|
import { FirestoreCursor } from "./utils/FirestoreCursor";
|
|
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
|
+
};
|
|
5
24
|
export declare class FirestoreQuery<T> {
|
|
6
25
|
private db;
|
|
7
26
|
private collectionName;
|
|
@@ -42,6 +61,7 @@ export declare class FirestoreQuery<T> {
|
|
|
42
61
|
/** Simple execution (Returns Array) */
|
|
43
62
|
execute(): Promise<WithSystemFields<T>[]>;
|
|
44
63
|
/**
|
|
64
|
+
* @deprecated Use the config-based version for more control over complexity.
|
|
45
65
|
* Paginated execution (Returns Data + Metadata)
|
|
46
66
|
*
|
|
47
67
|
* @example
|
|
@@ -59,10 +79,21 @@ export declare class FirestoreQuery<T> {
|
|
|
59
79
|
|
|
60
80
|
console.log(`Viewing page ${metadata.currentPage} of ${metadata.totalPages}`);
|
|
61
81
|
*/
|
|
62
|
-
paginate(alreadyLoadedCount?: number): Promise<
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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>>;
|
|
66
97
|
stream(): ReadableStream<T & {
|
|
67
98
|
_id: string;
|
|
68
99
|
}>;
|
|
@@ -85,4 +116,5 @@ export declare class FirestoreQuery<T> {
|
|
|
85
116
|
*/
|
|
86
117
|
private _sortObjectKeys;
|
|
87
118
|
}
|
|
119
|
+
export {};
|
|
88
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,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;
|
|
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,CAAA;CACzB,CAAC;AACF,KAAK,kBAAkB,CAAC,CAAC,IAAI;IACzB,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,EAAE,CAAC;QAAC,QAAQ,EA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
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const { data, metadata } = await Products.find({ category: 'electronics' })
|
|
115
|
-
.sort({ price: 'asc' })
|
|
116
|
-
.limit(10)
|
|
117
|
-
.page(currentPage) // Sets the metadata
|
|
118
|
-
.after(lastId) // Sets the cursor
|
|
119
|
-
.cache() // Speeds up the "Back" button
|
|
120
|
-
.paginate();
|
|
121
|
-
|
|
122
|
-
console.log(`Viewing page ${metadata.currentPage} of ${metadata.totalPages}`);
|
|
123
|
-
*/
|
|
124
|
-
paginate() {
|
|
125
|
-
return __awaiter(this, arguments, void 0, function* (alreadyLoadedCount = 0) {
|
|
106
|
+
// Implementation
|
|
107
|
+
paginate(arg) {
|
|
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" };
|
|
126
114
|
// set the global key for this query
|
|
127
115
|
const key = this._generateCacheKey();
|
|
128
116
|
// 1. Check Cache
|
|
@@ -135,25 +123,54 @@ class FirestoreQuery {
|
|
|
135
123
|
const data = yield this.execute();
|
|
136
124
|
const totalRecords = yield this.countDocs(this.filter);
|
|
137
125
|
const totalPages = this._limit > 0 ? Math.ceil(totalRecords / this._limit) : 1;
|
|
138
|
-
const currentTotalFetched = alreadyLoadedCount + data.length;
|
|
126
|
+
const currentTotalFetched = config.alreadyLoadedCount + data.length;
|
|
139
127
|
const totalRemaining = Math.max(0, totalRecords - currentTotalFetched);
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
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
|
|
150
137
|
};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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;
|
|
156
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
|
|
157
174
|
}
|
|
158
175
|
return result;
|
|
159
176
|
});
|
package/package.json
CHANGED