Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

javascript - Consume Dexie as module in Vue, how to add functionality or reload module?

I'm using Vue.js (v2.6.10) together with Dexie (v3.0.3) and Webpack. Everything works well, but I don't exactly understand how the export default db in my BackendIdb.js works.

Specifically, at one place in my Vue app I want to call dexie's db.delete() and reload the module in order to get a fresh empty database. But I don't want to repeat the code below. Thus I'd like to reload the module (BackendIdb.js), or add a method to it that does the delete and restore an empty database.

BackendIdb.js:

import Dexie from 'dexie'

const db = new Dexie('myDatabase')
const idb_schema = { .. schema .. }

const db_version = 111

db.version(db_version).stores(idb_schema)
db.open().then(function(){
    console.log('[IDB backend] Open: Success')
    // Success
}).catch(Dexie.UpgradeError, function (e) {
    // Failed with UpgradeError
    console.error ("[IDB backend] Upgrade error: " + e.message)
    console.log("[IDB backend] Dropping database and installing latest version")
    db.delete().then(() => {
        db.version(db_version).stores(idb_schema)
        db.open().catch(function (err) {
            console.error (err.stack || err);
        })
    })
}).catch(Error, function (e) {
    // Any other error derived from standard Error
    console.error ("[IDB backend] Error: " + e.message);
}).catch(function (e) {
    // Other error such as a string was thrown
    console.error (e);
});
    
export default db;

In my Vue app I import it using:

import db from '@/services/BackendIdb'

So, can I add methods/functions to my "db/BackendIdb" module? Or can I reload it at runtime somehow?

question from:https://stackoverflow.com/questions/65939064/consume-dexie-as-module-in-vue-how-to-add-functionality-or-reload-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...