Skip to content

Commit

Permalink
fix: write functions can each return an array of writes
Browse files Browse the repository at this point in the history
  • Loading branch information
puppybits committed Nov 19, 2021
1 parent d0d52b3 commit 619cba3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/actions/firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,27 +412,27 @@ export function runTransaction(firebase, dispatch, transactionPromise) {
* Unified solo, batch & transactions operations.
* @param {object} firebase - Internal firebase object
* @param {Function} dispatch - Redux's dispatch function
* @param {string} queryOption - Options for query
* @param {string} mutations - Mutations for query
* @returns {Promise} Resolves with results of update call
*/
export function mutate(firebase, dispatch, writes) {
export function mutate(firebase, dispatch, mutations) {
const timestamp = `${+new Date()}`;

return wrapInDispatch(dispatch, {
ref: firebase,
method: 'mutate',
meta: { timestamp },
args: [writes],
args: [mutations],
types: [
{
type: actionTypes.MUTATE_START,
payload: { data: writes },
payload: { data: mutations },
},
actionTypes.MUTATE_SUCCESS,
{
type: actionTypes.MUTATE_FAILURE,
meta: { timestamp },
payload: { data: writes },
payload: { data: mutations },
},
],
});
Expand Down
15 changes: 9 additions & 6 deletions src/reducers/cacheReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
isEmpty,
identity,
} from 'lodash';
import firebase from 'firebase';
import { Timestamp } from 'firebase/firestore';
import { actionTypes } from '../constants';
import { getBaseQueryName } from '../utils/query';
import mark from '../utils/profiling';
Expand Down Expand Up @@ -508,8 +508,7 @@ const arrayRemove = (key, val, cached) =>
const increment = (key, val, cached) =>
key === '::increment' && typeof val === 'number' && (cached() || 0) + val;

const serverTimestamp = (key) =>
key === '::serverTimestamp' && firebase.firestore.Timestamp.now();
const serverTimestamp = (key) => key === '::serverTimestamp' && Timestamp.now();

/**
* Process Mutation to a vanilla JSON
Expand Down Expand Up @@ -560,9 +559,6 @@ function translateMutationToOverrides({ payload }, db = {}, dbo = {}) {

const path = reads[key]?.path || reads[key]?.collection;
const id = reads[key]?.id || reads[key]?.doc;
if (!id) {
throw new Error("Firestore Transactions don't support query lookups.");
}

const collection = db[path] || {};
const overrides = dbo[path] || {};
Expand All @@ -576,6 +572,13 @@ function translateMutationToOverrides({ payload }, db = {}, dbo = {}) {
const overrides = writes
.map((writer) => (isFunction(writer) ? writer(optimistic) : writer))
.filter((data) => !data || !isEmpty(data))
.reduce(
(flat, result) => [
...flat,
...(Array.isArray(result) ? result : [result]),
],
[],
)
.map((write) => {
const { collection, path, doc, id, data, ...rest } = write;

Expand Down

0 comments on commit 619cba3

Please sign in to comment.