Remote / foldkit-remote-drizzle

remote-drizzle

Every endpoint that reads or updates a database table usually ends up hand-writing column lists, WHERE filters, keyset cursors, and optimistic-conflict checks separately for each screen. Whenever a screen adds a field or changes a sort order, that SQL drifts from what the client asked for.

Bind each Drizzle table once as a shared entity instead. Client screens select only the fields they render, foldkit-remote-server removes any fields the caller may not read, and foldkit-remote-drizzle compiles those requirements into parameterized SQLite or Postgres queries and normalizes the rows back into patches for foldkit-remote.

See it running in examples/kitchen-sink and the live Chirp demo. Your application keeps ownership of the DrizzleDatabase connection, authentication, and field authorization rules; this adapter compiles columns, joins, pagination, and guarded writes.

Install

bash
pnpm add foldkit-remote-drizzle drizzle-orm effect

effect and drizzle-orm are peer dependencies, so the adapter reads the tables you define with your own Drizzle; foldkit-entity, foldkit-remote, and foldkit-remote-server come with the package.

SQLite and Postgres

SQLite needs no extra setup because the package inspects Drizzle's table kinds at runtime without loading drizzle-orm/pg-core. For Postgres, call postgres() once at the top of the module that binds your tables:

ts
import { postgres } from 'foldkit-remote-drizzle/pg'

postgres()

Example: bind one Drizzle table to a Source

Define a Drizzle table, bind it once with entity, select the fields a screen needs, and expose a source:

ts
import { pgTable, text, uuid } from 'drizzle-orm/pg-core'
import { entity, source } from 'foldkit-remote-drizzle'
import { postgres } from 'foldkit-remote-drizzle/pg'

postgres()

export const projects = pgTable('projects', {
  id: uuid('id').primaryKey(),
  name: text('name').notNull(),
  status: text('status').notNull(),
})

const Project = entity('Project', projects)
const ProjectSummary = Project.select({ id: true, name: true })
const ProjectSource = source(Project)

When a screen selects ProjectSummary for 00000000-0000-4000-8000-000000000001, ProjectSource compiles a parameterized SQL query over only the needed columns (plus id for cache normalization):

sql
SELECT id, name
FROM projects
WHERE id IN ($1)

and returns the normalized Remote patch:

json
{
  "id": "00000000-0000-4000-8000-000000000001",
  "values": {
    "id": "00000000-0000-4000-8000-000000000001",
    "name": "Apollo"
  }
}

pgTable, entity, select, and source are pure declarations that stay separate from your connection pool. Run your migrations, mount ProjectSource in RemoteServer, and provide the database service when serving requests.