If you are like me, you love building simple web apps and websites with VueJS, and if youâve played around with Google Firebase before, Iâm sure you can agree:
- Vuex is great to manage your data in a VueJS app.
- Cloud Firestore is a great database for Google Firebase.
However, I found myself re-writing the connection between Vuex â Firestore for every single app I made. Thatâs when I though:
What if Vuex could be in sync with Firestore automatically? Why couldnât this be a simple feature or plugin, re-usable for any kind of project?
Thatâs why I made Vuex Easy Firestore đ„
â Easy coupling of firestore and vuex: 2-way sync with 0 boilerplate!
What is the current problem?
Letâs immidiately show some code examples to explain what vuex-easy-firestore solves.
Say you have a PokĂ©mon app đ and you have a pokeDex module like so:
const pokeDex = {
state: {
pokemon: {
â001â: {name: âBulbasaurâ, seen: false, caught: false}
}
}
}
const store = {
modules: { pokeDex }
}
That was the easy part, set up a simple Vuex Module.
The problem is now you have to go and write your actual logic:
- actions and mutations for adding pokemon
- the same for updating values
- syncing each time to Firestore
- loading all pokemon from Firestore when the user opens the app
Writing all this boilerplate is not that of a problem, right? But you see, you have to do this every single time! For any app you make, for multiple modules, for different scenarios. Not to start talking about testing for bugs in all kinds of use casesâŠ
Itâs a lot of time you spend on infrastructure. NOT creating your actual app, but just getting the sync right.
How can this be solved?
What if I told you that you get all those features for adding/updating data and having everything synced to Firestore automatically!
That is vuex-easy-firestore
I created it with a simple goal: minimal setup for automatic 2-way sync
As per the example below, just add 4 lines with info how to connect Vuex to Firestore:
const pokeDex = {
firestorePath: âusers/{userId}/pokeDexâ,
firestoreRefType: âcollectionâ,
moduleName: âpokeDexâ,
statePropName: âpokemonâ,
// your state
state: {
pokemon: {
â001â: {name: âBulbasaurâ, seen: false, caught: false}
}
}
}
This is enough to have complete auto sync! Vuex-easy-firestore will load your data from the server at the `firestorePath` and add it to your vuex module in `store[moduleName][statePropName]`.
You will also have auto-generated actions to add/edit/delete data!
Letâs add a PokĂ©mon:
const newPokemon = {name: âSquirtleâ}
dispatch(âpokeDex/insertâ, newPokemon)
Vuex-easy-firestore will automatically (1) attach an id (2) add the item to the state via mutation (3) sync to Firestore.
Besides `insert` you can also use:
ă»`patch` to edit data. Just pass an `id` as well.
ă» `set` will either edit or add, it will check if the item exists or not.
ă»`delete` and pass just the `id`.
And of course other modules can be synced with other Firestore paths just as easy!
Wait, whatâs so great about this all?
Here are a couple of features I worked hard on and hope will be helpful:
- 2-way sync which automatically adds your docs from the server into your Vuex module & syncs changes back
- It doesnât matter how much you `patch` or `insert`, vuex-easy-firestore automatically creates batches to optimise API call amount
- Setup hooks before/after edits
- Filters to define which documents are loaded upon opening the page
- Action to fetch & add other/more docs with the same path (with other filters)
- Automatic `created_at`, `updated_at`, `created_by`, `updated_by` fields
- Set fillables/guard to prevent certain props from being synced
And much moreâŠ
What are you waiting for? Go try it out and let me know what you think!! See the full documentation here.
One more thingâŠ
Finally let me share one more feature, itâs one of my favourites. đ
We all face the problem where we expect a value on a document from the database, but itâs not there. For example when you added a new feature that saves new data, older usersâ documents wonât have that data.
With vuex-easy-firestore you can set default values that get added to documents upon inserting, but also after being retrieved from the server.
defaultValues: {
newProp: ââ
}
Defining these `defaultValues` in the vuex-easy-firestore config will add the property `newProp` on every single doc retrieved from the server. So you have no Vuejs reactive problems, where a value is expected but not there.
Three claps for default values! đ đ đ
Thanks for sticking with me to the end! Let me know what you think about it. I hope I can make at least af few peopleâs life easier with this.
ă»Github âïž
ă»Documentation đ
ă»My Twitter: @mesqueeb
Iâm also open to any suggestions and feature requests! Open an issue on Github or tweet me. đ
Photo by Johannes Groll on Unsplash