Entity / foldkit-entity

entity

In most TypeScript apps, the same Product or Post gets written four times: once for SQL tables and queries, once for server input validation, once for form fields and error messages, and once for client filters and optimistic updates. Every copy can drift from the others, so the browser lets someone click a button that the server rejects.

foldkit-entity lets you declare your domain once as typed data — fields, relations, derived counts, invariants, queries, and writes — without importing a database driver or a UI framework. Other packages interpret that one declaration: foldkit-remote-drizzle compiles queries, writes, and invariants to SQL; foldkit-remote queries and updates normalized rows in the browser; foldkit-form derives form controls and validation; and foldkit-guard attaches row-level permissions.

See a full app built from one domain declaration running live at foldkit-chirp-demo.pages.dev and in examples/entity, or read One domain declaration for a step-by-step walkthrough.

What your domain module holds and who runs it

A domain declaration is pure data split into parts with clear owners:

ConcernPackage that owns it
Domain structure, Expr, Query, Writefoldkit-entity
SQL compilation and database storagefoldkit-remote-drizzle
Normalized client cache and subscriptionsfoldkit-remote
Form drafts and field validationfoldkit-form
Who may read or change each rowfoldkit-guard
Application state and MessagesYour Foldkit Model and update

Declarations are checked where they are written: mismatches fail at compile time when TypeScript can see them and throw at startup when it cannot, and interpreters refuse unsupported query operations by name.

Install

sh
pnpm add effect foldkit-entity

A first query

This declares a Task entity, filters for unfinished tasks with a Query, runs that query in memory with evaluate, and declares a minimal TaskTitle selection for a view:

ts
import { Schema } from 'effect'
import { Entity, Expr, Query, evaluate, type Selected } from 'foldkit-entity'

const Task = Entity.define(
  'Task',
  Schema.Struct({ id: Schema.String, title: Schema.String, done: Schema.Boolean }),
)

const OpenTasks = Query.from(Task).pipe(Query.where(Expr.eq(Task.fields.done, false)))
const rows = [
  { id: 't1', title: 'Write schema once', done: true },
  { id: 't2', title: 'Run query in memory', done: false },
]

evaluate(OpenTasks, {}, rows)
// [{ id: 't2', title: 'Run query in memory', done: false }]

const TaskTitle = Entity.select(Task, { id: true, title: true })
type TaskTitle = Selected<typeof TaskTitle> // { id: string; title: string }

evaluate runs OpenTasks over in-memory rows and returns only t2. The same OpenTasks value compiles to WHERE done = false in foldkit-remote-drizzle and filters cached rows in foldkit-remote. TaskTitle narrows reads to id and title and carries a schema for decoding responses; operation inputs are declared separately with Entity.input so readable fields stay distinct from writable ones.