Skip to content

Commit 1676e5e

Browse files
committed
refactor: accept primitives in converted types
1 parent 0210d14 commit 1676e5e

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script setup lang="ts">
2+
import { collection, doc } from 'firebase/firestore'
3+
import { ref } from 'vue'
4+
import { firestoreBind, useCollection } from 'vuefire'
5+
import { useFirestore } from '@/firebase'
6+
7+
const db = useFirestore()
8+
const numbers = useCollection(
9+
collection(db, 'numbers').withConverter<number>({
10+
toFirestore(n) {
11+
return { n }
12+
},
13+
fromFirestore(snapshot) {
14+
return snapshot.data().n as number
15+
},
16+
})
17+
)
18+
</script>
19+
20+
<template>
21+
<p>numbers:</p>
22+
<ul>
23+
<li v-for="num in numbers">{{ num }}</li>
24+
</ul>
25+
</template>

playground/typed-router.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ declare module 'vue-router/auto/routes' {
4242
Record<never, never>,
4343
Record<never, never>
4444
>
45+
'/converter-with-number': RouteRecordInfo<
46+
'/converter-with-number',
47+
'/converter-with-number',
48+
Record<never, never>,
49+
Record<never, never>
50+
>
4551
'/nested-collections': RouteRecordInfo<
4652
'/nested-collections',
4753
'/nested-collections',

src/firestore/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ export function bindCollection<T>(
198198
extraOptions: FirestoreOptions = DEFAULT_OPTIONS
199199
) {
200200
const options = Object.assign({}, DEFAULT_OPTIONS, extraOptions) // fill default values
201+
// a custom converter means we don't need a serializer
202+
if (collection.converter) {
203+
// @ts-expect-error: FIXME: remove this serialize option
204+
options.serialize = v => v.data()
205+
}
201206
const key = 'value'
202207
if (!options.wait) ops.set(target, key, [])
203208
let arrayRef = ref(options.wait ? [] : target[key])

src/firestore/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ export function createSnapshot<T = DocumentData>(
2424
export type FirestoreSerializer = typeof createSnapshot
2525

2626
export function extractRefs(
27+
// FIXME: unknown
2728
doc: DocumentData,
2829
oldDoc: DocumentData | void,
2930
subs: Record<string, { path: string; data: () => DocumentData | null }>
3031
): [DocumentData, Record<string, DocumentReference>] {
32+
if (!isObject(doc)) return [doc, {}]
33+
3134
const dataAndRefs: [DocumentData, Record<string, DocumentReference>] = [
3235
{},
3336
{},

0 commit comments

Comments
 (0)