-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexedDB.js
60 lines (49 loc) · 1.65 KB
/
indexedDB.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { useCallback } from "react";
let readwriteStore, readonlyStore;
let createIndexDB = (indexedDB) => {
return new Promise((resolve, reject) => {
const request = indexedDB.open('audioDatabase', 1);
request.onerror = (err) => reject(err);
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore('audio', { keyPath: "id" })
store.createIndex("nameDB", ["trackName"], { unique: false });
}
request.onsuccess = () => {
console.info("successfully open db")
resolve(request.result);
}
})
}
let option = db => (method, arg, callback) => {
const transaction = db.transaction("audio", method);
let store = transaction.objectStore("audio")
return callback(arg, store)
}
let modeOption = (option2func) => {
let getAlldb = (arg) => option2func("readwrite", arg, (arg, store) => store.getAll())
let dataViewdb = (arg) => option2func("readonly", arg, (arg, store) => store.getKey(arg))
let dataDeletedb = (arg) => option2func("readwrite", arg, (arg, store) => { store.delete(arg) })
let dataPutdb = (arg) => option2func("readwrite", arg, (arg, store) => { store.put(arg) })
return ({ dataViewdb, dataDeletedb, dataPutdb, getAlldb })
}
let pipe = (...func) => (input) => func.reduce((pro, fun) =>
pro.then(fun), Promise.resolve(input)
)
const indexedDB =
window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB ||
window.shimIndexedDB;
let globalstore = indexedDB == null ?
console.log("broswer didnt support")
:
pipe(
createIndexDB,
option,
modeOption
)(indexedDB)
export default () => {
return globalstore
}