Skip to content

Commit 402288b

Browse files
committed
docs: notes about converter and serialize
Close #1083
1 parent 4fe60b1 commit 402288b

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

Diff for: docs/guide/global-options.md

+33
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,36 @@ globalFirestoreOptions.converter = ...
1919
</FirestoreExample>
2020

2121
Changing these options will affect all calls to `useDocument()`, `useObject()`, ... in your application **and the Options API usage** as well (`$firestoreBind()`, `$rtdbBind()`).
22+
23+
In both scenarios, you need to make sure the returned objects contain their original `id` so other VueFire functionalities can work correctly. The easies way to do this is by reusing the default `serialize`/`converter`:
24+
25+
<FirestoreExample>
26+
27+
```ts
28+
import { globalDatabaseOptions } from 'vuefire'
29+
30+
const defaultSerialize = globalDatabaseOptions.serialize
31+
globalDatabaseOptions.serialize = (snapshot) => {
32+
const data = defaultSerialize(snapshot)
33+
// add anything custom to the returned object
34+
data.metadata = snapshot.metadata
35+
return data
36+
}
37+
```
38+
39+
```ts
40+
import { globalFirestoreOptions } from 'vuefire'
41+
42+
const defaultConverter = globalFirestoreOptions.converter
43+
globalFirestoreOptions.converter = {
44+
toFirestore: defaultConverter.toFirestore,
45+
fromFirestore: (snapshot, options) => {
46+
const data = defaultConverter.fromFirestore(snapshot, options)
47+
// add anything custom to the returned object
48+
data.metadata = snapshot.metadata
49+
return data
50+
},
51+
}
52+
```
53+
54+
</FirestoreExample>

Diff for: src/database/subscribe.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import type { Query, DatabaseReference, DataSnapshot } from 'firebase/database'
3030
*/
3131
export interface _DatabaseRefOptions extends _DataSourceOptions {
3232
/**
33-
* Function to transform snapshots into data. By default it will
33+
* Function to transform snapshots into data. Make sure to reuse the original serializer to add the object id. See
34+
* https://vuefire.vuejs.org/guide/global-options.html
3435
*/
3536
serialize?: DatabaseSnapshotSerializer
3637
}

Diff for: src/firestore/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ export const ops: OperationsType = {
4949
remove: (array, index) => array.splice(index, 1),
5050
}
5151

52-
export interface _UseFirestoreRefOptions extends FirestoreRefOptions {}
52+
export interface _UseFirestoreRefOptions extends FirestoreRefOptions {
53+
/**
54+
* @deprecated: use `.withConverter()` instead
55+
*/
56+
converter?: any
57+
}
5358

5459
/**
5560
* Internal version of `useDocument()` and `useCollection()`.

Diff for: src/firestore/subscribe.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ interface _FirestoreRefOptionsWithDefaults extends FirestoreRefOptions {
7171
maxRefDepth: number
7272

7373
/**
74-
* Default Firestore converter to use with snapshots.
74+
* Default Firestore converter to use with snapshots. Make sure to reuse the original serializer to add the object id.
75+
* See https://vuefire.vuejs.org/guide/global-options.html
7576
*/
7677
converter: FirestoreDataConverter<unknown>
7778
}

0 commit comments

Comments
 (0)