Repository navigation
feat(firestore): type cyclic DocumentReference fields through Collection Keys - #198
Merged
Merged
Conversation
…ion Keys A model can now hold a typed FirestoreSchema.DocumentReference into its own collection, a subcollection of it, or a collection whose model references it back. Collection.Key names the target collection, and FirestoreSchema.DocumentReference accepts either a collection or a key, resolving both to the same Collection.DocumentReference. Closes #197
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A model can hold a typed Document Reference into its own collection, into a subcollection of it, or into a collection whose model references it back. The cyclic field names its target with a Collection Key (ADR 0004):
u.bestFriendis aCollection.DocumentReference<typeof Users>. The key only resolves at the type level:Fields that close no cycle are unchanged, and nothing changes at runtime. Closes #197.
Evidence
typetest/FirestoreSchema.tst.tspass for a self-reference, a parent referencing its own subcollection, a mutual cycle, and one model backing two collections. Each covers thegetDocread type, anupdateDocpatch and a rejected wrong patch. A key that names the wrong collection is rejected, including a sibling collection with the same model.FirestoreSchema.DocumentReference<typeof Users>["Type"]is still exactlyCollection.DocumentReference<typeof Users>. A runtime test writes and reads back a self-reference and a subcollection reference, and both areEqualtoUsers.doc(...)/Posts.doc(...).code.charCodeAt is not a function) with or without this change, so none of their tests run.Merge Danger
Door: two-way
Collection.Keyis new@stability unstableAPI. The changeset bumps every package to 0.4.0.Blast Radius: types
Only types change. Existing
FirestoreSchema.DocumentReference(C)fields keep their exact types.