Skip to content

Commit 53aaf4f

Browse files
committed
Fix collection doc function
Consider that the snapshot might not exist and be undefined, in that case return null
1 parent 5a2c54b commit 53aaf4f

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

Diff for: package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typesaurus",
3-
"version": "10.0.0-alpha.44",
3+
"version": "10.0.0-alpha.45",
44
"description": "Type-safe ODM for Firestore",
55
"keywords": [
66
"Firebase",

Diff for: src/adapter/admin/core.mjs

+3-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ export class Collection {
8383

8484
doc(id, value, options) {
8585
if (!value && 'id' in id && 'data' in id && typeof id.data === 'function') {
86-
return this.doc(id.id, wrapData(id.data()))
86+
if (id.exists) return this.doc(id.id, wrapData(id.data()))
87+
else return null
8788
} else {
8889
assertEnvironment(options?.as)
8990
return new Doc(this, id, value)
@@ -156,9 +157,7 @@ export class Collection {
156157
...ids.map((id) => this.firebaseDoc(id))
157158
)
158159
return firebaseSnap.map((firebaseSnap) => {
159-
if (!firebaseSnap.exists) {
160-
return null
161-
}
160+
if (!firebaseSnap.exists) return null
162161
const firestoreData = firebaseSnap.data()
163162
const data = firestoreData && wrapData(firestoreData)
164163
return new Doc(this, firebaseSnap.id, data)

Diff for: src/adapter/web/core.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ export class Collection {
102102

103103
doc(id, value, options) {
104104
if (!value && 'id' in id && 'data' in id && typeof id.data === 'function') {
105-
return this.doc(id.id, wrapData(id.data()))
105+
const data = id.data()
106+
if (data) return this.doc(id.id, wrapData(data))
107+
else return null
106108
} else {
107109
assertEnvironment(options?.as)
108110
return new Doc(this, id, value)

Diff for: src/tests/doc.ts

+18
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,24 @@ describe('doc', () => {
138138
expect(user.data.birthday.getFullYear()).toBe(1987)
139139
})
140140

141+
it('returns null if the snapshot does not exist', async () => {
142+
const userId = await db.users.id()
143+
144+
const path = `users/${userId}`
145+
let snapshot
146+
147+
if (typeof window === 'undefined') {
148+
const admin = (await import('firebase-admin')).default
149+
snapshot = await admin.firestore().doc(path).get()
150+
} else {
151+
const { getDoc, doc, getFirestore } = await import('firebase/firestore')
152+
snapshot = await getDoc(doc(getFirestore(), path))
153+
}
154+
155+
const doc = db.users.doc(snapshot)
156+
expect(doc).toBe(null)
157+
})
158+
141159
it('allows to assert environment', async () => {
142160
const server = () =>
143161
db.users.doc(

Diff for: src/types/core.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ export namespace TypesaurusCore {
878878
Props extends DocProps & { environment: Environment }
879879
>(
880880
snapshot: Firebase.Snapshot
881-
): Doc<Def, Props>
881+
): Doc<Def, Props> | null
882882

883883
doc<
884884
Environment extends RuntimeEnvironment,

0 commit comments

Comments
 (0)