Possibly Read As Undefined
This section discusses how PossiblyReadAsUndefined
works and why do we need them.
Undefined Read Fieldβ
Firestore has the flexibility to add any field we want, in case like this, the read type of newly added data is undefined
if we try to read it from the old data.
And this is not the only reason a field could be missing, data could be missing for various reason a.k.a low data integrity.
This is what PossiblyReadAsUndefined
for, it allows you to read a particular field as undefined
, here is how you use it.
Why We Need To Read As Undefined?β
To understand why we do you need to read as undefined, we need to first understand how setDoc
typing work.
In setDoc
operation, both Firestore and FirelordJS typing forbid you from skipping any member(field).
If you are using set without merge or merge field, it is recommended to set all members and fill the field we do not need now with default value rather than dropping it.
Despite null
being a popular default value, you might not want to use null
because it messes up query
However those are edge cases so you should be fine for the most of the time.
Now recall thatupdateDoc
cannot update and throw exception if the doc does not exist, so what does all this mean?
This mean that with only set
and updateDoc
operation, it is guarantee that if certain data exists in the database, it has full members.
The type of everything we read is same as what we define in the Meta Type
, assuming that we do not modify the database by other means.
However set merge(setDoc with merge) update the doc if the doc exists and create it if it does not exist, also known as upsert
.
This is a convenient methods but it can be problematic because the data can now exist without full members and we may read certain members as undefined
.
This is where PossiblyReadAsUndefined
is useful, though it is not a perfect solution, it is a good enough solution.
A perfect solution is to forbid set merge and only allow set merge on field that has special type(like how delete field works), will give this deeper thoughts in future.
All Fields Read As Undefined π¦β
The setting allFieldsPossiblyUndefined
had been renamed to allFieldsPossiblyReadAsUndefined
in version 1.5.0
If you want maximum type safe, you can easily union every field read type with one configuration:
Union all read fields with undefined
degrade developer experience because you always need to check for undefined
(albeit safer).
The trade-off of security is ease of use.
Arrayβ
If you assign PossiblyReadAsUndefined
to an array type directly, FirelordJS will replaces them with error message.
Explanation: PossiblyReadAsUndefined[]
simply doesn't make sense.
However assigning to array indirectly and union with array are legal:
Writeβ
What happen to PossiblyReadAsUndefined
type in write operation?
Simple, FirelordJS excludes it.
If a field has PossiblyReadAsUndefined
unions:
If a field has only PossiblyReadAsUndefined
type: