-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added EagleStorage class to contain IndexedDB work
- Loading branch information
1 parent
20a058e
commit 1e42e75
Showing
4 changed files
with
69 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Repositories } from "./Repositories"; | ||
import { Repository } from "./Repository"; | ||
|
||
|
||
export class EagleStorage { | ||
|
||
public static db: IDBDatabase; | ||
|
||
static init(){ | ||
console.log("EagleStorage.init()"); | ||
|
||
const request = indexedDB.open("EAGLE"); | ||
request.onerror = (event) => { | ||
console.error("Why didn't you allow my web app to use IndexedDB?!"); | ||
}; | ||
request.onsuccess = (event) => { | ||
EagleStorage.db = (<any>event.target).result; | ||
|
||
EagleStorage.db.onerror = (event) => { | ||
// generic error handler for all errors targeted at this database's requests! | ||
console.error(`Database error: ${(<any>event.target).error?.message}`); | ||
}; | ||
}; | ||
|
||
request.onupgradeneeded = (event) => { | ||
console.log("onupgradeneeded"); | ||
const db = (<any>event.target).result; | ||
|
||
// create an objectStore for this database | ||
const objectStore = db.createObjectStore("repositories", {keyPath: "id"}); | ||
|
||
// Use transaction oncomplete to make sure the objectStore creation is | ||
// finished before adding data into it. | ||
objectStore.transaction.oncomplete = () => { | ||
console.log("oncomplete"); | ||
|
||
// Store values in the newly created objectStore. | ||
const customerObjectStore = db.transaction("repositories", "readwrite").objectStore("repositories"); | ||
|
||
// read list of repositories from localStorage | ||
const githubRepos = Repositories.listCustomRepositories(Repository.Service.GitHub); | ||
const gitlabRepos = Repositories.listCustomRepositories(Repository.Service.GitLab); | ||
console.log("found ", githubRepos.length + gitlabRepos.length, "repos"); | ||
|
||
githubRepos.forEach((repo) => { | ||
customerObjectStore.add(Repository.toJson(repo)); | ||
}); | ||
gitlabRepos.forEach((repo) => { | ||
customerObjectStore.add(Repository.toJson(repo)); | ||
}); | ||
}; | ||
} | ||
} | ||
} |
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
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