var d=Object.defineProperty;var p=(a,e,t)=>e in a?d(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var c=(a,e,t)=>(p(a,typeof e!="symbol"?e+"":e,t),t);import{A as u}from"./activeRecord.5794d4d2.js";import"./registerWidgets.60bba559.js";(function(){try{var a=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},e=new Error().stack;e&&(a._sentryDebugIds=a._sentryDebugIds||{},a._sentryDebugIds[e]="0d3d725c-1a80-4ffc-b4fa-65a63647ae69",a._sentryDebugIdIdentifier="sentry-dbid-0d3d725c-1a80-4ffc-b4fa-65a63647ae69")}catch{}})();class y{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 selectRow(e,t,s){return(await fetch(`/_editor/api/tables/${e}/rows`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"select",where:t,params:s})})).json()}async insertRow(e,t){const s=await fetch(`/_editor/api/tables/${e}/rows`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"insert",values:t})});return s.ok?{type:"insert-row:success",row:await s.json()}:{type:"insert-row:error",reason:await s.text()}}async updateRow(e,t,s,r){const o=await fetch(`/_editor/api/tables/${e}/rows`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update",params:r,where:t,set:s})});return o.ok?{type:"update-row:success",row:await o.json()}:{type:"update-row:error",reason:await o.text()}}async deleteRow(e,t,s){await fetch(`/_editor/api/tables/${e}/rows`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"delete",where:t,params:s})})}}const n=new y;class i{constructor(e){c(this,"record");this.record=u.create(n,e,"name")}static async list(){return(await n.list()).map(t=>new i(t))}static async create(){const e=await n.create();return new i(e)}static async get(e){const t=await n.get(e);return new i(t)}async delete(){await n.delete(this.name)}async duplicate(){const e=await n.duplicate(this.name);return new i(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)}getColumns(){return this.record.get("columns").map(e=>new Proxy(e,{get:(t,s)=>t[s],set:(t,s,r)=>(this.record.set("columns",this.record.get("columns").map(o=>o.name===t.name?{...o,[s]:r}:o)),!0)}))}async addColumn(e,t){const s={name:e,default:null,not_null:!1,primary_key:!1,type:t};this.record.set("columns",[...this.record.get("columns").map(r=>r),s])}static async dbTypes(){return n.dbTypes()}async deleteColumn(e){this.record.set("columns",this.record.get("columns").filter(t=>t.name!==e))}async select(e={}){const t=Object.keys(e).map(s=>`${s} = :${s}`).join(" AND ");return n.selectRow(this.name,t,e)}async insertRow(e){return n.insertRow(this.name,e)}async updateRow(e,t){const s=Object.keys(e).map(r=>`${r} = :${r}`).join(" AND ");return n.updateRow(this.name,s,t,e)}async deleteRow(e){const t=Object.keys(e).map(s=>`${s} = :${s}`).join(" AND ");return n.deleteRow(this.name,t,e)}}export{i as T};
//# sourceMappingURL=tables.11a3f408.js.map
