Remote / foldkit-remote-server

remote-server

When screens declare the exact fields they need, serving them usually forces you to choose between writing one custom endpoint per screen or exposing a raw query language that mixes row loading with field permissions.

This package lets you keep one server definition instead. Define a small Source for each entity, query, mutation, and live stream. When a client sends semantic requirements, the server validates the protocol and input, strips any fields the caller may not read before touching your database, de-duplicates shared IDs, and normalizes rows back into patches for foldkit-remote.

foldkit-remote-server runs the Chirp demo and examples/kitchen-sink. Your Sources decide how data is loaded and written; your application authenticates the caller into a principal and serves over Effect RPC, plain JSON over HTTP, Cloudflare Workers, or a MessagePort.

Install

bash
pnpm add foldkit-remote-server

effect is a peer dependency; foldkit-remote comes with the package. foldkit-remote-drizzle compiles Drizzle tables into Sources when your data lives in SQLite or Postgres.

Example: serve one Entity read with field permissions

Define an entity, authorize fields per caller, and mount the Source into Effect RPC handlers:

ts
import { Effect, Schema } from 'effect'
import { Entity, Remote, RemoteRpc } from 'foldkit-remote'
import { RemoteServer } from 'foldkit-remote-server'

const Project = Entity.make(
  'Project',
  Schema.Struct({ id: Schema.String, name: Schema.String, privateNotes: Schema.String }),
)
type Principal = { readonly isAdmin: boolean }
const rows = new Map([['p1', { id: 'p1', name: 'Apollo', privateNotes: 'Internal' }]])

const ProjectSource = RemoteServer.entity<Principal>(Project, {
  authorize: (principal, fields) =>
    fields.filter(field => field !== 'privateNotes' || principal.isAdmin),
  read: ({ ids, fields }) =>
    Effect.succeed(
      ids.flatMap(id => {
        const row = rows.get(id)
        if (row === undefined) return []
        const values = Object.fromEntries(Object.entries(row).filter(([k]) => fields.includes(k)))
        return [{ id, values }]
      }),
    ),
})

const Server = RemoteServer.make({ entities: [ProjectSource] })
const principal: Principal = { isAdmin: false }
const handlers = RemoteServer.handlers(Server, principal)
const layer = RemoteRpc.toLayer(handlers)

When a client requests Project:p1 [id, name, privateNotes] for a non-admin caller:

  1. Project declares id, name, and privateNotes, so all three pass schema validation.
  2. authorize removes privateNotes, leaving ['id', 'name'].
  3. read receives ids: ['p1'] and fields: ['id', 'name'], and returns { id: 'p1', values: { id: 'p1', name: 'Apollo' } }.
  4. The handler returns { id: 'p1', values: { id: 'p1', name: 'Apollo' } } and marks privateNotes as settled (unavailable).

Because privateNotes is settled without a value, the client stops asking for it on subsequent renders. A selection that omits privateNotes reads Ready; one that includes it reads Failed with an Unavailable error until Data.refresh asks again.