Skip to content

Commit

Permalink
fix(firestore): array with objects with references
Browse files Browse the repository at this point in the history
Fix #576
  • Loading branch information
posva committed Mar 17, 2020
1 parent f23540e commit b50ac72
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ describe('refs in documents', () => {
})
})

it.only('does not lose empty references in arrays of objects when updating a property', async () => {
const emptyItem = collection.doc()
await item.update({ todos: [{ ref: emptyItem }], toggle: true })
await bind('item', item)
expect(vm.item).toEqual({
todos: [{ ref: null }],
toggle: true,
})
await item.update({ toggle: false })
expect(vm.item).toEqual({
todos: [{ ref: null }],
toggle: false,
})
})

it('keeps array of references when updating a property', async () => {
await item.update({ a: [a, b, c, { foo: 'bar' }], toggle: true })
await bind('item', item)
Expand Down
2 changes: 1 addition & 1 deletion packages/@posva/vuefire-core/src/firestore/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSnapshot, extractRefs, FirestoreSerializer, FirestoreReference } from './utils'
import { createSnapshot, extractRefs, FirestoreSerializer } from './utils'
import { walkGet, callOnceWithArg, OperationsType } from '../shared'
import { firestore } from 'firebase'

Expand Down
5 changes: 3 additions & 2 deletions packages/@posva/vuefire-core/src/firestore/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ export function extractRefs(
// fill existing refs into data but leave the rest empty
for (let i = 0; i < ref.length; i++) {
const newRef = ref[i]
// TODO: this only works with array of primitives but not with nested properties like objects with References
if (newRef.path in subsByPath) data[key][i] = subsByPath[newRef.path]
}
// the oldArray is in this case the same array with holes
recursiveExtract(ref, data[key], path + key + '.', [data[key], refs])
// the oldArray is in this case the same array with holes unless the array already existed
recursiveExtract(ref, oldDoc[key] || data[key], path + key + '.', [data[key], refs])
} else if (isObject(ref)) {
data[key] = {}
recursiveExtract(ref, oldDoc[key], path + key + '.', [data[key], refs])
Expand Down

0 comments on commit b50ac72

Please sign in to comment.