acebase/src/acebase-browser.ts
2022-10-31 10:51:42 +01:00

46 lines
2 KiB
TypeScript

import { AceBase, AceBaseLocalSettings } from './acebase-local';
import { createIndexedDBInstance } from './storage/custom/indexed-db';
import { IndexedDBStorageSettings } from './storage/custom/indexed-db/settings';
const deprecatedConstructorError = `Using AceBase constructor in the browser to use localStorage is deprecated!
Switch to:
IndexedDB implementation (FASTER, MORE RELIABLE):
let db = AceBase.WithIndexedDB(name, settings)
Or, new LocalStorage implementation:
let db = AceBase.WithLocalStorage(name, settings)
Or, write your own CustomStorage adapter:
let myCustomStorage = new CustomStorageSettings({ ... });
let db = new AceBase(name, { storage: myCustomStorage })`;
export class BrowserAceBase extends AceBase {
/**
* Constructor that is used in browser context
* @param name database name
* @param settings settings
*/
constructor(name: string, settings: Partial<AceBaseLocalSettings> & { multipleTabs?: boolean }) {
if (typeof settings !== 'object' || typeof settings.storage !== 'object') {
// Client is using old AceBaseBrowser signature, eg:
// let db = new AceBase('name', { temp: false })
//
// Don't allow this anymore. If client wants to use localStorage,
// they need to switch to AceBase.WithLocalStorage('name', settings).
// If they want to use custom storage in the browser, they must
// use the same constructor signature AceBase has:
// let db = new AceBase('name', { storage: new CustomStorageSettings({ ... }) });
throw new Error(deprecatedConstructorError);
}
super(name, settings);
this.settings.ipcEvents = settings.multipleTabs === true;
}
/**
* Creates an AceBase database instance using IndexedDB as storage engine
* @param dbname Name of the database
* @param settings optional settings
*/
static WithIndexedDB(dbname: string, init: Partial<IndexedDBStorageSettings> = {}) {
return createIndexedDBInstance(dbname, init);
}
}