-
Notifications
You must be signed in to change notification settings - Fork 940
Add support for collection(coll) and doc(doc) #3403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,25 +283,43 @@ export function collection( | |
firestore: firestore.FirebaseFirestore, | ||
collectionPath: string | ||
): CollectionReference<firestore.DocumentData>; | ||
export function collection( | ||
reference: firestore.CollectionReference<unknown>, | ||
collectionPath: string | ||
): CollectionReference<firestore.DocumentData>; | ||
export function collection( | ||
reference: firestore.DocumentReference, | ||
collectionPath: string | ||
): CollectionReference<firestore.DocumentData>; | ||
export function collection( | ||
parent: firestore.FirebaseFirestore | firestore.DocumentReference<unknown>, | ||
parent: | ||
| firestore.FirebaseFirestore | ||
| firestore.DocumentReference<unknown> | ||
| firestore.CollectionReference<unknown>, | ||
relativePath: string | ||
): CollectionReference<firestore.DocumentData> { | ||
validateArgType('collection', 'non-empty string', 2, relativePath); | ||
const path = ResourcePath.fromString(relativePath); | ||
if (parent instanceof Firestore) { | ||
validateCollectionPath(path); | ||
return new CollectionReference(parent, path, /* converter= */ null); | ||
const absolutePath = ResourcePath.fromString(relativePath); | ||
validateCollectionPath(absolutePath); | ||
return new CollectionReference(parent, absolutePath, /* converter= */ null); | ||
} else { | ||
const doc = cast(parent, DocumentReference); | ||
const absolutePath = doc._key.path.child(path); | ||
if ( | ||
!(parent instanceof DocumentReference) && | ||
!(parent instanceof CollectionReference) | ||
) { | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
'Expected first argument to collection() to be a CollectionReference, ' + | ||
'a DocumentReference or FirebaseFirestore' | ||
); | ||
} | ||
const absolutePath = ResourcePath.fromString( | ||
`${parent.path}/${relativePath}` | ||
); | ||
validateCollectionPath(absolutePath); | ||
return new CollectionReference( | ||
doc.firestore, | ||
parent.firestore, | ||
absolutePath, | ||
/* converter= */ null | ||
); | ||
|
@@ -340,8 +358,15 @@ export function doc<T>( | |
reference: firestore.CollectionReference<T>, | ||
documentPath?: string | ||
): DocumentReference<T>; | ||
export function doc( | ||
reference: firestore.DocumentReference<unknown>, | ||
documentPath: string | ||
): DocumentReference<firestore.DocumentData>; | ||
export function doc<T>( | ||
parent: firestore.FirebaseFirestore | firestore.CollectionReference<T>, | ||
parent: | ||
| firestore.FirebaseFirestore | ||
| firestore.CollectionReference<T> | ||
| firestore.DocumentReference<unknown>, | ||
relativePath?: string | ||
): DocumentReference { | ||
// We allow omission of 'pathString' but explicitly prohibit passing in both | ||
|
@@ -350,22 +375,34 @@ export function doc<T>( | |
relativePath = AutoId.newId(); | ||
} | ||
validateArgType('doc', 'non-empty string', 2, relativePath); | ||
const path = ResourcePath.fromString(relativePath!); | ||
|
||
if (parent instanceof Firestore) { | ||
validateDocumentPath(path); | ||
const absolutePath = ResourcePath.fromString(relativePath!); | ||
validateDocumentPath(absolutePath); | ||
return new DocumentReference( | ||
parent, | ||
new DocumentKey(path), | ||
new DocumentKey(absolutePath), | ||
/* converter= */ null | ||
); | ||
} else { | ||
const coll = cast(parent, CollectionReference); | ||
const absolutePath = coll._path.child(path); | ||
if ( | ||
!(parent instanceof DocumentReference) && | ||
!(parent instanceof CollectionReference) | ||
) { | ||
throw new FirestoreError( | ||
Code.INVALID_ARGUMENT, | ||
'Expected first argument to collection() to be a CollectionReference, ' + | ||
'a DocumentReference or FirebaseFirestore' | ||
); | ||
} | ||
const absolutePath = ResourcePath.fromString( | ||
`${parent.path}/${relativePath}` | ||
); | ||
validateDocumentPath(absolutePath); | ||
return new DocumentReference( | ||
coll.firestore, | ||
parent.firestore, | ||
new DocumentKey(absolutePath), | ||
coll._converter | ||
parent instanceof CollectionReference ? parent._converter : null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test to make sure the converter is properly passed/omitted? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
); | ||
} | ||
} | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would we hit this error block in TS since the arguments are typed? Or is this for JS users? How could you test that this works properly in TS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be hit if someone passes a DocumentReference-looking type into our SDK. Our external types only validate that type passed in looks like a DocumentReference (has all the same properties) but they cannot enforce that the provided type is an actual instance of DocumentReference.