var p=Object.defineProperty;var h=(n,e,t)=>e in n?p(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var r=(n,e,t)=>(h(n,typeof e!="symbol"?e+"":e,t),t);import{A as d}from"./utils.3371dfe5.js";class l{async list(){return await(await fetch("/_editor/api/tables")).json()}async create(){return await(await fetch("/_editor/api/tables",{method:"POST",headers:{"Content-Type":"application/json"}})).json()}async get(e){return await(await fetch(`/_editor/api/tables/${e}`)).json()}async update(e,t){return await(await fetch(`/_editor/api/tables/${e}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})).json()}async delete(e){await fetch(`/_editor/api/tables/${e}`,{method:"DELETE"})}async duplicate(e){return await(await fetch(`/_editor/api/tables/${e}/duplicate`,{method:"POST"})).json()}async dbTypes(){return(await fetch("/_editor/api/workspace/db-types")).json()}async select(e,t){const a=new URLSearchParams(t).toString();return(await fetch(`/_editor/api/tables/${e}/rows?${a}`,{method:"GET",headers:{"Content-Type":"application/json"}})).json()}async insertRow(e,t){return(await fetch(`/_editor/api/tables/${e}/rows`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})).json()}async updateRow(e,t,a){await fetch(`/_editor/api/tables/${e}/rows`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({where:t,set:a})})}async deleteRow(e,t){await fetch(`/_editor/api/tables/${e}/rows`,{method:"DELETE",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})}}const s=new l;class c{constructor(e){r(this,"tableName");this.tableName=e}async list(){return(await fetch(`/_editor/api/tables/${this.tableName}/columns`)).json()}async get(e){return(await fetch(`/_editor/api/tables/${this.tableName}/columns/${e}`)).json()}async update(e,t){return(await fetch(`/_editor/api/tables/${this.tableName}/columns/${e}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})).json()}async create(){return(await fetch(`/_editor/api/tables/${this.tableName}/columns`,{method:"POST",headers:{"Content-Type":"application/json"}})).json()}async delete(e){await fetch(`/_editor/api/tables/${this.tableName}/columns/${e}`,{method:"DELETE"})}}class i{constructor(e,t){r(this,"record");r(this,"table");r(this,"api");this.api=new c(e.name),this.record=new d(this.api,t,"name"),this.table=e}static from(e,t){return new i(e,t)}static async create(e){const a=await new c(e.name).create();return i.from(e,a)}get name(){return this.record.get("name")}set name(e){this.record.set("name",e)}get type(){return this.record.get("type")}set type(e){this.record.set("type",e)}hasChanges(){return this.record.hasChanges()}async delete(){this.table.deleteColumn(this.name)}async save(){return this.record.save()}get primaryKey(){return this.record.get("primary_key")}get notNull(){return this.record.get("not_null")}set notNull(e){this.record.set("not_null",e)}get default(){return this.record.get("default")}set default(e){e||this.record.set("not_null",!1),this.record.set("default",e)}get id(){return this.record.get("id")}}class o{constructor(e){r(this,"record");r(this,"_columns");this.record=new d(s,e,"name"),this._columns=e.columns.map(t=>i.from(this,t))}static async list(){return(await s.list()).map(t=>new o(t))}static async create(){const e=await s.create();return new o(e)}static async get(e){const t=await s.get(e);return new o(t)}async delete(){await s.delete(this.name)}async duplicate(){const e=await s.duplicate(this.name);return new o(e)}async save(){await this.record.save()}onUpdate(e){this.record.pubsub.subscribe("update",e)}hasChanges(){return this.record.hasChanges()}get name(){return this.record.get("name")}set name(e){this.record.set("name",e)}get columns(){return this._columns}async createColumn(){const e=await i.create(this);this._columns.push(e)}static async dbTypes(){return s.dbTypes()}async deleteColumn(e){await new c(this.name).delete(e),this._columns=this._columns.filter(a=>a.name!==e)}async select(e={}){return s.select(this.name,e)}async insertRow(e){return s.insertRow(this.name,e)}async updateRow(e,t){return s.updateRow(this.name,e,t)}async deleteRow(e){return s.deleteRow(this.name,e)}}export{o as T};
//# sourceMappingURL=tables.831fc587.js.map
