@storybooker/gcp
Version: 
StoryBooker Adapter for interacting with GCP services.
1 lines • 5.23 kB
Source Map (JSON)
{"version":3,"file":"firestore.cjs","names":["#instance","list: Document[]"],"sources":["../src/firestore.ts"],"sourcesContent":["import type { Firestore } from \"@google-cloud/firestore\";\nimport type {\n  DatabaseDocumentListOptions,\n  DatabaseService,\n  DatabaseServiceOptions,\n  StoryBookerDatabaseDocument,\n} from \"@storybooker/core/types\";\n\nexport class GcpFirestoreDatabaseService implements DatabaseService {\n  #instance: Firestore;\n\n  constructor(instance: Firestore) {\n    this.#instance = instance;\n  }\n\n  listCollections: DatabaseService[\"listCollections\"] = async (_options) => {\n    const collections = await this.#instance.listCollections();\n    return collections.map((col) => col.id);\n  };\n\n  // oxlint-disable-next-line class-methods-use-this --- NOOP\n  createCollection: DatabaseService[\"createCollection\"] = async (\n    _collectionId,\n    _options,\n  ) => {\n    // Firestore creates collections implicitly when you add a document.\n  };\n\n  hasCollection: DatabaseService[\"hasCollection\"] = async (\n    collectionId,\n    _options,\n  ) => {\n    const col = this.#instance.collection(collectionId);\n    const snapshot = await col.limit(1).get();\n    return !snapshot.empty;\n  };\n\n  deleteCollection: DatabaseService[\"deleteCollection\"] = async (\n    collectionId,\n    _options,\n  ) => {\n    // Firestore doesn't have a direct way to delete a collection\n    // We need to delete all documents in the collection\n    const col = this.#instance.collection(collectionId);\n    const snapshot = await col.get();\n    if (snapshot.empty) {\n      return;\n    }\n    const batch = this.#instance.batch();\n    for (const doc of snapshot.docs) {\n      batch.delete(doc.ref);\n    }\n    await batch.commit();\n  };\n\n  listDocuments: DatabaseService[\"listDocuments\"] = async <\n    Document extends StoryBookerDatabaseDocument,\n  >(\n    collectionId: string,\n    _listOptions: DatabaseDocumentListOptions<Document>,\n    _options: DatabaseServiceOptions,\n  ) => {\n    const col = this.#instance.collection(collectionId);\n    const snapshot = await col.get();\n    const list: Document[] = [];\n    for (const doc of snapshot.docs) {\n      const data = doc.data() as Document;\n      list.push({ ...data, id: doc.id });\n    }\n\n    return list;\n  };\n\n  getDocument: DatabaseService[\"getDocument\"] = async <\n    Document extends StoryBookerDatabaseDocument,\n  >(\n    collectionId: string,\n    documentId: string,\n    _options: DatabaseServiceOptions,\n  ) => {\n    const docRef = this.#instance.collection(collectionId).doc(documentId);\n    const doc = await docRef.get();\n    if (!doc.exists) {\n      throw new Error(`Document '${documentId}' not found.`);\n    }\n    return { ...doc.data(), id: doc.id } as Document;\n  };\n\n  createDocument: DatabaseService[\"createDocument\"] = async (\n    collectionId,\n    documentData,\n    _options,\n  ) => {\n    const docRef = this.#instance.collection(collectionId).doc(documentData.id);\n    await docRef.create(documentData);\n  };\n\n  hasDocument: DatabaseService[\"hasDocument\"] = async (\n    collectionId,\n    documentId,\n    _options,\n  ) => {\n    const docRef = this.#instance.collection(collectionId).doc(documentId);\n    const doc = await docRef.get();\n    return doc.exists;\n  };\n\n  deleteDocument: DatabaseService[\"deleteDocument\"] = async (\n    collectionId,\n    documentId,\n    _options,\n  ) => {\n    const docRef = this.#instance.collection(collectionId).doc(documentId);\n    await docRef.delete();\n  };\n\n  updateDocument: DatabaseService[\"updateDocument\"] = async (\n    collectionId,\n    documentId,\n    documentData,\n  ) => {\n    const docRef = this.#instance.collection(collectionId).doc(documentId);\n    await docRef.set(documentData, {\n      merge: true,\n      mergeFields: Object.keys(documentData),\n    });\n  };\n}\n"],"mappings":";;AAQA,IAAa,8BAAb,MAAoE;CAClE;CAEA,YAAY,UAAqB;yBAIqB,OAAO,aAAa;AAExE,WADoB,MAAM,MAAKA,SAAU,iBAAiB,EACvC,KAAK,QAAQ,IAAI,GAAG;;0BAIe,OACtD,eACA,aACG;uBAI6C,OAChD,cACA,aACG;AAGH,UAAO,EADU,MADL,MAAKA,SAAU,WAAW,aAAa,CACxB,MAAM,EAAE,CAAC,KAAK,EACxB;;0BAGqC,OACtD,cACA,aACG;GAIH,MAAM,WAAW,MADL,MAAKA,SAAU,WAAW,aAAa,CACxB,KAAK;AAChC,OAAI,SAAS,MACX;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