UNPKG

svelte-idb-store

Version:

A library that allows you to sync the value of your Svelte stores with IndexedDB

57 lines (56 loc) 1.66 kB
import { writable } from "svelte/store"; import { exists, IDBArray, IDBObject, remove } from "./idb"; export const idbStoreArray = ({ name, key, initialValue = [], onLoad, onCreate }) => { const s = writable(initialValue); const idb = new IDBArray(name, key, initialValue, async (creating) => { s.set(await idb.get()); if (creating && onCreate) { onCreate(); } onLoad && onLoad(); }); return { ...s, set: async (val) => { s.set(await idb.set(val)); }, getItem: idb.getItem, setItem: async (val) => { s.set(await idb.setItem(val)); }, removeItem: async (id) => { s.set(await idb.removeItem(id)); }, clear: async () => { s.set(await idb.clear()); } }; }; export const idbStoreObject = ({ name, initialValue, onLoad, onCreate }) => { const s = writable(initialValue); const idb = new IDBObject(name, undefined, initialValue, async (creating) => { s.set(await idb.get()); if (creating && onCreate) { onCreate(); } onLoad && onLoad(); }); return { ...s, set: async (val) => { s.set(await idb.set(val)); }, getItem: idb.getItem, setItem: async (id, val) => { s.set(await idb.setItem(id, val)); }, removeItem: async (id) => { s.set(await idb.removeItem(id)); }, clear: async () => { s.set(await idb.clear()); } }; }; export const existsDB = exists; export const deleteDB = remove;