web-manager 4.1.25 → 4.1.26

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.
@@ -26,7 +26,7 @@ class Firestore {
26
26
  }
27
27
 
28
28
  // Dynamically import Firestore
29
- const { getFirestore, doc: firestoreDoc, collection: firestoreCollection, getDoc, setDoc, updateDoc, deleteDoc, getDocs, query, where, orderBy, limit, startAt, endAt } = await import('firebase/firestore');
29
+ const { getFirestore, doc: firestoreDoc, collection: firestoreCollection, getDoc, setDoc, updateDoc, deleteDoc, getDocs, query, where, orderBy, limit, startAt, endAt, onSnapshot } = await import('firebase/firestore');
30
30
 
31
31
  // Store references for later use
32
32
  this._firestoreMethods = {
@@ -43,7 +43,8 @@ class Firestore {
43
43
  orderBy,
44
44
  limit,
45
45
  startAt,
46
- endAt
46
+ endAt,
47
+ onSnapshot,
47
48
  };
48
49
 
49
50
  // Initialize Firestore
@@ -112,6 +113,25 @@ class Firestore {
112
113
  await self._ensureInitialized();
113
114
  const docRef = self._firestoreMethods.doc(self._db, docPath);
114
115
  return await self._firestoreMethods.deleteDoc(docRef);
116
+ },
117
+
118
+ onSnapshot(callback, errorCallback) {
119
+ let unsubscribe = function () {};
120
+
121
+ self._ensureInitialized().then(function () {
122
+ const docRef = self._firestoreMethods.doc(self._db, docPath);
123
+
124
+ unsubscribe = self._firestoreMethods.onSnapshot(docRef, function (docSnap) {
125
+ callback({
126
+ exists: () => docSnap.exists(),
127
+ data: () => docSnap.data(),
128
+ id: docSnap.id,
129
+ ref: docRef,
130
+ });
131
+ }, errorCallback);
132
+ });
133
+
134
+ return function () { unsubscribe(); };
115
135
  }
116
136
  };
117
137
  }
@@ -192,51 +212,87 @@ class Firestore {
192
212
  },
193
213
 
194
214
  async get() {
195
- await self._ensureInitialized();
196
- const collRef = self._firestoreMethods.collection(self._db, collectionPath);
197
-
198
- // Build query constraints
199
- const queryConstraints = [];
200
- for (const constraint of constraints) {
201
- switch (constraint.type) {
202
- case 'where':
203
- queryConstraints.push(self._firestoreMethods.where(constraint.field, constraint.operator, constraint.value));
204
- break;
205
- case 'orderBy':
206
- queryConstraints.push(self._firestoreMethods.orderBy(constraint.field, constraint.direction));
207
- break;
208
- case 'limit':
209
- queryConstraints.push(self._firestoreMethods.limit(constraint.count));
210
- break;
211
- case 'startAt':
212
- queryConstraints.push(self._firestoreMethods.startAt(...constraint.values));
213
- break;
214
- case 'endAt':
215
- queryConstraints.push(self._firestoreMethods.endAt(...constraint.values));
216
- break;
217
- }
218
- }
219
-
220
- const q = self._firestoreMethods.query(collRef, ...queryConstraints);
221
- const querySnapshot = await self._firestoreMethods.getDocs(q);
215
+ return self._executeQuery(collectionPath, constraints);
216
+ },
222
217
 
223
- return {
224
- docs: querySnapshot.docs.map(doc => ({
225
- id: doc.id,
226
- data: () => doc.data(),
227
- exists: () => doc.exists(),
228
- ref: doc.ref
229
- })),
230
- size: querySnapshot.size,
231
- empty: querySnapshot.empty,
232
- forEach: (callback) => querySnapshot.forEach(callback)
233
- };
218
+ onSnapshot(callback, errorCallback) {
219
+ let unsubscribe = function () {};
220
+
221
+ self._ensureInitialized().then(function () {
222
+ const collRef = self._firestoreMethods.collection(self._db, collectionPath);
223
+ const queryConstraints = self._buildConstraints(constraints);
224
+ const q = self._firestoreMethods.query(collRef, ...queryConstraints);
225
+
226
+ unsubscribe = self._firestoreMethods.onSnapshot(q, function (querySnapshot) {
227
+ callback({
228
+ docs: querySnapshot.docs.map(doc => ({
229
+ id: doc.id,
230
+ data: () => doc.data(),
231
+ exists: () => doc.exists(),
232
+ ref: doc.ref,
233
+ })),
234
+ size: querySnapshot.size,
235
+ empty: querySnapshot.empty,
236
+ forEach: (cb) => querySnapshot.forEach(cb),
237
+ });
238
+ }, errorCallback);
239
+ });
240
+
241
+ return function () { unsubscribe(); };
234
242
  }
235
243
  };
236
244
 
237
245
  return queryBuilder;
238
246
  }
239
247
 
248
+ // Build query constraint objects from constraint descriptors
249
+ _buildConstraints(constraints) {
250
+ const queryConstraints = [];
251
+
252
+ for (const constraint of constraints) {
253
+ switch (constraint.type) {
254
+ case 'where':
255
+ queryConstraints.push(this._firestoreMethods.where(constraint.field, constraint.operator, constraint.value));
256
+ break;
257
+ case 'orderBy':
258
+ queryConstraints.push(this._firestoreMethods.orderBy(constraint.field, constraint.direction));
259
+ break;
260
+ case 'limit':
261
+ queryConstraints.push(this._firestoreMethods.limit(constraint.count));
262
+ break;
263
+ case 'startAt':
264
+ queryConstraints.push(this._firestoreMethods.startAt(...constraint.values));
265
+ break;
266
+ case 'endAt':
267
+ queryConstraints.push(this._firestoreMethods.endAt(...constraint.values));
268
+ break;
269
+ }
270
+ }
271
+
272
+ return queryConstraints;
273
+ }
274
+
275
+ // Execute a query and return formatted results
276
+ async _executeQuery(collectionPath, constraints) {
277
+ await this._ensureInitialized();
278
+ const collRef = this._firestoreMethods.collection(this._db, collectionPath);
279
+ const queryConstraints = this._buildConstraints(constraints);
280
+ const q = this._firestoreMethods.query(collRef, ...queryConstraints);
281
+ const querySnapshot = await this._firestoreMethods.getDocs(q);
282
+
283
+ return {
284
+ docs: querySnapshot.docs.map(doc => ({
285
+ id: doc.id,
286
+ data: () => doc.data(),
287
+ exists: () => doc.exists(),
288
+ ref: doc.ref,
289
+ })),
290
+ size: querySnapshot.size,
291
+ empty: querySnapshot.empty,
292
+ forEach: (callback) => querySnapshot.forEach(callback),
293
+ };
294
+ }
295
+
240
296
  // Helper to generate document IDs (similar to Firebase auto-generated IDs)
241
297
  _generateId() {
242
298
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-manager",
3
- "version": "4.1.25",
3
+ "version": "4.1.26",
4
4
  "description": "Easily access important variables such as the query string, current domain, and current page in a single object.",
5
5
  "main": "dist/index.js",
6
6
  "module": "src/index.js",