Skip to content

Commit d4f87b7

Browse files
committed
chore: playground database
1 parent f055792 commit d4f87b7

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

playground/src/firebase.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { initializeApp, type FirebaseApp } from 'firebase/app'
22
import { Firestore, getFirestore } from 'firebase/firestore'
33
import { inject, type App, type InjectionKey } from 'vue'
44
import { getAnalytics, type Analytics } from 'firebase/analytics'
5+
import { Database, getDatabase } from 'firebase/database'
56

67
export function createFirebaseApp() {
78
const firebaseApp = initializeApp({
@@ -16,26 +17,30 @@ export function createFirebaseApp() {
1617
})
1718

1819
const firestore = getFirestore(firebaseApp)
20+
const database = getDatabase(firebaseApp)
1921
const analytics = getAnalytics(firebaseApp)
2022

2123
// connectFirestoreEmulator(firestore, 'localhost', 8080)
2224

23-
return { firebaseApp, firestore, analytics }
25+
return { firebaseApp, firestore, database, analytics }
2426
}
2527

2628
export function VueFirePlugin({
2729
firebaseApp,
2830
firestore,
2931
analytics,
32+
database,
3033
}: ReturnType<typeof createFirebaseApp>) {
3134
return (app: App) => {
3235
app.provide(FirebaseAppInjectKey, firebaseApp)
3336
app.provide(FirestoreInjectKey, firestore)
3437
app.provide(FirebaseAnalyticsInjectKey, analytics)
38+
app.provide(DatabaseInjectKey, database)
3539
}
3640
}
3741

3842
export const FirestoreInjectKey: InjectionKey<Firestore> = Symbol('firestore')
43+
export const DatabaseInjectKey: InjectionKey<Database> = Symbol('database')
3944
export const FirebaseAppInjectKey: InjectionKey<FirebaseApp> =
4045
Symbol('firebaseApp')
4146
export const FirebaseAnalyticsInjectKey: InjectionKey<Analytics> =
@@ -46,6 +51,10 @@ export function useFirestore() {
4651
return inject(FirestoreInjectKey)!
4752
}
4853

54+
export function useDatabase() {
55+
return inject(DatabaseInjectKey)!
56+
}
57+
4958
export function useFirebaseApp() {
5059
return inject(FirebaseAppInjectKey)!
5160
}

playground/src/pages/rtdb-todos.vue

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<script setup lang="ts">
2+
import {
3+
push,
4+
remove,
5+
ref as dbRef,
6+
serverTimestamp,
7+
update,
8+
} from 'firebase/database'
9+
import { ref } from 'vue'
10+
import { useList } from 'vuefire'
11+
import { useDatabase } from '@/firebase'
12+
13+
interface Todo {
14+
created: Date
15+
finished: boolean
16+
text: string
17+
}
18+
19+
const db = useDatabase()
20+
const todosRef = dbRef(db, 'todos')
21+
// TODO:
22+
// const finishedTodos = query(todosRef, where('finished', '==', true))
23+
// const unfinishedTodos = query(todosRef, where('finished', '==', false))
24+
25+
const todos = useList<Todo>(todosRef)
26+
27+
const newTodoText = ref('')
28+
29+
function addTodo() {
30+
if (newTodoText.value) {
31+
push(todosRef, {
32+
text: newTodoText.value,
33+
finished: false,
34+
created: serverTimestamp(),
35+
})
36+
newTodoText.value = ''
37+
}
38+
}
39+
40+
function updateTodoText(todo: Todo, newText: string) {
41+
update(dbRef(db, 'todos/' + todo.id), {
42+
text: newText,
43+
})
44+
}
45+
46+
function removeTodo(todo: Todo) {
47+
remove(dbRef(db, 'todos/' + todo.id))
48+
}
49+
50+
function toggleTodos() {
51+
// TODO:
52+
}
53+
</script>
54+
55+
<template>
56+
<button @click="toggleTodos">Toggle todos</button> <br />
57+
<form @submit.prevent="addTodo">
58+
<input v-model.trim="newTodoText" placeholder="Add new todo" />
59+
<button>Add Todo</button>
60+
</form>
61+
<ul>
62+
<li v-for="todo in todos">
63+
<input
64+
:value="todo.text"
65+
@input="updateTodoText(todo, $event.target.value)"
66+
/>
67+
<button @click="removeTodo(todo)">X</button>
68+
</li>
69+
</ul>
70+
</template>

playground/typed-router.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ declare module 'vue-router/auto/routes' {
6060
Record<never, never>,
6161
Record<never, never>
6262
>
63+
'/rtdb-todos': RouteRecordInfo<
64+
'/rtdb-todos',
65+
'/rtdb-todos',
66+
Record<never, never>,
67+
Record<never, never>
68+
>
6369
'/todos': RouteRecordInfo<
6470
'/todos',
6571
'/todos',

0 commit comments

Comments
 (0)