Im writing a program that will let users convert their tiled game scenes ( json ) to a dexiedb database. The intention is to convert the json files at the beginning of the game. once converted the game files don’t need to be converted again.
however i am having trouble utilizing dexieDB, when i restart my application and reopen my database, its as if my created table(s) is sort of missing? I can no longer reference my tables using db.tableName format. I can however see the tables I previously created in the storeNames and _alltables properties in the db object.
here is how my class is utilized by client:
const spatialdb = new SpatialDB() // create dexieDB instance
await spatialdb.init() // init the database
await spatialdb.convertMap(filepath) // converts the json if needed
and here is a sample code of my library:
the way it works is at the beginning i create a table called converted_maps
to store the names of any jsons that have been successfully converted.
export default class SpatialDB {
static instance;
constructor() { // create a singleton
if (!SpatialDB.instance) {
SpatialDB.instance = this;
this.db = new Dexie(DB_NAME);
}
return SpatialDB.instance;
}
async init() {
//create initial schema for database
if (this.db.verno < 1) {
const databaseExists = await Dexie.exists(DB_NAME);
if (!databaseExists) {
await this.db.version(1).stores({ converted_maps: "map_name" });
} else {
await this.db.open();
debugger;
}
}
}
async convertMap(path) {
const mapName = this._getPathFileName(path);
const isConverted = await this.db.converted_maps
.where("map_name")
.equals(mapName)
.toArray();
debugger;
const needToConvert = Array.isArray(isConverted) && isConverted.length == 0;
if (needToConvert) {
// convert the map then once done add this map to table of converted maps
// await this._convertTiledToMap(path);
await this.db.converted_maps.add({ map_name: mapName });
debugger;
}
}
/**
*
* @param {string} path
* @return returns the filename of the json based on the path given
*/
_getPathFileName(path) {
const parts = path.split("/");
const tableName = parts[parts.length - 1].replace(".json", "");
return tableName;
}
/**
*
* @param {*} mapName
* @param {*} schema
* @description creates a new schema in the database for the given map
*/
async _addSchema(mapName, schema) {
const currentVersion = this.db.verno;
if (currentVersion >= 1) {
// Close the database before modifying the schema
try {
if (this.db.isOpen()) {
await this.db.close();
}
} catch (e) {
debugger;
}
// Collect existing tables
const existingSchema = await this.db.tables.reduce((acc, table) => {
acc[table.name] = table.schema.primKey.src;
return acc;
}, {});
debugger;
// Add the new table schema
existingSchema[mapName] = schema;
// Increment the version and update schema
await this.db.version(currentVersion + 1).stores(existingSchema);
debugger;
// Re-open the database
await this.db.open();
debugger;
}
}
but after relaunching the table goes sort of missing: