Created
February 15, 2023 16:59
-
-
Save AustineA/73d039296f360d96e142056936647912 to your computer and use it in GitHub Desktop.
ionic storage sqlite service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Storage, Drivers } from "@ionic/storage"; | |
import * as CordovaSQLiteDriver from "localforage-cordovasqlitedriver"; | |
export default class StoreageService { | |
storageReady = false; | |
store = new Storage({ | |
driverOrder: [ | |
CordovaSQLiteDriver._driver, | |
Drivers.IndexedDB, | |
Drivers.LocalStorage, | |
], | |
}); | |
constructor() { | |
this.init(); | |
} | |
async init() { | |
await this.store.defineDriver(CordovaSQLiteDriver); | |
await this.store.create(); | |
this.storageReady = true; | |
} | |
async set(key: string, value: any) { | |
if (!this.storageReady) return; | |
await this.store?.set(key, value); | |
} | |
async get(key: string) { | |
if (!this.storageReady) return; | |
const data = await this.store?.get(key); | |
return data || null; | |
} | |
async remove(key: string) { | |
if (!this.storageReady) return; | |
await this.store?.remove(key); | |
} | |
async clear() { | |
if (!this.storageReady) return; | |
await this.store?.clear(); | |
} | |
} | |
export const storage = new StoreageService(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment