Skip to content

Commit 6d8282a

Browse files
committed
fix: support Timestamp values in a mutate
1 parent 8ab3b5d commit 6d8282a

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/utils/mutate.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chunk, isFunction, mapValues } from 'lodash';
1+
import { chunk, cloneDeep, isFunction, mapValues } from 'lodash';
22
import { firestoreRef } from './query';
33
import mark from './perfmarks';
44

@@ -131,26 +131,28 @@ const serverTimestamp = (firebase, key) =>
131131
/**
132132
* Process Mutation to a vanilla JSON
133133
* @param {object} firestore - firestore
134-
* @param {*} mutation - payload mutation
134+
* @param {*} operation - payload mutation
135135
* @returns
136136
*/
137-
function atomize(firebase, mutation) {
138-
return Object.keys(mutation).reduce((data, key) => {
137+
function atomize(firebase, operation) {
138+
return Object.keys(operation).reduce((data, key) => {
139139
const clone = { ...data };
140140
const val = clone[key];
141+
const value =
142+
primaryValue(val) ||
143+
serverTimestamp(firebase, val[0]) ||
144+
arrayUnion(firebase, val[0], val[1]) ||
145+
arrayRemove(firebase, val[0], val[1]) ||
146+
increment(firebase, val[0], val[1]);
147+
141148
if (key.includes('.')) {
142-
nestedMap(clone, key, val);
149+
nestedMap(clone, key, value);
143150
} else if (Array.isArray(val) && val.length > 0) {
144151
// eslint-disable-next-line no-param-reassign
145-
clone[key] =
146-
primaryValue(val) ||
147-
serverTimestamp(firebase, val[0]) ||
148-
arrayUnion(firebase, val[0], val[1]) ||
149-
arrayRemove(firebase, val[0], val[1]) ||
150-
increment(firebase, val[0], val[1]);
152+
clone[key] = value;
151153
}
152154
return clone;
153-
}, JSON.parse(JSON.stringify(mutation)));
155+
}, cloneDeep(operation));
154156
}
155157

156158
// ----- write functions -----
@@ -163,13 +165,9 @@ function atomize(firebase, mutation) {
163165
function writeSingle(firebase, operations) {
164166
mark('mutate.writeSingle');
165167
const { collection, path, doc, id, data, ...rest } = operations;
166-
const promise = docRef(
167-
firebase.firestore(),
168-
path || collection,
169-
id || doc,
170-
).set(atomize(firebase, data || rest), {
171-
merge: true,
172-
});
168+
const ref = docRef(firebase.firestore(), path || collection, id || doc);
169+
const changes = atomize(firebase, data || rest);
170+
const promise = ref.set(changes, { merge: true });
173171
mark('mutate.writeSingle', true);
174172
return promise;
175173
}

0 commit comments

Comments
 (0)