Get rid of as many mutable states as possible
The sole goal of vue-db is to unleash vue 3 reactivity system full potential to get rid of as many mutable states as possible. When other state management library encrouages maintaining a separate copy of data store, vue-db tries to do the opposite.
It looks like a lot, but it is a 500 line library only depending on vue. Install it via npm install vue-db
, then register to vue app
import * as vdb from 'vue-db'
app.use(vdb);
Instead of using https://vuex.vuejs.org/ to hold the state, vue-db use the vue component instance itself as data store.
vdb.load
or vdb.query
to locate the data source and keep data in syncvdb.walk
to dump the form state out and show validation error backCheckout following examples
code | live | demo |
---|---|---|
counter | counter | vdb.load with $root from current page |
flat form | flat form | vdb.walk to dump form state |
nested form | nested form | vdb.load with $parent allowing multiple form instances |
todo list | todo list | vdb.waitNextTick to add new todo item |
Data from backend need to be loaded asynchronously. Instead of using a mutable https://vuex.vuejs.org/ store to hold the backend data, vue-db provides async data binding to bind vue component data with backend table.
vdb.defineResource
to describe the data to be loaded from server table. table name is just a string, the server can interpret it as anything. vue-db does not specify the rpc protocol, and does not include any server side implementation.vdb.query
or vdb.load
the resource defined, bind to vue component datavdb.defineCommand
to define a function that can be used to call server to update dataaffectedTables
definition to trigger component rerenderCheckout following examples
code | live | demo |
---|---|---|
todo client server | todo client server | vdb.defineResource and vdb.defineCommand to bind with backend data |
If both server and client are written in typescript, the .d.ts
file can be used as type-safe RPC data schema. vue-db allow you to import type
and hand-write a RPC stub with very little code, instead of resorting to full blown code generation solution. Also vdb.defineResource
support declaring nested resource, allow client to query for a object graph in one RPC roundtrip. However, the wire-protocol and server side implementation is excluded from the scope. vue-db is just a tiny library depending only on vue 3, it will not enforce a server/client framework to you.
Checkout following examples
code | live | demo |
---|---|---|
nested resource | nested resource | vdb.defineResource refer other resource |
Fetching initial data for server side rendering is a hard job. It normally requires you to extract out async data dependency into a central place, which makes CSR and SSR code different. vue-db aims to making same component code run in both client and server. You no longer need to lift the data fetching logic to page level, every component can declare its own async data dependency.
render
function to dehydrate the state into data-dehydrated
attribute of rendered html elementbeforeMount
lifecycle hook, read data-dehydrated
and set state into component datavue-db can work with any SSR framework, we recommend vue-fusion
Checkout following examples
code | live | demo |
---|---|---|
static page | static page | renderToString in node with async data provided by vdb.query |
server side render | server side render | async data vdb.query in server side, then hydrated in client side |