generated from developmentseed/project-seed
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add indexedDB to sttore encode images
- Loading branch information
Rub21
committed
Jun 21, 2023
1 parent
dc5fb59
commit 4dcecb9
Showing
2 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export const openDB = () => { | ||
return new Promise((resolve, reject) => { | ||
const request = window.indexedDB.open("dsAnnotateDB", 1); | ||
request.onerror = (event) => { | ||
reject(event.target.error); | ||
}; | ||
request.onsuccess = (event) => { | ||
const db = event.target.result; | ||
resolve(db); | ||
}; | ||
request.onupgradeneeded = (event) => { | ||
const db = event.target.result; | ||
if (!db.objectStoreNames.contains("encodeiItems")) { | ||
const objectStore = db.createObjectStore("encodeiItems", { | ||
keyPath: "id", | ||
}); | ||
objectStore.createIndex("idIndex", "id", { unique: false }); | ||
} | ||
}; | ||
}); | ||
}; | ||
|
||
export const addData = (db, data) => { | ||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction(["encodeiItems"], "readwrite"); | ||
const objectStore = transaction.objectStore("encodeiItems"); | ||
const addRequest = objectStore.add(data); | ||
addRequest.onsuccess = (event) => { | ||
resolve(); | ||
}; | ||
addRequest.onerror = (event) => { | ||
reject(event.target.error); | ||
}; | ||
}); | ||
}; | ||
|
||
export const getAllData = (db) => { | ||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction(["encodeiItems"]); | ||
const objectStore = transaction.objectStore("encodeiItems"); | ||
const getAllRequest = objectStore.getAll(); | ||
getAllRequest.onsuccess = (event) => { | ||
console.log(event); | ||
const result = event.target.result; | ||
resolve(result); | ||
}; | ||
getAllRequest.onerror = (event) => { | ||
console.log(event); | ||
reject(event.target.error); | ||
}; | ||
}); | ||
}; |