@_henriquewilson/gabirubi-domain 1.2.2 → 1.2.3
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 +19 -47
- package/dist/firebase/firebase.js.map +1 -1
- package/dist/firebase/firebase.mjs +25 -44
- package/dist/firebase/firebase.mjs.map +1 -1
- 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 +24 -52
- package/dist/repository/base.repository.js.map +1 -1
- package/dist/repository/base.repository.mjs +30 -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 +28 -55
- package/dist/repository/product.repository.js.map +1 -1
- package/dist/repository/product.repository.mjs +34 -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,15 @@ 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");
|
|
400
399
|
return [
|
|
401
400
|
4,
|
|
402
401
|
(0, import_firestore.getDocs)((0, import_firestore.query).apply(this, [
|
|
403
|
-
(0, import_firestore.collection)(
|
|
402
|
+
(0, import_firestore.collection)(_this._firestore, _this._path)
|
|
404
403
|
].concat(_to_consumable_array(_this._where), _to_consumable_array(_this._orderBy.map(function(o) {
|
|
405
404
|
return (0, import_firestore.orderBy)(o.field, o.direction);
|
|
406
405
|
})), _to_consumable_array(_this._limit ? [
|
|
@@ -425,9 +424,8 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
425
424
|
key: "onSnapshotById",
|
|
426
425
|
value: function onSnapshotById(callback, onNotFound) {
|
|
427
426
|
var _this = this;
|
|
428
|
-
var firestore = this.ensureFirestore();
|
|
429
427
|
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)(
|
|
428
|
+
var unsubscribe = (0, import_firestore.onSnapshot)((0, import_firestore.doc)(this._firestore, this._path, "".concat(this._id)), {
|
|
431
429
|
includeMetadataChanges: true
|
|
432
430
|
}, function(snap) {
|
|
433
431
|
if (snap.exists()) {
|
|
@@ -446,10 +444,9 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
446
444
|
key: "onSnapshot",
|
|
447
445
|
value: function onSnapshot(callback, onNotFound) {
|
|
448
446
|
var _this = this;
|
|
449
|
-
var firestore = this.ensureFirestore();
|
|
450
447
|
if (!this._path) throw new Error("Path must be set");
|
|
451
448
|
var unsubscribe = (0, import_firestore.onSnapshot)((0, import_firestore.query).apply(this, [
|
|
452
|
-
(0, import_firestore.collection)(
|
|
449
|
+
(0, import_firestore.collection)(this._firestore, this._path)
|
|
453
450
|
].concat(_to_consumable_array(this._where), _to_consumable_array(this._orderBy.map(function(o) {
|
|
454
451
|
return (0, import_firestore.orderBy)(o.field, o.direction);
|
|
455
452
|
})), _to_consumable_array(this._limit ? [
|
|
@@ -474,15 +471,14 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
474
471
|
value: function add(data) {
|
|
475
472
|
var _this = this;
|
|
476
473
|
return _async_to_generator(function() {
|
|
477
|
-
var
|
|
474
|
+
var ref;
|
|
478
475
|
return _ts_generator(this, function(_state) {
|
|
479
476
|
switch(_state.label){
|
|
480
477
|
case 0:
|
|
481
|
-
firestore = _this.ensureFirestore();
|
|
482
478
|
if (!_this._path) throw new Error("Path must be set");
|
|
483
479
|
return [
|
|
484
480
|
4,
|
|
485
|
-
(0, import_firestore.addDoc)((0, import_firestore.collection)(
|
|
481
|
+
(0, import_firestore.addDoc)((0, import_firestore.collection)(_this._firestore, _this._path), _FirestoreService.replaceUndefined(data))
|
|
486
482
|
];
|
|
487
483
|
case 1:
|
|
488
484
|
ref = _state.sent();
|
|
@@ -504,11 +500,10 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
504
500
|
var merge = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true;
|
|
505
501
|
var _this = this;
|
|
506
502
|
return _async_to_generator(function() {
|
|
507
|
-
var
|
|
503
|
+
var _this__id, _ref, docId, ref;
|
|
508
504
|
return _ts_generator(this, function(_state) {
|
|
509
505
|
switch(_state.label){
|
|
510
506
|
case 0:
|
|
511
|
-
firestore = _this.ensureFirestore();
|
|
512
507
|
if (!_this._path) throw new Error("Path must be set");
|
|
513
508
|
docId = (_ref = (_this__id = _this._id) !== null && _this__id !== void 0 ? _this__id : data.id) !== null && _ref !== void 0 ? _ref : null;
|
|
514
509
|
if (!docId) return [
|
|
@@ -517,7 +512,7 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
517
512
|
];
|
|
518
513
|
return [
|
|
519
514
|
4,
|
|
520
|
-
(0, import_firestore.setDoc)((0, import_firestore.doc)(
|
|
515
|
+
(0, import_firestore.setDoc)((0, import_firestore.doc)(_this._firestore, _this._path, "".concat(docId)), _FirestoreService.replaceUndefined(data), {
|
|
521
516
|
merge: merge
|
|
522
517
|
})
|
|
523
518
|
];
|
|
@@ -533,7 +528,7 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
533
528
|
case 2:
|
|
534
529
|
return [
|
|
535
530
|
4,
|
|
536
|
-
(0, import_firestore.addDoc)((0, import_firestore.collection)(
|
|
531
|
+
(0, import_firestore.addDoc)((0, import_firestore.collection)(_this._firestore, _this._path), _FirestoreService.replaceUndefined(data))
|
|
537
532
|
];
|
|
538
533
|
case 3:
|
|
539
534
|
ref = _state.sent();
|
|
@@ -558,15 +553,13 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
558
553
|
value: function update(data) {
|
|
559
554
|
var _this = this;
|
|
560
555
|
return _async_to_generator(function() {
|
|
561
|
-
var firestore;
|
|
562
556
|
return _ts_generator(this, function(_state) {
|
|
563
557
|
switch(_state.label){
|
|
564
558
|
case 0:
|
|
565
|
-
firestore = _this.ensureFirestore();
|
|
566
559
|
if (!_this._path || _this._id == null) throw new Error("Path and ID must be set");
|
|
567
560
|
return [
|
|
568
561
|
4,
|
|
569
|
-
(0, import_firestore.updateDoc)((0, import_firestore.doc)(
|
|
562
|
+
(0, import_firestore.updateDoc)((0, import_firestore.doc)(_this._firestore, _this._path, "".concat(_this._id)), _FirestoreService.replaceUndefined(data))
|
|
570
563
|
];
|
|
571
564
|
case 1:
|
|
572
565
|
_state.sent();
|
|
@@ -584,15 +577,13 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
584
577
|
value: function _delete() {
|
|
585
578
|
var _this = this;
|
|
586
579
|
return _async_to_generator(function() {
|
|
587
|
-
var firestore;
|
|
588
580
|
return _ts_generator(this, function(_state) {
|
|
589
581
|
switch(_state.label){
|
|
590
582
|
case 0:
|
|
591
|
-
firestore = _this.ensureFirestore();
|
|
592
583
|
if (!_this._path || _this._id == null) throw new Error("Path and ID must be set");
|
|
593
584
|
return [
|
|
594
585
|
4,
|
|
595
|
-
(0, import_firestore.deleteDoc)((0, import_firestore.doc)(
|
|
586
|
+
(0, import_firestore.deleteDoc)((0, import_firestore.doc)(_this._firestore, _this._path, "".concat(_this._id)))
|
|
596
587
|
];
|
|
597
588
|
case 1:
|
|
598
589
|
_state.sent();
|
|
@@ -656,33 +647,15 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
656
647
|
this._orderBy = [];
|
|
657
648
|
this._limit = null;
|
|
658
649
|
}
|
|
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
650
|
}
|
|
669
651
|
], [
|
|
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
652
|
{
|
|
680
653
|
key: "getInstance",
|
|
681
654
|
value: /**
|
|
682
|
-
* Get singleton instance
|
|
683
|
-
*/ function getInstance() {
|
|
684
|
-
if (!_FirestoreService._instance
|
|
685
|
-
|
|
655
|
+
* Get singleton instance using provided Firestore
|
|
656
|
+
*/ function getInstance(firestore) {
|
|
657
|
+
if (!_FirestoreService._instance) {
|
|
658
|
+
_FirestoreService._instance = new _FirestoreService(firestore);
|
|
686
659
|
}
|
|
687
660
|
return _FirestoreService._instance;
|
|
688
661
|
}
|
|
@@ -713,7 +686,6 @@ var _FirestoreService = /*#__PURE__*/ function() {
|
|
|
713
686
|
return _FirestoreService;
|
|
714
687
|
}();
|
|
715
688
|
_FirestoreService._instance = null;
|
|
716
|
-
_FirestoreService._firestore = null;
|
|
717
689
|
var FirestoreService = _FirestoreService;
|
|
718
690
|
// Annotate the CommonJS export names for ESM import in node:
|
|
719
691
|
0 && (module.exports = {
|
|
@@ -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,kB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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":";;;;;;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,YAAY,KAAK,OAAO,GAAG,KAAK,GAAG,EAAE,GAAG,kBAAiB,iBAAiB,IAAI,CAAC;AACxG,SAAK,MAAM;AAAA,EACf;AAAA,EAEA,MAAa,SAAwB;AACjC,QAAI,CAAC,KAAK,SAAS,KAAK,OAAO;AAAM,YAAM,IAAI,MAAM,yBAAyB;AAC9E,cAAM,gCAAU,sBAAI,KAAK,YAAY,KAAK,OAAO,GAAG,KAAK,GAAG,EAAE,CAAC;AAC/D,SAAK,MAAM;AAAA,EACf;AAAA,EAEA,MAAa,KAAsB,MAAS,QAAQ,MAAkB;AAClE,QAAI,KAAK,IAAI;AACT,YAAM,KAAK,KAAK,KAAK,KAAM,EAAE,GAAG,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;AAlLa,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 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,29 @@ 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
|
-
const snap = await getDocs(query(
|
|
64
|
+
const snap = await getDocs(query(
|
|
65
|
+
collection(this._firestore, this._path),
|
|
66
|
+
...this._where,
|
|
67
|
+
...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),
|
|
68
|
+
...this._limit ? [this._limit] : []
|
|
69
|
+
));
|
|
74
70
|
this.reset();
|
|
75
71
|
return snap.docs.map((doc2) => _FirestoreService.dataToModel(doc2));
|
|
76
72
|
}
|
|
77
73
|
onSnapshotById(callback, onNotFound) {
|
|
78
|
-
const firestore = this.ensureFirestore();
|
|
79
74
|
if (!this._path || this._id == null)
|
|
80
75
|
throw new Error("Path and ID must be set");
|
|
81
76
|
const unsubscribe = onSnapshot(
|
|
82
|
-
doc(
|
|
77
|
+
doc(this._firestore, this._path, `${this._id}`),
|
|
83
78
|
{ includeMetadataChanges: true },
|
|
84
79
|
(snap) => {
|
|
85
80
|
if (snap.exists()) {
|
|
@@ -96,11 +91,15 @@ var _FirestoreService = class _FirestoreService {
|
|
|
96
91
|
return unsubscribe;
|
|
97
92
|
}
|
|
98
93
|
onSnapshot(callback, onNotFound) {
|
|
99
|
-
const firestore = this.ensureFirestore();
|
|
100
94
|
if (!this._path)
|
|
101
95
|
throw new Error("Path must be set");
|
|
102
96
|
const unsubscribe = onSnapshot(
|
|
103
|
-
query(
|
|
97
|
+
query(
|
|
98
|
+
collection(this._firestore, this._path),
|
|
99
|
+
...this._where,
|
|
100
|
+
...this._orderBy.map((o) => orderByConstraint(o.field, o.direction)),
|
|
101
|
+
...this._limit ? [this._limit] : []
|
|
102
|
+
),
|
|
104
103
|
(snapshot) => {
|
|
105
104
|
if (!snapshot.empty) {
|
|
106
105
|
callback(snapshot.docs.map((value) => _FirestoreService.dataToModel(value)));
|
|
@@ -116,47 +115,36 @@ var _FirestoreService = class _FirestoreService {
|
|
|
116
115
|
return unsubscribe;
|
|
117
116
|
}
|
|
118
117
|
async add(data) {
|
|
119
|
-
const firestore = this.ensureFirestore();
|
|
120
118
|
if (!this._path)
|
|
121
119
|
throw new Error("Path must be set");
|
|
122
|
-
const ref = await addDoc(collection(
|
|
120
|
+
const ref = await addDoc(collection(this._firestore, this._path), _FirestoreService.replaceUndefined(data));
|
|
123
121
|
this.reset();
|
|
124
122
|
return { ...data, id: ref.id };
|
|
125
123
|
}
|
|
126
124
|
async set(data, merge = true) {
|
|
127
|
-
const firestore = this.ensureFirestore();
|
|
128
125
|
if (!this._path)
|
|
129
126
|
throw new Error("Path must be set");
|
|
130
127
|
const docId = this._id ?? data.id ?? null;
|
|
131
128
|
if (docId) {
|
|
132
|
-
await setDoc(
|
|
133
|
-
doc(firestore, this._path, `${docId}`),
|
|
134
|
-
_FirestoreService.replaceUndefined(data),
|
|
135
|
-
{ merge }
|
|
136
|
-
);
|
|
129
|
+
await setDoc(doc(this._firestore, this._path, `${docId}`), _FirestoreService.replaceUndefined(data), { merge });
|
|
137
130
|
this.reset();
|
|
138
131
|
return { ...data, id: docId };
|
|
139
132
|
} else {
|
|
140
|
-
const ref = await addDoc(
|
|
141
|
-
collection(firestore, this._path),
|
|
142
|
-
_FirestoreService.replaceUndefined(data)
|
|
143
|
-
);
|
|
133
|
+
const ref = await addDoc(collection(this._firestore, this._path), _FirestoreService.replaceUndefined(data));
|
|
144
134
|
this.reset();
|
|
145
135
|
return { ...data, id: ref.id };
|
|
146
136
|
}
|
|
147
137
|
}
|
|
148
138
|
async update(data) {
|
|
149
|
-
const firestore = this.ensureFirestore();
|
|
150
139
|
if (!this._path || this._id == null)
|
|
151
140
|
throw new Error("Path and ID must be set");
|
|
152
|
-
await updateDoc(doc(
|
|
141
|
+
await updateDoc(doc(this._firestore, this._path, `${this._id}`), _FirestoreService.replaceUndefined(data));
|
|
153
142
|
this.reset();
|
|
154
143
|
}
|
|
155
144
|
async delete() {
|
|
156
|
-
const firestore = this.ensureFirestore();
|
|
157
145
|
if (!this._path || this._id == null)
|
|
158
146
|
throw new Error("Path and ID must be set");
|
|
159
|
-
await deleteDoc(doc(
|
|
147
|
+
await deleteDoc(doc(this._firestore, this._path, `${this._id}`));
|
|
160
148
|
this.reset();
|
|
161
149
|
}
|
|
162
150
|
async save(data, merge = true) {
|
|
@@ -189,15 +177,8 @@ var _FirestoreService = class _FirestoreService {
|
|
|
189
177
|
this._orderBy = [];
|
|
190
178
|
this._limit = null;
|
|
191
179
|
}
|
|
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
180
|
};
|
|
199
181
|
_FirestoreService._instance = null;
|
|
200
|
-
_FirestoreService._firestore = null;
|
|
201
182
|
var FirestoreService = _FirestoreService;
|
|
202
183
|
export {
|
|
203
184
|
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,CAAC;AACrC,SAAQ,WAA4D,CAAC;AAAA,EAG9C;AAAA;AAAA;AAAA;AAAA,EAKvB,OAAc,KAAK,WAAsB;AACrC,sBAAiB,aAAa;AAC9B,sBAAiB,YAAY,IAAI,kBAAiB;AAAA,EACtD;AAAA;AAAA;AAAA;AAAA,EAKA,OAAc,cAAgC;AAC1C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AAS,KAAK,OAAO;AAAM,YAAM,IAAI,MAAM,yBAAyB;AAC9E,UAAM,UAAU,IAAI,WAAW,KAAK,OAAO,GAAG,KAAK,GAAG,EAAE,CAAC;AACzD,SAAK,MAAM;AAAA,EACf;AAAA,EAEA,MAAa,KAAsB,MAAS,QAAQ,MAAkB;AAClE,QAAI,KAAK,IAAI;AACT,YAAM,KAAK,KAAK,KAAK,KAAM,EAAE,GAAG,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;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;","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 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;AAAA,EACX;AAAA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