UNPKG

arcstrum-sdk

Version:

Arcstrum SDK for authentication and database management

103 lines (102 loc) 3.56 kB
const API_BASE = "https://api.arcstrum.com"; export class ArcstrumDBClient { async query(db_file, sql) { const data = await this.post("/db/query", { db_file, sql }); return data.rows; } constructor({ userId, token, apiBase }) { this.userId = userId; this.token = token; this.apiBase = apiBase || API_BASE; } headers() { const h = { "Content-Type": "application/json" }; if (this.token) h.Authorization = `Bearer ${this.token}`; return h; } async post(path, body) { const res = await fetch(`${this.apiBase}${path}`, { method: "POST", headers: this.headers(), body: JSON.stringify({ user_id: this.userId, ...body }), }); if (!res.ok) throw new Error(await res.text()); return res.json(); } async get(path, params) { let url = `${this.apiBase}${path}`; const search = new URLSearchParams({ user_id: this.userId, ...params }).toString(); url += `?${search}`; const res = await fetch(url, { headers: this.headers() }); if (!res.ok) throw new Error(await res.text()); return res.json(); } async listDatabases() { const data = await this.post("/db/list", {}); return data.files; } async createDatabase(db_file) { await this.post("/db/create_database", { db_file }); } async deleteDatabase(db_file) { await this.post("/db/delete_db", { db_file }); } async renameDatabase(old_name, new_name) { await this.post("/db/rename_db", { old_name, new_name }); } async getTables(db_file) { const data = await this.get("/db/tables", { db_file }); return data.tables; } async queryTable(db_file, table) { const sql = `SELECT * FROM ${table};`; const data = await this.post("/db/query", { db_file, sql }); return data.rows; } async insertRow(db_file, table, row) { await this.post("/db/insert_row", { db_file, table, row }); } async deleteRow(db_file, table, row) { await this.post("/db/delete_row", { db_file, table, row }); } async execSql(db_file, sql) { await this.post("/db/exec", { db_file, sql }); } async validateSql(sql) { return this.post("/db/validate_sql", { sql }); } async getUsageAnalytics() { return this.get("/db/usage_analytics"); } async getTableSchema(db_file, table) { const data = await this.get("/db/table_schema", { db_file, table }); return data.columns; } async getDbStats(db_file) { return this.get("/db/db_stats", { db_file }); } async getPreviewRows(db_file, table, limit = 10) { const data = await this.get("/db/table_preview", { db_file, table, limit }); return data.preview; } async getColumnInfo(db_file, table) { const data = await this.get("/db/column_info", { db_file, table }); return data.columns; } async searchTable(db_file, table, filters) { const data = await this.post("/db/search", { db_file, table, filters }); return data.rows; } async getDbMetadata(db_file) { return this.get("/db/get_metadata", { db_file }); } async updateDbMetadata(db_file, metadata) { await this.post("/db/update_metadata", { db_file, metadata }); } async deleteDbMetadata(db_file) { await this.post("/db/delete_metadata", { db_file }); } }