|
| 1 | +import { Firestore, type Settings } from "@google-cloud/firestore"; |
| 2 | +import type { |
| 3 | + DatabaseDocumentListOptions, |
| 4 | + DatabaseService, |
| 5 | + DatabaseServiceOptions, |
| 6 | + StoryBookerDatabaseDocument, |
| 7 | +} from "@storybooker/core/types"; |
| 8 | + |
| 9 | +export class GcpFirestoreDatabaseService implements DatabaseService { |
| 10 | + #instance: Firestore; |
| 11 | + |
| 12 | + constructor(instance: Firestore); |
| 13 | + constructor(settings: Settings); |
| 14 | + constructor(instanceOrSettings: Firestore | Settings) { |
| 15 | + this.#instance = |
| 16 | + instanceOrSettings instanceof Firestore |
| 17 | + ? instanceOrSettings |
| 18 | + : new Firestore(instanceOrSettings); |
| 19 | + } |
| 20 | + |
| 21 | + listCollections: DatabaseService["listCollections"] = async (_options) => { |
| 22 | + const collections = await this.#instance.listCollections(); |
| 23 | + return collections.map((col) => col.id); |
| 24 | + }; |
| 25 | + |
| 26 | + // oxlint-disable-next-line class-methods-use-this --- NOOP |
| 27 | + createCollection: DatabaseService["createCollection"] = async ( |
| 28 | + _collectionId, |
| 29 | + _options, |
| 30 | + ) => { |
| 31 | + // Firestore creates collections implicitly when you add a document. |
| 32 | + }; |
| 33 | + |
| 34 | + hasCollection: DatabaseService["hasCollection"] = async ( |
| 35 | + collectionId, |
| 36 | + _options, |
| 37 | + ) => { |
| 38 | + const col = this.#instance.collection(collectionId); |
| 39 | + const snapshot = await col.limit(1).get(); |
| 40 | + return !snapshot.empty; |
| 41 | + }; |
| 42 | + |
| 43 | + deleteCollection: DatabaseService["deleteCollection"] = async ( |
| 44 | + collectionId, |
| 45 | + _options, |
| 46 | + ) => { |
| 47 | + // Firestore doesn't have a direct way to delete a collection |
| 48 | + // We need to delete all documents in the collection |
| 49 | + const col = this.#instance.collection(collectionId); |
| 50 | + const snapshot = await col.get(); |
| 51 | + if (snapshot.empty) { |
| 52 | + return; |
| 53 | + } |
| 54 | + const batch = this.#instance.batch(); |
| 55 | + for (const doc of snapshot.docs) { |
| 56 | + batch.delete(doc.ref); |
| 57 | + } |
| 58 | + await batch.commit(); |
| 59 | + }; |
| 60 | + |
| 61 | + listDocuments: DatabaseService["listDocuments"] = async < |
| 62 | + Document extends StoryBookerDatabaseDocument, |
| 63 | + >( |
| 64 | + collectionId: string, |
| 65 | + _listOptions: DatabaseDocumentListOptions<Document>, |
| 66 | + _options: DatabaseServiceOptions, |
| 67 | + ) => { |
| 68 | + const col = this.#instance.collection(collectionId); |
| 69 | + const snapshot = await col.get(); |
| 70 | + const list: Document[] = []; |
| 71 | + for (const doc of snapshot.docs) { |
| 72 | + const data = doc.data() as Document; |
| 73 | + list.push({ ...data, id: doc.id }); |
| 74 | + } |
| 75 | + |
| 76 | + return list; |
| 77 | + }; |
| 78 | + |
| 79 | + getDocument: DatabaseService["getDocument"] = async < |
| 80 | + Document extends StoryBookerDatabaseDocument, |
| 81 | + >( |
| 82 | + collectionId: string, |
| 83 | + documentId: string, |
| 84 | + _options: DatabaseServiceOptions, |
| 85 | + ) => { |
| 86 | + const docRef = this.#instance.collection(collectionId).doc(documentId); |
| 87 | + const doc = await docRef.get(); |
| 88 | + if (!doc.exists) { |
| 89 | + throw new Error(`Document '${documentId}' not found.`); |
| 90 | + } |
| 91 | + return { ...doc.data(), id: doc.id } as Document; |
| 92 | + }; |
| 93 | + |
| 94 | + createDocument: DatabaseService["createDocument"] = async ( |
| 95 | + collectionId, |
| 96 | + documentData, |
| 97 | + _options, |
| 98 | + ) => { |
| 99 | + const docRef = this.#instance.collection(collectionId).doc(documentData.id); |
| 100 | + await docRef.create(documentData); |
| 101 | + }; |
| 102 | + |
| 103 | + hasDocument: DatabaseService["hasDocument"] = async ( |
| 104 | + collectionId, |
| 105 | + documentId, |
| 106 | + _options, |
| 107 | + ) => { |
| 108 | + const docRef = this.#instance.collection(collectionId).doc(documentId); |
| 109 | + const doc = await docRef.get(); |
| 110 | + return doc.exists; |
| 111 | + }; |
| 112 | + |
| 113 | + deleteDocument: DatabaseService["deleteDocument"] = async ( |
| 114 | + collectionId, |
| 115 | + documentId, |
| 116 | + _options, |
| 117 | + ) => { |
| 118 | + const docRef = this.#instance.collection(collectionId).doc(documentId); |
| 119 | + await docRef.delete(); |
| 120 | + }; |
| 121 | + |
| 122 | + updateDocument: DatabaseService["updateDocument"] = async ( |
| 123 | + collectionId, |
| 124 | + documentId, |
| 125 | + documentData, |
| 126 | + ) => { |
| 127 | + const docRef = this.#instance.collection(collectionId).doc(documentId); |
| 128 | + await docRef.set(documentData, { |
| 129 | + merge: true, |
| 130 | + mergeFields: Object.keys(documentData), |
| 131 | + }); |
| 132 | + }; |
| 133 | +} |
0 commit comments