@3mo/moddable-data-grid
Version:
A moddable variant of @3mo/fetchable-data-grid
41 lines (40 loc) • 1.45 kB
JavaScript
import localForage from 'localforage';
import { ModdableDataGridMode } from './ModdableDataGridMode.js';
export class IndexedDbAdapter {
modesKey(dataGridKey) {
return `${dataGridKey}.Modes`;
}
async getAll(dataGridKey) {
const modes = await localForage.getItem(this.modesKey(dataGridKey)) ?? [];
return modes.map(m => new ModdableDataGridMode(m));
}
async get(dataGridKey, modeId) {
const all = await this.getAll(dataGridKey);
return all.find(m => m.id === modeId);
}
async save(dataGridKey, mode) {
const all = await this.getAll(dataGridKey);
const existing = all.find(m => m.id === mode.id);
if (existing) {
all.splice(all.indexOf(existing), 1, mode);
}
else {
all.unshift(mode);
}
await localForage.setItem(this.modesKey(dataGridKey), all);
return mode;
}
async delete(dataGridKey, mode) {
const all = await this.getAll(dataGridKey);
await localForage.setItem(this.modesKey(dataGridKey), all.filter(m => m.id !== mode.id));
}
async getSelectedId(dataGridKey) {
return await localForage.getItem(`${dataGridKey}.Mode`) ?? undefined;
}
async setSelectedId(dataGridKey, modeId) {
await localForage.setItem(`${dataGridKey}.Mode`, modeId);
}
}
(() => {
localForage.setDriver([localForage.INDEXEDDB, localForage.LOCALSTORAGE]);
})();