/** * Core Entity System * DO NOT MODIFY THIS FILE - You may break the entity functionality * * This module provides: * - EntityContext: Unified context for all entity operations * - EntityClass: Interface that all entity types implement * - EntityBase: Base class with instance methods for state management * - Entity: Local storage-backed entity class (replaces IndexedEntity) */ import type { IntegrationClient } from './core-integrations'; export interface EntityDOStub { getDoc(key: string): Promise<{ v: number; data: T; } | null>; casPut(key: string, expectedV: number, data: T): Promise<{ ok: boolean; v: number; }>; del(key: string): Promise; has(key: string): Promise; bulkCreate(items: Array<{ key: string; data: unknown; }>): Promise>; bulkUpdate(updates: Array<{ key: string; data: Record; }>): Promise>; bulkDelete(keys: string[]): Promise; queryEntities(options?: { limit?: number; offset?: number; cursor?: string | null; filters?: Record; search?: { query: string; fields: string[]; }; sort?: { field: string; order: 'asc' | 'desc'; }; }): Promise<{ items: Array<{ key: string; v: number; data: unknown; }>; total: number; hasMore: boolean; next: string | null; }>; countEntities(filters?: Record): Promise; exportAll(): Promise>; importAll(items: Array<{ key: string; v: number; data: unknown; }>): Promise<{ imported: number; }>; } import type { Env } from './core-utils'; /** * EntityContext - Unified context for all entity operations * Both Entity and IntegrationEntity use this same context type */ export interface EntityContext { env: Env; client?: IntegrationClient; } /** * Options for list operations */ export interface ListOptions { limit?: number; offset?: number; cursor?: string | null; filters?: Record; search?: { query: string; fields: string[]; }; sort?: { field: string; order: 'asc' | 'desc'; }; } /** * Paginated result from list operations */ export interface PaginatedResult { items: T[]; total: number; next: string | null; hasMore: boolean; } /** * EntityClass - Unified interface for ALL entity types * * Both Entity (local storage) and IntegrationEntity (external API) implement this. * All static CRUD methods take ctx as the first parameter for consistency. */ export interface EntityClass { readonly entityName: string; readonly schema?: Record; list(ctx: EntityContext, options?: ListOptions): Promise>; get(ctx: EntityContext, id: string): Promise; create(ctx: EntityContext, data: Partial): Promise; update(ctx: EntityContext, id: string, data: Partial): Promise; delete(ctx: EntityContext, id: string): Promise; } /** * Type-safe entity registry * Maps entity names to their class implementations */ export declare const entityRegistry: Map>; /** * Register an entity class with the registry */ export declare function registerEntity(entityClass: EntityClass): void; /** * Register multiple entity classes at once */ export declare function registerEntities(classes: readonly EntityClass<{ id: string; }>[]): void; export type Doc = { v: number; data: T; }; export interface EntityStatics> { new (env: Env, id: string): T; readonly entityName: string; readonly initialState: S; } /** * EntityBase - Internal base class with instance methods for state management * * Provides CAS-based (Compare-And-Swap) state mutations for optimistic concurrency. * Extended by Entity class which adds static CRUD methods. */ export declare abstract class EntityBase { protected _state: State; protected _version: number; protected readonly stub: EntityDOStub; protected readonly _id: string; protected readonly entityName: string; protected readonly env: Env; constructor(env: Env, id: string); get id(): string; get state(): State; protected key(): string; save(next: State): Promise; protected ensureState(): Promise; mutate(updater: (current: State) => State): Promise; getState(): Promise; patch(p: Partial): Promise; exists(): Promise; delete(): Promise; } type EntityState = T extends new (env: Env, id: string) => Entity ? S : never; type EntityCtor = new (env: Env, id: string) => Entity<{ id: string; }>; type EntityCtorStatic = TCtor & { entityName: string; keyOf(state: EntityState): string; seedData?: ReadonlyArray>; }; /** * Entity - Local storage-backed entity class * * Implements the unified EntityClass interface for local DO-backed storage. * All methods take EntityContext as the first parameter. * * @example * ```typescript * export class TodoEntity extends Entity { * static readonly entityName = 'Todo'; * static readonly initialState: Todo = { id: '', title: '', completed: false }; * } * * // Usage: * const ctx = { env }; * const todos = await TodoEntity.list(ctx, { limit: 50 }); * const todo = await TodoEntity.get(ctx, 'todo-123'); * await TodoEntity.create(ctx, { title: 'New task' }); * ``` */ export declare abstract class Entity extends EntityBase { static readonly entityName: string; static readonly schema?: Record; static keyOf(state: U): string; /** * List entities with server-side filtering, search, sort, and pagination. * All query operations run in SQL via EntityDO.queryEntities(). */ static list(this: EntityCtorStatic, ctx: EntityContext, options?: ListOptions): Promise>>; /** * Get a single entity by ID */ static get(this: EntityCtorStatic, ctx: EntityContext, id: string): Promise | null>; /** * Count entities with optional filters */ static count(this: EntityCtorStatic, ctx: EntityContext, filters?: Record): Promise; /** * Create a new entity */ static create(this: EntityCtorStatic, ctx: EntityContext, data: Partial>): Promise>; /** * Update an existing entity (throws if missing) */ static update(this: EntityCtorStatic, ctx: EntityContext, id: string, data: Partial>): Promise>; /** * Delete an entity */ static delete(this: EntityCtorStatic, ctx: EntityContext, id: string): Promise; /** * Ensure seed data exists (for initial data population) */ static ensureSeed(this: EntityCtorStatic, ctx: EntityContext): Promise; /** * Create multiple entities in a single RPC call to the DO. * Generates UUIDs for items without an id. Returns all created items. */ static createMany(this: EntityCtorStatic, ctx: EntityContext, dataArray: Partial>[]): Promise[]>; /** * Update multiple entities in a single RPC call to the DO. * Each update must include an id. Throws if any items are not found. */ static updateMany(this: EntityCtorStatic, ctx: EntityContext, updates: (Partial> & { id: string; })[]): Promise[]>; /** * Delete multiple entities in a single RPC call to the DO. * Returns the count of items that actually existed and were deleted. */ static deleteMany(this: EntityCtorStatic, ctx: EntityContext, ids: string[]): Promise; protected ensureState(): Promise; } import type { Hono } from 'hono'; /** * Mount entity routes on the Hono app * Provides internal API for entity CRUD from workspace (cross-app operations) */ export declare function entityRoutes(app: Hono<{ Bindings: Env; }>): void; export {};