Remote / foldkit-remote-drizzle
Binding a foldkit-entity domain
entity(name, table, …) makes the table the declaration. When the domain is
declared with foldkit-entity, the Entity already says
what each relation is, and bind says only how the database stores it:
Which to reach for. entity is the smaller of the two and exists so a field
is not declared twice — once in the table and once in the domain. Reach for it
when the database is the whole truth about a domain. Reach for bind when the
domain is its own thing: relations and derived members it declares rather than
infers, metadata other packages attach, and a query body. Only a
foldkit-entity Entity has the addressable fields an Expr is built from, so
Query.define cannot be written over an entity(…) binding — the same boundary
foldkit-remote's own Entity.make has, for the same reason.
import { Schema } from 'effect'
import { Derived, Entity, Relation } from 'foldkit-entity'
import { bind, source } from 'foldkit-remote-drizzle'
const User = Entity.define('User', Schema.Struct({ id: Schema.String, name: Schema.String }))
const Post = Entity.define('Post', Schema.Struct({ id: Schema.String, title: Schema.String })).pipe(
Entity.derived({ fanCount: Derived.make(Schema.Number) }),
)
const Blog = Entity.relate(
{ User, Post },
{ Post: { author: Relation.one(User), fans: Relation.many(User) }, User: {} },
)
const Db = bind(Blog, {
User: { table: users },
Post: {
table: posts,
relations: {
author: { field: posts.authorId },
fans: { through: postFans, localColumn: postFans.postId, foreignColumn: postFans.userId },
},
derived: { fanCount: { relation: 'fans' } },
},
})
source(Db.Post) // an ordinary binding| Entity member | Storage |
|---|---|
| Field | same-named column, or fields: { name: posts.title } |
one relation | { field }: foreign key on the owner's table |
many relation | { foreignKey, localKey? } on target, or { through, localColumn, foreignColumn } |
Derived.count(Other.relations.pointer) | counted on the pointing table by { field } |
Derived.exists(Other.relations.pointer, { viewer }) | checked on the pointing table by both { field } columns |
Derived.make | { relation, where? } for a count, or { supplied: true } |
For a many relation, localKey defaults to the owner's id. orderBy and where go on a many storage as they do on many(…).
A Derived.count says what it counts, so it takes no entry: bind counts the
rows of the pointing Entity's table by the column its one relation is stored
on ({ field }), under that table's visible rule, and refuses an entry for it.
The pointing Entity must be bound in the same call.
A Derived.exists takes no entry either. Its value depends on who reads it,
which the server the source is mounted in says, once, for every source, query
and write it serves:
import { Option } from 'effect'
import { RemoteServer } from 'foldkit-remote-server'
RemoteServer.make({
entities: [source(Db.Post)],
viewer: (principal: Option.Option<{ readonly id: string }>) =>
Option.map(principal, user => ({ entity: 'User', id: user.id })),
})For each row read it finds the least id among the pointing rows whose viewer
relation's column holds that id, under the pointing table's visible rule, in
one statement per read, as a count does. A visitor, or a reader of an Entity
the viewer relation does not point at, reads none. RemoteServer.make
refuses a source with one when it has no viewer, naming the binding; a read
of the member made outside a server (calling source(…).read directly) fails,
since nothing says who reads. A source listing it as shared throws: a
shared field goes to every live subscriber as written, and this one is each
reader's own.
A query body may read the same context: Expr.viewer(User) is that viewer's
id, and Expr.now the server's clock (Effect's Clock), each bound as a
parameter of the request. A write's precondition and its Entity's invariants
may not read them yet: writer refuses one that does where it is built.
A Derived.make member is a count of a many relation, { relation, where? },
which is how a join table with no Entity is counted. One it cannot compute is { supplied: true }: bind registers the
member and reads nothing for it, and you wrap the binding's source to answer
for that field yourself, as foldkit-cms-drizzle does for an entry's state.
A one may also be read from the other side, where the foreign key is a column
of the target's table, as a member's profile points at its member:
Member: { table: members, relations: { profile: { foreignKey: profiles.memberId } } },It reads as the one ref, or null when no row points back, so the relation must
be { optional: true }. One-to-one only holds if profiles.memberId is unique,
so bind refuses a column that is neither .unique() nor the primary key. A
unique index declared on the table is not visible on the column; vouch for it
with assumeUnique: true. Such a relation cannot be windowed or counted.
Target and cardinality are never repeated, so they cannot disagree with the
Entity. bind takes the whole Entity.relate result in one step, which lets
two bindings point at each other; entity(…, { relations }) cannot, because a
relation there needs its target binding to exist first.
Each result is the same EntityBinding entity returns, and the same Remote
descriptor Entity.from gives the client, so register Db.Post (or
Entity.from(Blog.Post)) in Remote.define and pass Db.Post to source
and query.
The types and bind itself reject: a field with no column, a relation or
derived member with no storage, a one stored as a many or the reverse, a
count over a one relation, a required one over a nullable column
(declare the relation { optional: true }), a one read from the target's
table whose foreign key is not unique, and a member named __proto__ (Drizzle
builds each row it reads by assignment, which would make the value the row's
prototype). Every other name is looked up as the storage's own key, so a field
named constructor finds its column, not Object.
bind also refuses a column that plainly cannot hold its field: text under a
number field (a Postgres numeric reads as text), a number under a flag, and a
nullable column under a field whose schema admits neither null nor
undefined. It compares only what both sides state plainly. A schema that
transforms (Schema.NumberFromString), a mixed union, a struct, and a custom,
JSON or date column all pass unchecked, so the check never refuses a mapping
that could work.