Skip to content

Commit 5a73aeb

Browse files
committed
docs: more about realtime data
1 parent df66d6e commit 5a73aeb

9 files changed

+408
-257
lines changed

Diff for: docs/.vitepress/components/FirebaseExample.vue

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
title="Realtime Database example"
88
@focus="selectOnFocus(0, $event)"
99
@click="select(0)"
10-
:disabled="disable === '0'"
10+
:disabled="disable == 0"
1111
>
1212
<rtdb-logo />
1313
</button>
@@ -17,7 +17,7 @@
1717
title="Firestore example"
1818
@focus="selectOnFocus(1, $event)"
1919
@click="select(1)"
20-
:disabled="disable === '1'"
20+
:disabled="disable == 1"
2121
>
2222
<firestore-logo />
2323
</button>
@@ -161,11 +161,15 @@ export default {
161161
fill: var(--vp-code-block-color);
162162
}
163163
164-
.tab-container > nav > button.is-selected:hover {
165-
filter: brightness(1);
166-
background-color: var(--vp-code-block-bg);
164+
.tab-container > nav > button:not([disabled]):hover svg {
165+
fill: var(--vp-code-block-color);
166+
}
167167
168-
/* var(--vp-code-block-bg); */
168+
.tab-container > nav > button svg {
169+
opacity: 0.7;
170+
}
171+
.tab-container > nav > button.is-selected svg {
172+
opacity: 1;
169173
}
170174
171175
.tab-container > nav > button[disabled] {

Diff for: docs/.vitepress/config.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export default defineConfig({
6666
text: 'Suggest changes to this page',
6767
},
6868

69+
outline: [2, 3],
70+
6971
socialLinks: [
7072
{ icon: 'twitter', link: twitter },
7173
{ icon: 'github', link: github },
@@ -120,10 +122,10 @@ export default defineConfig({
120122
text: 'Getting Started',
121123
link: '/guide/getting-started',
122124
},
123-
// {
124-
// text: 'Realtime data',
125-
// link: '/guide/binding-subscriptions',
126-
// },
125+
{
126+
text: 'Realtime Data',
127+
link: '/guide/realtime-data',
128+
},
127129
// {
128130
// text: 'Querying the database',
129131
// link: '/guide/querying',

Diff for: docs/guide/getting-started.md

+31-8
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ npm install vuefire@next firebase
2222

2323
VueFire expects you to use the existing APIs from Firebase as much as possible. It doesn't expose any configs to initialize your app or get the database/firestore instances. You should follow the official Firebase documentation for that. We do have [some recommendations](#TODO) for a Vue project and [a Nuxt module](#TODO) to help you get started.
2424

25-
Most of the time, you should gather collection references in one of your files and export them but to keep examples short, we will always create the database references whenever necessary. We will also consider that we have access to some globals (you usually import them from the file where you initialize your Firebase app):
25+
Most of the time, you should gather collection references in one of your files and export them but **to keep examples short, we will always create the database references whenever necessary** instead of gathering them in one place. We will also consider that we have access to some globals (you usually import them from the file where you initialize your Firebase app):
2626

2727
```js
28-
import { initializeApp FirebaseApp } from 'firebase/app'
28+
import { initializeApp } from 'firebase/app'
2929
import { getFirestore } from 'firebase/firestore'
3030
import { getDatabase } from 'firebase/database'
31-
import { getAnalytics, type Analytics } from 'firebase/analytics'
31+
// ... other firebase imports
3232

3333
export const firebase = initializeApp({
34-
// ...
34+
// your application settings
3535
})
3636
export const database = getFirestore(firebase)
3737
export const firestore = getDataBase(firebase)
38+
// ... other firebase exports
3839
```
3940

4041
:::tip
@@ -45,7 +46,9 @@ Note that we will refer to `database` and `firestore` as `db` in examples where
4546

4647
VueFire exposes a few [composables](https://vuejs.org/guide/reusability/composables.html#composables) to create reactive variables from Firebase references.
4748

48-
You can retrieve a reactive collection or list:
49+
#### Collections/Lists
50+
51+
You can retrieve a reactive collection (Firestore) or list (Realtime Database) with the `useCollection()`/`useList()` composable:
4952

5053
<FirebaseExample>
5154

@@ -85,9 +88,16 @@ const todos = useCollection(collection(db, 'todos'))
8588

8689
</FirebaseExample>
8790

88-
In both scenarios, `todos` will be a `ref()` of an array. You can use it as a readonly array, but it will be automatically updated when the data changes anywhere.
91+
In both scenarios, `todos` will be a `ref()` of an array. Note **this is a readonly array**, but it will be automatically updated when the data changes anywhere.
92+
93+
If you want to change the data, you should use the Firebase API (e.g. `addDoc()`, `updateDoc()`, `push()` etc) to update the data:
94+
95+
- [Firestore Documentation](https://firebase.google.com/docs/firestore/manage-data/add-data)
96+
- [Realtime Database Documentation](https://firebase.google.com/docs/database/web/read-and-write)
8997

90-
You can also retrieve a reactive object/document:
98+
#### Documents/Objects
99+
100+
Similarly, you can retrieve a reactive document (Firestore) or object (Realtime Database) with the `useDocument()`/`useObject()` composable:
91101

92102
<FirebaseExample>
93103

@@ -105,14 +115,23 @@ const settings = useObject(dbRef(db, 'settings', 'some_id'))
105115
import { useDocument } from 'vuefire'
106116
import { doc } from 'firebase/firestore'
107117
108-
const todos = useDocument(doc(db, 'settings', 'some_id'))
118+
const settings = useDocument(doc(db, 'settings', 'some_id'))
109119
</script>
110120
```
111121

112122
</FirebaseExample>
113123

124+
In both scenarios, `settings` becomes a reactive object. Note **this is a readonly object**, but it will be automatically updated when the data changes anywhere.
125+
126+
If you want to change the data, you should use the Firebase API (e.g. `setDoc()`, `updateDoc()`, `set()` etc) to update the data:
127+
128+
- [Firestore Documentation](https://firebase.google.com/docs/firestore/manage-data/add-data)
129+
- [Realtime Database Documentation](https://firebase.google.com/docs/database/web/read-and-write)
130+
114131
### Options API
115132

133+
TODO: complete this section. The composition API is the recommended way to use VueFire at the moment because its API is more stable and it's easier to use with TypeScript.
134+
116135
VueFire can also be used with the Options API, while less flexible, it's still a valid way to use VueFire. First, you need to install the options plugin:
117136

118137
- Install `firestorePlugin` to use _Firestore_
@@ -137,3 +156,7 @@ app.use(firestorePlugin)
137156
```
138157

139158
</FirebaseExample>
159+
160+
### Which API should I use?
161+
162+
The composition API is the recommended way to use VueFire. At the moment its API is more stable and it's easier to use with TypeScript. However, the Options API is still a valid way to use VueFire. The main difference is that the Options API is more verbose and requires you to install the plugin, also being slightly heavier than the composables.

Diff for: docs/guide/binding-subscriptions.md renamed to docs/guide/old__binding-subscriptions.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Binding / Subscribing to changes
1+
# Subscribing to firebase data changes
22

3-
In Vuefire, subscriptions to changes are handled transparently. That's why we always talk about _binding_: you only provide the key of the state where to bind, and the _Source_ (Collection, Query or Document), and Vuefire takes care of the rest!
3+
In VueFire, subscriptions to data changes are handled transparently. That's why we always talk about _binding_: you only provide the _data source_ (Collection, Query or Document), and VueFire takes care of the rest!
44

5-
There are two ways of binding a Reference to the Database with Vuefire:
5+
There are two ways of binding a Reference to the Database with VueFire:
66

77
- Declarative binding with the `firebase`/`firestore` option
88
- Programmatic binding with the injected methods `$rtdbBind`/`$firestoreBind`
99

10-
Once a Reference is bound, Vuefire will keep the local version synchronized with the remote Database. However, this synchronisation **is only one-way**. Do not modify the local variable (e.g. doing `this.user.name = 'John'`), because (a) it will not change the remote Database and (b) it can be overwritten at any time by Vuefire. To [write changes to the Database](./writing-data.md), use the Firebase JS SDK.
10+
Once a Reference is bound, VueFire will keep the local version synchronized with the remote Database. However, this synchronisation **is only one-way**. Do not modify the local variable (e.g. doing `this.user.name = 'John'`), because (a) it will not change the remote Database and (b) it can be overwritten at any time by VueFire. To [write changes to the Database](./writing-data.md), use the Firebase JS SDK.
1111

1212
## Declarative binding
1313

@@ -149,11 +149,11 @@ this.$firestoreBind('documents', documents.where('creator', '==', this.id)).then
149149

150150
</FirebaseExample>
151151

152-
## Using the data bound by Vuefire
152+
## Using the data bound by VueFire
153153

154154
### `.key` / `id`
155155

156-
Any document bound by Vuefire will retain it's _id_ in the Database as a non-enumerable, read-only property. This makes it easier to [write changes](./writing-data.md#updates-to-collection-and-documents) and allows you to copy the data only using the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) or [`Object.assign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign).
156+
Any document bound by VueFire will retain it's _id_ in the Database as a non-enumerable, read-only property. This makes it easier to [write changes](./writing-data.md#updates-to-collection-and-documents) and allows you to copy the data only using the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) or [`Object.assign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign).
157157

158158
<FirebaseExample>
159159

@@ -183,7 +183,7 @@ db.collection('users').doc('ada').id // 'ada'
183183

184184
### Geopoints (Firestore only)
185185

186-
In Firestore you can store [Geopoints](https://firebase.google.com/docs/reference/js/firebase.firestore.GeoPoint). They are retrieved as-is by Vuefire, meaning that you can directly use methods like `isEqual` and access its properties `latitude` and `longitude`.
186+
In Firestore you can store [Geopoints](https://firebase.google.com/docs/reference/js/firebase.firestore.GeoPoint). They are retrieved as-is by VueFire, meaning that you can directly use methods like `isEqual` and access its properties `latitude` and `longitude`.
187187

188188
> Refer to [Plugin installation](./getting-started.md#plugin) to retrieve the `Geopoint` class
189189
@@ -216,7 +216,7 @@ paris.location.longitude // 2.2770206
216216

217217
### Timestamps (Firestore only)
218218

219-
In Firestore you can store [Timestamps](https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp). They are stored as-is by Vuefire, meaning that you can directly use methods like `isEqual`, `toDate` and access its properties `seconds` and `nanoseconds`.
219+
In Firestore you can store [Timestamps](https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp). They are stored as-is by VueFire, meaning that you can directly use methods like `isEqual`, `toDate` and access its properties `seconds` and `nanoseconds`.
220220

221221
> Refer to [Plugin installation](./getting-started.md#plugin) to retrieve the `Timestamp` class
222222
@@ -250,7 +250,7 @@ prise.toDate() // Tue Jul 14 1789
250250

251251
### References (Firestore only)
252252

253-
In Firestore you can store [References](https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference) to other Documents in Documents. Vuefire automatically bind References found in Collections and documents. This also works for nested references (References found in bound References). By default, Vuefire will stop at that level (2 level nesting).
253+
In Firestore you can store [References](https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference) to other Documents in Documents. VueFire automatically bind References found in Collections and documents. This also works for nested references (References found in bound References). By default, VueFire will stop at that level (2 level nesting).
254254

255255
Given some _users_ with _documents_ that are being viewed by other _users_. This could be **users/1**:
256256

@@ -275,7 +275,7 @@ Given some _users_ with _documents_ that are being viewed by other _users_. This
275275
}
276276
```
277277

278-
`sharedWith` is also an array of References, but those references are users. Users also contain references to documents, therefore, if we automatically bind every nested reference, we could end up with an infinite-memory-consumming binding. By default, if we bind `users/1` with Vuefire, this is what we end up having:
278+
`sharedWith` is also an array of References, but those references are users. Users also contain references to documents, therefore, if we automatically bind every nested reference, we could end up with an infinite-memory-consumming binding. By default, if we bind `users/1` with VueFire, this is what we end up having:
279279

280280
```js
281281
{
@@ -303,7 +303,7 @@ Given some _users_ with _documents_ that are being viewed by other _users_. This
303303
}
304304
```
305305

306-
`documents.sharedWith.documents` end up as arrays of strings. Those strings can be passed to `db.doc()` as in `db.doc('documents/robin-book')` to get the actual reference to the document. By being a string instead of a Reference, it is possibe to display a bound document with Vuefire as plain text.
306+
`documents.sharedWith.documents` end up as arrays of strings. Those strings can be passed to `db.doc()` as in `db.doc('documents/robin-book')` to get the actual reference to the document. By being a string instead of a Reference, it is possibe to display a bound document with VueFire as plain text.
307307

308308
It is possible to customize this behaviour by providing a [`maxRefDepth` option](#TODO:) when invoking `$firestoreBind`:
309309

@@ -316,7 +316,7 @@ Read more about [writing References to the Database](./writing-data.md#reference
316316

317317
## Unbinding / Unsubscribing to changes
318318

319-
While Vuefire will automatically unbind any reference bound in a component whenever needed, you may still want to do it on your own to stop displaying updates on a document or collection or because the user logged out and they do not have read-access to a resource anymore.
319+
While VueFire will automatically unbind any reference bound in a component whenever needed, you may still want to do it on your own to stop displaying updates on a document or collection or because the user logged out and they do not have read-access to a resource anymore.
320320

321321
<FirebaseExample>
322322

@@ -334,7 +334,7 @@ this.$firestoreUnbind('documents')
334334

335335
</FirebaseExample>
336336

337-
By default, Vuefire **will reset** the property, you can customize this behaviour by providing a second argument to the `unbind`/`rtdbUnbind`
337+
By default, VueFire **will reset** the property, you can customize this behaviour by providing a second argument to the `unbind`/`rtdbUnbind`
338338

339339
<FirebaseExample>
340340

0 commit comments

Comments
 (0)