Skip to content

Commit

Permalink
Update for closeDBChannel 🦌
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Oct 14, 2018
1 parent 701f133 commit e15ba07
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,29 @@ Passing `{logging: true}` as second param will enable console.logging on each ap

## Open DB channel

The first thing you need to do is open the connection to Firestore. You can do so by just `dispatch('userData/openDBChannel')`.
The first thing you need to do is open the connection to Firestore. You can do so by simply doing:

```js
dispatch('moduleName/openDBChannel').catch(console.error)
```

### Store the docs in vuex

`openDBChannel` will retrieve the data/docs from Firestore and doesn't require any callback in particular; **the results will automatically be added to your vuex module** as per your configuration.

In the example below, Firestore docs will be saved in vuex module `userData/docs`. You can also leave statePropName empty to save the docs directly to the vuex module's state.

```js
{
moduleName: 'userData',
statePropName: 'docs',
}
```

With vuex-easy-firestore modules you should be using the Vuex [actions](guide.html) that were set up for you, then Firestore will always be in sync!

### Example with user ID:

**Example with user ID:**<br>
If your firestore path contains a user ID, you will need to wait for the user. After Firebase finds a user through `onAuthStateChanged` you can dispatch `openDBChannel` and it will automatically pickup the correct user ID.

```js
Expand All @@ -60,22 +80,29 @@ Firebase.auth().onAuthStateChanged(user => {
if (user) {
// user is logged in
store.dispatch('userData/openDBChannel')
.then(console.log)
.catch(console.error)
}
})
```

**Store the docs in vuex:**<br>
`openDBChannel` doesn't require any callback in particular; the results will be saved in your vuex store at the path you have set.
### Under the hood

Vuex-easy-firestore uses Firestore's [onSnapshot](https://firebase.google.com/docs/firestore/query-data/listen) to retrieve the doc(s) from the server and has added logic to save those doc(s) in a vuex module. If you do not want to open an `onSnapshot` listener you can also use [fetch](guide.html#fetching-docs-with-different-filters) instead.

### Close DB channel

In the example below Firestore docs will be saved in vuex module `userData/docs`. You can also leave statePropName empty to save the docs directly to the vuex module's state.
In most cases you never need to close the connection to Firestore. But if you do want to close it (unsubscribe from Firestore's `onSnapshot`) you can do so like this:

```js
{
moduleName: 'userData',
statePropName: 'docs',
}
dispatch('moduleName/closeDBChannel')
```

To automatically edit your vuex store & have firebase always in sync you just need to use the [actions](guide.html) that were set up for you.
This will close the connection using Firestore's [unsubscribe function](https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener).

Please note, `closeDBChannel` **will not clear out the vuex-module.** This means that you can continue to insert/patch/delete docs and they will still be synced to the server. However, changes on the server side will not be reflected to the app anymore.

You can also close the connection and completely clear out the module; removing all docs from vuex. (without deleting anything on the server, don't worry) In this case do:

```js
dispatch('moduleName/closeDBChannel', {clearModule: true})
```

0 comments on commit e15ba07

Please sign in to comment.