2
2
import {
3
3
addDoc ,
4
4
collection ,
5
+ deleteDoc ,
5
6
doc ,
6
7
query ,
7
8
serverTimestamp ,
8
9
updateDoc ,
9
10
where ,
10
11
} from ' firebase/firestore'
11
12
import { ref } from ' vue'
12
- import { firestoreBind } from ' vuefire'
13
+ import { useCollection } from ' vuefire'
13
14
import { useFirestore } from ' @/firebase'
14
15
15
16
interface Todo {
@@ -20,12 +21,24 @@ interface Todo {
20
21
21
22
const db = useFirestore ()
22
23
const todosRef = collection (db , ' todos' )
24
+ const todosWithConverterRef = collection (db , ' todos' ).withConverter <Todo >({
25
+ toFirestore(todoModel ) {
26
+ const { id, ... todo } = todoModel
27
+ return todo
28
+ },
29
+ fromFirestore(snapshot , options ) {
30
+ const todoData = snapshot .data (options ) as Omit <Todo , ' id' >
31
+ return {
32
+ id: snapshot .id ,
33
+ ... todoData ,
34
+ }
35
+ },
36
+ })
23
37
const finishedTodos = query (todosRef , where (' finished' , ' ==' , true ))
24
38
const unfinishedTodos = query (todosRef , where (' finished' , ' ==' , false ))
25
39
26
- const todos = ref <Todo []>([])
27
- // TODO: return an augmented typed ref
28
- firestoreBind (todos , todosRef )
40
+ const todos = useCollection <Todo >(todosRef )
41
+ const todosConverted = useCollection (todosWithConverterRef )
29
42
30
43
const newTodoText = ref (' ' )
31
44
@@ -40,15 +53,15 @@ function addTodo() {
40
53
}
41
54
}
42
55
43
- function updateTodoText(todo : any , newText : string ) {
44
- console .log (' update' , todo )
45
- return
56
+ function updateTodoText(todo : Todo , newText : string ) {
46
57
updateDoc (doc (db , ' todos' , todo .id ), {
47
58
text: newText ,
48
59
})
49
60
}
50
61
51
- function removeTodo() {}
62
+ function removeTodo(todo : Todo ) {
63
+ deleteDoc (doc (db , ' todos' , todo .id ))
64
+ }
52
65
53
66
function toggleTodos() {
54
67
// TODO:
0 commit comments