Skip to content

Commit

Permalink
update Docs 🐐
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Oct 14, 2018
1 parent ddd3afa commit 701f133
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,44 @@ Basically with just 4 actions (set, patch, insert, delete) you can make changes

There are two ways to use vuex-easy-firestore, in 'collection' or 'doc' mode. You can only choose one because this points to the path you sync your vuex module to:

- `firestoreRefType: 'collection'` for a firestore collection
- `firestoreRefType: 'doc'` for a single firestore document
**Collection mode**
- `firestoreRefType: 'collection'`
- `firestorePath` should be a firestore collection
- Use when working with multiple documents, all docs will automatically be retrieved and sync when making changes.
- eg. when a user has multiple "items" like a to-do list

**Doc mode**
- `firestoreRefType: 'doc'`
- `firestorePath` should be a single firestore document
- Use when working with a single doc.
- eg. the "settings" or "config" of a user.

Depending on which mode there are some small changes, but the syntax is mostly the same.

The sync is fully robust and **automatically groups any api calls per 1000 ms**. You don't have to worry about optimising/limiting the api calls, it's all done automatically! (Only one api call per 1000ms will be made for a maximum of 500 changes, if there are more changes queued it will automatically be split over 2 api calls).

## 'collection' mode

With these 4 actions: set, patch, insert and delete, you can edit **single docs** in your vuex module. Any updates made with these actions will keep your firestore in sync!
Opening [the DB channel](setup.html#open-db-channel) will retrieve all docs in your collection and add them to the vuex-module.

Then with these 4 actions: set, patch, insert and delete, you can edit **single docs** of your collection. These actions make sure Firestore will stay in sync.

```js
dispatch('moduleName/set', doc) // will choose to dispatch either `patch` OR `insert` automatically
dispatch('moduleName/set', doc)
// 'set' will choose to dispatch either `patch` OR `insert` automatically

dispatch('moduleName/patch', doc) // doc needs an 'id' prop
dispatch('moduleName/insert', doc)
dispatch('moduleName/delete', id)
```

There are two ways you can give a payload to `set`, `patch` or `insert`:
**Inserting will automatically add an ID**, but for patching you will need to add the `id` to the payload. There are two ways you can do this:

```js
const id = '123'
// Add the `id` as a prop to the item you want to set/update:
dispatch('moduleName/set', {id, name: 'my new name'})
// Or use the `id` as [key] and the item as its value:
// OR use the `id` as [key] and the item as its value:
dispatch('moduleName/set', {[id]: {name: 'my new name'}})

// Please note that only the `name` will be updated, and other fields are left alone!
Expand All @@ -56,6 +69,8 @@ dispatch('moduleName/delete', `${id}.tags.water`)

In the above example you can see that you can delete a sub-property by passing a string and separate sub-props with `.`

For batch actions see [here](#batch-updates-inserts-deletions).

### Auto-generated fields

When working with collections, each document insert or update will automatically receive these fields:
Expand Down Expand Up @@ -87,11 +102,13 @@ dispatch('moduleName/insert', newDoc)

As you can see in the example above, each vuex-easy-firestore module has a getter called `dbRef` with the reference of your `firestorePath`. So when you add `.doc().id` to that reference you will "create" a new ID, just how Firestore would do it. This way you can do whatever you want with the ID before / after the insert.

Please note you can also access the ID (even if you don't manually pass it) in the [hooks](https://mesqueeb.github.io/vuex-easy-firestore/extra-features.html#hooks-before-insert-patch-delete).
Please note you can also access the ID (even if you don't manually pass it) in the [hooks](extra-features.html#hooks-before-insert-patch-delete).

## 'doc' mode

In 'doc' mode all changes will take effect on the single document you have passed in the firestorePath. You will be able to use these actions:
In 'doc' mode all changes will take effect on the single document you have passed in the firestorePath.

You will be able to use these actions:

```js
dispatch('moduleName/set', {name: 'my new name'}) // same as `patch`
Expand Down

0 comments on commit 701f133

Please sign in to comment.