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 p=(s,e,t)=>(m(s,typeof e!="symbol"?e+"":e,t),t);import{E as b}from"./record.6f7f627d.js";import{C as u}from"./index.13fb7824.js";import{n as l}from"./string.b3ce1063.js";import{C as n}from"./router.9521f915.js";import"./outputWidgets.f0391a74.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]="94863634-5ce5-42ce-a197-4b37bacdf69e",s._sentryDebugIdIdentifier="sentry-dbid-94863634-5ce5-42ce-a197-4b37bacdf69e")}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})}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 a=new I;class i{constructor(e,t=null){p(this,"record");p(this,"columns");this.record=b.from(e),this.columns=t}static async list(e){return(await a.list(e)).map(r=>new i(r))}static async fromColumnId(e,t){const r=await a.getByColumnID({projectId:e,columnId:t});return i.get(e,r.table.id)}static async create(e,t){const r=l(t,!1),o=await a.create({projectId:e},{name:r});return new i(o.table,o.columns.map(c=>u.fromDTO(c)))}static async get(e,t){const r=await a.get({projectId:e,tableId:t}),o=r.table,d=r.columns.map(c=>u.fromDTO({...c,projectId:o.projectId}));return new i(o,d)}async delete(e,t){return a.delete({projectId:e,tableId:t})}fixTraillingName(){this.name=l(this.name,!1)}async save(){if(Object.keys(this.record.changes).length!==0){this.record.changes.name&&this.fixTraillingName();try{await a.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)&&l(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=l(e,!0);this.record.set("name",t)}get projectId(){return this.record.get("projectId")}async addColumn(e){const t=await u.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,o){const d=Object.keys(e).map(c=>`${c} = :${c}`).join(" AND ");return a.selectRows({name:this.name,where:d,params:e,tableId:this.id,projectId:this.projectId,limit:r,offset:t,search:o})}async insertRow(e){return a.insertRow({tableId:this.id,projectId:this.projectId},e)}async updateRow(e,t){return a.updateRow({tableId:this.id,projectId:this.projectId,rowId:e},t)}async deleteRow(e){return a.deleteRow({tableId:this.id,projectId:this.projectId,rowId:e})}}export{i as T,D as d,C as p};
//# sourceMappingURL=tables.1e84ed6e.js.map
