- Added VueIdb plugins: $db.mytable.add({myelem:'value'}) to insert a record, and $db.mytable.toArray() to receive the array.

This commit is contained in:
Paolo Arena
2019-01-05 23:34:29 +01:00
parent fd2321cd20
commit 46609baef8
7 changed files with 87 additions and 43 deletions

View File

@@ -59,7 +59,7 @@ module.exports = function (ctx) {
store: 'src/store/index.ts' store: 'src/store/index.ts'
}, },
// app plugins (/src/plugins) // app plugins (/src/plugins)
plugins: ['i18n', 'axios', 'vee-validate', 'myconfig', 'local-storage', 'error-handler', 'indexdb'], plugins: ['i18n', 'axios', 'vee-validate', 'myconfig', 'local-storage', 'error-handler', 'indexdb', 'vue-idb'],
css: [ css: [
'app.styl' 'app.styl'
], ],

View File

@@ -49,7 +49,7 @@
UserStore.actions.autologin() UserStore.actions.autologin()
.then((loginEseguito) => { .then((loginEseguito) => {
if (loginEseguito) { if (loginEseguito) {
this.$router.replace('/') // this.$router.replace('/')
} }
}) })
} }

View File

@@ -1,62 +1,67 @@
import Vue from 'vue' import Vue from 'vue'
import { Component } from 'vue-property-decorator' import { Component } from 'vue-property-decorator'
import { GlobalStore } from '@store'
import VueIdb from 'vue-idb'
Vue.use(VueIdb)
require('./category.scss') require('./category.scss')
@Component({}) @Component({})
export default class Category extends Vue { export default class Category extends Vue {
idb = null filter: boolean = false
category: string = '' category: string = ''
$t: any categories_arr: any[] = null
created() { created() {
this.createdb() this.loadCat()
this.caricaCat()
} }
createdb() { async loadCat() {
// Inserisci la Categoria nel DB await this.$db.categories.toArray().then(ris => this.categories_arr = ris)
this.idb = new VueIdb({
version: 1, this.updatetable()
database: 'test', }
schemas: [
{ categories: '++id, sub_categ_id, descr_it' } async insertCategory() {
]
let myid = 0
const mycat = this.category
// Add to Indexdb
await this.$db.categories.add(
{ descr_it: mycat }
).then(ris => {
myid = ris
}) })
// created_at: new Date(),
// Add into the memory
this.categories_arr.push({ descr_it: mycat, id: myid })
this.updatetable()
} }
updatetable() {
caricaCat() { this.filterCategories()
let mythis = this
this.idb.open().then(function () {
return mythis.idb.categories }
async filterCategories() {
if (this.filter) {
// #Todo If need to filter the output database ...
this.$db.categories
.where('descr_it').notEqual('nonlovedi')
.toArray() .toArray()
.then((response) => {
}).then(function () { Promise.all(response.map(key => key))
.then((myarr) => {
console.log('FINE LOAD') this.categories_arr = [...myarr]
return this.categories_arr
}) })
})
} else {
return this.categories_arr
} }
insertCategory(): any {
let mythis = this
this.idb.open().then(function () {
console.log('Inserisci Cat: ', mythis.category)
return mythis.idb.categories.add({ descr_it: mythis.category })
}).then(function () {
console.log('FINE')
})
} }
} }

View File

@@ -7,6 +7,10 @@
:after="[{icon: 'arrow_forward', content: true, handler () {}}]" :after="[{icon: 'arrow_forward', content: true, handler () {}}]"
v-on:keyup.enter="insertCategory"/> v-on:keyup.enter="insertCategory"/>
<ul>
Lista:
<li v-for="item in categories_arr" :key="item.id"> {{ item.descr_it }} </li>
</ul>
</div> </div>
</q-page> </q-page>

View File

@@ -8,7 +8,7 @@
{{replaceUnderlineToSpace(index)}} <div class="menu_freccina"><i aria-hidden="true" class="v-icon material-icons theme--light">keyboard_arrow_down</i></div> {{replaceUnderlineToSpace(index)}} <div class="menu_freccina"><i aria-hidden="true" class="v-icon material-icons theme--light">keyboard_arrow_down</i></div>
</div> </div>
<template v-for="child in parent.routes"> <template v-for="child in parent.routes">
<q-slide-transition duration="200"> <q-slide-transition :duration=200>
<div v-show="parent.show"> <div v-show="parent.show">
<q-item link :to="child.route" exact <q-item link :to="child.route" exact
class="item item-link drawer-closer cursor-pointer"> class="item item-link drawer-closer cursor-pointer">

28
src/plugins/vue-idb.js Normal file
View File

@@ -0,0 +1,28 @@
import Vue from 'vue'
import VueIdb from 'vue-idb'
export default ({ Vue }) => {
Vue.use(VueIdb)
// Insert here the database for IndexDB
new VueIdb({
version: 1,
database: 'test',
schemas: [
{ categories: '++id, sub_categ_id, descr_it' }
]
})
}
/*
export default new VueIdb({
version: 1,
database: 'test',
schemas: [
{ categories: '++id, sub_categ_id, descr_it' }
]
})
*/

7
src/typings/libs/vue-idb.d.ts vendored Normal file
View File

@@ -0,0 +1,7 @@
import { VueIdb } from 'vue-idb'
declare module 'vue/types/vue' {
interface Vue {
$db: VueIdb
}
}