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
| Piece | Joins | State of its own | Placed as |
|---|---|---|---|
| list | a Remote query and an Entity Selection | Remote holds pages | a value |
| detail | an Entity Selection and a Model ID | Remote holds the row | a value |
| editor | a form, its mutation, and their Entity | open ID, filled flag, request ID | a Bundle |
| remover | a delete mutation with confirmation | pending ID, request ID | a 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
| Fact | Owner |
|---|---|
| Records and save/delete request state | Remote, in its store |
| Record shape, labels, and relations | The Entity |
| Drafts and field validation | The form |
| Open ID, confirmation state, and request ID | The editor or remover slice in your Model |
| Active search, sort, and filters | Your Model, passed as query input |
| Navigation or toast after save/delete | Your update, reading the piece's status |
| Visual rendering | Your 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
pnpm add effect foldkit foldkit-crud foldkit-bundle foldkit-entity foldkit-form foldkit-remote foldkit-surfaceA 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:
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.listdeclares the list without starting requests.Authors.columnslists the selected columns in order withkey,member,label(from schematitle,Form.label, or the key), anddisplay. Passlive: liveQueryfromfoldkit-remote/liveto stream row updates live from apollLiveserver source.Authors.atbinds the list to your Remote domain and Model input selector.AuthorList.activeretains and fetches the current input's connection whileinput(model)returnsOption.some(...).AuthorList.more(model)returnsOption<Model>extended by one page (Option.none()once every row is loaded); returnOption.getOrElse(AuthorList.more(model), () => model)from yourupdate.AuthorList.refresh(model)marks the list stale for refetching (used by retry buttons after a failed read).AuthorList.connection(model)returnsOption<Connection>for passing to optimistic list helpers such asConnectionChange.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:
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.