CRUD / foldkit-crud

crud

An edit screen looks like a form plus a request. By hand it is much more: load the record when the screen opens and keep it loaded while it is shown; fill the form once when the value arrives without overwriting what the user typed; turn a valid submit into a mutation and remember which request belongs to this screen; show loading, saving, saved, and failed from that request; and notice if the record was deleted while it was open. A list also needs its pages loaded, retained, extended, and offered to relation pickers.

foldkit-crud wires those pieces once from parts your application already has: a foldkit-remote domain, foldkit-entity definitions, and foldkit-form forms. The records and save state stay in Remote's store, the drafts stay in the form, and every screen status derives from them. It is headless; draw tables, details, and forms with foldkit-mixins-crud and foldkit-mixins-form.

See a live list, editor, and remover running in examples/entity and at foldkit-chirp-demo.pages.dev.

List, detail, editor, and remover

PieceJoinsState of its ownPlaced as
lista Remote query and an Entity SelectionRemote holds pagesa value
detailan Entity Selection and a Model IDRemote holds the rowa value
editora form, its mutation, and their Entityopen ID, filled flag, request IDa Bundle
removera delete mutation with confirmationpending ID, request IDa Bundle

Screens are declared explicitly from your queries, forms, and mutations: an editor exists because you pass a form and a mutation, and a list exists because you pass a query your server authorizes.

Where records, drafts, and screens live

FactOwner
Records and save/delete request stateRemote, in its store
Record shape, labels, and relationsThe Entity
Drafts and field validationThe form
Open ID, confirmation state, and request IDThe editor or remover slice in your Model
Active search, sort, and filtersYour Model, passed as query input
Navigation or toast after save/deleteYour update, reading the piece's status
Visual renderingYour views, foldkit-mixins-crud, or -form

How active requirements drive loading and saves

Your Model holds what is on screen, and each piece's active tells Data.wiring (or Data.subscriptions) what Remote needs to fetch and retain. Each piece reads Remote's store back as a page, a value, or a status for your view.

When the user submits or confirms, a Message triggers the piece's helper or the editor's onOut, which calls Data.mutate; Remote tracks the request (busy reads directly from Remote's ledger) and updates the screen's status when it settles.

Every piece must have its active wired. Unwired pieces stay Initial without loading. Wiring a page gathers every piece's active in one call.

Install

sh
pnpm add effect foldkit foldkit-crud foldkit-bundle foldkit-entity foldkit-form foldkit-remote foldkit-surface

A first list

This defines an Author entity, a paginated query, and a Crud.list, then reads the list's current page state from an initial Model:

ts
import { Option, Schema } from 'effect'
import { Crud } from 'foldkit-crud'
import { Entity } from 'foldkit-entity'
import { Query, Remote } from 'foldkit-remote'
import { Surface } from 'foldkit-surface'

const Author = Entity.define('Author', Schema.Struct({ id: Schema.String, name: Schema.String }))
const AuthorsQuery = Query.make('Authors', {
  Input: { search: Schema.String },
  Result: Query.connection(Author),
})
const Authors = Crud.list('Authors', {
  query: AuthorsQuery,
  selection: Entity.select(Author, { id: true, name: true }),
  pageSize: 25,
  choice: { value: row => row.id, label: row => row.name },
})

const Model = Schema.Struct({ remote: Remote.Model, search: Schema.NullOr(Schema.String) })
const App = Surface.application({ Model, Message: Remote.Message })
const Data = Remote.make({ model: App.model.remote, entities: [Author], queries: [AuthorsQuery] })

const AuthorList = Authors.at({
  data: Data,
  input: model => Option.map(Option.fromNullOr(model.search), search => ({ search })),
})

AuthorList.page({ remote: Remote.initial, search: null }) // { _tag: 'Initial' } until active is wired
const subscriptions = Data.subscriptions({ authors: AuthorList.active })

When search is null, AuthorList.page returns { _tag: 'Initial' }. Once search becomes a string (such as "") and AuthorList.active runs in Data.subscriptions, Remote fetches the page and AuthorList.page(model) yields Loading and then Ready with typed items, hasNext, and hasPrevious.

What each member does:

  • Crud.list declares the list without starting requests. Authors.columns lists the selected columns in order with key, member, label (from schema title, Form.label, or the key), and display. Pass live: liveQuery from foldkit-remote/live to stream row updates live from a pollLive server source.
  • Authors.at binds the list to your Remote domain and Model input selector.
  • AuthorList.active retains and fetches the current input's connection while input(model) returns Option.some(...).
  • AuthorList.more(model) returns Option<Model> extended by one page (Option.none() once every row is loaded); return Option.getOrElse(AuthorList.more(model), () => model) from your update.
  • AuthorList.refresh(model) marks the list stale for refetching (used by retry buttons after a failed read).
  • AuthorList.connection(model) returns Option<Connection> for passing to optimistic list helpers such as ConnectionChange.prepend.

AuthorList.row(id) creates a Projection for one row using the list's Selection, and AuthorList.choiceOf(model, id) formats a loaded row as a picker choice.

Wiring a page

Every placed piece has an active. Crud.actives gathers them from the pieces themselves, so a page keeps all of them wired together:

ts
Data.wiring(Crud.actives({ posts: Posts, authors: Authors, editor: PostEditor, pickers }))

It accepts any placed editor, list, detail, and pickers from Crud.options, returning { posts: Posts.active, ... } for Data.wiring in a foldkit-bundle assembly or Data.subscriptions.