-
Notifications
You must be signed in to change notification settings - Fork 938
Support union types and optional fields with dot separation on UpdateData #5394
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
Changes from 7 commits
7a8431b
83fee2c
45140ad
881ee44
be8a42f
ff1b945
8865d09
7633ff5
160423a
cb69ae1
98bd1ad
831035f
073ab9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@firebase/firestore': minor | ||
--- | ||
|
||
Fixed a bug where `UpdateData` did not recognize optional, dot-separated string fields. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,15 +35,26 @@ export type NestedUpdateFields<T extends Record<string, unknown>> = | |
UnionToIntersection< | ||
{ | ||
// Check that T[K] extends Record to only allow nesting for map values. | ||
[K in keyof T & string]: T[K] extends Record<string, unknown> | ||
? // Recurse into the map and add the prefix in front of each key | ||
// (e.g. Prefix 'bar.' to create: 'bar.baz' and 'bar.qux'. | ||
AddPrefixToKeys<K, UpdateData<T[K]>> | ||
: // TypedUpdateData is always a map of values. | ||
never; | ||
// Union with `undefined` to allow for optional nested fields | ||
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. outdated 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. Good catch, thanks |
||
[K in keyof T & string]: ChildUpdateFields<T[K], K>; | ||
}[keyof T & string] // Also include the generated prefix-string keys. | ||
>; | ||
|
||
/** | ||
* Helper for calculating the nested fields for a given type T. This is needed | ||
* to distribute union types such as `undefined | {...}` (happens for optional | ||
* props) or `{a: A} | {b: B}`. | ||
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#distributive-conditional-types | ||
*/ | ||
export type ChildUpdateFields<T, K extends string> = | ||
// Only allow nesting for map values | ||
T extends Record<string, unknown> | ||
? // Recurse into the map and add the prefix in front of each key | ||
// (e.g. Prefix 'bar.' to create: 'bar.baz' and 'bar.qux'. | ||
AddPrefixToKeys<K, UpdateData<T>> | ||
: // UpdateData is always a map of values. | ||
never; | ||
|
||
/** | ||
* Returns a new map where every key is prefixed with the outer key appended | ||
* to a dot. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1542,6 +1542,106 @@ describe('withConverter() support', () => { | |
}); | ||
}); | ||
|
||
it('supports optional fields', () => { | ||
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 for 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. |
||
interface TestObjectOptional { | ||
optionalStr?: string; | ||
nested?: { | ||
requiredStr: string; | ||
}; | ||
} | ||
|
||
const testConverterOptional = { | ||
toFirestore(testObj: WithFieldValue<TestObjectOptional>) { | ||
return { ...testObj }; | ||
}, | ||
fromFirestore(snapshot: QueryDocumentSnapshot): TestObjectOptional { | ||
const data = snapshot.data(); | ||
return { | ||
optionalStr: data.optionalStr, | ||
nested: data.nested | ||
}; | ||
} | ||
}; | ||
|
||
return withTestDocAndInitialData(initialData, async docRef => { | ||
const testDocRef: DocumentReference<TestObjectOptional> = | ||
docRef.withConverter(testConverterOptional); | ||
|
||
await updateDoc(testDocRef, { | ||
optionalStr: 'foo' | ||
}); | ||
await updateDoc(testDocRef, { | ||
'optionalStr': 'foo' | ||
}); | ||
|
||
await updateDoc(testDocRef, { | ||
nested: { | ||
requiredStr: 'foo' | ||
} | ||
}); | ||
await updateDoc(testDocRef, { | ||
'nested.requiredStr': 'foo' | ||
}); | ||
}); | ||
}); | ||
|
||
it('supports union fields', () => { | ||
interface TestObjectUnion { | ||
optionalStr?: string; | ||
nested?: | ||
| { | ||
requiredStr: string; | ||
} | ||
| { requiredStrObject: string }; | ||
thebrianchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const testConverterUnion = { | ||
toFirestore(testObj: WithFieldValue<TestObjectUnion>) { | ||
return { ...testObj }; | ||
}, | ||
fromFirestore(snapshot: QueryDocumentSnapshot): TestObjectUnion { | ||
const data = snapshot.data(); | ||
return { | ||
optionalStr: data.optionalStr, | ||
nested: data.nested | ||
}; | ||
} | ||
}; | ||
|
||
return withTestDocAndInitialData(initialData, async docRef => { | ||
const testDocRef: DocumentReference<TestObjectUnion> = | ||
docRef.withConverter(testConverterUnion); | ||
|
||
await updateDoc(testDocRef, { | ||
optionalStr: 'foo' | ||
}); | ||
await updateDoc(testDocRef, { | ||
'optionalStr': 'foo' | ||
}); | ||
|
||
await updateDoc(testDocRef, { | ||
nested: { | ||
requiredStr: 'foo' | ||
} | ||
}); | ||
await updateDoc(testDocRef, { | ||
'nested.requiredStr': 'foo' | ||
}); | ||
await updateDoc(testDocRef, { | ||
// @ts-expect-error | ||
'nested.requiredStr': 1 | ||
}); | ||
await updateDoc(testDocRef, { | ||
'nested.requiredStrObject': 'foo' | ||
}); | ||
|
||
await updateDoc(testDocRef, { | ||
// @ts-expect-error | ||
'nested.requiredStringObject': 1 | ||
}); | ||
}); | ||
}); | ||
|
||
it('checks for nonexistent fields', () => { | ||
return withTestDocAndInitialData(initialData, async docRef => { | ||
const testDocRef: DocumentReference<TestObject> = | ||
|
Uh oh!
There was an error while loading. Please reload this page.