UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

2 lines 4.59 kB
import e from"../../db/query-builder/index.mjs";import t from"./base-repository.mjs";import{sql as n}from"kysely";var r=class extends t{async count(t){let r=this.db.selectFrom(this.tableName).select(n`count(*)`.as(`count`));t.where!==void 0&&t.where.length>0&&(r=e.select(r,t.where));let i=await this.executeQuery(()=>r.executeTakeFirst(),{method:`count`});return i.response.error?i.response:this.validateResponse(i,{...t.validation,mode:`count`})}async selectSingle(t){let n=this.db.selectFrom(this.tableName).select(t.select);n=e.select(n,t.where);let r=await this.executeQuery(()=>n.executeTakeFirst(),{method:`selectSingle`});return r.response.error?r.response:this.validateResponse(r,{...t.validation,mode:`single`,select:t.select})}async selectMultiple(t){let n=this.db.selectFrom(this.tableName).select(t.select);if(t.where&&(n=e.select(n,t.where)),t.orderBy)for(let e of t.orderBy)n=n.orderBy(e.column,e.direction);t.limit&&(n=n.limit(t.limit)),t.offset&&(n=n.offset(t.offset));let r=await this.executeQuery(()=>n.execute(),{method:`selectMultiple`});return r.response.error?r.response:this.validateResponse(r,{...t.validation,mode:`multiple`,select:t.select})}async selectMultipleFiltered(t){let r=await this.executeQuery(async()=>{let r=this.db.selectFrom(this.tableName).select(t.select),i=this.db.selectFrom(this.tableName).select(n`count(*)`.as(`count`));t.where&&(r=e.select(r,t.where),i=e.select(i,t.where));let{main:a,count:o}=e.main({main:r,count:i},{queryParams:t.queryParams,database:this.dbAdapter.config,meta:this.queryConfig}),[s,c]=await Promise.all([a.execute(),o?.executeTakeFirst()]);return[s,c]},{method:`selectMultipleFiltered`});return r.response.error?r.response:this.validateResponse(r,{...t.validation,mode:`multiple-count`,select:t.select})}async deleteSingle(t){let n=this.db.deleteFrom(this.tableName);t.returnAll!==!0&&t.returning&&t.returning.length>0&&(n=n.returning(t.returning)),t.returnAll&&(n=n.returningAll()),n=e.delete(n,t.where);let r=await this.executeQuery(()=>n.executeTakeFirst(),{method:`deleteSingle`});return r.response.error?r.response:this.validateResponse(r,{...t.validation,mode:`single`,select:t.returning,selectAll:t.returnAll})}async deleteMultiple(t){let n=this.db.deleteFrom(this.tableName);t.returnAll!==!0&&t.returning&&t.returning.length>0&&(n=n.returning(t.returning)),t.returnAll&&(n=n.returningAll()),n=e.delete(n,t.where);let r=await this.executeQuery(()=>n.execute(),{method:`deleteMultiple`});return r.response.error?r.response:this.validateResponse(r,{...t.validation,mode:`multiple`,select:t.returning,selectAll:t.returnAll})}async createSingle(e){let t=this.formatData(e.data,{type:`insert`}),n=Object.keys(t).length>0?this.db.insertInto(this.tableName).values(t):this.db.insertInto(this.tableName).defaultValues();e.returnAll!==!0&&e.returning&&e.returning.length>0&&(n=n.returning(e.returning)),e.returnAll&&(n=n.returningAll());let r=await this.executeQuery(()=>n.executeTakeFirst(),{method:`createSingle`});return r.response.error?r.response:this.validateResponse(r,{...e.validation,mode:`single`,select:e.returning,selectAll:e.returnAll})}async createMultiple(e){let t=this.db.insertInto(this.tableName).values(e.data.map(e=>this.formatData(e,{type:`insert`})));e.returnAll!==!0&&e.returning&&e.returning.length>0&&(t=t.returning(e.returning)),e.returnAll&&(t=t.returningAll());let n=await this.executeQuery(()=>t.execute(),{method:`createMultiple`});return n.response.error?n.response:this.validateResponse(n,{...e.validation,mode:`multiple`,select:e.returning,selectAll:e.returnAll})}async updateSingle(t){let n=this.db.updateTable(this.tableName).set(this.formatData(t.data,{type:`update`})).$if(t.returnAll!==!0&&t.returning!==void 0&&t.returning.length>0,e=>e.returning(t.returning)).$if(t.returnAll??!1,e=>e.returningAll());n=e.update(n,t.where);let r=await this.executeQuery(()=>n.executeTakeFirst(),{method:`updateSingle`});return r.response.error?r.response:this.validateResponse(r,{...t.validation,mode:`single`,select:t.returning,selectAll:t.returnAll})}async updateMultiple(t){let n=this.db.updateTable(this.tableName).set(this.formatData(t.data,{type:`update`})).$if(t.returnAll!==!0&&t.returning!==void 0&&t.returning.length>0,e=>e.returning(t.returning)).$if(t.returnAll??!1,e=>e.returningAll());n=e.update(n,t.where);let r=await this.executeQuery(()=>n.execute(),{method:`updateMultiple`});return r.response.error?r.response:this.validateResponse(r,{...t.validation,mode:`single`,select:t.returning,selectAll:t.returnAll})}};export{r as default}; //# sourceMappingURL=static-repository.mjs.map