Creating singleton instance that has stores with name as a parameter
Is there a good example of how to create a singleton in Dexie where we can also pass in names as a parameter? We're having memory issues and from one of the other posts(#873 ), it seems like the answer is to create a single instance but I can't seem to find a good example of one.
Update: this is what I have below (not sure if this is the right way to create a singleton for Dexie. However, we're still getting memory issues (#886 )
class DexieDB {
'use strict';
constructor() {
this.emailDBIndex = {};
}
fetch(email) {
if (this.emailDBIndex[email]) {
return this.emailDBIndex[email];
} else {
let newDB = new Dexie(email);
newDB.version(1).stores({
events: 'example1', 'example2'
});
this.emailDBIndex[email] = newDB;
return this.emailDBIndex[email];
}
}
}
let db = new DexieDB();
export default db;
This seems to work:
import Dexie from 'dexie';
class DexieDBSingleton {
static getDB() {
const db = new Dexie("dbName");
db.version(4).stores({
tableName: '&id, col_1, col_2
});
db.open().catch(function (err) {
console.error('Failed to open db: ' + (err.stack || err));
});
return db;
}
}
const AppDexie = DexieDBSingleton.getDB();
export default AppDexie ;
then import on your class
import AppDexie from './app-dexie';
export default class App{
constructor(props) {
this.db = AppDexie;
}
}
Why not just declare and export your db in a dedicated module db.js? Don't see no reason for doing any extra 'dance' around it. Database will open in background anyway on first query and in case open fails, the querier will get the error in it's returned promise.
import Dexie from 'dexie';
export const db = new Dexie('myDb');
db.version(1).stores({
tableName: 'id, index1, index2'
});
https://dexie.org/docs/Tutorial/React#3-create-a-file-dbjs-or-dbts
nice, that works. seems like the static cast was unnecessary.