import { openDB } from 'idb';
|
const DB_NAME = 'route-db';
|
const DB_VERSION = 1;
|
const STORE_NAME = 'data-store';
|
|
const dbPromise = openDB(DB_NAME, DB_VERSION, {
|
upgrade(db) {
|
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
db.createObjectStore(STORE_NAME, { keyPath: 'id' });
|
}
|
},
|
});
|
|
export default {
|
async addData(data) {
|
const db = await dbPromise;
|
const tx = db.transaction(STORE_NAME, 'readwrite');
|
const store = tx.objectStore(STORE_NAME);
|
const id = await store.add(data);
|
await tx.done;
|
return id;
|
},
|
|
async getAllData() {
|
const db = await dbPromise;
|
const tx = db.transaction(STORE_NAME, 'readonly');
|
const store = tx.objectStore(STORE_NAME);
|
return store.getAll();
|
},
|
|
async getDataById(id) {
|
const db = await dbPromise;
|
const tx = db.transaction(STORE_NAME, 'readonly');
|
const store = tx.objectStore(STORE_NAME);
|
return store.get(id);
|
},
|
|
async updateData(id, newData) {
|
const db = await dbPromise;
|
const tx = db.transaction(STORE_NAME, 'readwrite');
|
const store = tx.objectStore(STORE_NAME);
|
await store.put({ ...newData, id });
|
await tx.done;
|
},
|
|
async deleteData(id) {
|
const db = await dbPromise;
|
const tx = db.transaction(STORE_NAME, 'readwrite');
|
const store = tx.objectStore(STORE_NAME);
|
await store.delete(id);
|
await tx.done;
|
},
|
};
|