refactor: improve merge logic

This commit is contained in:
Carlos Valente
2025-12-20 15:07:47 +01:00
committed by Carlos Valente
parent 08b8e73393
commit a5daa76616
2 changed files with 107 additions and 24 deletions
@@ -7,24 +7,27 @@ export function safeMerge(existing: DatabaseModel, newData: Partial<DatabaseMode
const deepExisting = structuredClone(existing);
const deepNewData = structuredClone(newData);
// destructure each property to simplify merging not provided ie: ...{} has no effect
const {
rundowns = {},
project = {},
settings = {},
viewSettings = {},
urlPresets = deepExisting.urlPresets,
customFields = deepExisting.customFields,
automation = deepExisting.automation,
urlPresets = [],
customFields = {},
automation,
} = deepNewData;
return {
...deepExisting,
rundowns: { ...existing.rundowns, ...rundowns },
project: { ...deepExisting.project, ...project },
settings: { ...deepExisting.settings, ...settings },
viewSettings: { ...deepExisting.viewSettings, ...viewSettings },
urlPresets: urlPresets ?? deepExisting.urlPresets,
customFields: customFields ?? deepExisting.customFields,
automation: { ...deepExisting.automation, ...automation },
// URL presets are independent and we can append them together
urlPresets: [...deepExisting.urlPresets, ...urlPresets],
// custom fields can be merged
customFields: { ...deepExisting.customFields, ...customFields },
// trigger and automation are coupled and cannot be changed individually so we replace the whole automation
automation: automation ?? deepExisting.automation,
};
}