From 706abfc41448ffaf39d4b921f2b8cbeb1fa03a39 Mon Sep 17 00:00:00 2001 From: daeseong9388 Date: Wed, 30 Nov 2022 16:39:20 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20IndexedDB=20TodoList=20=EA=B5=AC?= =?UTF-8?q?=EB=8F=99=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/repository/repository.indexedDB.ts | 21 +++++-------------- client/src/util/GlobalState.ts | 2 +- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/client/src/core/repository/repository.indexedDB.ts b/client/src/core/repository/repository.indexedDB.ts index 6fbfb39..b29f278 100644 --- a/client/src/core/repository/repository.indexedDB.ts +++ b/client/src/core/repository/repository.indexedDB.ts @@ -15,7 +15,6 @@ export class IndexedDBFactory { }); const result = new IndexedDB(db); if (todoList === undefined) return result; - for (const todo of todoList) { await result.add(todo); } @@ -30,33 +29,25 @@ class IndexedDB implements ITodoListDataBase { } async get(id: string): Promise { - const tx = this.db.transaction(TABLE_NAME, 'readonly'); - const store = tx.objectStore(TABLE_NAME); - const result = await store.get(id); + const result = await this.db.get(TABLE_NAME, id); return result as PlainTodo | undefined; } async getAll(): Promise { - const tx = this.db.transaction(TABLE_NAME, 'readonly'); - const store = tx.objectStore(TABLE_NAME); - const result = await store.getAll(); + const result = await this.db.getAll(TABLE_NAME); return result as PlainTodo[]; } async add(todo: InputTodo): Promise { - const tx = this.db.transaction(TABLE_NAME, 'readwrite'); - const store = tx.objectStore(TABLE_NAME); const newTodo = new Todo(todo).toPlain(); - await store.add(newTodo, newTodo.id); + await this.db.add(TABLE_NAME, newTodo); return await this.getAll(); } async edit(id: string, todo: InputTodo): Promise { - const tx = this.db.transaction(TABLE_NAME, 'readwrite'); - const store = tx.objectStore(TABLE_NAME); const oldTodo = (await this.get(id)) as PlainTodo; const newTodo = new Todo({ ...oldTodo, ...todo, id: oldTodo.id }).toPlain(); - await store.put(newTodo, newTodo.id); + await this.db.put(TABLE_NAME, newTodo); return await this.getAll(); } @@ -68,9 +59,7 @@ class IndexedDB implements ITodoListDataBase { } async remove(id: string): Promise { - const tx = this.db.transaction(TABLE_NAME, 'readwrite'); - const store = tx.objectStore(TABLE_NAME); - await store.delete(id); + await this.db.delete(TABLE_NAME, id); return await this.getAll(); } } diff --git a/client/src/util/GlobalState.ts b/client/src/util/GlobalState.ts index 1f511a1..cdec0cf 100644 --- a/client/src/util/GlobalState.ts +++ b/client/src/util/GlobalState.ts @@ -24,7 +24,7 @@ export const readWriteAtom = atom( // const initTodo = createTodoList('IndexedDB'); // export const todoList = atom(initTodo); // export const todoList = atom(async () => await createTodoList('MemoryDB')); -const todoData = await createTodoList('MemoryDB'); +const todoData = await createTodoList('IndexedDB'); export const todoList = atom(todoData); // export const activeTodoAtom = atom(async (get) => await get(todoList).getActiveTodo());