@storybooker/gcp
Version:
StoryBooker Adapter for interacting with GCP services.
61 lines (59 loc) • 2.21 kB
JavaScript
//#region src/firestore.ts
var GcpFirestoreDatabaseService = class {
#instance;
constructor(instance) {
this.listCollections = async (_options) => {
return (await this.#instance.listCollections()).map((col) => col.id);
};
this.createCollection = async (_collectionId, _options) => {};
this.hasCollection = async (collectionId, _options) => {
return !(await this.#instance.collection(collectionId).limit(1).get()).empty;
};
this.deleteCollection = async (collectionId, _options) => {
const snapshot = await this.#instance.collection(collectionId).get();
if (snapshot.empty) return;
const batch = this.#instance.batch();
for (const doc of snapshot.docs) batch.delete(doc.ref);
await batch.commit();
};
this.listDocuments = async (collectionId, _listOptions, _options) => {
const snapshot = await this.#instance.collection(collectionId).get();
const list = [];
for (const doc of snapshot.docs) {
const data = doc.data();
list.push({
...data,
id: doc.id
});
}
return list;
};
this.getDocument = async (collectionId, documentId, _options) => {
const doc = await this.#instance.collection(collectionId).doc(documentId).get();
if (!doc.exists) throw new Error(`Document '${documentId}' not found.`);
return {
...doc.data(),
id: doc.id
};
};
this.createDocument = async (collectionId, documentData, _options) => {
await this.#instance.collection(collectionId).doc(documentData.id).create(documentData);
};
this.hasDocument = async (collectionId, documentId, _options) => {
return (await this.#instance.collection(collectionId).doc(documentId).get()).exists;
};
this.deleteDocument = async (collectionId, documentId, _options) => {
await this.#instance.collection(collectionId).doc(documentId).delete();
};
this.updateDocument = async (collectionId, documentId, documentData) => {
await this.#instance.collection(collectionId).doc(documentId).set(documentData, {
merge: true,
mergeFields: Object.keys(documentData)
});
};
this.#instance = instance;
}
};
//#endregion
exports.GcpFirestoreDatabaseService = GcpFirestoreDatabaseService;
//# sourceMappingURL=firestore.cjs.map