Skip to content

Instantly share code, notes, and snippets.

@AustineA
Created February 15, 2023 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AustineA/73d039296f360d96e142056936647912 to your computer and use it in GitHub Desktop.
Save AustineA/73d039296f360d96e142056936647912 to your computer and use it in GitHub Desktop.
ionic storage sqlite service
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