var h=Object.defineProperty;var m=(s,e,t)=>e in s?h(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var l=(s,e,t)=>(m(s,typeof e!="symbol"?e+"":e,t),t);import{E as b}from"./record.2a93f29c.js";import{C as p}from"./index.803d0576.js";import{n as i}from"./string.256d79cc.js";import{C as n}from"./gateway.9b472f57.js";import"./outputWidgets.52571dac.js";(function(){try{var s=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},e=new Error().stack;e&&(s._sentryDebugIds=s._sentryDebugIds||{},s._sentryDebugIds[e]="b912b7e0-34b6-428c-9ce5-e15745e46d26",s._sentryDebugIdIdentifier="sentry-dbid-b912b7e0-34b6-428c-9ce5-e15745e46d26")}catch{}})();const C=["varchar","int","boolean","json","date","timestamp","uuid"],D={varchar:"'DEFAULT_VALUE'",int:"42",boolean:"false",json:"'{}'::json",date:"now()",timestamp:"now()",uuid:"gen_random_uuid()"};class I{async list(e){return n.get(`projects/${e}/tables`)}async create(e,t){return await n.post(`projects/${e.projectId}/tables`,t)}async get(e){return n.get(`projects/${e.projectId}/tables/${e.tableId}`)}async delete(e){return n.delete(`projects/${e.projectId}/tables/${e.tableId}`)}async selectRows(e){return n.get(`projects/${e.projectId}/tables/${e.tableId}/rows`,{limit:e.limit.toString(),offset:e.offset.toString(),search:e.search,where:JSON.stringify(e.where)})}async update(e,t){return n.patch(`projects/${e.projectId}/tables/${e.tableId}`,t)}async insertRow(e,t){return n.post(`projects/${e.projectId}/tables/${e.tableId}/rows`,t)}async updateRow(e,t){return n.patch(`projects/${e.projectId}/tables/${e.tableId}/rows/${e.rowId}`,t)}async deleteRow(e){return n.delete(`projects/${e.projectId}/tables/${e.tableId}/rows/${e.rowId}`)}async getByColumnID(e){return n.get(`projects/${e.projectId}/columns/${e.columnId}`)}}const o=new I;class c{constructor(e,t=null){l(this,"record");l(this,"columns");this.record=b.from(e),this.columns=t}static async list(e){return(await o.list(e)).map(r=>new c(r))}static async fromColumnId(e,t){const r=await o.getByColumnID({projectId:e,columnId:t});return c.get(e,r.table.id)}static async create(e,t){const r=i(t,!1),a=await o.create({projectId:e},{name:r});return new c(a.table,a.columns.map(d=>p.fromDTO(d)))}static async get(e,t){const r=await o.get({projectId:e,tableId:t}),a=r.table,u=r.columns.map(d=>p.fromDTO({...d,projectId:a.projectId}));return new c(a,u)}async delete(e,t){return o.delete({projectId:e,tableId:t})}fixTraillingName(){this.name=i(this.name,!1)}async save(){if(Object.keys(this.record.changes).length!==0){this.record.changes.name&&this.fixTraillingName();try{await o.update({id:this.id,tableId:this.id,projectId:this.projectId},this.record.changes)}finally{this.record.resetChanges()}}}resetChanges(){this.record.resetChanges()}onUpdate(e){this.record.pubsub.subscribe("update",e)}hasChanges(){return this.record.hasChanges()}hasChangesDeep(e){return this.record.hasChangesDeep(e)&&i(this.name,!1)!==this.record.initialState.name}getColumns(){var e;return(e=this.columns)!=null?e:[]}getUnprotectedColumns(){var e,t;return(t=(e=this.columns)==null?void 0:e.filter(r=>!r.protected).map(r=>r.toDTO()))!=null?t:[]}get id(){return this.record.get("id")}get name(){return this.record.get("name")}set name(e){const t=i(e,!0);this.record.set("name",t)}get projectId(){return this.record.get("projectId")}async addColumn(e){const t=await p.create(e.name,e.type,e.default,e.nullable,e.unique,this.id,this.projectId,e.foreignKey);return"error"in t?{success:!1,error:t.error}:this.columns?(this.columns.push(t),{success:!0,error:""}):(this.columns=[t],{success:!0,error:""})}getColumn(e){var t;return(t=this.columns)==null?void 0:t.find(r=>r.id&&r.id===e)}async select(e={},t,r,a){return o.selectRows({name:this.name,where:e,tableId:this.id,projectId:this.projectId,limit:r,offset:t,search:a})}async insertRow(e){return o.insertRow({tableId:this.id,projectId:this.projectId},e)}async updateRow(e,t){return o.updateRow({tableId:this.id,projectId:this.projectId,rowId:e},t)}async deleteRow(e){return o.deleteRow({tableId:this.id,projectId:this.projectId,rowId:e})}}export{c as T,D as d,C as p};
//# sourceMappingURL=tables.206e81dd.js.map
