UNPKG

@lucidcms/core

Version:

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

85 lines (84 loc) 3.62 kB
import { LucidErrorData } from "../../../types/errors.mjs"; import { FilterOperator } from "../../../types/query-params.mjs"; import DatabaseAdapter from "../../db/adapter-base.mjs"; import { Insert, KyselyDB, LucidDB, Update } from "../../db/types.mjs"; import { ExecuteMeta, QueryResult, ValidationConfigExtend } from "../types.mjs"; import z, { ZodObject, ZodType } from "zod"; import { ColumnDataType, InsertObject, UpdateObject } from "kysely"; //#region src/libs/repositories/parents/base-repository.d.ts declare abstract class BaseRepository<Table extends keyof LucidDB, T extends LucidDB[Table] = LucidDB[Table]> { protected readonly db: KyselyDB; protected readonly dbAdapter: DatabaseAdapter; tableName: keyof LucidDB; constructor(db: KyselyDB, dbAdapter: DatabaseAdapter, tableName: keyof LucidDB); /** * A Zod schema for the table. */ protected abstract tableSchema: ZodObject<any>; /** * The column data types for the table. Repositories need to keep these in sync with the migrations and the database. */ protected abstract columnFormats: Partial<Record<keyof T, ColumnDataType>>; /** * The query configuration for the table. The main query builder fn uses this to map filter and sort query params to table columns, along with deciding which operators to use. */ protected abstract queryConfig?: { tableKeys?: { filters?: Record<string, string>; sorts?: Record<string, string>; }; operators?: Record<string, FilterOperator>; }; /** * Formats values that need special handling (like JSON or booleans) * Leaves other values and column names unchanged */ protected formatData<Type extends "insert" | "update">(data: Partial<Insert<T>> | Partial<Update<T>>, config: { type: Type; dynamicColumns?: Record<string, ColumnDataType>; }): Type extends "insert" ? InsertObject<LucidDB, Table> : UpdateObject<LucidDB, Table>; /** * Creates a validation schema based on selected columns * * - when selectAll is true, returns the full schema * - when the select array is passed, picks only those columns from the schema * - otherwise, makes all fields optional */ protected createValidationSchema<V extends boolean = false>(config: ValidationConfigExtend<V>): ZodType; /** * Responsible for creating schemas based on the mode */ private wrapSchemaForMode; /** * Merges the given schema with the tableSchema */ protected mergeSchema(schema?: ZodObject<any>): z.ZodObject<any, z.z.core.$strip> | z.ZodObject<{ [x: string]: any; }, any>; /** * Checks if the response data exists and successfully validates against a schema. * * Type narrows the response to not be undefined when the validation is enabled. */ protected validateResponse<QueryData, V extends boolean = false>(executeResponse: Awaited<ReturnType<typeof this.executeQuery<QueryData | undefined>>>, config?: ValidationConfigExtend<V>): Promise<QueryResult<QueryData, V>>; /** * Handles executing a query and logging * @todo add query data to debug log, add a sanitise data method that each repo can extend to mark certain columns to have data redacted, ie passwords, tokens, user data etc. */ protected executeQuery<QueryData>(executeFn: () => Promise<QueryData>, config: { method: string; tableName?: string; }): Promise<{ response: { error: LucidErrorData; data: undefined; } | { error: undefined; data: QueryData; }; meta: ExecuteMeta; }>; } //#endregion export { BaseRepository as default }; //# sourceMappingURL=base-repository.d.mts.map