Learn / From declarations to the screen

Declare a field and a query

An Entity gives each field a schema and an owner. Expr and Order build query predicates and sort keys as plain data structures you can inspect:

ts
import { Schema } from 'effect'
import { Entity, Expr, Order, Query as EntityQuery } from 'foldkit-entity'
import { Query } from 'foldkit-remote'

const Product = Entity.define(
  'Product',
  Schema.Struct({
    id: Schema.String,
    name: Schema.String,
    price: Schema.Number,
    status: Schema.Literals(['draft', 'published', 'archived']),
    revision: Schema.Number,
  }),
)

const PublishedByPrice = Query.define('PublishedByPrice', {}, () =>
  Query.from(Product).pipe(
    Query.where(Expr.eq(Product.fields.status, 'published')),
    Query.orderBy(Order.asc(Product.fields.price), Order.asc(Product.fields.id)),
  ),
)

const deps = EntityQuery.dependencies(PublishedByPrice.body)
console.log({
  predicate: deps.predicate.map(field => field.key),
  order: deps.order.map(field => field.key),
})

Running that prints:

{ predicate: [ 'status' ], order: [ 'price', 'id' ] }

PublishedByPrice builds the query body once without running SQL or touching a cache. EntityQuery.dependencies separates the predicate field (status) from the ordering fields (price, id), letting the Drizzle compiler generate SQL on the server and foldkit-remote evaluate row changes on the client.

See Expressions for the full expression language and One domain declaration for relations and derived fields.