LokiJS Adapter - 'Couldn't find database' error


#1

I followed the below tutorial to implement LokiJS storage for one of the apps.

Using adapter, it creates the file but I get random ‘couldn’t find database’ errors. Has anyone come across this issue?

I was using localStorage but its getting cleared sometimes when memory is low.Is SQLite the only option if I cant proceed further with LokiJS?


#2

I made a custom adapter using ionic Storage.

import { Storage } from '@ionic/storage';

const storage = new Storage({
    driverOrder: ['sqlite', 'indexeddb',  'websql', 'localstorage' ]
});
/**
 * A loki persistence adapter which persists to web browser's local storage object
 * @constructor IonicStorageAdapter
 */
export function IonicStorageAdapter() {
}

/**
 * loadDatabase() - Load data from IonicStorage
 * @param {string} dbname - the name of the database to load
 * @param {function} callback - the callback to handle the result
 * @memberof IonicStorageAdapter
 */
IonicStorageAdapter.prototype.loadDatabase = function loadDatabase(dbname, callback) {
    storage.get(dbname)
        .then(value => {
            callback(value);
        });
};

/**
 * saveDatabase() - save data to IonicStorage, will throw an error if the file can't be saved
 * might want to expand this to avoid dataloss on partial save
 * @param {string} dbname - the filename of the database to load
 * @param {function} callback - the callback to handle the result
 * @memberof IonicStorageAdapter
 */
IonicStorageAdapter.prototype.saveDatabase = function saveDatabase(dbname, dbstring, callback) {
    storage.set(dbname, dbstring);
    callback(null);
};

/**
 * deleteDatabase() - delete the database from IonicStorage, will throw an error if it
 * can't be deleted
 * @param {string} dbname - the filename of the database to delete
 * @param {function} callback - the callback to handle the result
 * @memberof IonicStorageAdapter
 */
IonicStorageAdapter.prototype.deleteDatabase = function deleteDatabase(dbname, callback) {
    storage.remove(dbname);
    callback(null);
};

Use LokiJS to give the adapter Property the exported function there.