@cravery/firebase 0.0.6 → 0.0.7

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.
@@ -0,0 +1,2 @@
1
+ export * from "./repository";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/asset/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./repository"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/asset/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B"}
@@ -0,0 +1,37 @@
1
+ import { Firestore } from "firebase-admin/firestore";
2
+ import { Asset } from "@cravery/core";
3
+ export declare class AssetRepository {
4
+ private db;
5
+ private collectionName;
6
+ constructor(db: Firestore, collectionName: string);
7
+ private get collection();
8
+ findById(id: string): Promise<Asset | null>;
9
+ exists(id: string): Promise<boolean>;
10
+ slugExists(slug: string): Promise<boolean>;
11
+ update(slug: string, data: Partial<Asset>): Promise<void>;
12
+ getLocale(slug: string, locale: string): Promise<{
13
+ locale: string;
14
+ name: string;
15
+ notes?: string;
16
+ } | null>;
17
+ setLocale(slug: string, locale: string, data: {
18
+ name: string;
19
+ notes?: string;
20
+ }): Promise<void>;
21
+ getAllLocales(slug: string): Promise<{
22
+ locale: string;
23
+ name: string;
24
+ notes?: string;
25
+ }[]>;
26
+ createWithLocale(slug: string, imageUrl: string, locale: string, localeData: {
27
+ name: string;
28
+ notes?: string;
29
+ }): Promise<Asset>;
30
+ }
31
+ export declare class IngredientRepository extends AssetRepository {
32
+ constructor(db: Firestore);
33
+ }
34
+ export declare class EquipmentRepository extends AssetRepository {
35
+ constructor(db: Firestore);
36
+ }
37
+ //# sourceMappingURL=repository.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../src/asset/repository.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAa,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,qBAAa,eAAe;IAExB,OAAO,CAAC,EAAE;IACV,OAAO,CAAC,cAAc;gBADd,EAAE,EAAE,SAAS,EACb,cAAc,EAAE,MAAM;IAGhC,OAAO,KAAK,UAAU,GAErB;IAEK,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC;IAM3C,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAKpC,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI1C,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAOzD,SAAS,CACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,GACb,OAAO,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IAW7D,SAAS,CACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,IAAI,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GACrC,OAAO,CAAC,IAAI,CAAC;IAKV,aAAa,CACjB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAcxD,gBAAgB,CACpB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,UAAU,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAC3C,OAAO,CAAC,KAAK,CAAC;CAmBlB;AAED,qBAAa,oBAAqB,SAAQ,eAAe;gBAC3C,EAAE,EAAE,SAAS;CAG1B;AAED,qBAAa,mBAAoB,SAAQ,eAAe;gBAC1C,EAAE,EAAE,SAAS;CAG1B"}
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EquipmentRepository = exports.IngredientRepository = exports.AssetRepository = void 0;
4
+ const firestore_1 = require("firebase-admin/firestore");
5
+ class AssetRepository {
6
+ constructor(db, collectionName) {
7
+ this.db = db;
8
+ this.collectionName = collectionName;
9
+ }
10
+ get collection() {
11
+ return this.db.collection(this.collectionName);
12
+ }
13
+ async findById(id) {
14
+ const doc = await this.collection.doc(id).get();
15
+ if (!doc.exists)
16
+ return null;
17
+ return doc.data();
18
+ }
19
+ async exists(id) {
20
+ const doc = await this.collection.doc(id).get();
21
+ return doc.exists;
22
+ }
23
+ async slugExists(slug) {
24
+ return this.exists(slug);
25
+ }
26
+ async update(slug, data) {
27
+ await this.collection.doc(slug).update(Object.assign(Object.assign({}, data), { updatedAt: firestore_1.Timestamp.now() }));
28
+ }
29
+ async getLocale(slug, locale) {
30
+ const docRef = this.collection.doc(slug).collection("locales").doc(locale);
31
+ const doc = await docRef.get();
32
+ if (!doc.exists)
33
+ return null;
34
+ return Object.assign({ locale }, doc.data());
35
+ }
36
+ async setLocale(slug, locale, data) {
37
+ const docRef = this.collection.doc(slug).collection("locales").doc(locale);
38
+ await docRef.set(Object.assign({ locale }, data));
39
+ }
40
+ async getAllLocales(slug) {
41
+ const snapshot = await this.collection
42
+ .doc(slug)
43
+ .collection("locales")
44
+ .get();
45
+ return snapshot.docs.map((doc) => (Object.assign({ locale: doc.id }, doc.data())));
46
+ }
47
+ async createWithLocale(slug, imageUrl, locale, localeData) {
48
+ const now = firestore_1.Timestamp.now();
49
+ const batch = this.db.batch();
50
+ const docRef = this.collection.doc(slug);
51
+ const assetData = {
52
+ id: slug,
53
+ imageUrl,
54
+ createdAt: now,
55
+ updatedAt: now,
56
+ };
57
+ batch.set(docRef, assetData);
58
+ const localeRef = docRef.collection("locales").doc(locale);
59
+ batch.set(localeRef, Object.assign({ locale }, localeData));
60
+ await batch.commit();
61
+ return assetData;
62
+ }
63
+ }
64
+ exports.AssetRepository = AssetRepository;
65
+ class IngredientRepository extends AssetRepository {
66
+ constructor(db) {
67
+ super(db, "ingredients");
68
+ }
69
+ }
70
+ exports.IngredientRepository = IngredientRepository;
71
+ class EquipmentRepository extends AssetRepository {
72
+ constructor(db) {
73
+ super(db, "equipment");
74
+ }
75
+ }
76
+ exports.EquipmentRepository = EquipmentRepository;
77
+ //# sourceMappingURL=repository.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repository.js","sourceRoot":"","sources":["../../src/asset/repository.ts"],"names":[],"mappings":";;;AAAA,wDAAgE;AAGhE,MAAa,eAAe;IAC1B,YACU,EAAa,EACb,cAAsB;QADtB,OAAE,GAAF,EAAE,CAAW;QACb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K,CAAC,GAAG,CAAC,SAAS,kBAAI,MAAM,IAAK,UAAU,EAAG,CAAC;QAEhD,MAAM,KAAK,CAAC,MAAM,EAAE,CAAC;QACrB,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AA/FD,0CA+FC;AAED,MAAa,oBAAqB,SAAQ,eAAe;IACvD,YAAY,EAAa;QACvB,KAAK,CAAC,EAAE,EAAE,aAAa,CAAC,CAAC;IAC3B,CAAC;CACF;AAJD,oDAIC;AAED,MAAa,mBAAoB,SAAQ,eAAe;IACtD,YAAY,EAAa;QACvB,KAAK,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC;IACzB,CAAC;CACF;AAJD,kDAIC"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./asset";
1
2
  export * from "./iam";
2
3
  export * from "./recipe";
3
4
  export * from "./utils";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./asset"), exports);
17
18
  __exportStar(require("./iam"), exports);
18
19
  __exportStar(require("./recipe"), exports);
19
20
  __exportStar(require("./utils"), exports);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wCAAsB;AACtB,2CAAyB;AACzB,0CAAwB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,wCAAsB;AACtB,2CAAyB;AACzB,0CAAwB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cravery/firebase",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Shared Firebase Admin SDK utilities for Cravery",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1 @@
1
+ export * from "./repository";
@@ -0,0 +1,111 @@
1
+ import { Firestore, Timestamp } from "firebase-admin/firestore";
2
+ import { Asset } from "@cravery/core";
3
+
4
+ export class AssetRepository {
5
+ constructor(
6
+ private db: Firestore,
7
+ private collectionName: string,
8
+ ) {}
9
+
10
+ private get collection() {
11
+ return this.db.collection(this.collectionName);
12
+ }
13
+
14
+ async findById(id: string): Promise<Asset | null> {
15
+ const doc = await this.collection.doc(id).get();
16
+ if (!doc.exists) return null;
17
+ return doc.data() as Asset;
18
+ }
19
+
20
+ async exists(id: string): Promise<boolean> {
21
+ const doc = await this.collection.doc(id).get();
22
+ return doc.exists;
23
+ }
24
+
25
+ async slugExists(slug: string): Promise<boolean> {
26
+ return this.exists(slug);
27
+ }
28
+
29
+ async update(slug: string, data: Partial<Asset>): Promise<void> {
30
+ await this.collection.doc(slug).update({
31
+ ...data,
32
+ updatedAt: Timestamp.now(),
33
+ });
34
+ }
35
+
36
+ async getLocale(
37
+ slug: string,
38
+ locale: string,
39
+ ): Promise<{ locale: string; name: string; notes?: string } | null> {
40
+ const docRef = this.collection.doc(slug).collection("locales").doc(locale);
41
+ const doc = await docRef.get();
42
+ if (!doc.exists) return null;
43
+ return { locale, ...doc.data() } as {
44
+ locale: string;
45
+ name: string;
46
+ notes?: string;
47
+ };
48
+ }
49
+
50
+ async setLocale(
51
+ slug: string,
52
+ locale: string,
53
+ data: { name: string; notes?: string },
54
+ ): Promise<void> {
55
+ const docRef = this.collection.doc(slug).collection("locales").doc(locale);
56
+ await docRef.set({ locale, ...data });
57
+ }
58
+
59
+ async getAllLocales(
60
+ slug: string,
61
+ ): Promise<{ locale: string; name: string; notes?: string }[]> {
62
+ const snapshot = await this.collection
63
+ .doc(slug)
64
+ .collection("locales")
65
+ .get();
66
+ return snapshot.docs.map(
67
+ (doc) =>
68
+ ({
69
+ locale: doc.id,
70
+ ...doc.data(),
71
+ }) as { locale: string; name: string; notes?: string },
72
+ );
73
+ }
74
+
75
+ async createWithLocale(
76
+ slug: string,
77
+ imageUrl: string,
78
+ locale: string,
79
+ localeData: { name: string; notes?: string },
80
+ ): Promise<Asset> {
81
+ const now = Timestamp.now();
82
+ const batch = this.db.batch();
83
+
84
+ const docRef = this.collection.doc(slug);
85
+ const assetData: Asset = {
86
+ id: slug,
87
+ imageUrl,
88
+ createdAt: now,
89
+ updatedAt: now,
90
+ };
91
+ batch.set(docRef, assetData);
92
+
93
+ const localeRef = docRef.collection("locales").doc(locale);
94
+ batch.set(localeRef, { locale, ...localeData });
95
+
96
+ await batch.commit();
97
+ return assetData;
98
+ }
99
+ }
100
+
101
+ export class IngredientRepository extends AssetRepository {
102
+ constructor(db: Firestore) {
103
+ super(db, "ingredients");
104
+ }
105
+ }
106
+
107
+ export class EquipmentRepository extends AssetRepository {
108
+ constructor(db: Firestore) {
109
+ super(db, "equipment");
110
+ }
111
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./asset";
1
2
  export * from "./iam";
2
3
  export * from "./recipe";
3
4
  export * from "./utils";