Skip to content

Commit 91f3fb9

Browse files
author
Bobby
authored
feat: support read queries for optimistic updates
feat: support read queries for optimistic updates
2 parents 97d2db4 + 3056242 commit 91f3fb9

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-firestore",
3-
"version": "1.1.2",
3+
"version": "1.1.5",
44
"description": "Redux bindings for Firestore.",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/reducers/cacheReducer.js

+19
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ const formatTimestamp = ({ seconds } = {}) =>
105105
new Intl.DateTimeFormat('en-US', { dateStyle: 'short' }).format(
106106
new Date(seconds * 1000),
107107
);
108+
const isDocRead = ({ doc, id } = {}) =>
109+
typeof id === 'string' || typeof doc === 'string';
108110

109111
const PROCESSES = {
110112
'<': (a, b) => a < b,
@@ -563,6 +565,23 @@ function translateMutationToOverrides({ payload }, db = {}, dbo = {}) {
563565

564566
const collection = db[path] || {};
565567
const overrides = dbo[path] || {};
568+
569+
if (!isDocRead(reads[key])) {
570+
const pathIds = processOptimistic(reads[key], {
571+
database: db,
572+
databaseOverrides: dbo,
573+
});
574+
return {
575+
...result,
576+
[key]: pathIds.map(([path, id]) => ({
577+
id,
578+
path,
579+
...collection[id],
580+
...(overrides[id] || {}),
581+
})),
582+
};
583+
}
584+
566585
return {
567586
...result,
568587
[key]: { id, path, ...collection[id], ...(overrides[id] || {}) },

src/utils/mutate.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ async function writeInTransaction(firebase, operations) {
187187
const coll = firestoreRef(firebase, read);
188188
const snapshot = await coll.get();
189189
if (hasNothing(snapshot) || snapshot.docs.length === 0) return [];
190-
const unserializedDocs = await Promise.all(snapshot.docs.map(getter));
190+
const unserializedDocs = await Promise.all(
191+
snapshot.docs.map(({ ref }) => getter(ref)),
192+
);
191193
return unserializedDocs.map(serialize);
192194
});
193195

0 commit comments

Comments
 (0)