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:
- Fields and schemas (
Entity.define): intrinsic values backed by an EffectSchema.Structthat validates single-field values. - Relations (
Entity.relate): typedoneandmanynavigation edges between Entities, independent of foreign keys or join tables. - Derived members (
Entity.derived): read-only counts (Derived.count), viewer existence flags (Derived.exists), or server-supplied values (Derived.make). - Context-set members (
Entity.sets): values filled on write fromExpr.viewerorExpr.now. - Invariants and states (
Entity.invariant,Entity.refine): rules across fields or acrossbefore/afterrows, plus named row states. - Operations (
Entity.select,Query,Write): explicit projections of what a view reads, which rows a query returns, and what a mutation writes.
| Concern | Package that owns it |
|---|---|
Domain structure, Expr, Query, Write | foldkit-entity |
| SQL compilation and database storage | foldkit-remote-drizzle |
| Normalized client cache and subscriptions | foldkit-remote |
| Form drafts and field validation | foldkit-form |
| Who may read or change each row | foldkit-guard |
| Application state and Messages | Your 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
pnpm add effect foldkit-entityA 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:
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.