@_henriquewilson/gabirubi-domain 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/firebase/firebase.d.mts +3 -8
- package/dist/firebase/firebase.d.ts +3 -8
- package/dist/firebase/firebase.js +20 -47
- package/dist/firebase/firebase.js.map +1 -1
- package/dist/firebase/firebase.mjs +26 -44
- package/dist/firebase/firebase.mjs.map +1 -1
- package/dist/model/iugu/iugu-charge.d.mts +2 -2
- package/dist/model/iugu/iugu-charge.d.ts +2 -2
- package/dist/model/iugu/iugu-client.d.mts +12 -12
- package/dist/model/iugu/iugu-client.d.ts +12 -12
- package/dist/model/melhorenvio/order.d.mts +16 -16
- package/dist/model/melhorenvio/order.d.ts +16 -16
- package/dist/repository/base.repository.d.mts +2 -1
- package/dist/repository/base.repository.d.ts +2 -1
- package/dist/repository/base.repository.js +25 -52
- package/dist/repository/base.repository.js.map +1 -1
- package/dist/repository/base.repository.mjs +31 -49
- package/dist/repository/base.repository.mjs.map +1 -1
- package/dist/repository/product.repository.d.mts +3 -2
- package/dist/repository/product.repository.d.ts +3 -2
- package/dist/repository/product.repository.js +29 -55
- package/dist/repository/product.repository.js.map +1 -1
- package/dist/repository/product.repository.mjs +35 -52
- package/dist/repository/product.repository.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -11,7 +11,7 @@ interface Operator {
|
|
|
11
11
|
}
|
|
12
12
|
declare class FirestoreService {
|
|
13
13
|
private static _instance;
|
|
14
|
-
private
|
|
14
|
+
private readonly _firestore;
|
|
15
15
|
private _path;
|
|
16
16
|
private _id;
|
|
17
17
|
private _limit;
|
|
@@ -19,13 +19,9 @@ declare class FirestoreService {
|
|
|
19
19
|
private _orderBy;
|
|
20
20
|
private constructor();
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* Get singleton instance using provided Firestore
|
|
23
23
|
*/
|
|
24
|
-
static
|
|
25
|
-
/**
|
|
26
|
-
* Get singleton instance
|
|
27
|
-
*/
|
|
28
|
-
static getInstance(): FirestoreService;
|
|
24
|
+
static getInstance(firestore: Firestore): FirestoreService;
|
|
29
25
|
path(path: string): this;
|
|
30
26
|
id(id: string | number): this;
|
|
31
27
|
where(...where: QueryConstraint[]): this;
|
|
@@ -43,7 +39,6 @@ declare class FirestoreService {
|
|
|
43
39
|
private static dataToModel;
|
|
44
40
|
private static replaceUndefined;
|
|
45
41
|
private reset;
|
|
46
|
-
private ensureFirestore;
|
|
47
42
|
}
|
|
48
43
|
|
|
49
44
|
export { FirestoreService, Operator };
|
|
@@ -11,7 +11,7 @@ interface Operator {
|
|
|
11
11
|
}
|
|
12
12
|
declare class FirestoreService {
|
|
13
13
|
private static _instance;
|
|
14
|
-
private
|
|
14
|
+
private readonly _firestore;
|
|
15
15
|
private _path;
|
|
16
16
|
private _id;
|
|
17
17
|
private _limit;
|
|
@@ -19,13 +19,9 @@ declare class FirestoreService {
|
|
|
19
19
|
private _orderBy;
|
|
20
20
|
private constructor();
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* Get singleton instance using provided Firestore
|
|
23
23
|
*/
|
|
24
|
-
static
|
|
25
|
-
/**
|
|
26
|
-
* Get singleton instance
|
|
27
|
-
*/
|
|
28
|
-
static getInstance(): FirestoreService;
|
|
24
|
+
static getInstance(firestore: Firestore): FirestoreService;
|
|
29
25
|
path(path: string): this;
|
|
30
26
|
id(id: string | number): this;
|
|
31
27
|
where(...where: QueryConstraint[]): this;
|
|
@@ -43,7 +39,6 @@ declare class FirestoreService {
|
|
|
43
39
|
private static dataToModel;
|
|
44
40
|
private static replaceUndefined;
|
|
45
41
|
private reset;
|
|
46
|
-
private ensureFirestore;
|
|
47
42
|
}
|
|
48
43
|
|
|
49
44
|
export { FirestoreService, Operator };
|
|
@@ -308,13 +308,14 @@ __export(firebase_exports, {
|
|
|
308
308
|
module.exports = __toCommonJS(firebase_exports);
|
|
309
309
|
var import_firestore = require("firebase/firestore");
|
|
310
310
|
var _FirestoreService = /*#__PURE__*/ function() {
|
|
311
|
-
function _FirestoreService() {
|
|
311
|
+
function _FirestoreService(firestore) {
|
|
312
312
|
_class_call_check(this, _FirestoreService);
|
|
313
313
|
this._path = null;
|
|
314
314
|
this._id = null;
|
|
315
315
|
this._limit = null;
|
|
316
316
|
this._where = [];
|
|
317
317
|
this._orderBy = [];
|
|
318
|
+
this._firestore = firestore;
|
|
318
319
|
}
|
|
319
320
|
_create_class(_FirestoreService, [
|
|
320
321
|
{
|
|
@@ -364,15 +365,14 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
364
365
|
value: function get() {
|
|
365
366
|
var _this = this;
|
|
366
367
|
return _async_to_generator(function() {
|
|
367
|
-
var
|
|
368
|
+
var snap;
|
|
368
369
|
return _ts_generator(this, function(_state) {
|
|
369
370
|
switch(_state.label){
|
|
370
371
|
case 0:
|
|
371
|
-
firestore = _this.ensureFirestore();
|
|
372
372
|
if (!_this._path || _this._id == null) throw new Error("Path and ID must be set");
|
|
373
373
|
return [
|
|
374
374
|
4,
|
|
375
|
-
(0, import_firestore.getDoc)((0, import_firestore.doc)(
|
|
375
|
+
(0, import_firestore.getDoc)((0, import_firestore.doc)(_this._firestore, _this._path, "".concat(_this._id)))
|
|
376
376
|
];
|
|
377
377
|
case 1:
|
|
378
378
|
snap = _state.sent();
|
|
@@ -391,16 +391,16 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
391
391
|
value: function getAll() {
|
|
392
392
|
var _this = this;
|
|
393
393
|
return _async_to_generator(function() {
|
|
394
|
-
var
|
|
394
|
+
var snap;
|
|
395
395
|
return _ts_generator(this, function(_state) {
|
|
396
396
|
switch(_state.label){
|
|
397
397
|
case 0:
|
|
398
|
-
firestore = _this.ensureFirestore();
|
|
399
398
|
if (!_this._path) throw new Error("Path must be set");
|
|
399
|
+
console.log(_this._path);
|
|
400
400
|
return [
|
|
401
401
|
4,
|
|
402
402
|
(0, import_firestore.getDocs)((0, import_firestore.query).apply(this, [
|
|
403
|
-
(0, import_firestore.collection)(
|
|
403
|
+
(0, import_firestore.collection)(_this._firestore, _this._path)
|
|
404
404
|
].concat(_to_consumable_array(_this._where), _to_consumable_array(_this._orderBy.map(function(o) {
|
|
405
405
|
return (0, import_firestore.orderBy)(o.field, o.direction);
|
|
406
406
|
})), _to_consumable_array(_this._limit ? [
|
|
@@ -425,9 +425,8 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
425
425
|
key: "onSnapshotById",
|
|
426
426
|
value: function onSnapshotById(callback, onNotFound) {
|
|
427
427
|
var _this = this;
|
|
428
|
-
var firestore = this.ensureFirestore();
|
|
429
428
|
if (!this._path || this._id == null) throw new Error("Path and ID must be set");
|
|
430
|
-
var unsubscribe = (0, import_firestore.onSnapshot)((0, import_firestore.doc)(
|
|
429
|
+
var unsubscribe = (0, import_firestore.onSnapshot)((0, import_firestore.doc)(this._firestore, this._path, "".concat(this._id)), {
|
|
431
430
|
includeMetadataChanges: true
|
|
432
431
|
}, function(snap) {
|
|
433
432
|
if (snap.exists()) {
|
|
@@ -446,10 +445,9 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
446
445
|
key: "onSnapshot",
|
|
447
446
|
value: function onSnapshot(callback, onNotFound) {
|
|
448
447
|
var _this = this;
|
|
449
|
-
var firestore = this.ensureFirestore();
|
|
450
448
|
if (!this._path) throw new Error("Path must be set");
|
|
451
449
|
var unsubscribe = (0, import_firestore.onSnapshot)((0, import_firestore.query).apply(this, [
|
|
452
|
-
(0, import_firestore.collection)(
|
|
450
|
+
(0, import_firestore.collection)(this._firestore, this._path)
|
|
453
451
|
].concat(_to_consumable_array(this._where), _to_consumable_array(this._orderBy.map(function(o) {
|
|
454
452
|
return (0, import_firestore.orderBy)(o.field, o.direction);
|
|
455
453
|
})), _to_consumable_array(this._limit ? [
|
|
@@ -474,15 +472,14 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
474
472
|
value: function add(data) {
|
|
475
473
|
var _this = this;
|
|
476
474
|
return _async_to_generator(function() {
|
|
477
|
-
var
|
|
475
|
+
var ref;
|
|
478
476
|
return _ts_generator(this, function(_state) {
|
|
479
477
|
switch(_state.label){
|
|
480
478
|
case 0:
|
|
481
|
-
firestore = _this.ensureFirestore();
|
|
482
479
|
if (!_this._path) throw new Error("Path must be set");
|
|
483
480
|
return [
|
|
484
481
|
4,
|
|
485
|
-
(0, import_firestore.addDoc)((0, import_firestore.collection)(
|
|
482
|
+
(0, import_firestore.addDoc)((0, import_firestore.collection)(_this._firestore, _this._path), _FirestoreService.replaceUndefined(data))
|
|
486
483
|
];
|
|
487
484
|
case 1:
|
|
488
485
|
ref = _state.sent();
|
|
@@ -504,11 +501,10 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
504
501
|
var merge = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true;
|
|
505
502
|
var _this = this;
|
|
506
503
|
return _async_to_generator(function() {
|
|
507
|
-
var
|
|
504
|
+
var _this__id, _ref, docId, ref;
|
|
508
505
|
return _ts_generator(this, function(_state) {
|
|
509
506
|
switch(_state.label){
|
|
510
507
|
case 0:
|
|
511
|
-
firestore = _this.ensureFirestore();
|
|
512
508
|
if (!_this._path) throw new Error("Path must be set");
|
|
513
509
|
docId = (_ref = (_this__id = _this._id) !== null && _this__id !== void 0 ? _this__id : data.id) !== null && _ref !== void 0 ? _ref : null;
|
|
514
510
|
if (!docId) return [
|
|
@@ -517,7 +513,7 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
517
513
|
];
|
|
518
514
|
return [
|
|
519
515
|
4,
|
|
520
|
-
(0, import_firestore.setDoc)((0, import_firestore.doc)(
|
|
516
|
+
(0, import_firestore.setDoc)((0, import_firestore.doc)(_this._firestore, _this._path, "".concat(docId)), _FirestoreService.replaceUndefined(data), {
|
|
521
517
|
merge: merge
|
|
522
518
|
})
|
|
523
519
|
];
|
|
@@ -533,7 +529,7 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
533
529
|
case 2:
|
|
534
530
|
return [
|
|
535
531
|
4,
|
|
536
|
-
(0, import_firestore.addDoc)((0, import_firestore.collection)(
|
|
532
|
+
(0, import_firestore.addDoc)((0, import_firestore.collection)(_this._firestore, _this._path), _FirestoreService.replaceUndefined(data))
|
|
537
533
|
];
|
|
538
534
|
case 3:
|
|
539
535
|
ref = _state.sent();
|
|
@@ -558,15 +554,13 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
558
554
|
value: function update(data) {
|
|
559
555
|
var _this = this;
|
|
560
556
|
return _async_to_generator(function() {
|
|
561
|
-
var firestore;
|
|
562
557
|
return _ts_generator(this, function(_state) {
|
|
563
558
|
switch(_state.label){
|
|
564
559
|
case 0:
|
|
565
|
-
firestore = _this.ensureFirestore();
|
|
566
560
|
if (!_this._path || _this._id == null) throw new Error("Path and ID must be set");
|
|
567
561
|
return [
|
|
568
562
|
4,
|
|
569
|
-
(0, import_firestore.updateDoc)((0, import_firestore.doc)(
|
|
563
|
+
(0, import_firestore.updateDoc)((0, import_firestore.doc)(_this._firestore, _this._path, "".concat(_this._id)), _FirestoreService.replaceUndefined(data))
|
|
570
564
|
];
|
|
571
565
|
case 1:
|
|
572
566
|
_state.sent();
|
|
@@ -584,15 +578,13 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
584
578
|
value: function _delete() {
|
|
585
579
|
var _this = this;
|
|
586
580
|
return _async_to_generator(function() {
|
|
587
|
-
var firestore;
|
|
588
581
|
return _ts_generator(this, function(_state) {
|
|
589
582
|
switch(_state.label){
|
|
590
583
|
case 0:
|
|
591
|
-
firestore = _this.ensureFirestore();
|
|
592
584
|
if (!_this._path || _this._id == null) throw new Error("Path and ID must be set");
|
|
593
585
|
return [
|
|
594
586
|
4,
|
|
595
|
-
(0, import_firestore.deleteDoc)((0, import_firestore.doc)(
|
|
587
|
+
(0, import_firestore.deleteDoc)((0, import_firestore.doc)(_this._firestore, _this._path, "".concat(_this._id)))
|
|
596
588
|
];
|
|
597
589
|
case 1:
|
|
598
590
|
_state.sent();
|
|
@@ -656,33 +648,15 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
656
648
|
this._orderBy = [];
|
|
657
649
|
this._limit = null;
|
|
658
650
|
}
|
|
659
|
-
},
|
|
660
|
-
{
|
|
661
|
-
key: "ensureFirestore",
|
|
662
|
-
value: function ensureFirestore() {
|
|
663
|
-
if (!_FirestoreService._firestore) {
|
|
664
|
-
throw new Error("FirestoreService not initialized. Call FirestoreService.init(firestore) first.");
|
|
665
|
-
}
|
|
666
|
-
return _FirestoreService._firestore;
|
|
667
|
-
}
|
|
668
651
|
}
|
|
669
652
|
], [
|
|
670
|
-
{
|
|
671
|
-
key: "init",
|
|
672
|
-
value: /**
|
|
673
|
-
* Initialize singleton with Firestore instance
|
|
674
|
-
*/ function init(firestore) {
|
|
675
|
-
_FirestoreService._firestore = firestore;
|
|
676
|
-
_FirestoreService._instance = new _FirestoreService();
|
|
677
|
-
}
|
|
678
|
-
},
|
|
679
653
|
{
|
|
680
654
|
key: "getInstance",
|
|
681
655
|
value: /**
|
|
682
|
-
* Get singleton instance
|
|
683
|
-
*/ function getInstance() {
|
|
684
|
-
if (!_FirestoreService._instance
|
|
685
|
-
|
|
656
|
+
* Get singleton instance using provided Firestore
|
|
657
|
+
*/ function getInstance(firestore) {
|
|
658
|
+
if (!_FirestoreService._instance) {
|
|
659
|
+
_FirestoreService._instance = new _FirestoreService(firestore);
|
|
686
660
|
}
|
|
687
661
|
return _FirestoreService._instance;
|
|
688
662
|
}
|
|
@@ -713,7 +687,6 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
713
687
|
return _FirestoreService;
|
|
714
688
|
}();
|
|
715
689
|
_FirestoreService._instance = null;
|
|
716
|
-
_FirestoreService._firestore = null;
|
|
717
690
|
var FirestoreService = _FirestoreService;
|
|
718
691
|
// Annotate the CommonJS export names for ESM import in node:
|
|
719
692
|
0 && (module.exports = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
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,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;AAAA,EAEQ,kBAA6B;AACjC,QAAI,CAAC,kBAAiB,YAAY;AAC9B,YAAM,IAAI,MAAM,gFAAgF;AAAA,IACpG;AACA,WAAO,kBAAiB;AAAA,EAC5B;AACJ;AAtMa,kBACM,YAAqC;AAD3C,kBAEM,aAA+B;AAF3C,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 static _firestore: Firestore | null = null\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\n private constructor() {}\n\n /**\n * Initialize singleton with Firestore instance\n */\n public static init(firestore: Firestore) {\n FirestoreService._firestore = firestore\n FirestoreService._instance = new FirestoreService()\n }\n\n /**\n * Get singleton instance\n */\n public static getInstance(): FirestoreService {\n if (!FirestoreService._instance || !FirestoreService._firestore) {\n throw new Error('FirestoreService not initialized. Call FirestoreService.init(firestore) first.')\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 const firestore = this.ensureFirestore()\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const snap = await getDoc(doc(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 const firestore = this.ensureFirestore()\n if (!this._path) throw new Error('Path must be set')\n const snap = await getDocs(query(collection(firestore, this._path), ...this._where, ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)), ...(this._limit ? [this._limit] : [])))\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 const firestore = this.ensureFirestore()\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const unsubscribe = onSnapshot(\n doc(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 const firestore = this.ensureFirestore()\n if (!this._path) throw new Error('Path must be set')\n const unsubscribe = onSnapshot(\n query(collection(firestore, this._path), ...this._where, ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)), ...(this._limit ? [this._limit] : [])),\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 const firestore = this.ensureFirestore()\n if (!this._path) throw new Error('Path must be set')\n const ref = await addDoc(collection(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 const firestore = this.ensureFirestore()\n if (!this._path) throw new Error('Path must be set')\n\n const docId = this._id ?? data.id ?? null\n\n if (docId) {\n await setDoc(\n doc(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(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 const firestore = this.ensureFirestore()\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await updateDoc(doc(firestore, this._path, `${this._id}`), FirestoreService.replaceUndefined(data))\n this.reset()\n }\n\n public async delete(): Promise<void> {\n const firestore = this.ensureFirestore()\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await deleteDoc(doc(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 private ensureFirestore(): Firestore {\n if (!FirestoreService._firestore) {\n throw new Error('FirestoreService not initialized. Call FirestoreService.init(firestore) first.')\n }\n return FirestoreService._firestore\n }\n}\n"]}
|
|
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,OAAgC;AAC5C,SAAK,SAAS;AACd,WAAO;AAAA,EACX;AAAA,EAEO,MAAM,OAAqB;AAC9B,SAAK,aAAS,iBAAAA,OAAgB,KAAK;AACnC,WAAO;AAAA,EACX;AAAA,EAEO,QAAQ,OAAe,YAA4B,QAAc;AACpE,SAAK,SAAS,KAAK,EAAE,OAAO,UAAU,CAAC;AACvC,WAAO;AAAA,EACX;AAAA,EAEA,MAAa,MAA4B;AACrC,QAAI,CAAC,KAAK,SAAS,KAAK,OAAO;AAAM,YAAM,IAAI,MAAM,yBAAyB;AAC9E,UAAM,OAAO,UAAM,6BAAO,sBAAI,KAAK,YAAY,KAAK,OAAO,GAAG,KAAK,GAAG,EAAE,CAAC;AACzE,SAAK,MAAM;AACX,WAAO,KAAK,OAAO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C;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"]}
|
|
@@ -14,27 +14,20 @@ import {
|
|
|
14
14
|
updateDoc
|
|
15
15
|
} from "firebase/firestore";
|
|
16
16
|
var _FirestoreService = class _FirestoreService {
|
|
17
|
-
|
|
18
|
-
constructor() {
|
|
17
|
+
constructor(firestore) {
|
|
19
18
|
this._path = null;
|
|
20
19
|
this._id = null;
|
|
21
20
|
this._limit = null;
|
|
22
21
|
this._where = [];
|
|
23
22
|
this._orderBy = [];
|
|
23
|
+
this._firestore = firestore;
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
26
|
+
* Get singleton instance using provided Firestore
|
|
27
27
|
*/
|
|
28
|
-
static
|
|
29
|
-
_FirestoreService.
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Get singleton instance
|
|
34
|
-
*/
|
|
35
|
-
static getInstance() {
|
|
36
|
-
if (!_FirestoreService._instance || !_FirestoreService._firestore) {
|
|
37
|
-
throw new Error("FirestoreService not initialized. Call FirestoreService.init(firestore) first.");
|
|
28
|
+
static getInstance(firestore) {
|
|
29
|
+
if (!_FirestoreService._instance) {
|
|
30
|
+
_FirestoreService._instance = new _FirestoreService(firestore);
|
|
38
31
|
}
|
|
39
32
|
return _FirestoreService._instance;
|
|
40
33
|
}
|
|
@@ -59,27 +52,30 @@ var _FirestoreService = class _FirestoreService {
|
|
|
59
52
|
return this;
|
|
60
53
|
}
|
|
61
54
|
async get() {
|
|
62
|
-
const firestore = this.ensureFirestore();
|
|
63
55
|
if (!this._path || this._id == null)
|
|
64
56
|
throw new Error("Path and ID must be set");
|
|
65
|
-
const snap = await getDoc(doc(
|
|
57
|
+
const snap = await getDoc(doc(this._firestore, this._path, `${this._id}`));
|
|
66
58
|
this.reset();
|
|
67
59
|
return snap.exists() ? _FirestoreService.dataToModel(snap) : null;
|
|
68
60
|
}
|
|
69
61
|
async getAll() {
|
|
70
|
-
const firestore = this.ensureFirestore();
|
|
71
62
|
if (!this._path)
|
|
72
63
|
throw new Error("Path must be set");
|
|
73
|
-
|
|
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
|
+
));
|
|
74
71
|
this.reset();
|
|
75
72
|
return snap.docs.map((doc2) => _FirestoreService.dataToModel(doc2));
|
|
76
73
|
}
|
|
77
74
|
onSnapshotById(callback, onNotFound) {
|
|
78
|
-
const firestore = this.ensureFirestore();
|
|
79
75
|
if (!this._path || this._id == null)
|
|
80
76
|
throw new Error("Path and ID must be set");
|
|
81
77
|
const unsubscribe = onSnapshot(
|
|
82
|
-
doc(
|
|
78
|
+
doc(this._firestore, this._path, `${this._id}`),
|
|
83
79
|
{ includeMetadataChanges: true },
|
|
84
80
|
(snap) => {
|
|
85
81
|
if (snap.exists()) {
|
|
@@ -96,11 +92,15 @@ var _FirestoreService = class _FirestoreService {
|
|
|
96
92
|
return unsubscribe;
|
|
97
93
|
}
|
|
98
94
|
onSnapshot(callback, onNotFound) {
|
|
99
|
-
const firestore = this.ensureFirestore();
|
|
100
95
|
if (!this._path)
|
|
101
96
|
throw new Error("Path must be set");
|
|
102
97
|
const unsubscribe = onSnapshot(
|
|
103
|
-
query(
|
|
98
|
+
query(
|
|
99
|
+
collection(this._firestore, this._path),
|
|
100
|
+
...this._where,
|
|
101
|
+
...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),
|
|
102
|
+
...this._limit ? [this._limit] : []
|
|
103
|
+
),
|
|
104
104
|
(snapshot) => {
|
|
105
105
|
if (!snapshot.empty) {
|
|
106
106
|
callback(snapshot.docs.map((value) => _FirestoreService.dataToModel(value)));
|
|
@@ -116,47 +116,36 @@ var _FirestoreService = class _FirestoreService {
|
|
|
116
116
|
return unsubscribe;
|
|
117
117
|
}
|
|
118
118
|
async add(data) {
|
|
119
|
-
const firestore = this.ensureFirestore();
|
|
120
119
|
if (!this._path)
|
|
121
120
|
throw new Error("Path must be set");
|
|
122
|
-
const ref = await addDoc(collection(
|
|
121
|
+
const ref = await addDoc(collection(this._firestore, this._path), _FirestoreService.replaceUndefined(data));
|
|
123
122
|
this.reset();
|
|
124
123
|
return { ...data, id: ref.id };
|
|
125
124
|
}
|
|
126
125
|
async set(data, merge = true) {
|
|
127
|
-
const firestore = this.ensureFirestore();
|
|
128
126
|
if (!this._path)
|
|
129
127
|
throw new Error("Path must be set");
|
|
130
128
|
const docId = this._id ?? data.id ?? null;
|
|
131
129
|
if (docId) {
|
|
132
|
-
await setDoc(
|
|
133
|
-
doc(firestore, this._path, `${docId}`),
|
|
134
|
-
_FirestoreService.replaceUndefined(data),
|
|
135
|
-
{ merge }
|
|
136
|
-
);
|
|
130
|
+
await setDoc(doc(this._firestore, this._path, `${docId}`), _FirestoreService.replaceUndefined(data), { merge });
|
|
137
131
|
this.reset();
|
|
138
132
|
return { ...data, id: docId };
|
|
139
133
|
} else {
|
|
140
|
-
const ref = await addDoc(
|
|
141
|
-
collection(firestore, this._path),
|
|
142
|
-
_FirestoreService.replaceUndefined(data)
|
|
143
|
-
);
|
|
134
|
+
const ref = await addDoc(collection(this._firestore, this._path), _FirestoreService.replaceUndefined(data));
|
|
144
135
|
this.reset();
|
|
145
136
|
return { ...data, id: ref.id };
|
|
146
137
|
}
|
|
147
138
|
}
|
|
148
139
|
async update(data) {
|
|
149
|
-
const firestore = this.ensureFirestore();
|
|
150
140
|
if (!this._path || this._id == null)
|
|
151
141
|
throw new Error("Path and ID must be set");
|
|
152
|
-
await updateDoc(doc(
|
|
142
|
+
await updateDoc(doc(this._firestore, this._path, `${this._id}`), _FirestoreService.replaceUndefined(data));
|
|
153
143
|
this.reset();
|
|
154
144
|
}
|
|
155
145
|
async delete() {
|
|
156
|
-
const firestore = this.ensureFirestore();
|
|
157
146
|
if (!this._path || this._id == null)
|
|
158
147
|
throw new Error("Path and ID must be set");
|
|
159
|
-
await deleteDoc(doc(
|
|
148
|
+
await deleteDoc(doc(this._firestore, this._path, `${this._id}`));
|
|
160
149
|
this.reset();
|
|
161
150
|
}
|
|
162
151
|
async save(data, merge = true) {
|
|
@@ -189,15 +178,8 @@ var _FirestoreService = class _FirestoreService {
|
|
|
189
178
|
this._orderBy = [];
|
|
190
179
|
this._limit = null;
|
|
191
180
|
}
|
|
192
|
-
ensureFirestore() {
|
|
193
|
-
if (!_FirestoreService._firestore) {
|
|
194
|
-
throw new Error("FirestoreService not initialized. Call FirestoreService.init(firestore) first.");
|
|
195
|
-
}
|
|
196
|
-
return _FirestoreService._firestore;
|
|
197
|
-
}
|
|
198
181
|
};
|
|
199
182
|
_FirestoreService._instance = null;
|
|
200
|
-
_FirestoreService._firestore = null;
|
|
201
183
|
var FirestoreService = _FirestoreService;
|
|
202
184
|
export {
|
|
203
185
|
FirestoreService
|
|
@@ -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 static _firestore: Firestore | null = null\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\n private constructor() {}\n\n /**\n * Initialize singleton with Firestore instance\n */\n public static init(firestore: Firestore) {\n FirestoreService._firestore = firestore\n FirestoreService._instance = new FirestoreService()\n }\n\n /**\n * Get singleton instance\n */\n public static getInstance(): FirestoreService {\n if (!FirestoreService._instance || !FirestoreService._firestore) {\n throw new Error('FirestoreService not initialized. Call FirestoreService.init(firestore) first.')\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 const firestore = this.ensureFirestore()\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const snap = await getDoc(doc(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 const firestore = this.ensureFirestore()\n if (!this._path) throw new Error('Path must be set')\n const snap = await getDocs(query(collection(firestore, this._path), ...this._where, ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)), ...(this._limit ? [this._limit] : [])))\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 const firestore = this.ensureFirestore()\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n const unsubscribe = onSnapshot(\n doc(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 const firestore = this.ensureFirestore()\n if (!this._path) throw new Error('Path must be set')\n const unsubscribe = onSnapshot(\n query(collection(firestore, this._path), ...this._where, ...this._orderBy.map(o => orderByConstraint(o.field, o.direction)), ...(this._limit ? [this._limit] : [])),\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 const firestore = this.ensureFirestore()\n if (!this._path) throw new Error('Path must be set')\n const ref = await addDoc(collection(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 const firestore = this.ensureFirestore()\n if (!this._path) throw new Error('Path must be set')\n\n const docId = this._id ?? data.id ?? null\n\n if (docId) {\n await setDoc(\n doc(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(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 const firestore = this.ensureFirestore()\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await updateDoc(doc(firestore, this._path, `${this._id}`), FirestoreService.replaceUndefined(data))\n this.reset()\n }\n\n public async delete(): Promise<void> {\n const firestore = this.ensureFirestore()\n if (!this._path || this._id == null) throw new Error('Path and ID must be set')\n await deleteDoc(doc(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 private ensureFirestore(): Firestore {\n if (!FirestoreService._firestore) {\n throw new Error('FirestoreService not initialized. Call FirestoreService.init(firestore) first.')\n }\n return FirestoreService._firestore\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;AAAA,EAWlB,cAAc;AAPtB,SAAQ,QAAuB;AAC/B,SAAQ,MAA8B;AACtC,SAAQ,SAAsC;AAC9C,SAAQ,SAA4B,CA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iB;AAAA,EAC5B;AACJ;AAtMa,kBACM,YAAqC;AAD3C,kBAEM,aAA+B;AAF3C,IAAM,mBAAN;","names":["doc"]}
|
|
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,k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G,KAAK,EAAE,EAAE,IAAI,MAAM,KAAK;AACxD,aAAO;AAAA,IACX,OAAO;AACH,aAAO,MAAM,KAAK,KAAK,KAAK,KAAM,EAAE,IAAI,IAAI;AAAA,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;","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;
|