-
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.
- Loading branch information
Showing
5 changed files
with
99 additions
and
6 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
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,33 @@ | ||
import indexedDB from "@/repositories/indexedDB"; | ||
|
||
const model = async (connection = "indexedDB") => { | ||
let store = null; | ||
if (connection === "indexedDB") { | ||
store = indexedDB; | ||
} | ||
|
||
// Check if store is set | ||
if (!store) { | ||
throw new Error("Store not found."); | ||
} | ||
|
||
return { | ||
all: async () => { | ||
return await store.index(); | ||
}, | ||
find: (id) => { | ||
return store.find(id); | ||
}, | ||
create: (collection) => { | ||
store.create(collection); | ||
}, | ||
update: async (id, collection) => { | ||
await store.update(collection); | ||
}, | ||
delete: (id) => { | ||
store.delete(id); | ||
}, | ||
}; | ||
}; | ||
|
||
export default model; |
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,50 @@ | ||
import { openDB } from "idb"; | ||
/** | ||
* @typedef {import('@/types').Collection} Collection | ||
*/ | ||
export const dbPromise = openDB("collection-db", 1, { | ||
upgrade(database) { | ||
database.createObjectStore("collections", { | ||
keyPath: "id", | ||
}); | ||
}, | ||
}); | ||
|
||
const indexedDB = { | ||
async index() { | ||
return (await dbPromise).getAll("collections"); | ||
}, | ||
/** | ||
* @param {Collection} collection | ||
* @returns {Promise<Collection>} | ||
*/ | ||
async create(collection) { | ||
return (await dbPromise).add("collections", collection); | ||
}, | ||
|
||
/** | ||
* @param {Collection} collection | ||
* @returns {Promise<Collection>} | ||
*/ | ||
async update(collection) { | ||
return (await dbPromise).put("collections", collection); | ||
}, | ||
|
||
/** | ||
* @param {string} id | ||
* @returns {Promise<Collection>} | ||
*/ | ||
async delete(id) { | ||
return (await dbPromise).delete("collections", id); | ||
}, | ||
|
||
/** | ||
* @param {string} id | ||
* @returns {Promise<Collection>} | ||
*/ | ||
async find(id) { | ||
return (await dbPromise).get("collections", id); | ||
}, | ||
}; | ||
|
||
export default indexedDB; |