UNPKG

bknd

Version:

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

103 lines (102 loc) 4.22 kB
import { type Static } from "../../core/utils"; import { type Field, PrimaryField, type TActionContext, type TRenderContext } from "../fields"; import * as tbbox from "@sinclair/typebox"; export declare const entityConfigSchema: tbbox.TObject<{ name: tbbox.TOptional<tbbox.TString>; name_singular: tbbox.TOptional<tbbox.TString>; description: tbbox.TOptional<tbbox.TString>; sort_field: tbbox.TOptional<tbbox.TString>; sort_dir: tbbox.TOptional<tbbox.TUnsafe<"asc" | "desc">>; primary_format: tbbox.TOptional<tbbox.TUnsafe<"uuid" | "integer">>; }>; export type EntityConfig = 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 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("./EntityTypescript").TFieldTSType; }; }; toJSON(): { type: "regular" | "system" | "generated"; fields: { [k: string]: { type: any; config: { required?: boolean | undefined; description?: string | undefined; default_value?: any; label?: string | undefined; fillable?: boolean | ("create" | "read" | "update" | "delete")[] | undefined; hidden?: boolean | ("form" | "table" | "create" | "read" | "update" | "delete" | "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?: "uuid" | "integer" | undefined; }; }; }