Skip to content

Commit 9620877

Browse files
committed
docs: global composables
1 parent 1041e53 commit 9620877

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

Diff for: docs/guide/firebase-composables.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Firebase Services
2+
3+
You can access all of the Firebase services within components with the different composables provided by VueFire:
4+
5+
```vue
6+
<script setup>
7+
import {
8+
useFirebaseApp,
9+
useAuth,
10+
useDatabase,
11+
useFirestore,
12+
useStorage,
13+
} from 'vuefire'
14+
15+
const firebaseApp = useFirebaseApp()
16+
const auth = useAuth()
17+
const database = useDatabase()
18+
const firestore = useFirestore()
19+
const storage = useStorage()
20+
</script>
21+
```
22+
23+
As [all composables](https://vuejs.org/guide/reusability/composables.html), these must be called within the _setup_ of a component. However, you can call these specific Firebase Services composables anywhere in your application as long as you pass the **Firebase App name as the parameter**.
24+
25+
```ts
26+
27+
```
28+
29+
::: tip
30+
The Firebase Name parameter is only needed when using the composable outside of _setup_ and one of these condition are met:
31+
32+
- You are doing SSR
33+
- You have multiple Firebase Apps
34+
35+
**Omit the name in all other scenarios**, it's just not needed.
36+
:::

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

+32-7
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,49 @@ VueFire requires Firebase JS SDK >= 9 but **is compatible with Vue 2 and Vue 3**
2020

2121
## Usage
2222

23-
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](./nuxt.md) to help you get started.
23+
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 [a Nuxt module](./nuxt.md) that makes it even easier to use VueFire with Nuxt.
2424

2525
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

27+
<FirebaseExample>
28+
2729
```js
2830
import { initializeApp } from 'firebase/app'
2931
import { getFirestore } from 'firebase/firestore'
30-
import { getDatabase } from 'firebase/database'
32+
import { getDatabase, dbRef } from 'firebase/database'
33+
// ... other firebase imports
34+
35+
export const firebaseApp = initializeApp({
36+
// your application settings
37+
})
38+
39+
// used for the databas refs
40+
const db = getDatabase(firebase)
41+
42+
// here we can export reusable database references
43+
export const todosRef = dbRef(db, 'todos')
44+
```
45+
46+
```js
47+
import { initializeApp } from 'firebase/app'
48+
import { getFirestore, collection } from 'firebase/firestore'
3149
// ... other firebase imports
3250

3351
export const firebaseApp = initializeApp({
3452
// your application settings
3553
})
36-
export const database = getDatabase(firebase)
37-
export const firestore = getFirestore(firebase)
38-
// ... other firebase exports
54+
55+
// used for the firestore refs
56+
const db = getFirestore(firebase)
57+
58+
// here we can export reusable database references
59+
export const todosRef = collection(db, 'todos')
3960
```
4061

62+
</FirebaseExample>
63+
64+
Note exporting Database and Firestore isn't necessary as you can always accessing Firebase services within your components with [`useFirebaseApp()` and other composables](./firebase-composables.md).
65+
4166
::: tip
4267
Note that we will refer to `database` and `firestore` as `db` in examples where only one of them is used.
4368
:::
@@ -67,7 +92,7 @@ app
6792
app.mount('#app')
6893
```
6994

70-
This will give you access to some convenient composables like `useFirebaseApp()`, `useFirestore()` and `useDatabase()` in your components:
95+
This will give you access to some [convenient composables](./firebase-composables.md) like `useFirebaseApp()`, `useFirestore()` and `useDatabase()` in your components:
7196

7297
```vue
7398
<script setup>
@@ -171,7 +196,7 @@ If you want to change the data, you should use the Firebase API (e.g. `setDoc()`
171196

172197
### Options API
173198

174-
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.
199+
The composition API is the recommended way to use VueFire because its API is more flexible and it's easier to use with TypeScript.
175200

176201
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:
177202

Diff for: docs/guide/realtime-data.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ Note this is only a type annotation, it does not perform any runtime validation.
300300
The recommended Firebase approach is to use the `withConverter()` for Firestore:
301301

302302
::: info
303-
`.withConverter()` is a Firestore feature that doesn't have an equivalent in Database but you can use VueFire's [`serialize()` option](#TODO:global options when installing plugin) instead.
303+
`.withConverter()` is a Firestore feature that doesn't have an equivalent in Database but you can use VueFire's [`serialize()` option](./global-options.md#firestore-and-database-global-options) instead.
304304
:::
305305

306306
```ts

0 commit comments

Comments
 (0)