@hyper-fetch/firebase-admin 9.9.0 → 10.1.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/README.md +111 -63
- package/__tests__/features/constraints/constraints.base.spec.ts +44 -0
- package/__tests__/features/constraints/listener-extra.types.spec.ts +14 -0
- package/__tests__/features/constraints/query-params.types.spec.ts +83 -0
- package/__tests__/features/firestore/admin/constraints.admin.spec.ts +1 -0
- package/__tests__/features/firestore/admin/sockets.base.spec.ts +4 -1
- package/__tests__/features/firestore/shared/constraints.shared.tests.ts +87 -6
- package/__tests__/features/firestore/shared/methods/get-doc.test.suite.ts +15 -0
- package/__tests__/features/firestore/shared/methods/get-docs.test.suite.ts +1 -1
- package/__tests__/features/firestore/shared/methods/on-snapshot.test.suite.ts +79 -5
- package/__tests__/features/firestore/shared/methods/update-doc.test.suite.ts +1 -0
- package/__tests__/features/firestore/shared/methods.shared.tests.ts +2 -2
- package/__tests__/features/realtime/shared/constraints.shared.tests.ts +58 -8
- package/__tests__/features/realtime/shared/methods/get.test.suite.ts +23 -0
- package/__tests__/features/realtime/shared/methods/on-value.test.suite.ts +108 -5
- package/__tests__/features/realtime/shared/methods/push.test.suite.ts +3 -3
- package/__tests__/features/realtime/shared/methods/remove.test.suite.ts +3 -3
- package/__tests__/features/realtime/shared/methods/update.test.suite.ts +3 -3
- package/__tests__/shared/request-events.shared.ts +1 -4
- package/dist/adapter/adapter.d.ts +19 -0
- package/dist/adapter/adapter.d.ts.map +1 -1
- package/dist/adapter/adapter.sockets.d.ts +17 -0
- package/dist/adapter/adapter.sockets.d.ts.map +1 -1
- package/dist/adapter/types/adapter.firestore.types.d.ts +28 -6
- package/dist/adapter/types/adapter.firestore.types.d.ts.map +1 -1
- package/dist/adapter/types/adapter.realtime.types.d.ts +26 -6
- package/dist/adapter/types/adapter.realtime.types.d.ts.map +1 -1
- package/dist/adapter/types/firestore.socket.types.d.ts +6 -4
- package/dist/adapter/types/firestore.socket.types.d.ts.map +1 -1
- package/dist/adapter/types/realtime.socket.types.d.ts +2 -4
- package/dist/adapter/types/realtime.socket.types.d.ts.map +1 -1
- package/dist/constraints/constraints.builder.d.ts +98 -0
- package/dist/constraints/constraints.builder.d.ts.map +1 -0
- package/dist/constraints/firebase.constraints.d.ts +109 -1
- package/dist/constraints/firebase.constraints.d.ts.map +1 -1
- package/dist/constraints/index.d.ts +1 -0
- package/dist/constraints/index.d.ts.map +1 -1
- package/dist/firestore/firestore.sockets.d.ts.map +1 -1
- package/dist/index.mjs +277 -94
- package/dist/index.mjs.map +1 -1
- package/dist/realtime/realtime.methods.d.ts +7 -5
- package/dist/realtime/realtime.methods.d.ts.map +1 -1
- package/dist/realtime/realtime.sockets.d.ts.map +1 -1
- package/dist/utils/misc.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.sockets.ts +17 -0
- package/src/adapter/adapter.ts +77 -46
- package/src/adapter/types/adapter.firestore.types.ts +40 -7
- package/src/adapter/types/adapter.realtime.types.ts +39 -7
- package/src/adapter/types/firestore.socket.types.ts +10 -2
- package/src/adapter/types/realtime.socket.types.ts +6 -3
- package/src/constraints/constraints.builder.ts +214 -0
- package/src/constraints/firebase.constraints.ts +111 -3
- package/src/constraints/index.ts +1 -0
- package/src/firestore/firestore.methods.ts +8 -8
- package/src/firestore/firestore.sockets.ts +65 -56
- package/src/firestore/utils/constraints.utils.ts +4 -4
- package/src/realtime/realtime.methods.ts +19 -20
- package/src/realtime/realtime.sockets.ts +57 -52
- package/src/realtime/utils/constraints.utils.ts +10 -10
- package/src/utils/misc.ts +1 -1
- package/vite.config.ts +2 -0
package/dist/index.mjs
CHANGED
|
@@ -30,45 +30,6 @@ var isDocOrQuery = (fullUrl) => {
|
|
|
30
30
|
return fullUrl.replace(/^\/|\/$/g, "").split("/").length % 2 === 0 ? "doc" : "query";
|
|
31
31
|
};
|
|
32
32
|
//#endregion
|
|
33
|
-
//#region src/firestore/utils/result.utils.ts
|
|
34
|
-
var getOrderedResultFirestore = (snapshot) => {
|
|
35
|
-
const result = [];
|
|
36
|
-
snapshot.docs.forEach((d) => {
|
|
37
|
-
result.push({
|
|
38
|
-
...d.data(),
|
|
39
|
-
__key: d.id
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
return result;
|
|
43
|
-
};
|
|
44
|
-
var getGroupedResultFirestore = (snapshot) => {
|
|
45
|
-
const groupedResult = {
|
|
46
|
-
added: [],
|
|
47
|
-
modified: [],
|
|
48
|
-
removed: []
|
|
49
|
-
};
|
|
50
|
-
snapshot.docChanges().forEach((change) => {
|
|
51
|
-
groupedResult[change.type].push(change.doc.data());
|
|
52
|
-
});
|
|
53
|
-
return [
|
|
54
|
-
...groupedResult.added,
|
|
55
|
-
...groupedResult.modified,
|
|
56
|
-
...groupedResult.removed
|
|
57
|
-
].length > 0 ? groupedResult : null;
|
|
58
|
-
};
|
|
59
|
-
//#endregion
|
|
60
|
-
//#region src/firestore/utils/ref.utils.ts
|
|
61
|
-
var getRef = (db, fullUrl) => {
|
|
62
|
-
return fullUrl.replace(/^\/|\/$/g, "").split("/").map((element, index) => {
|
|
63
|
-
return index % 2 === 0 ? ["collection", element] : ["doc", element];
|
|
64
|
-
}).reduce((_db, value) => {
|
|
65
|
-
const [method, pathPart] = value;
|
|
66
|
-
if (method === "doc" && "doc" in _db) return _db.doc(pathPart);
|
|
67
|
-
if (method === "collection" && "collection" in _db) return _db.collection(pathPart);
|
|
68
|
-
return _db;
|
|
69
|
-
}, db);
|
|
70
|
-
};
|
|
71
|
-
//#endregion
|
|
72
33
|
//#region src/constraints/firebase.constraints.ts
|
|
73
34
|
var SharedQueryConstraints = /* @__PURE__ */ function(SharedQueryConstraints) {
|
|
74
35
|
SharedQueryConstraints["START_AT"] = "startAt";
|
|
@@ -92,6 +53,15 @@ var FirestoreQueryConstraints = /* @__PURE__ */ function(FirestoreQueryConstrain
|
|
|
92
53
|
FirestoreQueryConstraints["LIMIT"] = "limit";
|
|
93
54
|
return FirestoreQueryConstraints;
|
|
94
55
|
}({});
|
|
56
|
+
/**
|
|
57
|
+
* Firestore filter, the HyperFetch form of Firestore's `where()`.
|
|
58
|
+
*
|
|
59
|
+
* @param fieldPath - Field to compare, dot notation reaches nested fields (`"address.city"`)
|
|
60
|
+
* @param opStr - Firestore operator such as `"=="`, `">="`, `"in"` or `"array-contains"`
|
|
61
|
+
* @param value - Value to compare the field with
|
|
62
|
+
* @example
|
|
63
|
+
* getTeas.send({ queryParams: [$where("type", "==", "Green"), $where("year", ">=", 2020)] });
|
|
64
|
+
*/
|
|
95
65
|
var $where = (fieldPath, opStr, value) => {
|
|
96
66
|
return {
|
|
97
67
|
toString: () => `${FirestoreQueryConstraints.WHERE}_${fieldPath}${opStr}${value}`,
|
|
@@ -103,6 +73,15 @@ var $where = (fieldPath, opStr, value) => {
|
|
|
103
73
|
]
|
|
104
74
|
};
|
|
105
75
|
};
|
|
76
|
+
/**
|
|
77
|
+
* Firestore ordering, the HyperFetch form of Firestore's `orderBy()`. Cursors take one value per `$orderBy`, in the same
|
|
78
|
+
* order.
|
|
79
|
+
*
|
|
80
|
+
* @param fieldPath - Field to order by
|
|
81
|
+
* @param directionStr - `"asc"` (default) or `"desc"`
|
|
82
|
+
* @example
|
|
83
|
+
* getTeas.send({ queryParams: [$orderBy("year", "desc"), $orderBy("name")] });
|
|
84
|
+
*/
|
|
106
85
|
var $orderBy = (fieldPath, directionStr) => {
|
|
107
86
|
return {
|
|
108
87
|
toString: () => `${FirestoreQueryConstraints.ORDER_BY}_${fieldPath}_${directionStr || ""}`,
|
|
@@ -110,6 +89,13 @@ var $orderBy = (fieldPath, directionStr) => {
|
|
|
110
89
|
values: [fieldPath, directionStr]
|
|
111
90
|
};
|
|
112
91
|
};
|
|
92
|
+
/**
|
|
93
|
+
* Firestore limit, the HyperFetch form of Firestore's `limit()`.
|
|
94
|
+
*
|
|
95
|
+
* @param num - Maximum number of documents to return
|
|
96
|
+
* @example
|
|
97
|
+
* getTeas.send({ queryParams: [$orderBy("year"), $limit(20)] });
|
|
98
|
+
*/
|
|
113
99
|
var $limit = (num) => {
|
|
114
100
|
return {
|
|
115
101
|
toString: () => `${FirestoreQueryConstraints.LIMIT}_${num}`,
|
|
@@ -131,10 +117,57 @@ var startEndConstraintsImpl = (constraintType) => {
|
|
|
131
117
|
};
|
|
132
118
|
};
|
|
133
119
|
};
|
|
120
|
+
/**
|
|
121
|
+
* Cursor that starts the ordered query at the given values, inclusive.
|
|
122
|
+
*
|
|
123
|
+
* In Firestore pass one value per `$orderBy`, in the same order. In Realtime Database pass the value of the
|
|
124
|
+
* `$orderBy*` field and, optionally, a key that breaks ties between equal values.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* // Firestore
|
|
128
|
+
* getTeas.send({ queryParams: [$orderBy("year"), $orderBy("name"), $startAt(2022, "Bi Luo Chun")] });
|
|
129
|
+
* // Realtime Database
|
|
130
|
+
* getTeas.send({ queryParams: [$orderByChild("year"), $startAt(2022)] });
|
|
131
|
+
*/
|
|
134
132
|
var $startAt = startEndConstraintsImpl(SharedQueryConstraints.START_AT);
|
|
133
|
+
/**
|
|
134
|
+
* Cursor that starts the ordered query after the given values, exclusive. Use it to load the next page.
|
|
135
|
+
*
|
|
136
|
+
* In Firestore pass one value per `$orderBy`, in the same order. In Realtime Database pass the value of the
|
|
137
|
+
* `$orderBy*` field and, optionally, a key that breaks ties between equal values.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* const last = page.data?.at(-1);
|
|
141
|
+
* getTeas.send({ queryParams: [$orderBy("year"), $startAfter(last?.year), $limit(20)] });
|
|
142
|
+
*/
|
|
135
143
|
var $startAfter = startEndConstraintsImpl(SharedQueryConstraints.START_AFTER);
|
|
144
|
+
/**
|
|
145
|
+
* Cursor that ends the ordered query at the given values, inclusive.
|
|
146
|
+
*
|
|
147
|
+
* In Firestore pass one value per `$orderBy`, in the same order. In Realtime Database pass the value of the
|
|
148
|
+
* `$orderBy*` field and, optionally, a key that breaks ties between equal values.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* getTeas.send({ queryParams: [$orderBy("year"), $endAt(2011)] });
|
|
152
|
+
*/
|
|
136
153
|
var $endAt = startEndConstraintsImpl(SharedQueryConstraints.END_AT);
|
|
154
|
+
/**
|
|
155
|
+
* Cursor that ends the ordered query before the given values, exclusive.
|
|
156
|
+
*
|
|
157
|
+
* In Firestore pass one value per `$orderBy`, in the same order. In Realtime Database pass the value of the
|
|
158
|
+
* `$orderBy*` field and, optionally, a key that breaks ties between equal values.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* getTeas.send({ queryParams: [$orderBy("year"), $endBefore(2011)] });
|
|
162
|
+
*/
|
|
137
163
|
var $endBefore = startEndConstraintsImpl(SharedQueryConstraints.END_BEFORE);
|
|
164
|
+
/**
|
|
165
|
+
* Realtime Database ordering by a child value, the HyperFetch form of `orderByChild()`. A query can have one `$orderBy*`.
|
|
166
|
+
*
|
|
167
|
+
* @param path - Child path to order by, for example `"year"` or `"author/name"`
|
|
168
|
+
* @example
|
|
169
|
+
* getTeas.send({ queryParams: [$orderByChild("year"), $limitToFirst(10)] });
|
|
170
|
+
*/
|
|
138
171
|
var $orderByChild = (path) => {
|
|
139
172
|
return {
|
|
140
173
|
toString: () => `${RealtimeQueryConstraints.ORDER_BY_CHILD}_${path}`,
|
|
@@ -142,6 +175,12 @@ var $orderByChild = (path) => {
|
|
|
142
175
|
values: [path]
|
|
143
176
|
};
|
|
144
177
|
};
|
|
178
|
+
/**
|
|
179
|
+
* Realtime Database ordering by key, the HyperFetch form of `orderByKey()`. A query can have one `$orderBy*`.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* getTeas.send({ queryParams: [$orderByKey(), $limitToLast(10)] });
|
|
183
|
+
*/
|
|
145
184
|
var $orderByKey = () => {
|
|
146
185
|
return {
|
|
147
186
|
toString: () => `${RealtimeQueryConstraints.ORDER_BY_KEY}`,
|
|
@@ -149,6 +188,13 @@ var $orderByKey = () => {
|
|
|
149
188
|
values: []
|
|
150
189
|
};
|
|
151
190
|
};
|
|
191
|
+
/**
|
|
192
|
+
* Realtime Database ordering by the child values themselves, the HyperFetch form of `orderByValue()`. A query can have
|
|
193
|
+
* one `$orderBy*`.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* getScores.send({ queryParams: [$orderByValue(), $limitToLast(3)] });
|
|
197
|
+
*/
|
|
152
198
|
var $orderByValue = () => {
|
|
153
199
|
return {
|
|
154
200
|
toString: () => `${RealtimeQueryConstraints.ORDER_BY_VALUE}`,
|
|
@@ -156,6 +202,13 @@ var $orderByValue = () => {
|
|
|
156
202
|
values: []
|
|
157
203
|
};
|
|
158
204
|
};
|
|
205
|
+
/**
|
|
206
|
+
* Realtime Database limit from the start of the ordered list, the HyperFetch form of `limitToFirst()`.
|
|
207
|
+
*
|
|
208
|
+
* @param num - Maximum number of children to return
|
|
209
|
+
* @example
|
|
210
|
+
* getTeas.send({ queryParams: [$orderByChild("year"), $limitToFirst(5)] });
|
|
211
|
+
*/
|
|
159
212
|
var $limitToFirst = (num) => {
|
|
160
213
|
return {
|
|
161
214
|
toString: () => `${RealtimeQueryConstraints.LIMIT_TO_FIRST}_${num}`,
|
|
@@ -163,6 +216,13 @@ var $limitToFirst = (num) => {
|
|
|
163
216
|
values: [num]
|
|
164
217
|
};
|
|
165
218
|
};
|
|
219
|
+
/**
|
|
220
|
+
* Realtime Database limit from the end of the ordered list, the HyperFetch form of `limitToLast()`.
|
|
221
|
+
*
|
|
222
|
+
* @param num - Maximum number of children to return
|
|
223
|
+
* @example
|
|
224
|
+
* getMessages.send({ queryParams: [$orderByChild("sentAt"), $limitToLast(50)] });
|
|
225
|
+
*/
|
|
166
226
|
var $limitToLast = (num) => {
|
|
167
227
|
return {
|
|
168
228
|
toString: () => `${RealtimeQueryConstraints.LIMIT_TO_LAST}_${num}`,
|
|
@@ -170,13 +230,106 @@ var $limitToLast = (num) => {
|
|
|
170
230
|
values: [num]
|
|
171
231
|
};
|
|
172
232
|
};
|
|
173
|
-
|
|
233
|
+
/**
|
|
234
|
+
* Realtime Database filter for children equal to a value, the HyperFetch form of `equalTo()`. Combine it with an
|
|
235
|
+
* `$orderBy*` that picks the compared value.
|
|
236
|
+
*
|
|
237
|
+
* @param value - Value the ordered field must equal
|
|
238
|
+
* @param key - Optional key that breaks ties between children with the same value
|
|
239
|
+
* @example
|
|
240
|
+
* getTeas.send({ queryParams: [$orderByChild("type"), $equalTo("Green")] });
|
|
241
|
+
*/
|
|
242
|
+
var $equalTo = (value, key) => {
|
|
174
243
|
return {
|
|
175
|
-
toString: () => `${RealtimeQueryConstraints.
|
|
244
|
+
toString: () => `${RealtimeQueryConstraints.EQUAL_TO}_${value}${key === void 0 ? "" : `_${key}`}`,
|
|
176
245
|
type: RealtimeQueryConstraints.EQUAL_TO,
|
|
177
|
-
values: [value]
|
|
246
|
+
values: key === void 0 ? [value] : [value, key]
|
|
247
|
+
};
|
|
248
|
+
};
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/constraints/constraints.builder.ts
|
|
251
|
+
var firestoreChain = (constraints) => {
|
|
252
|
+
const add = (constraint) => firestoreChain([...constraints, constraint]);
|
|
253
|
+
return {
|
|
254
|
+
where: (field, operator, value) => add($where(field, operator, value)),
|
|
255
|
+
orderBy: (field, direction) => add($orderBy(field, direction)),
|
|
256
|
+
limit: (limit) => add($limit(limit)),
|
|
257
|
+
startAt: (...values) => add($startAt(...values)),
|
|
258
|
+
startAfter: (...values) => add($startAfter(...values)),
|
|
259
|
+
endAt: (...values) => add($endAt(...values)),
|
|
260
|
+
endBefore: (...values) => add($endBefore(...values)),
|
|
261
|
+
toJSON: () => constraints
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
var realtimeChain = (constraints) => {
|
|
265
|
+
const add = (constraint) => realtimeChain([...constraints, constraint]);
|
|
266
|
+
return {
|
|
267
|
+
orderByChild: (path) => add($orderByChild(path)),
|
|
268
|
+
orderByKey: () => add($orderByKey()),
|
|
269
|
+
orderByValue: () => add($orderByValue()),
|
|
270
|
+
limitToFirst: (limit) => add($limitToFirst(limit)),
|
|
271
|
+
limitToLast: (limit) => add($limitToLast(limit)),
|
|
272
|
+
equalTo: (value, key) => add($equalTo(value, key)),
|
|
273
|
+
startAt: (value, key) => add(key === void 0 ? $startAt(value) : $startAt(value, key)),
|
|
274
|
+
startAfter: (value, key) => add(key === void 0 ? $startAfter(value) : $startAfter(value, key)),
|
|
275
|
+
endAt: (value, key) => add(key === void 0 ? $endAt(value) : $endAt(value, key)),
|
|
276
|
+
endBefore: (value, key) => add(key === void 0 ? $endBefore(value) : $endBefore(value, key)),
|
|
277
|
+
toJSON: () => constraints
|
|
178
278
|
};
|
|
179
279
|
};
|
|
280
|
+
/** Start an empty Firestore constraints chain. The adapter passes one to every `queryParams` callback. */
|
|
281
|
+
var createFirestoreConstraints = () => firestoreChain([]);
|
|
282
|
+
/** Start an empty Realtime Database constraints chain. The adapter passes one to every `queryParams` callback. */
|
|
283
|
+
var createRealtimeConstraints = () => realtimeChain([]);
|
|
284
|
+
/** Resolve a `queryParams` callback with a fresh chain into the constraints it collected. */
|
|
285
|
+
var resolveConstraints = (createChain) => (callback) => callback(createChain()).toJSON();
|
|
286
|
+
/**
|
|
287
|
+
* The constraints of `queryParams` or listener options: an array, or the deprecated `{ constraints }` object.
|
|
288
|
+
*/
|
|
289
|
+
var getConstraints = (queryParams) => {
|
|
290
|
+
if (Array.isArray(queryParams)) return queryParams;
|
|
291
|
+
if (queryParams && typeof queryParams === "object" && Array.isArray(queryParams.constraints)) return queryParams.constraints;
|
|
292
|
+
return [];
|
|
293
|
+
};
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region src/firestore/utils/result.utils.ts
|
|
296
|
+
var getOrderedResultFirestore = (snapshot) => {
|
|
297
|
+
const result = [];
|
|
298
|
+
snapshot.docs.forEach((d) => {
|
|
299
|
+
result.push({
|
|
300
|
+
...d.data(),
|
|
301
|
+
__key: d.id
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
return result;
|
|
305
|
+
};
|
|
306
|
+
var getGroupedResultFirestore = (snapshot) => {
|
|
307
|
+
const groupedResult = {
|
|
308
|
+
added: [],
|
|
309
|
+
modified: [],
|
|
310
|
+
removed: []
|
|
311
|
+
};
|
|
312
|
+
snapshot.docChanges().forEach((change) => {
|
|
313
|
+
groupedResult[change.type].push(change.doc.data());
|
|
314
|
+
});
|
|
315
|
+
return [
|
|
316
|
+
...groupedResult.added,
|
|
317
|
+
...groupedResult.modified,
|
|
318
|
+
...groupedResult.removed
|
|
319
|
+
].length > 0 ? groupedResult : null;
|
|
320
|
+
};
|
|
321
|
+
//#endregion
|
|
322
|
+
//#region src/firestore/utils/ref.utils.ts
|
|
323
|
+
var getRef = (db, fullUrl) => {
|
|
324
|
+
return fullUrl.replace(/^\/|\/$/g, "").split("/").map((element, index) => {
|
|
325
|
+
return index % 2 === 0 ? ["collection", element] : ["doc", element];
|
|
326
|
+
}).reduce((_db, value) => {
|
|
327
|
+
const [method, pathPart] = value;
|
|
328
|
+
if (method === "doc" && "doc" in _db) return _db.doc(pathPart);
|
|
329
|
+
if (method === "collection" && "collection" in _db) return _db.collection(pathPart);
|
|
330
|
+
return _db;
|
|
331
|
+
}, db);
|
|
332
|
+
};
|
|
180
333
|
//#endregion
|
|
181
334
|
//#region src/firestore/utils/constraints.utils.ts
|
|
182
335
|
var applyFireStoreAdminConstraint = (collectionRef, { type, values }) => {
|
|
@@ -195,19 +348,19 @@ var applyFireStoreAdminConstraint = (collectionRef, { type, values }) => {
|
|
|
195
348
|
}
|
|
196
349
|
case SharedQueryConstraints.START_AT: {
|
|
197
350
|
const [docOrFields] = values;
|
|
198
|
-
return collectionRef.startAt(docOrFields);
|
|
351
|
+
return collectionRef.startAt(...docOrFields);
|
|
199
352
|
}
|
|
200
353
|
case SharedQueryConstraints.START_AFTER: {
|
|
201
354
|
const [docOrFields] = values;
|
|
202
|
-
return collectionRef.startAfter(docOrFields);
|
|
355
|
+
return collectionRef.startAfter(...docOrFields);
|
|
203
356
|
}
|
|
204
357
|
case SharedQueryConstraints.END_AT: {
|
|
205
358
|
const [docOrFields] = values;
|
|
206
|
-
return collectionRef.endAt(docOrFields);
|
|
359
|
+
return collectionRef.endAt(...docOrFields);
|
|
207
360
|
}
|
|
208
361
|
case SharedQueryConstraints.END_BEFORE: {
|
|
209
362
|
const [docOrFields] = values;
|
|
210
|
-
return collectionRef.endBefore(docOrFields);
|
|
363
|
+
return collectionRef.endBefore(...docOrFields);
|
|
211
364
|
}
|
|
212
365
|
default: throw new Error(`Unknown method ${type}`);
|
|
213
366
|
}
|
|
@@ -223,7 +376,7 @@ var firestoreAdminSockets = (database) => {
|
|
|
223
376
|
return new SocketAdapter({
|
|
224
377
|
name: "firebase-admin-firestore",
|
|
225
378
|
defaultConnected: true
|
|
226
|
-
}).setConnector(({ socket, onReconnect, onListen, onEvent, onError }) => {
|
|
379
|
+
}).setListenerQueryParamsResolver(resolveConstraints(createFirestoreConstraints)).setConnector(({ socket, onReconnect, onListen, onEvent, onError }) => {
|
|
227
380
|
const connect = () => {
|
|
228
381
|
throw new Error("Connect function is not implemented for Firestore Admin socket.");
|
|
229
382
|
};
|
|
@@ -240,7 +393,7 @@ var firestoreAdminSockets = (database) => {
|
|
|
240
393
|
const fullUrl = socket.url + listener.topic;
|
|
241
394
|
const { options } = listener;
|
|
242
395
|
let pathRef = getRef(database, fullUrl);
|
|
243
|
-
if (pathRef instanceof CollectionReference) pathRef = applyFireStoreAdminConstraints(pathRef, options?.constraints
|
|
396
|
+
if (pathRef instanceof CollectionReference) pathRef = applyFireStoreAdminConstraints(pathRef, getConstraints(listener.queryParams ?? options?.constraints));
|
|
244
397
|
let unsubscribe = () => {};
|
|
245
398
|
let unmount = () => {};
|
|
246
399
|
let clearListeners = () => {};
|
|
@@ -251,7 +404,7 @@ var firestoreAdminSockets = (database) => {
|
|
|
251
404
|
} : null;
|
|
252
405
|
const response = snapshot instanceof DocumentSnapshot ? getSnapshotData(snapshot) : getOrderedResultFirestore(snapshot);
|
|
253
406
|
const status = getStatus(response);
|
|
254
|
-
const groupedResult = options?.groupByChangeType === true ? getGroupedResultFirestore(snapshot) : null;
|
|
407
|
+
const groupedResult = options?.groupByChangeType === true && !(snapshot instanceof DocumentSnapshot) ? getGroupedResultFirestore(snapshot) : null;
|
|
255
408
|
const extra = {
|
|
256
409
|
ref: pathRef,
|
|
257
410
|
snapshot,
|
|
@@ -259,10 +412,6 @@ var firestoreAdminSockets = (database) => {
|
|
|
259
412
|
groupedResult,
|
|
260
413
|
status
|
|
261
414
|
};
|
|
262
|
-
callback({
|
|
263
|
-
data: response,
|
|
264
|
-
extra
|
|
265
|
-
});
|
|
266
415
|
onEvent({
|
|
267
416
|
topic: listener.topic,
|
|
268
417
|
data: response,
|
|
@@ -328,31 +477,31 @@ var getFirestoreAdminMethods = ({ database, url, onSuccess, onError, onResponseS
|
|
|
328
477
|
}
|
|
329
478
|
};
|
|
330
479
|
},
|
|
331
|
-
setDoc: async ({
|
|
480
|
+
setDoc: async ({ payload, options }) => {
|
|
332
481
|
const path = getRef(database, cleanUrl);
|
|
333
482
|
const merge = options?.merge === true;
|
|
334
483
|
return {
|
|
335
|
-
result: await path.set(
|
|
484
|
+
result: await path.set(payload, { merge }),
|
|
336
485
|
status: "success",
|
|
337
486
|
extra: { ref: path }
|
|
338
487
|
};
|
|
339
488
|
},
|
|
340
|
-
addDoc: async ({
|
|
341
|
-
const docRef = await getRef(database, cleanUrl).add(
|
|
489
|
+
addDoc: async ({ payload }) => {
|
|
490
|
+
const docRef = await getRef(database, cleanUrl).add(payload);
|
|
342
491
|
return {
|
|
343
492
|
result: {
|
|
344
|
-
...
|
|
493
|
+
...payload,
|
|
345
494
|
__key: docRef.id
|
|
346
495
|
},
|
|
347
496
|
status: "success",
|
|
348
497
|
extra: { ref: docRef }
|
|
349
498
|
};
|
|
350
499
|
},
|
|
351
|
-
updateDoc: async ({
|
|
500
|
+
updateDoc: async ({ payload }) => {
|
|
352
501
|
const path = getRef(database, cleanUrl);
|
|
353
|
-
await path.update(
|
|
502
|
+
await path.update(payload);
|
|
354
503
|
return {
|
|
355
|
-
result:
|
|
504
|
+
result: payload,
|
|
356
505
|
status: "success",
|
|
357
506
|
extra: { ref: path }
|
|
358
507
|
};
|
|
@@ -414,20 +563,20 @@ var applyRealtimeAdminConstraint = (ref, { type, values }) => {
|
|
|
414
563
|
case RealtimeQueryConstraints.ORDER_BY_KEY: return ref.orderByKey();
|
|
415
564
|
case RealtimeQueryConstraints.ORDER_BY_VALUE: return ref.orderByValue();
|
|
416
565
|
case SharedQueryConstraints.START_AT: {
|
|
417
|
-
const [[value]] = values;
|
|
418
|
-
return ref.startAt(value);
|
|
566
|
+
const [[value, key]] = values;
|
|
567
|
+
return ref.startAt(value, key);
|
|
419
568
|
}
|
|
420
569
|
case SharedQueryConstraints.START_AFTER: {
|
|
421
|
-
const [[value]] = values;
|
|
422
|
-
return ref.startAfter(value);
|
|
570
|
+
const [[value, key]] = values;
|
|
571
|
+
return ref.startAfter(value, key);
|
|
423
572
|
}
|
|
424
573
|
case SharedQueryConstraints.END_AT: {
|
|
425
|
-
const [[value]] = values;
|
|
426
|
-
return ref.endAt(value);
|
|
574
|
+
const [[value, key]] = values;
|
|
575
|
+
return ref.endAt(value, key);
|
|
427
576
|
}
|
|
428
577
|
case SharedQueryConstraints.END_BEFORE: {
|
|
429
|
-
const [[value]] = values;
|
|
430
|
-
return ref.endBefore(value);
|
|
578
|
+
const [[value, key]] = values;
|
|
579
|
+
return ref.endBefore(value, key);
|
|
431
580
|
}
|
|
432
581
|
case RealtimeQueryConstraints.LIMIT_TO_FIRST: {
|
|
433
582
|
const [value] = values;
|
|
@@ -438,8 +587,8 @@ var applyRealtimeAdminConstraint = (ref, { type, values }) => {
|
|
|
438
587
|
return ref.limitToLast(value);
|
|
439
588
|
}
|
|
440
589
|
case RealtimeQueryConstraints.EQUAL_TO: {
|
|
441
|
-
const [value] = values;
|
|
442
|
-
return ref.equalTo(value);
|
|
590
|
+
const [value, key] = values;
|
|
591
|
+
return ref.equalTo(value, key);
|
|
443
592
|
}
|
|
444
593
|
default: throw new Error(`Unknown method ${type}`);
|
|
445
594
|
}
|
|
@@ -468,19 +617,19 @@ var getRealtimeDbAdminMethods = ({ database, url, onSuccess, onError, onResponse
|
|
|
468
617
|
}
|
|
469
618
|
};
|
|
470
619
|
},
|
|
471
|
-
set: async ({
|
|
472
|
-
await path.set(
|
|
620
|
+
set: async ({ payload }) => {
|
|
621
|
+
await path.set(payload);
|
|
473
622
|
return {
|
|
474
|
-
result:
|
|
623
|
+
result: payload,
|
|
475
624
|
status: "success",
|
|
476
625
|
extra: { ref: path }
|
|
477
626
|
};
|
|
478
627
|
},
|
|
479
|
-
push: async ({
|
|
480
|
-
const resRef = await path.push(
|
|
628
|
+
push: async ({ payload }) => {
|
|
629
|
+
const resRef = await path.push(payload);
|
|
481
630
|
return {
|
|
482
631
|
result: {
|
|
483
|
-
...
|
|
632
|
+
...payload,
|
|
484
633
|
__key: resRef.key
|
|
485
634
|
},
|
|
486
635
|
status: "success",
|
|
@@ -490,10 +639,10 @@ var getRealtimeDbAdminMethods = ({ database, url, onSuccess, onError, onResponse
|
|
|
490
639
|
}
|
|
491
640
|
};
|
|
492
641
|
},
|
|
493
|
-
update: async ({
|
|
494
|
-
await path.update(
|
|
642
|
+
update: async ({ payload }) => {
|
|
643
|
+
await path.update(payload);
|
|
495
644
|
return {
|
|
496
|
-
result:
|
|
645
|
+
result: payload,
|
|
497
646
|
status: "success",
|
|
498
647
|
extra: { ref: path }
|
|
499
648
|
};
|
|
@@ -537,7 +686,7 @@ var realtimeSocketsAdmin = (database) => {
|
|
|
537
686
|
return new SocketAdapter({
|
|
538
687
|
name: "firebase-admin-realtime",
|
|
539
688
|
defaultConnected: true
|
|
540
|
-
}).setConnector(({ socket, onReconnect, onConnect, onConnected, onDisconnect, onDisconnected, onListen, onEvent, onError }) => {
|
|
689
|
+
}).setListenerQueryParamsResolver(resolveConstraints(createRealtimeConstraints)).setConnector(({ socket, onReconnect, onConnect, onConnected, onDisconnect, onDisconnected, onListen, onEvent, onError }) => {
|
|
541
690
|
const connect = async () => {
|
|
542
691
|
if (await onConnect()) {
|
|
543
692
|
database.goOnline();
|
|
@@ -560,8 +709,9 @@ var realtimeSocketsAdmin = (database) => {
|
|
|
560
709
|
const path = database.ref(fullUrl);
|
|
561
710
|
const { options } = listener;
|
|
562
711
|
const onlyOnce = options?.onlyOnce || false;
|
|
563
|
-
const q = applyRealtimeAdminConstraints(path, options?.constraints
|
|
564
|
-
|
|
712
|
+
const q = applyRealtimeAdminConstraints(path, getConstraints(listener.queryParams ?? options?.constraints));
|
|
713
|
+
const method = onlyOnce === true ? "once" : "on";
|
|
714
|
+
const onValue = (snapshot) => {
|
|
565
715
|
const getSnapshotData = (s) => s.val() ? {
|
|
566
716
|
...s.val(),
|
|
567
717
|
__key: s.key
|
|
@@ -572,19 +722,16 @@ var realtimeSocketsAdmin = (database) => {
|
|
|
572
722
|
snapshot,
|
|
573
723
|
status: getStatus(response)
|
|
574
724
|
};
|
|
575
|
-
callback({
|
|
576
|
-
data: response,
|
|
577
|
-
extra
|
|
578
|
-
});
|
|
579
725
|
onEvent({
|
|
580
726
|
topic: listener.topic,
|
|
581
727
|
data: response,
|
|
582
728
|
extra
|
|
583
729
|
});
|
|
584
|
-
}
|
|
730
|
+
};
|
|
731
|
+
q[method]("value", onValue, (error) => {
|
|
585
732
|
onError({ error });
|
|
586
733
|
});
|
|
587
|
-
const unsubscribe = () => q.off("value");
|
|
734
|
+
const unsubscribe = () => q.off("value", onValue);
|
|
588
735
|
const unmount = onListen({
|
|
589
736
|
listener,
|
|
590
737
|
callback,
|
|
@@ -610,20 +757,56 @@ var realtimeSocketsAdmin = (database) => {
|
|
|
610
757
|
};
|
|
611
758
|
//#endregion
|
|
612
759
|
//#region src/adapter/adapter.sockets.ts
|
|
760
|
+
/**
|
|
761
|
+
* Socket adapter for live data from the Firebase Admin SDK: Firestore `onSnapshot` or Realtime Database `on("value")`.
|
|
762
|
+
*
|
|
763
|
+
* Create one adapter per socket. The listener topic is appended to `socket.url`, listener `options` take `constraints`
|
|
764
|
+
* plus `groupByChangeType` (Firestore) or `onlyOnce` (Realtime Database), and every snapshot is delivered as the full
|
|
765
|
+
* current result. The Firestore socket cannot `connect()` or `disconnect()`; stop listeners with the function `listen()`
|
|
766
|
+
* returns.
|
|
767
|
+
*
|
|
768
|
+
* @param database - Instance from `getFirestore()` or `getDatabase()` of `firebase-admin`
|
|
769
|
+
* @example
|
|
770
|
+
* const socket = new Socket({ url: "jobs/", adapter: FirebaseSocketsAdminAdapter(getFirestore()) });
|
|
771
|
+
* const onQueued = socket.createListener<Job[]>()({
|
|
772
|
+
* topic: "",
|
|
773
|
+
* options: { constraints: [$where("state", "==", "queued")] },
|
|
774
|
+
* });
|
|
775
|
+
* const stop = onQueued.listen(({ data }) => process(data));
|
|
776
|
+
*/
|
|
613
777
|
var FirebaseSocketsAdminAdapter = (database) => {
|
|
614
778
|
if (database instanceof Firestore) return firestoreAdminSockets(database);
|
|
615
779
|
return realtimeSocketsAdmin(database);
|
|
616
780
|
};
|
|
617
781
|
//#endregion
|
|
618
782
|
//#region src/adapter/adapter.ts
|
|
783
|
+
/**
|
|
784
|
+
* Request adapter for the Firebase Admin SDK. Pass a Firestore or Realtime Database instance from `firebase-admin` and
|
|
785
|
+
* the adapter picks the matching methods and types. Admin access bypasses security rules.
|
|
786
|
+
*
|
|
787
|
+
* - Firestore methods: `getDoc` (default), `getDocs`, `addDoc`, `setDoc`, `updateDoc`, `deleteDoc`
|
|
788
|
+
* - Realtime Database methods: `get` (default), `set`, `push`, `update`, `remove`
|
|
789
|
+
*
|
|
790
|
+
* The full path is `client.url + endpoint`. Constraints go in `queryParams` as an array. Firebase errors resolve the
|
|
791
|
+
* request with `status: "error"`; `send()` never rejects.
|
|
792
|
+
*
|
|
793
|
+
* @param database - Instance from `getFirestore()` or `getDatabase()` of `firebase-admin`
|
|
794
|
+
* @example
|
|
795
|
+
* const client = new Client({ url: "orders/" }).setAdapter(FirebaseAdminAdapter(getFirestore()));
|
|
796
|
+
* const getOpenOrders = client.createRequest<{ response: Order[]; queryParams?: FirestoreQueryParams }>()({
|
|
797
|
+
* endpoint: "",
|
|
798
|
+
* method: "getDocs",
|
|
799
|
+
* });
|
|
800
|
+
* const { data } = await getOpenOrders.send({ queryParams: [$where("status", "==", "open")] });
|
|
801
|
+
*/
|
|
619
802
|
var FirebaseAdminAdapter = (database) => {
|
|
620
803
|
return new Adapter({
|
|
621
804
|
name: "firebase-admin",
|
|
622
|
-
defaultMethod:
|
|
805
|
+
defaultMethod: database instanceof Firestore ? FirestoreMethods.getDoc : RealtimeDBMethods.get,
|
|
623
806
|
defaultExtra: {},
|
|
624
807
|
systemErrorStatus: "error",
|
|
625
808
|
systemErrorExtra: {}
|
|
626
|
-
}).setFetcher(async ({ request, onSuccess, onError, onResponseStart, onResponseEnd, onRequestStart, onRequestEnd }) => {
|
|
809
|
+
}).setQueryParamsResolver(resolveConstraints(!(database instanceof Firestore) ? createRealtimeConstraints : createFirestoreConstraints)).setFetcher(async ({ request, onSuccess, onError, onResponseStart, onResponseEnd, onRequestStart, onRequestEnd }) => {
|
|
627
810
|
const fullUrl = `${request.client.url}${request.endpoint}`;
|
|
628
811
|
if (database instanceof Firestore) {
|
|
629
812
|
const { method = FirestoreMethods.getDocs, queryParams, payload, options } = request;
|
|
@@ -639,7 +822,7 @@ var FirebaseAdminAdapter = (database) => {
|
|
|
639
822
|
});
|
|
640
823
|
if (!Object.values(FirestoreMethods).includes(method)) throw new Error(`Cannot find method ${method} in Firestore available methods.`);
|
|
641
824
|
availableMethods(method, {
|
|
642
|
-
constraints: queryParams
|
|
825
|
+
constraints: getConstraints(queryParams),
|
|
643
826
|
payload,
|
|
644
827
|
options
|
|
645
828
|
});
|
|
@@ -657,7 +840,7 @@ var FirebaseAdminAdapter = (database) => {
|
|
|
657
840
|
});
|
|
658
841
|
if (!Object.values(RealtimeDBMethods).includes(method)) throw new Error(`Cannot find method ${method} in Realtime database available methods.`);
|
|
659
842
|
availableMethods(method, {
|
|
660
|
-
constraints: queryParams
|
|
843
|
+
constraints: getConstraints(queryParams),
|
|
661
844
|
options,
|
|
662
845
|
payload
|
|
663
846
|
});
|
|
@@ -665,6 +848,6 @@ var FirebaseAdminAdapter = (database) => {
|
|
|
665
848
|
});
|
|
666
849
|
};
|
|
667
850
|
//#endregion
|
|
668
|
-
export { $endAt, $endBefore, $equalTo, $limit, $limitToFirst, $limitToLast, $orderBy, $orderByChild, $orderByKey, $orderByValue, $startAfter, $startAt, $where, FirebaseAdminAdapter, FirebaseSocketsAdminAdapter, FirestoreMethods, FirestoreQueryConstraints, RealtimeDBMethods, RealtimeQueryConstraints, SharedQueryConstraints, applyFireStoreAdminConstraint, applyFireStoreAdminConstraints, applyRealtimeAdminConstraint, applyRealtimeAdminConstraints, firestoreAdminSockets, getFirestoreAdminMethods, getGroupedResultFirestore, getOrderedResultFirestore, getOrderedResultRealtime, getRealtimeDbAdminMethods, getRef, realtimeSocketsAdmin };
|
|
851
|
+
export { $endAt, $endBefore, $equalTo, $limit, $limitToFirst, $limitToLast, $orderBy, $orderByChild, $orderByKey, $orderByValue, $startAfter, $startAt, $where, FirebaseAdminAdapter, FirebaseSocketsAdminAdapter, FirestoreMethods, FirestoreQueryConstraints, RealtimeDBMethods, RealtimeQueryConstraints, SharedQueryConstraints, applyFireStoreAdminConstraint, applyFireStoreAdminConstraints, applyRealtimeAdminConstraint, applyRealtimeAdminConstraints, createFirestoreConstraints, createRealtimeConstraints, firestoreAdminSockets, getConstraints, getFirestoreAdminMethods, getGroupedResultFirestore, getOrderedResultFirestore, getOrderedResultRealtime, getRealtimeDbAdminMethods, getRef, realtimeSocketsAdmin, resolveConstraints };
|
|
669
852
|
|
|
670
853
|
//# sourceMappingURL=index.mjs.map
|