UNPKG

@lucidcms/core

Version:

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

5 lines 2.43 kB
import e from"./parents/static-repository.mjs";import{optionsNameSchema as t}from"../../schemas/options.mjs";import n from"zod";import{sql as r}from"kysely";var i=class extends e{constructor(e,t){super(e,t,`lucid_options`)}tableSchema=n.object({name:t,value_int:n.number().nullable(),value_text:n.string().nullable(),value_bool:n.union([n.literal(this.dbAdapter.config.defaults.boolean.true),n.literal(this.dbAdapter.config.defaults.boolean.false)]).nullable()});columnFormats={name:this.dbAdapter.getDataType(`text`),value_int:this.dbAdapter.getDataType(`integer`),value_text:this.dbAdapter.getDataType(`text`),value_bool:this.dbAdapter.getDataType(`boolean`)};queryConfig=void 0;async upsertSingle(e){let t=this.db.insertInto(`lucid_options`).values(this.formatData(e.data,{type:`insert`})).onConflict(e=>e.column(`name`).doUpdateSet(e=>({value_int:e.ref(`excluded.value_int`),value_text:e.ref(`excluded.value_text`),value_bool:e.ref(`excluded.value_bool`)}))).$if(e.returnAll!==!0&&e.returning!==void 0&&e.returning.length>0,t=>t.returning(e.returning)).$if(e.returnAll??!1,e=>e.returningAll()),n=await this.executeQuery(()=>t.executeTakeFirst(),{method:`upsertSingle`});return n.response.error?n.response:this.validateResponse(n,{...e.validation,mode:`single`,select:e.returning,selectAll:e.returnAll})}async selectMediaStorageUsageOptions(){let e=this.db.selectFrom(`lucid_options`).select([`name`,`value_int`]),t=await this.executeQuery(()=>e.execute(),{method:`selectMediaStorageUsageOptions`});if(t.response.error)return t.response;let n={...t,response:{...t.response,data:t.response.data.filter(e=>e.name===`media_storage_used`||e.name.startsWith(`media_storage_used:t:`))}};return this.validateResponse(n,{enabled:!0,mode:`multiple`,select:[`name`,`value_int`]})}async adjustInt(e){let t=e.data.min??0,n=this.db.updateTable(`lucid_options`).set({value_int:r`CASE WHEN COALESCE(value_int, 0) + ${e.data.delta} < ${t} THEN ${t} ELSE COALESCE(value_int, 0) + ${e.data.delta} END`}).where(`name`,`=`,e.data.name);e.data.max!==void 0&&e.data.delta>0&&(n=n.where(r`COALESCE(value_int, 0) + ${e.data.delta}`,`<=`,e.data.max));let i=await this.executeQuery(async()=>{let e=await n.executeTakeFirst();return{count:Number(e.numUpdatedRows??0n)}},{method:`adjustInt`});return i.response.error?i.response:this.validateResponse(i,{...e.validation,mode:`count`})}};export{i as default}; //# sourceMappingURL=options.mjs.map