-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from akvo/feature/155-implement-indexed-DB
Feature/155 implement indexed db
- Loading branch information
Showing
9 changed files
with
284 additions
and
64 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
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,93 @@ | ||
import Dexie from "dexie"; | ||
|
||
const dbName = "siwins"; | ||
const db = new Dexie(dbName); | ||
|
||
db.version(1).stores({ | ||
sync: "++id, cursor", | ||
sources: "++endpoint, data", // store resources of dropdown data | ||
maps: "++endpoint, data", // store data of maps page | ||
dashboards: "++endpoint, data", // store data of dashboard page | ||
}); | ||
|
||
const checkDB = () => | ||
Dexie.exists(dbName) | ||
.then((exists) => { | ||
if (exists) { | ||
console.info("Database exists"); | ||
} else { | ||
console.info("Database doesn't exist"); | ||
} | ||
}) | ||
.catch((e) => { | ||
console.error( | ||
"Oops, an error occurred when trying to check database existance" | ||
); | ||
console.error(e); | ||
}); | ||
|
||
const truncateTables = () => { | ||
db.sources.clear(); | ||
db.maps.clear(); | ||
db.dashboards.clear(); | ||
}; | ||
|
||
const getSource = async (endpoint) => { | ||
const res = await db.sources.get({ endpoint }); | ||
if (!res) { | ||
return null; | ||
} | ||
return { | ||
...res, | ||
data: JSON.parse(res.data), | ||
}; | ||
}; | ||
|
||
const saveSource = ({ endpoint, data }) => { | ||
return db.sources.put({ endpoint, data: JSON.stringify(data) }); | ||
}; | ||
|
||
const getMap = async (endpoint) => { | ||
const res = await db.maps.get({ endpoint }); | ||
if (!res) { | ||
return null; | ||
} | ||
return { | ||
...res, | ||
data: JSON.parse(res.data), | ||
}; | ||
}; | ||
|
||
const saveMap = ({ endpoint, data }) => { | ||
return db.maps.put({ endpoint, data: JSON.stringify(data) }); | ||
}; | ||
|
||
const getDashboard = async (endpoint) => { | ||
const res = await db.dashboards.get({ endpoint }); | ||
if (!res) { | ||
return null; | ||
} | ||
return { | ||
...res, | ||
data: JSON.parse(res.data), | ||
}; | ||
}; | ||
|
||
const saveDashboard = ({ endpoint, data }) => { | ||
return db.dashboards.put({ endpoint, data: JSON.stringify(data) }); | ||
}; | ||
|
||
const ds = { | ||
checkDB, | ||
truncateTables, | ||
getSource, | ||
saveSource, | ||
getMap, | ||
saveMap, | ||
getDashboard, | ||
saveDashboard, | ||
getCursor: async () => await db.sync.get({ id: 1 }), | ||
saveCursor: ({ cursor }) => db.sync.put({ id: 1, cursor }), | ||
}; | ||
|
||
export default ds; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as api } from "./api"; | ||
export { default as ds } from "./db"; |
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
Oops, something went wrong.