Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB versioning support #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,29 @@ IndexedDB wrapper for Vuejs based on Dexie

Vue.use(VueIdb)

const idb = new VueIdb({
const dbName = "database"

const idb = new VueIdb(database, [{
version: 1,
database: 'test',
schemas: [
{ tests: 'id, title, created_at, updated_at' },
{ posts: 'id, owner' }
]
},
{
version: 2,
schemas: [
{ cars: 'id, name, created_at, updated_at'},
],
upgrade: function(tx) {
return tx.tests.toCollection().modify(item => {
// modify your table
})
}
})

new Vue({
el: '#app',
idb: idb,
render: h => h(App)
})
```
# 0.2.0 BUGFIX
BUGFIX on adding schemas on existing DB
UPDATES dependencies #32

# 0.1.11 BUGFIX
ADD xxxReset action
ADD payload to load action

# 0.1.10 BUGFIX
RETURN Promise.reject()
BUGFIX #22

# 0.1.4 Enhancement
BUGFIX on listSelect vuex action
BUGFIX on toggleSelect vuex action

# 0.1.3 Enhancement
Add Dexie DB version in options

# 0.1.2 Enhancement
Add Select action in biglist
28 changes: 25 additions & 3 deletions dev/idb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Vue.use(VueIdb)

import axios from 'axios'

export default new VueIdb({
const dbName = "bigtest"

export default new VueIdb(dbName, [{
version: 1,
database: 'bigtest',
schemas: [
{ tests: 'id, title, created_at, updated_at' },
{ bigs: 'uuid, caption, creation, update' },
Expand All @@ -23,4 +24,25 @@ export default new VueIdb({
all: () => axios.get('/dev/data/bigdata.json')
}
}
})
},
{
version: 2,
schemas: [
{ bigs: 'uuid, caption, creation, update, newField' }
],
options: {

}
},
{
version: 3,
schemas: [
{ tests: 'id, title, created_at, updated_at, name' }
],
upgrade: function (tx) {
return tx.tests.toCollection().modify(item => {
item.name = item.title.split(" ")[0]
})
}
}
])
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-idb",
"version": "0.2.0",
"version": "1.0.0",
"description": "IndexedDB wrapper for Vuejs based on Dexie",
"main": "dist/index.js",
"author": "David Grill <grilldotcom@gmail.com>",
Expand Down
18 changes: 12 additions & 6 deletions src/db-open.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@

export default (db, schemas, version) => {
for(let schema of schemas){
console.log('Create DB schema')
db.version(version ? version : 1).stores(schema)
export default (db, options) => {
for (let migration of options) {
for (let schema of migration.schemas) {
console.log('Create/Migrate DB schema, Version:', migration.version ? migration.version : 1)
db.version(migration.version ? migration.version : 1).stores(schema).upgrade( tx => {
if (typeof migration.upgrade === "function") {
return migration.upgrade(tx)
}
})
}
}

if(!db.isOpen()){
if (!db.isOpen()) {
console.log('DB not open, opening...')
db.open().catch(function(error){
db.open().catch(function (error) {
console.error('A IndexedDB error occured', error)
})
}
Expand Down
30 changes: 17 additions & 13 deletions src/hydrators/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ import { VIDB, deepFreeze } from '../contants'
import listDefaults from './list-defaults'
import bigListDefaults from './biglist-defaults'

export default (options_, db_) => {
export default (migrations_, db_) => {
let defaults = {}, options
for(let schema in options_.options){
options = options_.options[schema]
switch (options.type) {
case VIDB.TYPE.LIST:
defaults[schema] = listDefaults(schema, options, db_)
break
case VIDB.TYPE.BIG_LIST:
defaults[schema] = bigListDefaults(schema, options, db_)
break
default:
defaults[schema] = listDefaults(schema, options, db_)
break
for (let index in migrations_) {
let migration = migrations_[index]

for (let schema in migration.options) {
options = migration.options[schema]
switch (options.type) {
case VIDB.TYPE.LIST:
defaults[schema] = listDefaults(schema, options, db_)
break
case VIDB.TYPE.BIG_LIST:
defaults[schema] = bigListDefaults(schema, options, db_)
break
default:
defaults[schema] = listDefaults(schema, options, db_)
break
}
}
}
deepFreeze(defaults)
Expand Down
21 changes: 13 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,23 @@ export default class VueIdb {
static _plugin
static _options

constructor (options) {
this._initDB(options)
constructor (dbName, migrations) {
this._initDB(dbName, migrations)
}

_initDB (options) {
if(!VueIdb._options) VueIdb._options = options
_initDB (dbName, migrations) {
if(!VueIdb._options) VueIdb._options = migrations
if(VueIdb._db) return
if(!options.schemas){
console.error('VueIdb configuration error: schemas must be set')
migrations.forEach(migration => {
if(!migration.schemas) {
console.error('VueIdb configuration error: schemas must be set in each version')
}
});
if(!dbName) {
console.error('VueIdb configuration error: database name need to be present')
}
VueIdb._db = new Dexie(options.database ? options.database : 'database')
dbOpen(VueIdb._db, options.schemas, options.version ? options.version : 1)
VueIdb._db = new Dexie(dbName)
dbOpen(VueIdb._db, migrations)
}

get plugin () {
Expand Down
30 changes: 17 additions & 13 deletions src/modules/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ import { VIDB, deepFreeze } from '../contants'
import listModule from './list-module'
import bigListModule from './biglist-module'

export default (options_, db_) => {
export default (migrations_, db_) => {
let modules = {}, options
for(let schema in options_.options){
options = options_.options[schema]
switch (options.type) {
case VIDB.TYPE.LIST:
modules[schema] = listModule(schema, options, db_, options_.apis ? options_.apis[schema] : null )
break
case VIDB.TYPE.BIG_LIST:
modules[schema] = bigListModule(schema, options, db_, options_.apis ? options_.apis[schema] : null)
break
default:
modules[schema] = listModule(schema, options, db_, options_.apis ? options_.apis[schema] : null)
break

for (let index in migrations_) {
let migration = migrations_[index]
for (let schema in migration.options) {
options = migration.options[schema]
switch (options.type) {
case VIDB.TYPE.LIST:
modules[schema] = listModule(schema, options, db_, options.apis ? options.apis[schema] : null)
break
case VIDB.TYPE.BIG_LIST:
modules[schema] = bigListModule(schema, options, db_, options.apis ? options.apis[schema] : null)
break
default:
modules[schema] = listModule(schema, options, db_, options.apis ? options.apis[schema] : null)
break
}
}
}
//deepFreeze(modules)
Expand Down
29 changes: 16 additions & 13 deletions src/mutations/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ import { VIDB, deepFreeze } from '../contants'
import listDefaults from './list-defaults'
import bigListDefaults from './biglist-defaults'

export default (options_, db_) => {
export default (migrations_, db_) => {
let defaults = {}, options

for(let schema in options_.options){
options = options_.options[schema]
switch (options.type) {
case VIDB.TYPE.LIST:
defaults = { ...defaults, ...listDefaults(schema, options, db_) }
break
case VIDB.TYPE.BIG_LIST:
defaults = { ...defaults, ...bigListDefaults(schema, options, db_) }
break
default:
defaults = { ...defaults, ...listDefaults(schema, options, db_) }
break
for (let index in migrations_) {
let migration = migrations_[index]
for (let schema in migration.options) {
options = migration.options[schema]
switch (options.type) {
case VIDB.TYPE.LIST:
defaults = { ...defaults, ...listDefaults(schema, options, db_) }
break
case VIDB.TYPE.BIG_LIST:
defaults = { ...defaults, ...bigListDefaults(schema, options, db_) }
break
default:
defaults = { ...defaults, ...listDefaults(schema, options, db_) }
break
}
}
}
deepFreeze(defaults)
Expand Down
2 changes: 1 addition & 1 deletion src/vuex-idb.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function (db, options) {

if(mutation.type === 'DELETE_INDEXED_DB'){
db.delete().then(() => {
dbOpen(db, options.schemas, options.version ? options.version : 1)
dbOpen(db, options)
if(typeof mutation.payload === 'function'){
mutation.payload()
}
Expand Down