Usage
npm i -D @dsbn/unrest
// main.js
import Vue from 'vue'
import Unrest from '@dsbn/unrest'
Vue.use(Unrest, {
application: 'your-site.com/data/v1/my-application',
token: Promise.resolve(`my.JWT.token`),
vue: Vue
})
DreamRealcode
The // my-component.vue
<script>
export default {
methods: {
setupDB() {
this.$db.create('new-bucket', {
create: 'Role:Developers,Role:ERT',
read: '*', // readable by anyone
write: '@self'
})
this.$db.list() // return all buckets for app-slug
this.$db.update('new-bucket', {new_permissions})
this.$db.remove('new-bucket') // and all documents inside
}
},
data() {
let [items, othertable] = this.$db.sync('new-bucket', 'othertable')
othertable.on('delete', (deletedItem) => doSomething());
return { items, othertable }
}
}
</script>
<template>
<ul>
<!-- .new() return a promise if you need it -->
<li @click="items.new({ template_object })">Create New</li>
<li v-for="item in items">
<!-- patch only the `title` property -->
<input type="text" v-model="item.title" @keyup="item.patch('title')">
<!-- PUT the entire object with .save() -->
<input type="checkbox" v-model="item.done" @change="item.save()">
<button @click="item.remove()"></button>
</li>
</ul>
</template>
Methods
For modifing the bucket structure, requires Admin
permissions. AKA having your Role:
be in the Admin
section of an application.
$db.list() : Promise<Array<String>>
Returns a list of buckets that are attached to this application.
$db.create(name, permissions) : Promise<Object>
Returns a promise for when the given bucket named name
is created with the given permissions
. An example of a permission object is below.
{ create: 'Role:Developers,Role:ERT',
read: '*',
write: '@self' }
The above object allows only users with the "Developer" or "ERT" roles to create new documents, allow anyone to read anyone elses document inside this bucket, but restrict the editors of a document to only the author who created it.
$db.update(name, permissions) : Promise<Object>
It's the exact same as .create
, just for an existing bucket.
$db.remove(name) : Promise<Object>
Does what you'd expect.
$db.sync(bucket...) : Array<Table>
Begins syncing the entire bucket, all documents inside, into the returned Object asyncronously.
let [ people ] = this.$db.sync('people')
...
<li v-for="person in people">{{person.name}}</li>
table.new(obj) : Promise<Item>
Creates a new object, will inject the given object with the ._id
property when saved to the database. Will also resolve the returned promise.
table.on(event, callback<Item>)
Allows you to recieve events as they happen and react to them. create
patch
update
delete
or meta
.
table.synced : Promise
This property is a promise that is resolved when the entire table has been synced to the client. AKA fully "loaded".
item.save()
Does a PUT, saving all properties attached to the object.
item.remove()
Removes the item from the database, doesn't change the local item instance at all.
item.patch(property)
Saves a single updated property of the item. Good for multi-user collaberation without squashing changes.
let item = $db.tables['some-table']['111111111']
item.something = 'new-val'
item.patch('something')