UNPKG

@lucidcms/core

Version:

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

2 lines 2.91 kB
import{copy as e}from"../../i18n/copy.mjs";import t from"../../../constants/constants.mjs";import n from"../../logger/index.mjs";import r from"../../../utils/errors/tidy-zod-errors.mjs";import i from"zod";var a=class{db;dbAdapter;tableName;constructor(e,t,n){this.db=e,this.dbAdapter=t,this.tableName=n}formatData(e,t){let n={},r=t.dynamicColumns===void 0?this.columnFormats:{...this.columnFormats,...t.dynamicColumns};for(let[t,i]of Object.entries(e)){let e=r[t];n[t]=e?this.dbAdapter.formatInsertValue(e,i):i}return n}createValidationSchema(e){let t=e.schema||this.tableSchema,n;return n=e.selectAll?t:Array.isArray(e.select)&&e.select.length>0?t.pick(e.select.reduce((e,t)=>(e[t]=!0,e),{})):t.partial(),this.wrapSchemaForMode(n,e.mode)}wrapSchemaForMode(e,t){switch(t){case`count`:return i.object({count:i.union([i.number(),i.string()])}).optional();case`multiple-count`:return i.tuple([i.array(e),i.object({count:i.union([i.number(),i.string()])}).optional()]);case`multiple`:return i.array(e);case`single`:return e}}mergeSchema(e){return e?this.tableSchema.merge(e.shape):this.tableSchema}async validateResponse(i,a){let o=i.response;if(a?.enabled!==!0)return o;if(o.data===void 0||o.data===null)return{error:{...a.defaultError,status:a.defaultError?.status??404},data:void 0};let s=this.createValidationSchema(a);if(!s)return o;let c=await s.safeParseAsync(o.data);if(!c.success){let o=r(c.error);return n.error({event:`query.response.validation.failed`,message:`Query response validation failed`,scope:t.logScopes.query,data:{table:i.meta.tableName,method:i.meta.method,executionTime:i.meta.executionTime,validationError:o}}),{data:void 0,error:{...a?.defaultError,message:a?.defaultError?.message??e(`server:core.errors.validation.name`),type:a?.defaultError?.type??`validation`,status:a?.defaultError?.status??400}}}return{data:o.data,error:void 0}}async executeQuery(r,i){let a=process.hrtime();try{let e=await r(),o=process.hrtime(a),s=(o[0]*1e3+o[1]/1e6).toFixed(2);return n.debug({event:`query.execution.completed`,message:`Query execution completed`,scope:t.logScopes.query,data:{table:i?.tableName??this.tableName,method:i.method,executionTime:`${s}ms`}}),{response:{data:e,error:void 0},meta:{method:i.method,executionTime:`${s}ms`,tableName:i?.tableName??this.tableName}}}catch(r){let o=process.hrtime(a),s=(o[0]*1e3+o[1]/1e6).toFixed(2);return n.error({error:r,event:`query.execution.failed`,message:`Query execution failed`,scope:t.logScopes.query,data:{table:i?.tableName??this.tableName,method:i.method,executionTime:`${s}ms`,errorMessage:r instanceof Error?r.message:`An unknown error occurred`}}),{response:{data:void 0,error:{message:e(`server:core.errors.unknown`,r instanceof Error?{defaultMessage:r.message}:void 0),status:500}},meta:{method:i.method,executionTime:`${s}ms`,tableName:i?.tableName??this.tableName}}}}};export{a as default}; //# sourceMappingURL=base-repository.mjs.map