mirror of
https://github.com/rowyio/rowy.git
synced 2025-12-29 00:16:39 +01:00
use Set if updating a nested field
This commit is contained in:
@@ -407,12 +407,6 @@ export const updateFieldAtom = atom(
|
||||
}
|
||||
// Otherwise, apply the update
|
||||
|
||||
// Check if nested path
|
||||
if (fieldName.split(".").length > 1) {
|
||||
const p = tableRows.find((r) => r._rowy_ref.path === path);
|
||||
// add the parent object
|
||||
_set(update, fieldName.split(".")[0], _get(p, fieldName.split(".")[0]));
|
||||
}
|
||||
_set(update, fieldName, value);
|
||||
}
|
||||
|
||||
@@ -493,17 +487,26 @@ export const updateFieldAtom = atom(
|
||||
row._rowy_ref.path,
|
||||
omitRowyFields(newRowValues),
|
||||
deleteField ? [fieldName] : [],
|
||||
arrayTableData
|
||||
{
|
||||
...arrayTableData,
|
||||
// using set if we are updating a nested field
|
||||
useSet: fieldName.split(".").length > 1,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
// Otherwise, update single field in database and write audit update field
|
||||
else {
|
||||
console.log("newRowValues", fieldName);
|
||||
await updateRowDb(
|
||||
row._rowy_ref.path,
|
||||
omitRowyFields(dbUpdate),
|
||||
deleteField ? [fieldName] : [],
|
||||
arrayTableData
|
||||
{
|
||||
...arrayTableData,
|
||||
// using set if we are updating a nested field
|
||||
useSet: fieldName.split(".").length > 1,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -256,22 +256,37 @@ export function useFirestoreCollectionWithAtom<
|
||||
// set the atom’s value to a function that updates a doc in the collection
|
||||
if (updateDocAtom) {
|
||||
setUpdateDocAtom(
|
||||
() => async (path: string, update: T, deleteFields?: string[]) => {
|
||||
const updateToDb = { ...update };
|
||||
() =>
|
||||
async (
|
||||
path: string,
|
||||
update: T,
|
||||
deleteFields?: string[],
|
||||
options?: {
|
||||
useSet?: boolean;
|
||||
}
|
||||
) => {
|
||||
const updateToDb = { ...update };
|
||||
|
||||
if (Array.isArray(deleteFields)) {
|
||||
for (const field of deleteFields) {
|
||||
set(updateToDb as any, field, deleteField());
|
||||
if (Array.isArray(deleteFields)) {
|
||||
for (const field of deleteFields) {
|
||||
set(updateToDb as any, field, deleteField());
|
||||
}
|
||||
}
|
||||
|
||||
if (options?.useSet) {
|
||||
return await setDoc(doc(firebaseDb, path), updateToDb, {
|
||||
merge: true,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
return await updateDoc(doc(firebaseDb, path), updateToDb);
|
||||
} catch (e) {
|
||||
return await setDoc(doc(firebaseDb, path), updateToDb, {
|
||||
merge: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
try {
|
||||
return await updateDoc(doc(firebaseDb, path), updateToDb);
|
||||
} catch (e) {
|
||||
return await setDoc(doc(firebaseDb, path), updateToDb, {
|
||||
merge: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
3
src/types/table.d.ts
vendored
3
src/types/table.d.ts
vendored
@@ -26,13 +26,14 @@ export type UpdateDocFunction<T = TableRow> = (
|
||||
* @param path - The full path to the doc
|
||||
* @param update - The updates to be deeply merged with the existing doc. Note arrays should be ovewritten to match Firestore set with merge behavior
|
||||
* @param deleteFields - Optionally, fields to be deleted from the doc. Access nested fields with dot notation
|
||||
* @param options - Optionally, filed to pass extra data to the function
|
||||
* @returns Promise
|
||||
*/
|
||||
export type UpdateCollectionDocFunction<T = TableRow> = (
|
||||
path: string,
|
||||
update: Partial<T>,
|
||||
deleteFields?: string[],
|
||||
options?: ArrayTableRowData
|
||||
options?: ArrayTableRowData & { useSet?: boolean }
|
||||
) => Promise<void>;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user