Skip to content

Commit

Permalink
feat: IndexedDB TodoList 구동하도록 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
daeseong9388 committed Nov 30, 2022
1 parent ab90692 commit 706abfc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
21 changes: 5 additions & 16 deletions client/src/core/repository/repository.indexedDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -30,33 +29,25 @@ class IndexedDB implements ITodoListDataBase {
}

async get(id: string): Promise<PlainTodo | undefined> {
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<PlainTodo[]> {
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<PlainTodo[]> {
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<PlainTodo[]> {
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();
}

Expand All @@ -68,9 +59,7 @@ class IndexedDB implements ITodoListDataBase {
}

async remove(id: string): Promise<PlainTodo[]> {
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();
}
}
2 changes: 1 addition & 1 deletion client/src/util/GlobalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit 706abfc

Please sign in to comment.