Skip to content

Commit

Permalink
wip: storage adapater
Browse files Browse the repository at this point in the history
  • Loading branch information
nnivxix committed Nov 17, 2024
1 parent ef43a68 commit 1ec0e0c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/composables/useCollection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ref, toRaw, computed } from "vue";
import dbCollection from "@/repositories/db-collection";
import model from "@/repositories/adapter";
/**
* @typedef {import('@/types').Collection} Collection
* @typedef {import('vue').Ref<Collection>} CollectionRef
Expand All @@ -15,8 +16,15 @@ const collection = ref({
todos: [],
});

/**
* Work on
* 1. Memory
* 2. DB
*/

/** @type {CollectionsRef} */
const collections = ref([]);
const store = await model();

const useCollection = () => {
/** @param {Collection} collection */
Expand All @@ -26,7 +34,7 @@ const useCollection = () => {
dbCollection.add(collection);
};
const getCollections = async () => {
collections.value = await dbCollection.index();
collections.value = await store.all();
};

/**
Expand All @@ -51,15 +59,16 @@ const useCollection = () => {
* @param {Collection} collection
* @returns {Collection}
*/
const updateCollection = (collection) => {
const updateCollection = async (collection) => {
const index = collections.value.findIndex(
(coll) => coll.id === collection.id
);

collections.value.splice(index, 1, collection);
const rawCollection = toRaw(collection);

const rawCollection = toRaw(collection);
dbCollection.update(rawCollection);

return rawCollection;
};

Expand Down
3 changes: 2 additions & 1 deletion src/composables/useFormCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ const useFormCollection = () => {
};
};

function editCurrentCollection() {
async function editCurrentCollection() {
form.value.id = collection.value.id;
const updatedCollection = updateCollection(form.value);
console.log(updatedCollection);
return updatedCollection;
}
function addNewCollection() {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/collection/edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ const schema = yup.object({
const isEdit = route.fullPath.includes("edit");
/** @param {import('@/types').Collection} values */
const onSubmit = (values) => {
const onSubmit = async (values) => {
form.value = {
...values,
todos: toRaw(collection.value.todos),
};
const updatedCollection = editCurrentCollection();
const updatedCollection = await editCurrentCollection();
router.push(`/collection/${updatedCollection.id}`);
};
Expand Down
33 changes: 33 additions & 0 deletions src/repositories/adapter.js
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;
50 changes: 50 additions & 0 deletions src/repositories/indexedDB.js
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;

0 comments on commit 1ec0e0c

Please sign in to comment.