/** * Entity definition and compilation to IR * @module entity */ import { FieldBuilder, FieldConfig } from './fields'; import { RelationBuilder, RelationConfig } from './relations'; import { ExternalSourceConfig } from './source'; /** * Shorthand protection options for common patterns */ export type ProtectedShorthand = 'write' | 'all' | boolean; /** * Granular protection config per CRUD operation */ export interface ProtectedConfig { list?: boolean; get?: boolean; create?: boolean; update?: boolean; remove?: boolean; } /** * Protection option - shorthand or granular * @example * protected: 'write' // list/get public, mutations protected (most common) * protected: 'all' // everything requires auth * protected: true // alias for 'all' * protected: false // everything public (default) * protected: { list: false, get: false, create: true, update: true, remove: true } */ export type ProtectedOption = ProtectedShorthand | ProtectedConfig; /** * Normalized protection config (always granular) */ export interface ProtectedIR { list: boolean; get: boolean; create: boolean; update: boolean; remove: boolean; } /** * Entity definition input - what you write when defining an entity */ export interface EntityDefinition { /** Fields for this entity, using field builders like `text()`, `number()` */ fields: Record; /** Relations to other entities using `hasOne()`, `hasMany()`, `belongsToMany()` */ relations?: Record; /** Entity behaviors like timestamps, soft delete, audit logging */ behaviors?: EntityBehaviors; /** Mark as auth entity for next-auth integration */ auth?: boolean; /** * Protection level for CRUD operations (requires auth.enabled in config) * - 'write': list/get public, create/update/remove protected (most common) * - 'all' or true: all operations require auth * - false: all operations public (default) * - object: granular control per operation */ protected?: ProtectedOption; /** * External API source for this entity. * If not specified, inherits from manifest.source or uses database. */ source?: ExternalSourceConfig; /** * CRUD hooks configuration. * Enable specific hooks or use `true` to enable all hooks. * @example * hooks: true // Enable all hooks * hooks: { beforeCreate: true, afterCreate: true } // Enable specific hooks */ hooks?: boolean | HooksConfig; } /** * Configurable behaviors for an entity */ export interface EntityBehaviors { /** Enable soft delete (adds `deletedAt` field instead of hard delete) */ softDelete?: boolean; /** Enable audit logging for all changes */ audit?: boolean; /** Add `createdAt` and `updatedAt` timestamps (default: true) */ timestamps?: boolean; } /** * CRUD hook configuration - enables hook points in generated routers * * When enabled, hooks are invoked at the appropriate points in CRUD operations. * Hook implementations are defined in user-managed files (hooks/{entity}.ts). */ export interface HooksConfig { /** Called before creating a record. Can modify input or throw to abort. */ beforeCreate?: boolean; /** Called after a record is created. For side effects (email, audit, etc). */ afterCreate?: boolean; /** Called before updating a record. Can modify input or throw to abort. */ beforeUpdate?: boolean; /** Called after a record is updated. For side effects. */ afterUpdate?: boolean; /** Called before removing a record. Can throw to abort. */ beforeRemove?: boolean; /** Called after a record is removed. For cleanup/archival. */ afterRemove?: boolean; } /** * Normalized hooks config (all boolean values) */ export interface HooksIR { beforeCreate: boolean; afterCreate: boolean; beforeUpdate: boolean; afterUpdate: boolean; beforeRemove: boolean; afterRemove: boolean; } /** * Compiled entity intermediate representation - consumed by templates */ export interface EntityIR { /** Entity name in PascalCase */ name: string; /** Compiled field configurations */ fields: Record; /** Compiled relation configurations */ relations: Record; /** Entity behaviors */ behaviors: EntityBehaviors; /** Whether this is an auth entity */ auth: boolean; /** Normalized protection config for CRUD operations */ protected: ProtectedIR; /** Whether protected was explicitly set (for validation) */ _hasProtected: boolean; /** External API source (optional - inherits from manifest if not specified) */ source?: ExternalSourceConfig; /** Normalized hooks config */ hooks: HooksIR; } /** * Define an entity with fields, relations, and behaviors * * @param name - Entity name in PascalCase (e.g., 'User', 'BlogPost') * @param definition - Entity configuration with fields, relations, and behaviors * @returns Compiled EntityIR for use by generators * * @example * ```typescript * const User = defineEntity('User', { * fields: { * email: text().required().unique().email(), * name: text().required().min(2).max(100), * age: number().optional().min(0).integer(), * }, * relations: { * posts: hasMany('Post'), * }, * behaviors: { * timestamps: true, * softDelete: true, * } * }) * ``` */ export declare function defineEntity(name: Name, definition: EntityDefinition): EntityIR; //# sourceMappingURL=entity.d.ts.map