mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-28 17:52:38 +00:00
Add IDB custom store wrapper
This commit is contained in:
54
client/homebrew/utils/customIDBStoreWrapper.js
Normal file
54
client/homebrew/utils/customIDBStoreWrapper.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import * as IDB from 'idb-keyval/dist/index.js';
|
||||
|
||||
export function initCustomStore(db, store){
|
||||
const createCustomStore = async ()=>{
|
||||
return await IDB.createStore(db, store);
|
||||
};
|
||||
return {
|
||||
entries : async ()=>{
|
||||
// Return all entries : [[key1, value1], [key2, value2], ... [keyN, valueN] ]
|
||||
return await IDB.entries(await createCustomStore());
|
||||
},
|
||||
keys : async ()=>{
|
||||
// Return all keys : [ key1, key2, ... keyN ]
|
||||
return await IDB.keys(await createCustomStore());
|
||||
},
|
||||
values : async ()=>{
|
||||
// Return all values : [ value1, value2, ... valueN ]
|
||||
return await IDB.values(await createCustomStore());
|
||||
},
|
||||
clear : async ()=>{
|
||||
// Delete all keys and values
|
||||
return await IDB.clear(await createCustomStore);
|
||||
},
|
||||
get : async (key)=>{
|
||||
// Get a value by its key
|
||||
return await IDB.get(key, await createCustomStore());
|
||||
},
|
||||
getMany : async (keys)=>{
|
||||
// Get multiple values at once
|
||||
return await IDB.getMany(keys, await createCustomStore());
|
||||
},
|
||||
set : async (key, value)=>{
|
||||
// Set a value in the store by the key
|
||||
return await IDB.set(key, value, await createCustomStore());
|
||||
},
|
||||
setMany : async (entries)=>{
|
||||
// Set multiple values at once
|
||||
// `entries` is in the form : [ [key1, value1], [key2, value2], ... [keyN, valueN] ]
|
||||
return await IDB.setMany(entries, await createCustomStore());
|
||||
},
|
||||
update : async (key, updateFn)=>{
|
||||
// Update a value in a single atomic action
|
||||
return await IDB.update(key, updateFn, await createCustomStore());
|
||||
},
|
||||
del : async (key)=>{
|
||||
// Delete a single key and associated value from the store
|
||||
return await IDB.del(key, await createCustomStore());
|
||||
},
|
||||
delMany : async (keys)=>{
|
||||
// Delete multiple keys at once
|
||||
return await IDB.delMany(keys, await createCustomStore());
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user