@@ -126,16 +126,6 @@ const { data, isError } = useQuery({
126
126
127
127
## Working with Documents
128
128
129
- I prefer to perform all document mutations server-side via an API call,
130
- especially if older versions of your app could be around for a while like with
131
- react-native, because a bug in client-side code could have lasting effects on
132
- the consistency of your data.
133
-
134
- Facilitating client-side writes in a safe way also requires you to write
135
- database rules for your Firestore documents, which can get very complex, so
136
- mutating documents server-side is not only easier to to reason about but also
137
- secure by default.
138
-
139
129
The default immutable document type is ` FsDocument<T> ` . Use this type when you
140
130
write functions that take data without needing to change it.
141
131
@@ -154,9 +144,8 @@ you to pass in properties that exist on the type. Firestore FieldValue is
154
144
allowed to be used to set things like Timestamps.
155
145
156
146
With some more complex nested data, it can happend that the Firestore
157
- ` UpdateData<T> ` type doesn't accept your perfectly ok data. In that case, if you
158
- do not need to use ` FieldValue ` you can use the ` updatePartial ` method as a
159
- workaround.
147
+ ` UpdateData<T> ` type doesn't accept your data. For those situations
148
+ ` updateWithPartial ` is available as an alternative.
160
149
161
150
The original document ` ref ` is also available, in case you need functionality
162
151
that is not covered by this library, or you need to call ` update ` untyped.
@@ -172,28 +161,31 @@ export type FsMutableDocument<T> = {
172
161
};
173
162
```
174
163
175
- In the case of a transaction, the `
164
+ See the
165
+ [ @typed-firestore/server docs] ( https://github.com/0x80/typed-firestore-server#document-types )
166
+ for more info.
176
167
177
- ``` ts
178
- export type FsMutableDocumentInTransaction <T > = {
179
- id: string ;
180
- data: T ;
181
- ref: DocumentReference <T >;
182
- update: (data : UpdateData <T >) => Transaction ;
183
- updatePartial: (data : Partial <T >) => Transaction ;
184
- delete: () => Transaction ;
185
- };
186
- ```
168
+ ## Client-Side Mutations
169
+
170
+ In my projects I prefer to have all mutations happen on the server-side via an
171
+ API call, so you might want to consider that, especially if older versions of
172
+ your app could be around for a while like with mobile apps, because a bug in
173
+ client-side code could have lasting effects on the consistency of your data, and
174
+ time-consuming to have to work around.
175
+
176
+ Facilitating client-side writes in a safe way also requires you to write lots of
177
+ database rules for your Firestore documents, which can get very complex, so
178
+ mutating documents server-side is not only easier to reason about but also more
179
+ secure by default.
187
180
188
181
## Throwing Errors
189
182
190
183
The hooks in this library throw errors, which is not a common practice, but this
191
184
was a deliberate choice.
192
185
193
186
In my experience, runtime exceptions for Firestore documents and collection
194
- queries are very rare. By throwing we can avoid having to handle errors or even
195
- loading state separately in every calling context, and optimize for the
196
- happy-path.
187
+ queries are very rare. By throwing we can avoid having to handle errors, and
188
+ optimize for the happy-path.
197
189
198
190
The most common errors are:
199
191
@@ -204,16 +196,16 @@ The most common errors are:
204
196
I think all of these are likely to be caught during development and testing and
205
197
should not occur in production code.
206
198
207
- In some cases it is a valid state that the document might not exist, so for
208
- those situations we have the ` *Maybe ` variants like ` useDocumentMaybe() ` . These
209
- functions do not throw but simply return undefined if the document does not
199
+ In some cases it is expected that the document might not exist, so for those
200
+ situations we have the ` *Maybe ` variants like ` useDocumentMaybe() ` . These
201
+ functions do not throw, and simply return undefined if the document does not
210
202
exist.
211
203
212
- This approach of not handling errors in the calling context, also has a nice
213
- benefit, because now the loading state is directly tied to the data
214
- availability. If you wait for the loading state from ` useDocument() ` to be true,
215
- the Typescript compiler is also guaranteed that the data is defined .
204
+ This approach also has a nice benefit, because now the loading state is directly
205
+ tied to the data availability. If you wait for the loading state from
206
+ ` useDocument() ` to become true, the Typescript compiler is also guaranteed that
207
+ the data exists .
216
208
217
- In that sense, you do not need the loading state at all.It would be sufficient
218
- to just wait for the data to become defined, but for readability I would still
219
- recommend using the loading state variable.
209
+ In that sense, you do not even need the loading state at all. It would be
210
+ sufficient to simply wait for the data to become defined, but for code
211
+ readability I would still recommend using the loading state variable.
0 commit comments