@gensx/storage
Version:
Cloud storage, blobs, sqlite, and vector database providers/hooks for GenSX.
86 lines (82 loc) • 2.86 kB
JavaScript
/**
* Check out the docs at https://www.gensx.com/docs
* Find us on Github https://github.com/gensx-inc/gensx
* Find us on Discord https://discord.gg/F5BSU8Kc
*/
import { join } from 'path';
import { getProjectAndEnvironment } from '../utils/config.js';
import { FileSystemDatabaseStorage } from './filesystem.js';
import { RemoteDatabaseStorage } from './remote.js';
/**
* Client for interacting with database functionality outside of JSX context
*/
class DatabaseClient {
storage;
/**
* Create a new DatabaseClient
* @param options Optional configuration properties for the database storage
*/
constructor(options = {}) {
const kind = options.kind ??
(process.env.GENSX_RUNTIME === "cloud" ? "cloud" : "filesystem");
if (kind === "filesystem") {
const rootDir = options.kind === "filesystem" && options.rootDir
? options.rootDir
: join(process.cwd(), ".gensx", "databases");
this.storage = new FileSystemDatabaseStorage(rootDir);
}
else {
const { project, environment } = getProjectAndEnvironment({
project: options.project,
environment: options.environment,
});
this.storage = new RemoteDatabaseStorage(project, environment);
}
}
/**
* Get a database (ensures it exists first)
* @param name The database name
* @returns A Promise resolving to a Database
*/
async getDatabase(name) {
if (!this.storage.hasEnsuredDatabase(name)) {
await this.storage.ensureDatabase(name);
}
return this.storage.getDatabase(name);
}
/**
* Ensure a database exists (idempotent operation)
* @param name The database name
* @returns A Promise resolving to the ensure result
*/
async ensureDatabase(name) {
return this.storage.ensureDatabase(name);
}
/**
* List all databases
* @param options Optional pagination options
* @returns A Promise resolving to an array of database names and optional next cursor
*/
async listDatabases(options) {
return this.storage.listDatabases(options);
}
/**
* Delete a database
* @param name The database name
* @returns A Promise resolving to the deletion result
*/
async deleteDatabase(name) {
return this.storage.deleteDatabase(name);
}
/**
* Check if a database exists
* @param name The database name
* @returns A Promise resolving to a boolean indicating if the database exists
*/
async databaseExists(name) {
const result = await this.storage.listDatabases();
return result.databases.some((db) => db.name === name);
}
}
export { DatabaseClient };
//# sourceMappingURL=databaseClient.js.map