UNPKG

bknd

Version:

Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.

107 lines (106 loc) 4.55 kB
import { s } from "bknd/utils"; import { type Field, PrimaryField, type TActionContext, type TRenderContext } from "../fields"; export declare const entityConfigSchema: s.ObjectSchema<{ readonly name: s.Schema<s.ISchemaOptions, string | undefined, string | undefined>; readonly name_singular: s.Schema<s.ISchemaOptions, string | undefined, string | undefined>; readonly description: s.Schema<s.ISchemaOptions, string | undefined, string | undefined>; readonly sort_field: s.Schema<s.ISchemaOptions, string | undefined, string | undefined>; readonly sort_dir: s.Schema<s.ISchemaOptions, "asc" | "desc" | undefined, "asc" | "desc" | undefined>; readonly primary_format: s.Schema<s.ISchemaOptions, "integer" | "uuid" | undefined, "integer" | "uuid" | undefined>; }, s.Merge<{ readonly default: {}; } & { additionalProperties: false; }>>; export type EntityConfig = s.Static<typeof entityConfigSchema>; export type EntityData = Record<string, any>; export type EntityJSON = ReturnType<Entity["toJSON"]>; /** * regular: normal defined entity * system: generated by the system, e.g. "users" from auth * generated: result of a relation, e.g. many-to-many relation's connection entity */ export declare const entityTypes: readonly ["regular", "system", "generated"]; export type TEntityType = (typeof entityTypes)[number]; /** * @todo: add check for adding fields (primary and relation not allowed) * @todo: add option to disallow api deletes (or api actions in general) */ export declare class Entity<EntityName extends string = string, Fields extends Record<string, Field<any, any, any>> = Record<string, Field<any, any, any>>> { #private; readonly name: string; readonly fields: Field[]; readonly config: EntityConfig; protected data: EntityData[] | undefined; readonly type: TEntityType; constructor(name: string, fields?: Field[], config?: EntityConfig, type?: TEntityType); static isEntity(e: unknown): e is Entity; static create(args: { name: string; fields?: Field[]; config?: EntityConfig; type?: TEntityType; }): Entity<string, Record<string, Field<any, any, any>>>; getType(): TEntityType; getSelect(alias?: string, context?: TActionContext | TRenderContext): string[]; getDefaultSort(): { by: string; dir: "asc" | "desc"; }; getAliasedSelectFrom(select: string[], _alias?: string, context?: TActionContext | TRenderContext): string[]; getFillableFields(context?: TActionContext, include_virtual?: boolean): Field[]; getRequiredFields(): Field[]; getDefaultObject(): EntityData; getField(name: string): Field | undefined; __replaceField(name: string, field: Field): void; getPrimaryField(): PrimaryField; id(): PrimaryField; get label(): string; field(name: string): Field | undefined; hasField(name: string): boolean; hasField(field: Field): boolean; getFields(include_virtual?: boolean): Field[]; addField(field: Field): void; __setData(data: EntityData[]): void; isValidData(data: EntityData, context: TActionContext, options?: { explain?: boolean; ignoreUnknown?: boolean; }): boolean; toSchema(options?: { clean: boolean; context?: "create" | "update"; }): object; toTypes(): { name: string; type: "regular" | "system" | "generated"; comment: string | undefined; fields: { [k: string]: import("../..").TFieldTSType; }; }; toJSON(): { type: "regular" | "system" | "generated"; fields: { [k: string]: { type: any; config: { description?: string | undefined; required?: boolean | undefined; default_value?: any; label?: string | undefined; fillable?: boolean | ("create" | "read" | "update" | "delete")[] | undefined; hidden?: boolean | ("table" | "create" | "read" | "update" | "delete" | "form" | "submit")[] | undefined; virtual?: boolean | undefined; }; }; }; config: { description?: string | undefined; name?: string | undefined; name_singular?: string | undefined; sort_field?: string | undefined; sort_dir?: "asc" | "desc" | undefined; primary_format?: "integer" | "uuid" | undefined; }; }; }