@storybooker/gcp
Version:
StoryBooker Adapter for interacting with GCP services.
67 lines (65 loc) • 2.51 kB
JavaScript
import { SERVICE_NAME } from "@storybooker/core/constants";
//#region src/big-table.ts
const COLUMN_FAMILY = "cf1";
var GcpBigtableDatabaseService = class {
#instance;
constructor(client, instanceName = SERVICE_NAME) {
this.init = async (_options) => {
const [exists] = await this.#instance.exists();
if (!exists) throw new Error(`Bigtable instance '${this.#instance.id}' does not exist.`);
};
this.listCollections = async (_options) => {
const [tables] = await this.#instance.getTables();
return tables.map((table) => table.id);
};
this.createCollection = async (collectionId, _options) => {
await this.#instance.createTable(collectionId, { families: [COLUMN_FAMILY] });
};
this.hasCollection = async (collectionId, _options) => {
const [exists] = await this.#instance.table(collectionId).exists();
return exists;
};
this.deleteCollection = async (collectionId, _options) => {
await this.#instance.table(collectionId).delete();
};
this.listDocuments = async (collectionId, _listOptions, _options) => {
const [rows] = await this.#instance.table(collectionId).getRows();
const list = [];
for (const row of rows) {
const document = {
...row.data[COLUMN_FAMILY],
id: row.id
};
list.push(document);
}
return list;
};
this.getDocument = async (collectionId, documentId, _options) => {
const row = this.#instance.table(collectionId).row(documentId);
const [exists] = await row.exists();
if (!exists) throw new Error(`Document '${documentId}' not found.`);
const [rowData] = await row.get([COLUMN_FAMILY]);
return {
...rowData,
id: documentId
};
};
this.createDocument = async (collectionId, documentData, _options) => {
await this.#instance.table(collectionId).row(documentData.id).create({ entry: { [COLUMN_FAMILY]: documentData } });
};
this.hasDocument = async (collectionId, documentId, _options) => {
const [exists] = await this.#instance.table(collectionId).row(documentId).exists();
return exists;
};
this.deleteDocument = async (collectionId, documentId, _options) => {
await this.#instance.table(collectionId).row(documentId).delete();
};
this.updateDocument = async (collectionId, documentId, documentData) => {
await this.#instance.table(collectionId).row(documentId).save({ [COLUMN_FAMILY]: documentData });
};
this.#instance = client.instance(instanceName);
}
};
//#endregion
export { GcpBigtableDatabaseService };
//# sourceMappingURL=big-table.js.map