v1.1.0 - Faster + Optimistic + Idempotent Mutations
- Cache Reducer. reflect UI mutations instantaneous.
- Enables optimistic updates for new, deleted & updated data.
- All data changes synchronously reflected in the redux state cache reducer queries.
- Once Firestore approves/rejects a change it also removes the optimistic commit.
// selector for cache reducer
(state) => {
const { firebase: { cache } } = state;
return cache[myStoreAs].ordered.map(([path, id]) => {
const override = cache.databaseOverrides?.[path]?.[id];
const doc = cache.database[path][id];
return (override ? { ...doc, ...override } :doc;
));
// access read-only fragment of Firestore based on all active queries
const document = state.firebase.firestore.cache.database[full_path_to_collection][document_id];
- Mutate. Simplifies saving, batching and transactions.
- Easily switch between single saves and atomic transactions.
- Simplifies complex saves that have to coordinate through multiple actions.
- Synchronously updates queries in the cache reducer for instance UI feedback for the app.
// single save
getFirestore().mutate({collection: 'tasks', doc: 'task-1', data: {status: 2}});
// batch
getFirestore().mutate([
{collection: 'tasks', doc: 'task-1', data: {status: 2}},
{collection: 'tasks', doc: 'task-2', data: {status: 2}},
]);
// transaction, 'reads' firestore docs or provide local variables to dependency injection for 'writes' functions.
const incrementTaskStatus = ({ uid, task1 }) => ({
collection: 'tasks',
doc: task1.id,
data: { status: task1.status +1, updatedBy: uid }
});
getFirestore().mutate({
reads: {
uid: () => 'my-user-id',
task1: { collection: 'tasks', doc: 'task-1' }
},
writes: [ incrementTaskStatus ]
});