Remote / foldkit-remote-drizzle
Collection relations
A many relation says that the foreign key lives on the target:
import { entity, many } from 'foldkit-remote-drizzle'
const Comment = entity('Comment', comments)
const Post = entity('Post', posts, {
relations: {
comments: many(Comment, {
foreignKey: comments.postId,
localKey: posts.id,
}),
},
})A normal collection read batches children across all requested parents instead of issuing one query per parent. The wire value remains normalized refs:
Post:p1.comments = ["Comment:c1", "Comment:c2"]Collection relations may also declare:
orderBy— defaults to target id;where— a static filter, such as excluding soft-deleted rows.
Many-to-many
Use a through table when the relation is many-to-many:
import { entity, manyToMany } from 'foldkit-remote-drizzle'
const Tag = entity('Tag', tags)
const Post = entity('Post', posts, {
relations: {
tags: manyToMany(Tag, {
through: postTags,
localColumn: postTags.postId,
foreignColumn: postTags.tagId,
}),
},
})The Source joins through the link table to the target, drops dangling through rows, and emits target refs.
Principal-scoped collection policy
A collection can add a per-principal SQL predicate:
import { eq } from 'drizzle-orm'
const PostSource = source(Post, {
policies: {
comments: (principal: { readonly id: string }) => eq(comments.visibleTo, principal.id),
},
})That predicate is combined with the relation's static where. Policies are only
valid for collection relations; putting one on a singular relation is rejected
at definition time because it would not filter the target read correctly.
Each row's revision
A table with a revision column (one every write moves, as expect and
revisionTrigger use) can send it with every row it reads, asked for or
not, so a client never lets an older answer over a row it holds at a newer
revision (foldkit-remote's
revisions and read-after tokens).
pollLive sends it with each polled row patch. A field with no number
column is refused where the source is made.
const ItemSource = source(Item, { revision: 'revision' })A table whose revisions start again when a row is created again should send none: the client keeps a deleted row's revision, and holds a row of that revision or a lower one away.
Give the writer the same column and every update it lands moves the revision
on by one in its statement (once, beside revisionTrigger or an expect of
it) and sends it with the answer. A client whose pending increment landed
right after the row it read then takes the answer without reading the row
again.
const ItemWriter = writer(Item, { revision: 'revision' })A collection read whole is bounded
A collection relation read with no window (comments: true) comes back as the
whole list of refs, which has no hasNext to say it was cut short. So the
Source reads at most maxCollection rows of it for one parent (100 unless
given) and refuses a read that finds more, naming the relation, rather than
sending the first 100 as all of them. The statement asks each parent for no
more than one past the bound, with a limit of its own (a correlated in,
on SQLite and Postgres alike), and so does a window for its page.
With an index on the child's parent column followed by the relation's order
(by default the child's id; for a through table, its two columns in that
order), the database stops there, so a parent with a million children costs
what one with 101 does, on a read and on every tick of pollLive. Without
such an index it still reads and sorts every child of the parent to find the
first: index the relation you read. A collection that can grow without limit
is read with a window (next section); one the application knows is larger
but bounded raises the bound:
const PostSource = source(Post, { maxCollection: 500 })The inverse side of a one-to-one reads one child.