Vue Sync documentation
Vue Sync is a library that brings a unified syntax for reading and writing data and allows you to easily work with this data in your app like a "global" store.
You can power Vue Sync with "store plugins" that work in the background to keep this data automatically in sync with whatever service/database or local store you use.
It's like graphQL but much simpler with CRUD-like functions
In most cases you use Vue Sync with two store plugins installed:
When reading data: the remote store will fetch the data; the local store will then add that data for you, so you can easily access and use it in your app.
When writing data: both the local and remote stores will update the data for you. You can choose if you want to show updates immidiately and sync remote store in the background (optimistic UI) or wait for the remote store to complete before its reflected locally.
Will be organised and accessible through modules. A module could be a single object (a "document") or an map of objects (a "collection").
Modules are dynamically created and destroyed when you read/write data! JavaScript will automatically garbage-collect modules that are not referenced anymore.
A "module" is something that points to a certain chunk of data in your database.
A module instance has these four things: (1) functions to read data (2) functions to write data (3) the data itself (4) other meta data.
Every module can be one of two types:
Collections can hold several documents, and documents can have sub-collections with more documents. Like so:
As an example: pokedex/001/moves/leafAttack
pokedex
& moves
are "collections"001
& leafAttack
are "documents"Collections and documents was inspired by Google Firebase's database called Firestore.
If you are still new to this concept you should also read their documentation on the "data model".
You can access modules with a simple collection(id)
& doc(id)
syntax.
With the collection or document instance, you can then access the data
, id
and several functions on of this "module". Eg.
↑
To be decided
As an added layer of protection, any data accesses is readonly! To "mutate" the data, you'll have to use the functions that are provided (which we explain down below).
This is good because any mutation is supposed to go through a function so it can be synced to all your stores.
In the following examples we use this collection instance:
You can instanciate modules and then export them for to use in other files. (more on this here…) ← add link
When you insert a new document without specifying an ID, you can do so by calling insert
directly on the collection:
insert
returns a newly created instance of a document-module. You can then retrieve both the generated id
and the data
from this.
If you want to provide an ID yourself, you can do so by calling insert
on the doc instance of that preset id:
In the case of using a presetId, it will return Promise<void>
.
You can make a simple loop. Vue Sync will automatically optimise so that only a single "batch" API call is made!
You can make a simple loop. Vue Sync will automatically optimise so that only a single "batch" API call is made!
You can delete a single prop or an array of props of a document:
You can also delete nested props by using the "dot" notation:
There are 3 ways to modify documents:
Merge will update your document and deep-merge any nested properties.
In the example above, Bulbasaur kept grass: true
when { poison: true }
was merged onto it.
Assign will update your document and shallow-merge any nested properties. That means that nested objects are replaced by what ever you pass.
In the example above, Bulbasaur lost grass: true
when { poison: true }
was assigned onto it.
Replace will replace the entire object of your document with whatever you pass.
In the example above, Bulbasaur lost all of its data and it was replaced with whatever was passed.
There are two ways to retrieve data from your remote stores. Either of these methods can be used with documents and collections.
When you get data by executing get()
, the data will be fetched from a server by your "remote" store plugin and then added to your module's data by your "local" store plugin.
When you create a module instance of a document, but the data is still on the server, you can retrieve it with get()
like so:
When you set up a "stream" for a document or collection, just like get()
, your the data will be fetched from a server by your "remote" store plugin and then added to your module's data by your "local" store plugin.
Afterwards, if there are any changes to this document, they will automatically be updated in your "local" data while the stream is open.
:::hint
The promise returned from a stream is only resolved if the stream channel was closed. Either by the dev or because of an error (in which case it will reject). As long as the stream is open, the promise returned will not resolve.
:::
You can list all open streams like so:
openStreams
is a [Map](link to mdn) with the payload you passed to start the stream as key, and the "close" function as value.
Here is a full example of opening and closing a stream:
Note if you didn't pass any options when you opened the stream, like so: stream()
then it's "id" in the openStreams map will be undefined
.
You might only want to get or stream some documents, filtered by a field; limited to X docs; or in a certain order.
MORE DOCUMENTATION COMING ON THIS SOON
You can pass an object with options to get()
and stream()
like so:
More information on what kind of options you can pass can be found in the documentation of your store plugin.