Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { openDB as openConnection, IDBPDatabase, DBSchema } from 'idb';
- interface Schema extends DBSchema {
- 'completed': {
- key: string;
- value: boolean;
- };
- }
- // type Names<Schema extends Object> = keyof Schema;
- class IndexedDB {
- private static _instance: IndexedDB | null = null;
- private _database!: IDBPDatabase<Schema>;
- private constructor() {}
- public static get instance(): IndexedDB {
- if (IndexedDB._instance === null) {
- IndexedDB._instance = new IndexedDB();
- }
- return IndexedDB._instance;
- }
- public async connect() {
- if(this._database === undefined) {
- this._database = await openConnection<Schema>('advancements_', 1, {
- upgrade(db: IDBPDatabase<Schema>) {
- if (!db.objectStoreNames.contains('completed')) {
- db.createObjectStore('completed', { keyPath: '_id' });
- }
- }
- });
- }
- return this._database;
- }
- }
- export default IndexedDB;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement