Skip to content

Commit 38e4f2c

Browse files
committed
Allow pass falsy values to update fields
1 parent fbfcb81 commit 38e4f2c

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typesaurus",
3-
"version": "8.0.0-alpha.30",
3+
"version": "8.0.0-alpha.31",
44
"description": "Type-safe ODM for Firestore",
55
"keywords": [
66
"Firebase",

Diff for: src/field/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export interface Field<_Model> {
2424
value: any
2525
}
2626

27+
export type FieldsWithFalsyValues<Model> = Array<
28+
Field<Model> | undefined | null | false
29+
>
30+
2731
export function field<Model, Key extends keyof Model>(
2832
key: Key | [Key],
2933
value: Model[Key] | UpdateValue<Model, Key>

Diff for: src/update/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import adaptor from '../adaptor'
22
import type { Collection } from '../collection'
33
import { unwrapData } from '../data'
4-
import type { Field } from '../field'
4+
import type { Field, FieldsWithFalsyValues } from '../field'
55
import type { Ref } from '../ref'
66
import type { OperationOptions, RuntimeEnvironment } from '../types'
77
import type { UpdateValue } from '../value'
@@ -30,7 +30,7 @@ export async function update<
3030
>(
3131
collection: Collection<Model>,
3232
id: string,
33-
data: Field<Model>[],
33+
data: FieldsWithFalsyValues<Model>,
3434
options?: UpdateOptions<Environment>
3535
): Promise<void>
3636

@@ -107,9 +107,9 @@ export async function update<
107107
Environment extends RuntimeEnvironment | undefined
108108
>(
109109
collectionOrRef: Collection<Model> | Ref<Model>,
110-
idOrData: string | Field<Model>[] | UpdateModel<Model>,
110+
idOrData: string | FieldsWithFalsyValues<Model> | UpdateModel<Model>,
111111
maybeDataOrOptions?:
112-
| Field<Model>[]
112+
| FieldsWithFalsyValues<Model>
113113
| UpdateModel<Model>
114114
| UpdateOptions<Environment>,
115115
maybeOptions?: UpdateOptions<Environment>
@@ -137,7 +137,9 @@ export async function update<
137137

138138
const firebaseDoc = a.firestore.collection(collection.path).doc(id)
139139
const updateData = Array.isArray(data)
140-
? data.reduce((acc, { key, value }) => {
140+
? data.reduce((acc, field) => {
141+
if (!field) return
142+
const { key, value } = field
141143
acc[Array.isArray(key) ? key.join('.') : key] = value
142144
return acc
143145
}, {} as { [key: string]: any })

0 commit comments

Comments
 (0)