@_henriquewilson/gabirubi-domain 1.2.5 → 1.2.6

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.
@@ -1,7 +1,7 @@
1
1
  import * as _firebase_firestore from '@firebase/firestore';
2
- import { Model } from '../model.mjs';
3
2
  import { Firestore, QueryConstraint } from 'firebase/firestore';
4
3
  import { FieldPath, WhereFilterOp } from '@google-cloud/firestore';
4
+ import { Model } from '../model.mjs';
5
5
  import 'zod';
6
6
 
7
7
  interface Operator {
@@ -1,7 +1,7 @@
1
1
  import * as _firebase_firestore from '@firebase/firestore';
2
- import { Model } from '../model.js';
3
2
  import { Firestore, QueryConstraint } from 'firebase/firestore';
4
3
  import { FieldPath, WhereFilterOp } from '@google-cloud/firestore';
4
+ import { Model } from '../model.js';
5
5
  import 'zod';
6
6
 
7
7
  interface Operator {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/firebase/firebase.ts"],"names":["limitConstraint","orderByConstraint","doc"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,uBAgBO;AAYA,IAAM,oBAAN,MAAM,kBAAiB;AAAA,EAUlB,YAAY,WAAsB;AAN1C,SAAQ,QAAuB;AAC/B,SAAQ,MAA8B;AACtC,SAAQ,SAAsC;AAC9C,SAAQ,SAA4B,CAAC;AACrC,SAAQ,WAA4D,CAAC;AAGjE,SAAK,aAAa;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA,EAKA,OAAc,YAAY,WAAwC;AAC9D,QAAI,CAAC,kBAAiB,WAAW;AAC7B,wBAAiB,YAAY,IAAI,kBAAiB,SAAS;AAAA,IAC/D;AACA,WAAO,kBAAiB;AAAA,EAC5B;AAAA,EAEO,KAAK,MAAoB;AAC5B,SAAK,QAAQ;AACb,WAAO;AAAA,EACX;AAAA,EAEO,GAAG,IAA2B;AACjC,SAAK,MAAM;AACX,WAAO;AAAA,EACX;AAAA,EAEO,SAAS,OA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AA,IAChD;AAAA,EACJ;AAAA;AAAA,EAIA,OAAe,YAAe,UAA+B;AACzD,WAAO;AAAA,MACH,GAAG,SAAS,KAAK;AAAA,MACjB,IAAI,SAAS,KAAK,GAAG,MAAM,SAAS;AAAA,IACxC;AAAA,EACJ;AAAA,EAEA,OAAe,iBAAoB,MAAS;AACxC,WAAO,OAAO;AAAA,MACZ,OAAO,QAAQ,IAAa,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AAAA,QAChD;AAAA,QACA,UAAU,SAAY,OAAO;AAAA,MACjC,CAAC;AAAA,IACH;AAAA,EACJ;AAAA,EAEQ,QAAQ;AACZ,SAAK,QAAQ;AACb,SAAK,MAAM;AACX,SAAK,SAAS,CAAC;AACf,SAAK,WAAW,CAAC;AACjB,SAAK,SAAS;AAAA,EAClB;AACJ;AAnLa,kBACM,YAAqC;AADjD,IAAM,mBAAN","sourcesContent":["import {Model} from \"../model\";\nimport {\n addDoc,\n collection,\n deleteDoc,\n doc,\n getDoc,\n getDocs,\n onSnapshot,\n limit as limitConstraint,\n query,\n orderBy as orderByConstraint,\n setDoc,\n updateDoc,\n Firestore,\n QueryConstraint,\n DocumentSnapshot,\n} from 'firebase/firestore'\nimport {QueryLimitConstraint} from \"@firebase/firestore\";\nimport {FieldPath, WhereFilterOp} from \"@google-cloud/firestore\";\n\n\nexport interface Operator {\n fieldPath: string | FieldPath,\n opStr: WhereFilterOp,\n value: string | object | number | boolean\n}\n\n\nexport class FirestoreService {\n private static _instance: FirestoreService | null = null\n private readonly _firestore: Firestore\n\n private _path: string | null = null\n private _id: string | number | null = null\n private _limit: QueryLimitConstraint | null = null\n private _where: QueryConstraint[] = []\n private _orderBy: { field: string; direction?: 'asc' | 'desc' }[] = []\n\n private constructor(firestore: Firestore) {\n this._firestore = firestore\n }\n\n /**\n * Get singleton instance using provided Firestore\n */\n public static getInstance(firestore: Firestore): FirestoreService {\n if (!FirestoreService._instance) {\n FirestoreService._instance = new FirestoreService(firestore)\n }\n return FirestoreService._instance\n }\n\n public path(path: string): this {\n this._path = path\n return this\n }\n\n public id(id: string | number): this {\n this._id = id\n return this\n }\n\n public where(...where: QueryConstraint[]): this {\n this._where = where\n return this\n }\n\n public limit(limit: number): this {\n this._limit = limitConstraint(limit)\n return this\n }\n\n public orderBy(field: string, direction: 'asc' | 'desc' = 'desc'): this {\n this._orderBy.push({ field, direction })\n return this\n }\n\n public async get<T>(): Promise<T | null> {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const snap = await getDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n return snap.exists() ? FirestoreService.dataToModel<T>(snap) : null\n }\n\n public async getAll<T>(): Promise<T[]> {\n if (!this._path) throw new Error('Path must be set')\n console.log(this._path)\n const snap = await getDocs(query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : [])\n ))\n this.reset()\n return snap.docs.map((doc) => FirestoreService.dataToModel<T>(doc))\n }\n\n public onSnapshotById<T>(callback: (doc: T | null) => void, onNotFound?: () => void) {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const unsubscribe = onSnapshot(\n doc(this._firestore, this._path, `${this._id}`),\n { includeMetadataChanges: true },\n (snap) => {\n if (snap.exists()) {\n callback(FirestoreService.dataToModel<T>(snap))\n } else {\n console.warn(`doc not found: ${this._id} path: ${this._path}`)\n callback(null)\n if (onNotFound) onNotFound()\n }\n }\n )\n this.reset()\n return unsubscribe\n }\n\n public onSnapshot<T>(callback: (docs: T[]) => void, onNotFound?: () => void) {\n if (!this._path) throw new Error('Path must be set')\n const unsubscribe = onSnapshot(\n query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : [])\n ),\n (snapshot) => {\n if (!snapshot.empty) {\n callback(snapshot.docs.map((value) => FirestoreService.dataToModel<T>(value)))\n } else {\n console.warn(`no docs found in path: ${this._path}`)\n callback([])\n if (onNotFound) onNotFound()\n }\n }\n )\n this.reset()\n return unsubscribe\n }\n\n public async add<T>(data: T): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const ref = await addDoc(collection(this._firestore, this._path), FirestoreService.replaceUndefined(data))\n this.reset()\n return { ...data, id: ref.id } as T\n }\n\n public async set<T extends Model>(data: T, merge = true): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const docId = this._id ?? data.id ?? null\n\n if (docId) {\n await setDoc(doc(this._firestore, this._path, `${docId}`), FirestoreService.replaceUndefined(data), { merge })\n this.reset()\n return { ...data, id: docId } as T\n } else {\n const ref = await addDoc(collection(this._firestore, this._path), FirestoreService.replaceUndefined(data))\n this.reset()\n return { ...data, id: ref.id } as T\n }\n }\n\n public async update<T>(data: T): Promise<void> {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await updateDoc(doc(this._firestore, this._path, `${this._id}`), FirestoreService.replaceUndefined(data))\n this.reset()\n }\n\n public async delete(): Promise<void> {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await deleteDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n }\n\n public async save<T extends Model>(data: T, merge = true): Promise<T> {\n if (data.id) {\n await this.path(this._path!).id(data.id).set(data, merge)\n return data\n } else {\n return await this.path(this._path!).add(data)\n }\n }\n\n // ---------- UTILS ----------\n\n private static dataToModel<T>(snapshot: DocumentSnapshot): T {\n return {\n ...snapshot.data(),\n id: snapshot.data()?.id || snapshot.id,\n } as T\n }\n\n private static replaceUndefined<T>(data: T) {\n return Object.fromEntries(\n Object.entries(data as Model).map(([key, value]) => [\n key,\n value === undefined ? null : value,\n ])\n )\n }\n\n private reset() {\n this._path = null\n this._id = null\n this._where = []\n this._orderBy = []\n this._limit = null\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/firebase/firebase.ts"],"names":["limitConstraint","orderByConstraint","doc"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAA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["import {\n addDoc,\n collection,\n deleteDoc,\n doc,\n getDoc,\n getDocs,\n onSnapshot,\n limit as limitConstraint,\n query,\n orderBy as orderByConstraint,\n setDoc,\n updateDoc,\n Firestore,\n QueryConstraint,\n DocumentSnapshot,\n} from 'firebase/firestore'\nimport { QueryLimitConstraint } from '@firebase/firestore'\nimport { FieldPath, WhereFilterOp } from '@google-cloud/firestore'\nimport {Model} from \"../model\";\n\nexport interface Operator {\n fieldPath: string | FieldPath\n opStr: WhereFilterOp\n value: string | object | number | boolean\n}\n\nexport class FirestoreService {\n private static _instance: FirestoreService | null = null\n private readonly _firestore: Firestore\n\n private _path: string | null = null\n private _id: string | number | null = null\n private _limit: QueryLimitConstraint | null = null\n private _where: QueryConstraint[] = []\n private _orderBy: { field: string; direction?: 'asc' | 'desc' }[] = []\n\n private constructor(firestore: Firestore) {\n this._firestore = firestore\n }\n\n /**\n * Get singleton instance using provided Firestore\n */\n public static getInstance(firestore: Firestore): FirestoreService {\n if (!FirestoreService._instance) {\n FirestoreService._instance = new FirestoreService(firestore)\n }\n return FirestoreService._instance\n }\n\n public path(path: string): this {\n this._path = path\n return this\n }\n\n public id(id: string | number): this {\n this._id = id\n return this\n }\n\n public where(...where: QueryConstraint[]): this {\n this._where = where\n return this\n }\n\n public limit(limit: number): this {\n this._limit = limitConstraint(limit)\n return this\n }\n\n public orderBy(field: string, direction: 'asc' | 'desc' = 'desc'): this {\n this._orderBy.push({ field, direction })\n return this\n }\n\n public async get<T>(): Promise<T | null> {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n const snap = await getDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n return snap.exists() ? FirestoreService.dataToModel<T>(snap) : null\n }\n\n public async getAll<T>(): Promise<T[]> {\n if (!this._path) throw new Error('Path must be set')\n console.log(this._path)\n const snap = await getDocs(\n query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : []),\n ),\n )\n this.reset()\n return snap.docs.map((doc) => FirestoreService.dataToModel<T>(doc))\n }\n\n public onSnapshotById<T>(\n callback: (doc: T | null) => void,\n onNotFound?: () => void,\n ) {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n const unsubscribe = onSnapshot(\n doc(this._firestore, this._path, `${this._id}`),\n { includeMetadataChanges: true },\n (snap) => {\n if (snap.exists()) {\n callback(FirestoreService.dataToModel<T>(snap))\n } else {\n console.warn(`doc not found: ${this._id} path: ${this._path}`)\n callback(null)\n if (onNotFound) onNotFound()\n }\n },\n )\n this.reset()\n return unsubscribe\n }\n\n public onSnapshot<T>(callback: (docs: T[]) => void, onNotFound?: () => void) {\n if (!this._path) throw new Error('Path must be set')\n const unsubscribe = onSnapshot(\n query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : []),\n ),\n (snapshot) => {\n if (!snapshot.empty) {\n callback(\n snapshot.docs.map((value) =>\n FirestoreService.dataToModel<T>(value),\n ),\n )\n } else {\n console.warn(`no docs found in path: ${this._path}`)\n callback([])\n if (onNotFound) onNotFound()\n }\n },\n )\n this.reset()\n return unsubscribe\n }\n\n public async add<T>(data: T): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const ref = await addDoc(\n collection(this._firestore, this._path),\n FirestoreService.replaceUndefined(data),\n )\n this.reset()\n return { ...data, id: ref.id } as T\n }\n\n public async set<T extends Model>(data: T, merge = true): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const docId = this._id ?? data.id ?? null\n\n if (docId) {\n await setDoc(\n doc(this._firestore, this._path, `${docId}`),\n FirestoreService.replaceUndefined(data),\n { merge },\n )\n this.reset()\n return { ...data, id: docId } as T\n } else {\n const ref = await addDoc(\n collection(this._firestore, this._path),\n FirestoreService.replaceUndefined(data),\n )\n this.reset()\n return { ...data, id: ref.id } as T\n }\n }\n\n public async update<T>(data: T): Promise<void> {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n await updateDoc(\n doc(this._firestore, this._path, `${this._id}`),\n FirestoreService.replaceUndefined(data),\n )\n this.reset()\n }\n\n public async delete(): Promise<void> {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n await deleteDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n }\n\n public async save<T extends Model>(data: T, merge = true): Promise<T> {\n if (data.id) {\n await this.path(this._path!).id(data.id).set(data, merge)\n return data\n } else {\n return await this.path(this._path!).add(data)\n }\n }\n\n // ---------- UTILS ----------\n\n private static dataToModel<T>(snapshot: DocumentSnapshot): T {\n return {\n ...snapshot.data(),\n id: snapshot.data()?.id || snapshot.id,\n } as T\n }\n\n private static replaceUndefined<T>(data: T) {\n return Object.fromEntries(\n Object.entries(data as Model).map(([key, value]) => [\n key,\n value === undefined ? null : value,\n ]),\n )\n }\n\n private reset() {\n this._path = null\n this._id = null\n this._where = []\n this._orderBy = []\n this._limit = null\n }\n}\n"]}
@@ -62,12 +62,14 @@ var _FirestoreService = class _FirestoreService {
62
62
  if (!this._path)
63
63
  throw new Error("Path must be set");
64
64
  console.log(this._path);
65
- const snap = await getDocs(query(
66
- collection(this._firestore, this._path),
67
- ...this._where,
68
- ...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),
69
- ...this._limit ? [this._limit] : []
70
- ));
65
+ const snap = await getDocs(
66
+ query(
67
+ collection(this._firestore, this._path),
68
+ ...this._where,
69
+ ...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),
70
+ ...this._limit ? [this._limit] : []
71
+ )
72
+ );
71
73
  this.reset();
72
74
  return snap.docs.map((doc2) => _FirestoreService.dataToModel(doc2));
73
75
  }
@@ -103,7 +105,11 @@ var _FirestoreService = class _FirestoreService {
103
105
  ),
104
106
  (snapshot) => {
105
107
  if (!snapshot.empty) {
106
- callback(snapshot.docs.map((value) => _FirestoreService.dataToModel(value)));
108
+ callback(
109
+ snapshot.docs.map(
110
+ (value) => _FirestoreService.dataToModel(value)
111
+ )
112
+ );
107
113
  } else {
108
114
  console.warn(`no docs found in path: ${this._path}`);
109
115
  callback([]);
@@ -118,7 +124,10 @@ var _FirestoreService = class _FirestoreService {
118
124
  async add(data) {
119
125
  if (!this._path)
120
126
  throw new Error("Path must be set");
121
- const ref = await addDoc(collection(this._firestore, this._path), _FirestoreService.replaceUndefined(data));
127
+ const ref = await addDoc(
128
+ collection(this._firestore, this._path),
129
+ _FirestoreService.replaceUndefined(data)
130
+ );
122
131
  this.reset();
123
132
  return { ...data, id: ref.id };
124
133
  }
@@ -127,11 +136,18 @@ var _FirestoreService = class _FirestoreService {
127
136
  throw new Error("Path must be set");
128
137
  const docId = this._id ?? data.id ?? null;
129
138
  if (docId) {
130
- await setDoc(doc(this._firestore, this._path, `${docId}`), _FirestoreService.replaceUndefined(data), { merge });
139
+ await setDoc(
140
+ doc(this._firestore, this._path, `${docId}`),
141
+ _FirestoreService.replaceUndefined(data),
142
+ { merge }
143
+ );
131
144
  this.reset();
132
145
  return { ...data, id: docId };
133
146
  } else {
134
- const ref = await addDoc(collection(this._firestore, this._path), _FirestoreService.replaceUndefined(data));
147
+ const ref = await addDoc(
148
+ collection(this._firestore, this._path),
149
+ _FirestoreService.replaceUndefined(data)
150
+ );
135
151
  this.reset();
136
152
  return { ...data, id: ref.id };
137
153
  }
@@ -139,7 +155,10 @@ var _FirestoreService = class _FirestoreService {
139
155
  async update(data) {
140
156
  if (!this._path || this._id == null)
141
157
  throw new Error("Path and ID must be set");
142
- await updateDoc(doc(this._firestore, this._path, `${this._id}`), _FirestoreService.replaceUndefined(data));
158
+ await updateDoc(
159
+ doc(this._firestore, this._path, `${this._id}`),
160
+ _FirestoreService.replaceUndefined(data)
161
+ );
143
162
  this.reset();
144
163
  }
145
164
  async delete() {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/firebase/firebase.ts"],"sourcesContent":["import {Model} from \"../model\";\nimport {\n addDoc,\n collection,\n deleteDoc,\n doc,\n getDoc,\n getDocs,\n onSnapshot,\n limit as limitConstraint,\n query,\n orderBy as orderByConstraint,\n setDoc,\n updateDoc,\n Firestore,\n QueryConstraint,\n DocumentSnapshot,\n} from 'firebase/firestore'\nimport {QueryLimitConstraint} from \"@firebase/firestore\";\nimport {FieldPath, WhereFilterOp} from \"@google-cloud/firestore\";\n\n\nexport interface Operator {\n fieldPath: string | FieldPath,\n opStr: WhereFilterOp,\n value: string | object | number | boolean\n}\n\n\nexport class FirestoreService {\n private static _instance: FirestoreService | null = null\n private readonly _firestore: Firestore\n\n private _path: string | null = null\n private _id: string | number | null = null\n private _limit: QueryLimitConstraint | null = null\n private _where: QueryConstraint[] = []\n private _orderBy: { field: string; direction?: 'asc' | 'desc' }[] = []\n\n private constructor(firestore: Firestore) {\n this._firestore = firestore\n }\n\n /**\n * Get singleton instance using provided Firestore\n */\n public static getInstance(firestore: Firestore): FirestoreService {\n if (!FirestoreService._instance) {\n FirestoreService._instance = new FirestoreService(firestore)\n }\n return FirestoreService._instance\n }\n\n public path(path: string): this {\n this._path = path\n return this\n }\n\n public id(id: string | number): this {\n this._id = id\n return this\n }\n\n public where(...where: QueryConstraint[]): this {\n this._where = where\n return this\n }\n\n public limit(limit: number): this {\n this._limit = limitConstraint(limit)\n return this\n }\n\n public orderBy(field: string, direction: 'asc' | 'desc' = 'desc'): this {\n this._orderBy.push({ field, direction })\n return this\n }\n\n public async get<T>(): Promise<T | null> {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const snap = await getDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n return snap.exists() ? FirestoreService.dataToModel<T>(snap) : null\n }\n\n public async getAll<T>(): Promise<T[]> {\n if (!this._path) throw new Error('Path must be set')\n console.log(this._path)\n const snap = await getDocs(query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : [])\n ))\n this.reset()\n return snap.docs.map((doc) => FirestoreService.dataToModel<T>(doc))\n }\n\n public onSnapshotById<T>(callback: (doc: T | null) => void, onNotFound?: () => void) {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const unsubscribe = onSnapshot(\n doc(this._firestore, this._path, `${this._id}`),\n { includeMetadataChanges: true },\n (snap) => {\n if (snap.exists()) {\n callback(FirestoreService.dataToModel<T>(snap))\n } else {\n console.warn(`doc not found: ${this._id} path: ${this._path}`)\n callback(null)\n if (onNotFound) onNotFound()\n }\n }\n )\n this.reset()\n return unsubscribe\n }\n\n public onSnapshot<T>(callback: (docs: T[]) => void, onNotFound?: () => void) {\n if (!this._path) throw new Error('Path must be set')\n const unsubscribe = onSnapshot(\n query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : [])\n ),\n (snapshot) => {\n if (!snapshot.empty) {\n callback(snapshot.docs.map((value) => FirestoreService.dataToModel<T>(value)))\n } else {\n console.warn(`no docs found in path: ${this._path}`)\n callback([])\n if (onNotFound) onNotFound()\n }\n }\n )\n this.reset()\n return unsubscribe\n }\n\n public async add<T>(data: T): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const ref = await addDoc(collection(this._firestore, this._path), FirestoreService.replaceUndefined(data))\n this.reset()\n return { ...data, id: ref.id } as T\n }\n\n public async set<T extends Model>(data: T, merge = true): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const docId = this._id ?? data.id ?? null\n\n if (docId) {\n await setDoc(doc(this._firestore, this._path, `${docId}`), FirestoreService.replaceUndefined(data), { merge })\n this.reset()\n return { ...data, id: docId } as T\n } else {\n const ref = await addDoc(collection(this._firestore, this._path), FirestoreService.replaceUndefined(data))\n this.reset()\n return { ...data, id: ref.id } as T\n }\n }\n\n public async update<T>(data: T): Promise<void> {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await updateDoc(doc(this._firestore, this._path, `${this._id}`), FirestoreService.replaceUndefined(data))\n this.reset()\n }\n\n public async delete(): Promise<void> {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await deleteDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n }\n\n public async save<T extends Model>(data: T, merge = true): Promise<T> {\n if (data.id) {\n await this.path(this._path!).id(data.id).set(data, merge)\n return data\n } else {\n return await this.path(this._path!).add(data)\n }\n }\n\n // ---------- UTILS ----------\n\n private static dataToModel<T>(snapshot: DocumentSnapshot): T {\n return {\n ...snapshot.data(),\n id: snapshot.data()?.id || snapshot.id,\n } as T\n }\n\n private static replaceUndefined<T>(data: T) {\n return Object.fromEntries(\n Object.entries(data as Model).map(([key, value]) => [\n key,\n value === undefined ? null : value,\n ])\n )\n }\n\n private reset() {\n this._path = null\n this._id = null\n this._where = []\n this._orderBy = []\n this._limit = null\n }\n}\n"],"mappings":";AACA;AAAA,EACI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,SAAS;AAAA,EACT;AAAA,EACA,WAAW;AAAA,EACX;AAAA,EACA;AAAA,OAIG;AAYA,IAAM,oBAAN,MAAM,kBAAiB;AAAA,EAUlB,YAAY,WAAsB;AAN1C,SAAQ,QAAuB;AAC/B,SAAQ,MAA8B;AACtC,SAAQ,SAAsC;AAC9C,SAAQ,SAA4B,CAAC;AACrC,SAAQ,WAA4D,CAAC;AAGjE,SAAK,aAAa;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA,EAKA,OAAc,YAAY,WAAwC;AAC9D,QAAI,CAAC,kBAAiB,WAAW;AAC7B,wBAAiB,YAAY,IAAI,kBAAiB,SAAS;AAAA,IAC/D;AACA,WAAO,kBAAiB;AAAA,EAC5B;AAAA,EAEO,KAAK,MAAoB;AAC5B,SAAK,QAAQ;AACb,WAAO;AAAA,EACX;AAAA,EAEO,GAAG,IAA2B;AACjC,SAAK,MAAM;AACX,WAAO;AAAA,EACX;AAAA,EAEO,SAAS,OAAgC;AAC5C,SAAK,SAAS;AACd,WAAO;A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,GAAG,SAAS,KAAK;AAAA,MACjB,IAAI,SAAS,KAAK,GAAG,MAAM,SAAS;AAAA,IACxC;AAAA,EACJ;AAAA,EAEA,OAAe,iBAAoB,MAAS;AACxC,WAAO,OAAO;AAAA,MACZ,OAAO,QAAQ,IAAa,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AAAA,QAChD;AAAA,QACA,UAAU,SAAY,OAAO;AAAA,MACjC,CAAC;AAAA,IACH;AAAA,EACJ;AAAA,EAEQ,QAAQ;AACZ,SAAK,QAAQ;AACb,SAAK,MAAM;AACX,SAAK,SAAS,CAAC;AACf,SAAK,WAAW,CAAC;AACjB,SAAK,SAAS;AAAA,EAClB;AACJ;AAnLa,kBACM,YAAqC;AADjD,IAAM,mBAAN;","names":["doc"]}
1
+ {"version":3,"sources":["../../src/firebase/firebase.ts"],"sourcesContent":["import {\n addDoc,\n collection,\n deleteDoc,\n doc,\n getDoc,\n getDocs,\n onSnapshot,\n limit as limitConstraint,\n query,\n orderBy as orderByConstraint,\n setDoc,\n updateDoc,\n Firestore,\n QueryConstraint,\n DocumentSnapshot,\n} from 'firebase/firestore'\nimport { QueryLimitConstraint } from '@firebase/firestore'\nimport { FieldPath, WhereFilterOp } from '@google-cloud/firestore'\nimport {Model} from \"../model\";\n\nexport interface Operator {\n fieldPath: string | FieldPath\n opStr: WhereFilterOp\n value: string | object | number | boolean\n}\n\nexport class FirestoreService {\n private static _instance: FirestoreService | null = null\n private readonly _firestore: Firestore\n\n private _path: string | null = null\n private _id: string | number | null = null\n private _limit: QueryLimitConstraint | null = null\n private _where: QueryConstraint[] = []\n private _orderBy: { field: string; direction?: 'asc' | 'desc' }[] = []\n\n private constructor(firestore: Firestore) {\n this._firestore = firestore\n }\n\n /**\n * Get singleton instance using provided Firestore\n */\n public static getInstance(firestore: Firestore): FirestoreService {\n if (!FirestoreService._instance) {\n FirestoreService._instance = new FirestoreService(firestore)\n }\n return FirestoreService._instance\n }\n\n public path(path: string): this {\n this._path = path\n return this\n }\n\n public id(id: string | number): this {\n this._id = id\n return this\n }\n\n public where(...where: QueryConstraint[]): this {\n this._where = where\n return this\n }\n\n public limit(limit: number): this {\n this._limit = limitConstraint(limit)\n return this\n }\n\n public orderBy(field: string, direction: 'asc' | 'desc' = 'desc'): this {\n this._orderBy.push({ field, direction })\n return this\n }\n\n public async get<T>(): Promise<T | null> {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n const snap = await getDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n return snap.exists() ? FirestoreService.dataToModel<T>(snap) : null\n }\n\n public async getAll<T>(): Promise<T[]> {\n if (!this._path) throw new Error('Path must be set')\n console.log(this._path)\n const snap = await getDocs(\n query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : []),\n ),\n )\n this.reset()\n return snap.docs.map((doc) => FirestoreService.dataToModel<T>(doc))\n }\n\n public onSnapshotById<T>(\n callback: (doc: T | null) => void,\n onNotFound?: () => void,\n ) {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n const unsubscribe = onSnapshot(\n doc(this._firestore, this._path, `${this._id}`),\n { includeMetadataChanges: true },\n (snap) => {\n if (snap.exists()) {\n callback(FirestoreService.dataToModel<T>(snap))\n } else {\n console.warn(`doc not found: ${this._id} path: ${this._path}`)\n callback(null)\n if (onNotFound) onNotFound()\n }\n },\n )\n this.reset()\n return unsubscribe\n }\n\n public onSnapshot<T>(callback: (docs: T[]) => void, onNotFound?: () => void) {\n if (!this._path) throw new Error('Path must be set')\n const unsubscribe = onSnapshot(\n query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : []),\n ),\n (snapshot) => {\n if (!snapshot.empty) {\n callback(\n snapshot.docs.map((value) =>\n FirestoreService.dataToModel<T>(value),\n ),\n )\n } else {\n console.warn(`no docs found in path: ${this._path}`)\n callback([])\n if (onNotFound) onNotFound()\n }\n },\n )\n this.reset()\n return unsubscribe\n }\n\n public async add<T>(data: T): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const ref = await addDoc(\n collection(this._firestore, this._path),\n FirestoreService.replaceUndefined(data),\n )\n this.reset()\n return { ...data, id: ref.id } as T\n }\n\n public async set<T extends Model>(data: T, merge = true): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const docId = this._id ?? data.id ?? null\n\n if (docId) {\n await setDoc(\n doc(this._firestore, this._path, `${docId}`),\n FirestoreService.replaceUndefined(data),\n { merge },\n )\n this.reset()\n return { ...data, id: docId } as T\n } else {\n const ref = await addDoc(\n collection(this._firestore, this._path),\n FirestoreService.replaceUndefined(data),\n )\n this.reset()\n return { ...data, id: ref.id } as T\n }\n }\n\n public async update<T>(data: T): Promise<void> {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n await updateDoc(\n doc(this._firestore, this._path, `${this._id}`),\n FirestoreService.replaceUndefined(data),\n )\n this.reset()\n }\n\n public async delete(): Promise<void> {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n await deleteDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n }\n\n public async save<T extends Model>(data: T, merge = true): Promise<T> {\n if (data.id) {\n await this.path(this._path!).id(data.id).set(data, merge)\n return data\n } else {\n return await this.path(this._path!).add(data)\n }\n }\n\n // ---------- UTILS ----------\n\n private static dataToModel<T>(snapshot: DocumentSnapshot): T {\n return {\n ...snapshot.data(),\n id: snapshot.data()?.id || snapshot.id,\n } as T\n }\n\n private static replaceUndefined<T>(data: T) {\n return Object.fromEntries(\n Object.entries(data as Model).map(([key, value]) => [\n key,\n value === undefined ? null : value,\n ]),\n )\n }\n\n private reset() {\n this._path = null\n this._id = null\n this._where = []\n this._orderBy = []\n this._limit = null\n }\n}\n"],"mappings":";AAAA;AAAA,EACI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA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,IAAM,mBAAN;","names":["doc"]}
@@ -26,9 +26,9 @@ declare const IuguChargeSchema: z.ZodObject<{
26
26
  pdf: string;
27
27
  invoice_id: string;
28
28
  LR: string;
29
- reversible?: string | null | undefined;
30
29
  brand?: string | null | undefined;
31
30
  bin?: string | null | undefined;
31
+ reversible?: string | null | undefined;
32
32
  last4?: string | null | undefined;
33
33
  issuer?: string | null | undefined;
34
34
  identification?: string | null | undefined;
@@ -42,9 +42,9 @@ declare const IuguChargeSchema: z.ZodObject<{
42
42
  pdf: string;
43
43
  invoice_id: string;
44
44
  LR: string;
45
- reversible?: string | null | undefined;
46
45
  brand?: string | null | undefined;
47
46
  bin?: string | null | undefined;
47
+ reversible?: string | null | undefined;
48
48
  last4?: string | null | undefined;
49
49
  issuer?: string | null | undefined;
50
50
  identification?: string | null | undefined;
@@ -26,9 +26,9 @@ declare const IuguChargeSchema: z.ZodObject<{
26
26
  pdf: string;
27
27
  invoice_id: string;
28
28
  LR: string;
29
- reversible?: string | null | undefined;
30
29
  brand?: string | null | undefined;
31
30
  bin?: string | null | undefined;
31
+ reversible?: string | null | undefined;
32
32
  last4?: string | null | undefined;
33
33
  issuer?: string | null | undefined;
34
34
  identification?: string | null | undefined;
@@ -42,9 +42,9 @@ declare const IuguChargeSchema: z.ZodObject<{
42
42
  pdf: string;
43
43
  invoice_id: string;
44
44
  LR: string;
45
- reversible?: string | null | undefined;
46
45
  brand?: string | null | undefined;
47
46
  bin?: string | null | undefined;
47
+ reversible?: string | null | undefined;
48
48
  last4?: string | null | undefined;
49
49
  issuer?: string | null | undefined;
50
50
  identification?: string | null | undefined;
@@ -12,9 +12,9 @@ declare const dataSchema: z.ZodObject<{
12
12
  masked_number: z.ZodString;
13
13
  }, "strip", z.ZodTypeAny, {
14
14
  brand: string;
15
- bin: string;
16
15
  holder_name: string;
17
16
  display_number: string;
17
+ bin: string;
18
18
  year: number;
19
19
  month: number;
20
20
  last_digits: string;
@@ -22,9 +22,9 @@ declare const dataSchema: z.ZodObject<{
22
22
  masked_number: string;
23
23
  }, {
24
24
  brand: string;
25
- bin: string;
26
25
  holder_name: string;
27
26
  display_number: string;
27
+ bin: string;
28
28
  year: number;
29
29
  month: number;
30
30
  last_digits: string;
@@ -47,9 +47,9 @@ declare const paymentMethodSchema: z.ZodObject<{
47
47
  masked_number: z.ZodString;
48
48
  }, "strip", z.ZodTypeAny, {
49
49
  brand: string;
50
- bin: string;
51
50
  holder_name: string;
52
51
  display_number: string;
52
+ bin: string;
53
53
  year: number;
54
54
  month: number;
55
55
  last_digits: string;
@@ -57,9 +57,9 @@ declare const paymentMethodSchema: z.ZodObject<{
57
57
  masked_number: string;
58
58
  }, {
59
59
  brand: string;
60
- bin: string;
61
60
  holder_name: string;
62
61
  display_number: string;
62
+ bin: string;
63
63
  year: number;
64
64
  month: number;
65
65
  last_digits: string;
@@ -72,9 +72,9 @@ declare const paymentMethodSchema: z.ZodObject<{
72
72
  item_type: string;
73
73
  data: {
74
74
  brand: string;
75
- bin: string;
76
75
  holder_name: string;
77
76
  display_number: string;
77
+ bin: string;
78
78
  year: number;
79
79
  month: number;
80
80
  last_digits: string;
@@ -87,9 +87,9 @@ declare const paymentMethodSchema: z.ZodObject<{
87
87
  item_type: string;
88
88
  data: {
89
89
  brand: string;
90
- bin: string;
91
90
  holder_name: string;
92
91
  display_number: string;
92
+ bin: string;
93
93
  year: number;
94
94
  month: number;
95
95
  last_digits: string;
@@ -139,9 +139,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
139
139
  masked_number: z.ZodString;
140
140
  }, "strip", z.ZodTypeAny, {
141
141
  brand: string;
142
- bin: string;
143
142
  holder_name: string;
144
143
  display_number: string;
144
+ bin: string;
145
145
  year: number;
146
146
  month: number;
147
147
  last_digits: string;
@@ -149,9 +149,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
149
149
  masked_number: string;
150
150
  }, {
151
151
  brand: string;
152
- bin: string;
153
152
  holder_name: string;
154
153
  display_number: string;
154
+ bin: string;
155
155
  year: number;
156
156
  month: number;
157
157
  last_digits: string;
@@ -164,9 +164,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
164
164
  item_type: string;
165
165
  data: {
166
166
  brand: string;
167
- bin: string;
168
167
  holder_name: string;
169
168
  display_number: string;
169
+ bin: string;
170
170
  year: number;
171
171
  month: number;
172
172
  last_digits: string;
@@ -179,9 +179,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
179
179
  item_type: string;
180
180
  data: {
181
181
  brand: string;
182
- bin: string;
183
182
  holder_name: string;
184
183
  display_number: string;
184
+ bin: string;
185
185
  year: number;
186
186
  month: number;
187
187
  last_digits: string;
@@ -216,9 +216,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
216
216
  item_type: string;
217
217
  data: {
218
218
  brand: string;
219
- bin: string;
220
219
  holder_name: string;
221
220
  display_number: string;
221
+ bin: string;
222
222
  year: number;
223
223
  month: number;
224
224
  last_digits: string;
@@ -253,9 +253,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
253
253
  item_type: string;
254
254
  data: {
255
255
  brand: string;
256
- bin: string;
257
256
  holder_name: string;
258
257
  display_number: string;
258
+ bin: string;
259
259
  year: number;
260
260
  month: number;
261
261
  last_digits: string;
@@ -12,9 +12,9 @@ declare const dataSchema: z.ZodObject<{
12
12
  masked_number: z.ZodString;
13
13
  }, "strip", z.ZodTypeAny, {
14
14
  brand: string;
15
- bin: string;
16
15
  holder_name: string;
17
16
  display_number: string;
17
+ bin: string;
18
18
  year: number;
19
19
  month: number;
20
20
  last_digits: string;
@@ -22,9 +22,9 @@ declare const dataSchema: z.ZodObject<{
22
22
  masked_number: string;
23
23
  }, {
24
24
  brand: string;
25
- bin: string;
26
25
  holder_name: string;
27
26
  display_number: string;
27
+ bin: string;
28
28
  year: number;
29
29
  month: number;
30
30
  last_digits: string;
@@ -47,9 +47,9 @@ declare const paymentMethodSchema: z.ZodObject<{
47
47
  masked_number: z.ZodString;
48
48
  }, "strip", z.ZodTypeAny, {
49
49
  brand: string;
50
- bin: string;
51
50
  holder_name: string;
52
51
  display_number: string;
52
+ bin: string;
53
53
  year: number;
54
54
  month: number;
55
55
  last_digits: string;
@@ -57,9 +57,9 @@ declare const paymentMethodSchema: z.ZodObject<{
57
57
  masked_number: string;
58
58
  }, {
59
59
  brand: string;
60
- bin: string;
61
60
  holder_name: string;
62
61
  display_number: string;
62
+ bin: string;
63
63
  year: number;
64
64
  month: number;
65
65
  last_digits: string;
@@ -72,9 +72,9 @@ declare const paymentMethodSchema: z.ZodObject<{
72
72
  item_type: string;
73
73
  data: {
74
74
  brand: string;
75
- bin: string;
76
75
  holder_name: string;
77
76
  display_number: string;
77
+ bin: string;
78
78
  year: number;
79
79
  month: number;
80
80
  last_digits: string;
@@ -87,9 +87,9 @@ declare const paymentMethodSchema: z.ZodObject<{
87
87
  item_type: string;
88
88
  data: {
89
89
  brand: string;
90
- bin: string;
91
90
  holder_name: string;
92
91
  display_number: string;
92
+ bin: string;
93
93
  year: number;
94
94
  month: number;
95
95
  last_digits: string;
@@ -139,9 +139,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
139
139
  masked_number: z.ZodString;
140
140
  }, "strip", z.ZodTypeAny, {
141
141
  brand: string;
142
- bin: string;
143
142
  holder_name: string;
144
143
  display_number: string;
144
+ bin: string;
145
145
  year: number;
146
146
  month: number;
147
147
  last_digits: string;
@@ -149,9 +149,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
149
149
  masked_number: string;
150
150
  }, {
151
151
  brand: string;
152
- bin: string;
153
152
  holder_name: string;
154
153
  display_number: string;
154
+ bin: string;
155
155
  year: number;
156
156
  month: number;
157
157
  last_digits: string;
@@ -164,9 +164,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
164
164
  item_type: string;
165
165
  data: {
166
166
  brand: string;
167
- bin: string;
168
167
  holder_name: string;
169
168
  display_number: string;
169
+ bin: string;
170
170
  year: number;
171
171
  month: number;
172
172
  last_digits: string;
@@ -179,9 +179,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
179
179
  item_type: string;
180
180
  data: {
181
181
  brand: string;
182
- bin: string;
183
182
  holder_name: string;
184
183
  display_number: string;
184
+ bin: string;
185
185
  year: number;
186
186
  month: number;
187
187
  last_digits: string;
@@ -216,9 +216,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
216
216
  item_type: string;
217
217
  data: {
218
218
  brand: string;
219
- bin: string;
220
219
  holder_name: string;
221
220
  display_number: string;
221
+ bin: string;
222
222
  year: number;
223
223
  month: number;
224
224
  last_digits: string;
@@ -253,9 +253,9 @@ declare const IuguClientSchema: z.ZodObject<z.objectUtil.extendShape<{
253
253
  item_type: string;
254
254
  data: {
255
255
  brand: string;
256
- bin: string;
257
256
  holder_name: string;
258
257
  display_number: string;
258
+ bin: string;
259
259
  year: number;
260
260
  month: number;
261
261
  last_digits: string;
@@ -1,9 +1,9 @@
1
- import { Model } from '../model.mjs';
2
1
  import { Operator } from '../firebase/firebase.mjs';
3
2
  import { Firestore } from 'firebase/firestore';
4
- import 'zod';
3
+ import { Model } from '../model.mjs';
5
4
  import '@firebase/firestore';
6
5
  import '@google-cloud/firestore';
6
+ import 'zod';
7
7
 
8
8
  declare abstract class BaseRepository<T extends Model> {
9
9
  protected abstract path: string;
@@ -1,9 +1,9 @@
1
- import { Model } from '../model.js';
2
1
  import { Operator } from '../firebase/firebase.js';
3
2
  import { Firestore } from 'firebase/firestore';
4
- import 'zod';
3
+ import { Model } from '../model.js';
5
4
  import '@firebase/firestore';
6
5
  import '@google-cloud/firestore';
6
+ import 'zod';
7
7
 
8
8
  declare abstract class BaseRepository<T extends Model> {
9
9
  protected abstract path: string;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/repository/base.repository.ts","../../src/firebase/firebase.ts"],"names":["where","limitConstra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,wBAAkB,gBAAgB,QAAQ,KAAK,UAAU;AAAA,IAC3D;AAEA,QAAI,KAAK,eAAe,QAAW;AACjC,wBAAkB,gBAAgB,MAAM,KAAK,UAAU;AAAA,IACzD;AACA,UAAM,SAAS,MAAM,gBAAgB,OAAU;AAC/C,SAAK,iBAAiB;AACtB,WAAO;AAAA,EACT;AAAA,EAEQ,mBAAmB;AACzB,SAAK,SAAS,CAAC;AACf,SAAK,aAAa;AAClB,SAAK,aAAa;AAAA,EACpB;AACF","sourcesContent":["import {Model} from \"../model\";\nimport {FirestoreService, Operator} from \"../firebase/firebase\";\nimport {Firestore, where} from 'firebase/firestore'\n\nexport abstract class BaseRepository<T extends Model> {\n protected abstract path: string;\n protected abstract firestore: Firestore;\n\n private wheres: Operator[] = [];\n private orderField?: string;\n private limitCount?: number;\n\n getById(id: string | number) {\n return FirestoreService.getInstance(this.firestore).path(this.path).id(id).get<T>();\n }\n\n getAll() {\n this.resetFluentState();\n return FirestoreService.getInstance(this.firestore).path(this.path).getAll<T>();\n }\n\n async update(doc: T) {\n await FirestoreService.getInstance(this.firestore).path(this.path).id(doc.id).update(doc);\n }\n\n set(doc: T, customId?: string | number, merge?: boolean) {\n let firebaseService = FirestoreService.getInstance(this.firestore).path(this.path)\n if(customId) firebaseService = firebaseService.id(customId)\n return firebaseService.set(doc, merge);\n }\n\n where(fieldPath: string, opStr: Operator['opStr'], value: string | number | boolean | object): this {\n this.wheres.push({ fieldPath, opStr, value });\n return this;\n }\n\n orderBy(field: string): this {\n this.orderField = field;\n return this;\n }\n\n limit(n: number): this {\n this.limitCount = n;\n return this;\n }\n\n async get(): Promise<T[]> {\n let firebaseService = FirestoreService.getInstance(this.firestore).path(this.path)\n\n if (this.wheres.length > 0) {\n firebaseService = firebaseService.where(\n ...this.wheres.map(w => where(w.fieldPath, w.opStr, w.value))\n )\n }\n\n if (this.orderField) {\n firebaseService = firebaseService.orderBy(this.orderField)\n }\n\n if (this.limitCount !== undefined) {\n firebaseService = firebaseService.limit(this.limitCount)\n }\n const result = await firebaseService.getAll<T>();\n this.resetFluentState();\n return result;\n }\n\n private resetFluentState() {\n this.wheres = [];\n this.orderField = undefined;\n this.limitCount = undefined;\n }\n}\n","import {Model} from \"../model\";\nimport {\n addDoc,\n collection,\n deleteDoc,\n doc,\n getDoc,\n getDocs,\n onSnapshot,\n limit as limitConstraint,\n query,\n orderBy as orderByConstraint,\n setDoc,\n updateDoc,\n Firestore,\n QueryConstraint,\n DocumentSnapshot,\n} from 'firebase/firestore'\nimport {QueryLimitConstraint} from \"@firebase/firestore\";\nimport {FieldPath, WhereFilterOp} from \"@google-cloud/firestore\";\n\n\nexport interface Operator {\n fieldPath: string | FieldPath,\n opStr: WhereFilterOp,\n value: string | object | number | boolean\n}\n\n\nexport class FirestoreService {\n private static _instance: FirestoreService | null = null\n private readonly _firestore: Firestore\n\n private _path: string | null = null\n private _id: string | number | null = null\n private _limit: QueryLimitConstraint | null = null\n private _where: QueryConstraint[] = []\n private _orderBy: { field: string; direction?: 'asc' | 'desc' }[] = []\n\n private constructor(firestore: Firestore) {\n this._firestore = firestore\n }\n\n /**\n * Get singleton instance using provided Firestore\n */\n public static getInstance(firestore: Firestore): FirestoreService {\n if (!FirestoreService._instance) {\n FirestoreService._instance = new FirestoreService(firestore)\n }\n return FirestoreService._instance\n }\n\n public path(path: string): this {\n this._path = path\n return this\n }\n\n public id(id: string | number): this {\n this._id = id\n return this\n }\n\n public where(...where: QueryConstraint[]): this {\n this._where = where\n return this\n }\n\n public limit(limit: number): this {\n this._limit = limitConstraint(limit)\n return this\n }\n\n public orderBy(field: string, direction: 'asc' | 'desc' = 'desc'): this {\n this._orderBy.push({ field, direction })\n return this\n }\n\n public async get<T>(): Promise<T | null> {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const snap = await getDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n return snap.exists() ? FirestoreService.dataToModel<T>(snap) : null\n }\n\n public async getAll<T>(): Promise<T[]> {\n if (!this._path) throw new Error('Path must be set')\n console.log(this._path)\n const snap = await getDocs(query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : [])\n ))\n this.reset()\n return snap.docs.map((doc) => FirestoreService.dataToModel<T>(doc))\n }\n\n public onSnapshotById<T>(callback: (doc: T | null) => void, onNotFound?: () => void) {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const unsubscribe = onSnapshot(\n doc(this._firestore, this._path, `${this._id}`),\n { includeMetadataChanges: true },\n (snap) => {\n if (snap.exists()) {\n callback(FirestoreService.dataToModel<T>(snap))\n } else {\n console.warn(`doc not found: ${this._id} path: ${this._path}`)\n callback(null)\n if (onNotFound) onNotFound()\n }\n }\n )\n this.reset()\n return unsubscribe\n }\n\n public onSnapshot<T>(callback: (docs: T[]) => void, onNotFound?: () => void) {\n if (!this._path) throw new Error('Path must be set')\n const unsubscribe = onSnapshot(\n query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : [])\n ),\n (snapshot) => {\n if (!snapshot.empty) {\n callback(snapshot.docs.map((value) => FirestoreService.dataToModel<T>(value)))\n } else {\n console.warn(`no docs found in path: ${this._path}`)\n callback([])\n if (onNotFound) onNotFound()\n }\n }\n )\n this.reset()\n return unsubscribe\n }\n\n public async add<T>(data: T): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const ref = await addDoc(collection(this._firestore, this._path), FirestoreService.replaceUndefined(data))\n this.reset()\n return { ...data, id: ref.id } as T\n }\n\n public async set<T extends Model>(data: T, merge = true): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const docId = this._id ?? data.id ?? null\n\n if (docId) {\n await setDoc(doc(this._firestore, this._path, `${docId}`), FirestoreService.replaceUndefined(data), { merge })\n this.reset()\n return { ...data, id: docId } as T\n } else {\n const ref = await addDoc(collection(this._firestore, this._path), FirestoreService.replaceUndefined(data))\n this.reset()\n return { ...data, id: ref.id } as T\n }\n }\n\n public async update<T>(data: T): Promise<void> {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await updateDoc(doc(this._firestore, this._path, `${this._id}`), FirestoreService.replaceUndefined(data))\n this.reset()\n }\n\n public async delete(): Promise<void> {\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await deleteDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n }\n\n public async save<T extends Model>(data: T, merge = true): Promise<T> {\n if (data.id) {\n await this.path(this._path!).id(data.id).set(data, merge)\n return data\n } else {\n return await this.path(this._path!).add(data)\n }\n }\n\n // ---------- UTILS ----------\n\n private static dataToModel<T>(snapshot: DocumentSnapshot): T {\n return {\n ...snapshot.data(),\n id: snapshot.data()?.id || snapshot.id,\n } as T\n }\n\n private static replaceUndefined<T>(data: T) {\n return Object.fromEntries(\n Object.entries(data as Model).map(([key, value]) => [\n key,\n value === undefined ? null : value,\n ])\n )\n }\n\n private reset() {\n this._path = null\n this._id = null\n this._where = []\n this._orderBy = []\n this._limit = null\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/repository/base.repository.ts","../../src/firebase/firebase.ts"],"names":["where","limitConstraint","orderByConstraint","doc","import_firestore"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,uBAgBO;AAWA,IAAM,oBAAN,MAAM,kBAAiB;AAAA,EAUlB,YAAY,WAAsB;AAN1C,SAAQ,QAAuB;AAC/B,SAAQ,MAA8B;AACtC,SAAQ,SAAsC;AAC9C,SAAQ,SAA4B,CAAC;AACrC,SAAQ,WAA4D,CAAC;AAGjE,SAAK,aAAa;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA,EAKA,OAAc,YAAY,WAAwC;AAC9D,QAAI,CAAC,kBAAiB,WAAW;AAC7B,wBAAiB,YAAY,IAAI,kBAAiB,SAAS;AAAA,IAC/D;AACA,WAAO,kBAAiB;AAAA,E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firestore, where } from 'firebase/firestore'\nimport {Model} from \"../model\";\n\nexport abstract class BaseRepository<T extends Model> {\n protected abstract path: string\n protected abstract firestore: Firestore\n\n private wheres: Operator[] = []\n private orderField?: string\n private limitCount?: number\n\n getById(id: string | number) {\n return FirestoreService.getInstance(this.firestore)\n .path(this.path)\n .id(id)\n .get<T>()\n }\n\n getAll() {\n this.resetFluentState()\n return FirestoreService.getInstance(this.firestore)\n .path(this.path)\n .getAll<T>()\n }\n\n async update(doc: T) {\n await FirestoreService.getInstance(this.firestore)\n .path(this.path)\n .id(doc.id)\n .update(doc)\n }\n\n set(doc: T, customId?: string | number, merge?: boolean) {\n let firebaseService = FirestoreService.getInstance(this.firestore).path(\n this.path,\n )\n if (customId) firebaseService = firebaseService.id(customId)\n return firebaseService.set(doc, merge)\n }\n\n where(\n fieldPath: string,\n opStr: Operator['opStr'],\n value: string | number | boolean | object,\n ): this {\n this.wheres.push({ fieldPath, opStr, value })\n return this\n }\n\n orderBy(field: string): this {\n this.orderField = field\n return this\n }\n\n limit(n: number): this {\n this.limitCount = n\n return this\n }\n\n async get(): Promise<T[]> {\n let firebaseService = FirestoreService.getInstance(this.firestore)\n .path(\n this.path,\n )\n\n if (this.wheres.length > 0) {\n firebaseService = firebaseService.where(\n ...this.wheres.map((w) => where(w.fieldPath, w.opStr, w.value)),\n )\n }\n\n if (this.orderField) {\n firebaseService = firebaseService.orderBy(this.orderField)\n }\n\n if (this.limitCount !== undefined) {\n firebaseService = firebaseService.limit(this.limitCount)\n }\n const result = await firebaseService.getAll<T>()\n this.resetFluentState()\n return result\n }\n\n private resetFluentState() {\n this.wheres = []\n this.orderField = undefined\n this.limitCount = undefined\n }\n}\n","import {\n addDoc,\n collection,\n deleteDoc,\n doc,\n getDoc,\n getDocs,\n onSnapshot,\n limit as limitConstraint,\n query,\n orderBy as orderByConstraint,\n setDoc,\n updateDoc,\n Firestore,\n QueryConstraint,\n DocumentSnapshot,\n} from 'firebase/firestore'\nimport { QueryLimitConstraint } from '@firebase/firestore'\nimport { FieldPath, WhereFilterOp } from '@google-cloud/firestore'\nimport {Model} from \"../model\";\n\nexport interface Operator {\n fieldPath: string | FieldPath\n opStr: WhereFilterOp\n value: string | object | number | boolean\n}\n\nexport class FirestoreService {\n private static _instance: FirestoreService | null = null\n private readonly _firestore: Firestore\n\n private _path: string | null = null\n private _id: string | number | null = null\n private _limit: QueryLimitConstraint | null = null\n private _where: QueryConstraint[] = []\n private _orderBy: { field: string; direction?: 'asc' | 'desc' }[] = []\n\n private constructor(firestore: Firestore) {\n this._firestore = firestore\n }\n\n /**\n * Get singleton instance using provided Firestore\n */\n public static getInstance(firestore: Firestore): FirestoreService {\n if (!FirestoreService._instance) {\n FirestoreService._instance = new FirestoreService(firestore)\n }\n return FirestoreService._instance\n }\n\n public path(path: string): this {\n this._path = path\n return this\n }\n\n public id(id: string | number): this {\n this._id = id\n return this\n }\n\n public where(...where: QueryConstraint[]): this {\n this._where = where\n return this\n }\n\n public limit(limit: number): this {\n this._limit = limitConstraint(limit)\n return this\n }\n\n public orderBy(field: string, direction: 'asc' | 'desc' = 'desc'): this {\n this._orderBy.push({ field, direction })\n return this\n }\n\n public async get<T>(): Promise<T | null> {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n const snap = await getDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n return snap.exists() ? FirestoreService.dataToModel<T>(snap) : null\n }\n\n public async getAll<T>(): Promise<T[]> {\n if (!this._path) throw new Error('Path must be set')\n console.log(this._path)\n const snap = await getDocs(\n query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : []),\n ),\n )\n this.reset()\n return snap.docs.map((doc) => FirestoreService.dataToModel<T>(doc))\n }\n\n public onSnapshotById<T>(\n callback: (doc: T | null) => void,\n onNotFound?: () => void,\n ) {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n const unsubscribe = onSnapshot(\n doc(this._firestore, this._path, `${this._id}`),\n { includeMetadataChanges: true },\n (snap) => {\n if (snap.exists()) {\n callback(FirestoreService.dataToModel<T>(snap))\n } else {\n console.warn(`doc not found: ${this._id} path: ${this._path}`)\n callback(null)\n if (onNotFound) onNotFound()\n }\n },\n )\n this.reset()\n return unsubscribe\n }\n\n public onSnapshot<T>(callback: (docs: T[]) => void, onNotFound?: () => void) {\n if (!this._path) throw new Error('Path must be set')\n const unsubscribe = onSnapshot(\n query(\n collection(this._firestore, this._path),\n ...this._where,\n ...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),\n ...(this._limit ? [this._limit] : []),\n ),\n (snapshot) => {\n if (!snapshot.empty) {\n callback(\n snapshot.docs.map((value) =>\n FirestoreService.dataToModel<T>(value),\n ),\n )\n } else {\n console.warn(`no docs found in path: ${this._path}`)\n callback([])\n if (onNotFound) onNotFound()\n }\n },\n )\n this.reset()\n return unsubscribe\n }\n\n public async add<T>(data: T): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const ref = await addDoc(\n collection(this._firestore, this._path),\n FirestoreService.replaceUndefined(data),\n )\n this.reset()\n return { ...data, id: ref.id } as T\n }\n\n public async set<T extends Model>(data: T, merge = true): Promise<T> {\n if (!this._path) throw new Error('Path must be set')\n const docId = this._id ?? data.id ?? null\n\n if (docId) {\n await setDoc(\n doc(this._firestore, this._path, `${docId}`),\n FirestoreService.replaceUndefined(data),\n { merge },\n )\n this.reset()\n return { ...data, id: docId } as T\n } else {\n const ref = await addDoc(\n collection(this._firestore, this._path),\n FirestoreService.replaceUndefined(data),\n )\n this.reset()\n return { ...data, id: ref.id } as T\n }\n }\n\n public async update<T>(data: T): Promise<void> {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n await updateDoc(\n doc(this._firestore, this._path, `${this._id}`),\n FirestoreService.replaceUndefined(data),\n )\n this.reset()\n }\n\n public async delete(): Promise<void> {\n if (!this._path || this._id == null)\n throw new Error('Path and ID must be set')\n await deleteDoc(doc(this._firestore, this._path, `${this._id}`))\n this.reset()\n }\n\n public async save<T extends Model>(data: T, merge = true): Promise<T> {\n if (data.id) {\n await this.path(this._path!).id(data.id).set(data, merge)\n return data\n } else {\n return await this.path(this._path!).add(data)\n }\n }\n\n // ---------- UTILS ----------\n\n private static dataToModel<T>(snapshot: DocumentSnapshot): T {\n return {\n ...snapshot.data(),\n id: snapshot.data()?.id || snapshot.id,\n } as T\n }\n\n private static replaceUndefined<T>(data: T) {\n return Object.fromEntries(\n Object.entries(data as Model).map(([key, value]) => [\n key,\n value === undefined ? null : value,\n ]),\n )\n }\n\n private reset() {\n this._path = null\n this._id = null\n this._where = []\n this._orderBy = []\n this._limit = null\n }\n}\n"]}