Make a space's collections reference their record schemas by matthieusieben · Pull Request #5929 · bluesky-social/atproto
Pull request (OPEN) · PR #5929 Make a space's collections reference their record schemas · @matthieusieben What & why
A space's collections were NSID strings, so nothing tied a space type to the schemas of its records, and SpaceReader / SpaceClient had to accept (and trust) whatever record schema the caller passed. This makes the space definition the source of truth for its collections' schemas.
lex-schema
l.space(key, nsid, collections, options) takes the RecordSchema s of the collections, or l.typedRef() s to them. Space<TNsid, TKey, TRecord extends RecordSchema> ; Space#collections returns the record schemas, resolved on first access (so refs work across circular imports).
Collection types are only inferred from a list using a single form (all schemas, or all refs): TypeScript doesn't infer a union from a mix of R | TypedRefSchema<R> .
lex-builder / lex-document / lex-installer
Codegen emits l.typedRef(() => ns.main) for each collection, and fails if a collection is not a record. The record schema type is inferred (no as any ), which can't create an inference cycle since generated record schemas are typed from their explicit type alias (covered by a fixture in packages/lex where the record references a definition of the space's lexicon).
LexiconSchemaBuilder builds the same references at runtime.
lex install now installs a space's collections as its dependencies, since its generated schema needs them.
lex-space
records() / allRecords() validate every record against the space type's schema of its collection (no more collections option).
list() , listAll() , get() , delete() take a collection NSID ( get(com.example.post.$type, { rkey }) ), typed by SpaceRecordSchema<S, C> .
create() , put() take a record ( create(com.example.post.$build({ text })) ), always validated against the space type's schema before being written. This replaces the previous lookup of the space's schema from a caller-provided schema, which was typed with an unsound as T .
The applyWrites() helper follows the same rules ( op.create(record) , op.update(record, { rkey }) , op.delete(collection, { rkey }) , op.getSchema(collection) ), and is built from the Space . Hand-built operations are still accepted.
SpaceCollectionSchema<S, T> is removed; SpaceCollection<S> , SpaceRecordSchema<S, C> and SpaceRecordInput<S, C> describe the collections, schemas and records of a space type.
All bumps are minor (breaking, pre-v1).
Checklist
pnpm build --force && pnpm verify passes — not run repo-wide. Ran tsc --build (including test projects) and vitest in lex-schema, lex-builder, lex-document, lex-installer, lex-space (including the dev-env integration tests) and lex, plus prettier/eslint on the changed files.
Tests pass, and new behavior is covered by tests
A changeset is included for every package this touches ( packages/lex only gains test fixtures)
Written with the help of an LLM or coding agent: Claude Code. Also reviewed with Roast (no findings).
🤖 Generated with Claude Code Ouvrir sur GitHub →