@edgedev/firebase 1.0.28 → 1.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.
Files changed (2) hide show
  1. package/index.ts +142 -13
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { initializeApp } from "firebase/app";
2
- import { reactive } from "vue";
2
+ import { reactive, ref } from "vue";
3
3
 
4
4
  import {
5
5
  getFirestore,
@@ -19,8 +19,8 @@ import {
19
19
  orderBy,
20
20
  limit,
21
21
  Query,
22
- startAt,
23
- startAfter
22
+ startAfter,
23
+ DocumentData
24
24
  } from "firebase/firestore";
25
25
 
26
26
  import {
@@ -70,6 +70,11 @@ interface Credentials {
70
70
  password: string;
71
71
  }
72
72
 
73
+ interface StaticDataResult {
74
+ data: object;
75
+ next: DocumentData | null;
76
+ }
77
+
73
78
  const firebaseConfig = {
74
79
  apiKey: import.meta.env.VITE_FIREBASE_API_KEY as string,
75
80
  authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN as string,
@@ -184,30 +189,154 @@ export const getStaticData = async (
184
189
  collectionPath: string,
185
190
  queryList: FirestoreQuery[] = [],
186
191
  orderList: FirestoreOrderBy[] = [],
187
- max: 0,
188
- after = ""
189
- ): Promise<{ [key: string]: unknown }> => {
190
- const data: { [key: string]: unknown } = {};
191
- const q = getQuery(collectionPath, queryList, orderList, max, after);
192
+ max = 0,
193
+ last: DocumentData | null = null
194
+ ): Promise<StaticDataResult> => {
195
+ const data: object = {};
196
+
197
+ const q = getQuery(collectionPath, queryList, orderList, max, last);
198
+
192
199
  const docs = await getDocs(q);
200
+ const nextLast: DocumentData = docs.docs[docs.docs.length - 1];
201
+
193
202
  docs.forEach((doc) => {
194
203
  const item = doc.data();
195
204
  item.docId = doc.id;
196
205
  data[doc.id] = item;
197
206
  });
198
- return data;
207
+ return { data, next: nextLast };
199
208
  };
200
209
 
210
+ export class SearchStaticData {
211
+ collectionPath = "";
212
+ queryList: FirestoreQuery[] = [];
213
+ orderList: FirestoreOrderBy[] = [];
214
+ max = 0;
215
+
216
+ data = ref({});
217
+ pagination = ref([]);
218
+ staticIsLastPage = ref<boolean>(true);
219
+ staticIsFirstPage = ref<boolean>(true);
220
+ staticCurrentPage = ref("");
221
+
222
+ prev = async (): Promise<void> => {
223
+ const findIndex = this.pagination.value.findIndex(
224
+ (x) => x.key === this.staticCurrentPage.value
225
+ );
226
+ let last = null;
227
+ if (findIndex === 1) {
228
+ this.staticCurrentPage.value = "";
229
+ this.staticIsLastPage.value = false;
230
+ this.staticIsFirstPage.value = true;
231
+ } else {
232
+ last = this.pagination.value[findIndex - 2].next;
233
+ this.staticCurrentPage.value = this.pagination.value[findIndex - 2].key;
234
+ }
235
+ await this.afterNextPrev(last);
236
+ };
237
+
238
+ next = async (): Promise<void> => {
239
+ const findIndex = this.pagination.value.findIndex(
240
+ (x) => x.key === this.staticCurrentPage.value
241
+ );
242
+ const last = this.pagination.value[findIndex].next;
243
+ if (this.pagination.value.length === 1) {
244
+ this.staticIsFirstPage.value = true;
245
+ } else {
246
+ this.staticIsFirstPage.value = false;
247
+ }
248
+ await this.afterNextPrev(last);
249
+ };
250
+
251
+ afterNextPrev = async (last): Promise<void> => {
252
+ let results = await getStaticData(
253
+ "users",
254
+ this.queryList,
255
+ this.orderList,
256
+ this.max,
257
+ last
258
+ );
259
+
260
+ if (last && Object.keys(results.data).length === 0) {
261
+ this.staticIsLastPage.value = true;
262
+ if (this.pagination.value.length === 1) {
263
+ last = null;
264
+ this.staticCurrentPage.value = "";
265
+ this.staticIsFirstPage.value = true;
266
+ } else {
267
+ last = this.pagination.value[this.pagination.value.length - 2].next;
268
+ this.staticCurrentPage.value =
269
+ this.pagination.value[this.pagination.value.length - 2].key;
270
+ }
271
+ results = await getStaticData(
272
+ "users",
273
+ this.queryList,
274
+ this.orderList,
275
+ this.max,
276
+ last
277
+ );
278
+ } else {
279
+ this.staticIsLastPage.value = false;
280
+ if (this.pagination.value.length === 1) {
281
+ this.staticIsFirstPage.value = false;
282
+ }
283
+ }
284
+ this.data.value = results.data;
285
+ this.staticCurrentPage.value = results.next.id;
286
+ if (!this.staticIsLastPage.value) {
287
+ if (results.next) {
288
+ const findItem = this.pagination.value.find(
289
+ (x) => x.key === results.next.id
290
+ );
291
+ if (!findItem) {
292
+ this.pagination.value.push({
293
+ key: results.next.id,
294
+ next: results.next
295
+ });
296
+ }
297
+ }
298
+ }
299
+ };
300
+
301
+ getData = async (
302
+ collectionPath: string,
303
+ queryList: FirestoreQuery[] = [],
304
+ orderList: FirestoreOrderBy[] = [],
305
+ max = 0
306
+ ): Promise<void> => {
307
+ this.collectionPath = collectionPath;
308
+ this.queryList = queryList;
309
+ this.orderList = orderList;
310
+ this.max = max;
311
+ this.staticIsLastPage.value = false;
312
+ this.staticIsFirstPage.value = true;
313
+ this.staticCurrentPage.value = "";
314
+ this.pagination.value = [];
315
+ this.pagination.value = [];
316
+ this.data.value = {};
317
+ const results = await getStaticData(
318
+ collectionPath,
319
+ queryList,
320
+ orderList,
321
+ max
322
+ );
323
+ if (Object.keys(results.data).length > 0) {
324
+ this.data.value = results.data;
325
+ this.staticCurrentPage.value = results.next.id;
326
+ this.pagination.value.push({ key: results.next.id, next: results.next });
327
+ }
328
+ };
329
+ }
330
+
201
331
  // Composable to start snapshot listener and set unsubscribe function
202
332
  export const startSnapshot = (
203
333
  collectionPath: string,
204
334
  queryList: FirestoreQuery[] = [],
205
335
  orderList: FirestoreOrderBy[] = [],
206
- max = 0,
207
- after = ""
336
+ max = 0
208
337
  ): void => {
209
338
  data[collectionPath] = {};
210
- const q = getQuery(collectionPath, queryList, orderList, max, after);
339
+ const q = getQuery(collectionPath, queryList, orderList, max);
211
340
  const unsubscribe = onSnapshot(q, (querySnapshot) => {
212
341
  const items = {};
213
342
  querySnapshot.forEach((doc) => {
@@ -225,7 +354,7 @@ const getQuery = (
225
354
  queryList: FirestoreQuery[] = [],
226
355
  orderList: FirestoreOrderBy[] = [],
227
356
  max = 0,
228
- after = ""
357
+ after: DocumentData | null = null
229
358
  ): Query => {
230
359
  const queryConditions: QueryConstraint[] = queryList.map((condition) =>
231
360
  where(condition.field, condition.operator, condition.value)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.0.28",
3
+ "version": "1.1.0",
4
4
  "description": "Composables and stores for firebase",
5
5
  "main": "index.ts",
6
6
  "author": "Seth Fischer",