-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdexie.ts
55 lines (46 loc) · 1.36 KB
/
dexie.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import Dexie, { Table } from 'dexie';
import { Todo } from './App';
class TodoDatabase extends Dexie {
todos!: Table<Todo, number>;
constructor() {
super('todo_db');
this.version(1).stores({
todos: '++id,text,completed',
});
}
}
const db = new TodoDatabase();
export type TodoType = Omit<Todo, 'id'>;
// Function to get all todos
export const getTodos = async (): Promise<Todo[]> => {
return await db.todos.toArray();
};
// Function to add a new todo
export const addTodo = async (todo: Omit<Todo, 'id'>) => {
return await db.todos.add(todo); // Add a new todo
};
export const deleteTodo = async (id: number): Promise<void> => {
await db.todos.delete(id);
};
export const searchTodos = async (searchTerm: string): Promise<Todo[]> => {
return await db.todos
.filter((todo) =>
todo.text.toLowerCase().includes(searchTerm.toLowerCase())
)
.toArray();
};
export const toggleTodoCompleted = async (id: number): Promise<void> => {
const todo = await db.todos.get(id); // Get the todo by id
if (todo) {
await db.todos.update(id, { completed: !todo.completed });
}
};
export const filterTodosByCompleted = async (
isCompleted: boolean
): Promise<Todo[]> => {
return await db.todos
.filter((todo) => todo.completed === isCompleted)
.toArray();
};