Patterns - msupply-foundation/open-msupply GitHub Wiki
Any data that needs to be synced to a central server should have a change record in the changelog table each time the record is updated or created. This allows open-mSupply to identify which records need to be sent to central server next time it syncs.
To Implement this, we are using a pattern like this.
Example from asset_row.rs
pub fn upsert_one(&self, asset_row: &AssetRow) -> Result<i64, RepositoryError> {
self._upsert_one(asset_row)?;
self.insert_changelog(
asset_row.id.to_owned(),
RowActionType::Upsert,
Some(asset_row.clone()),
)
}
fn insert_changelog(
&self,
asset_id: String,
action: RowActionType,
row: Option<AssetRow>,
) -> Result<i64, RepositoryError> {
let row = ChangeLogInsertRow {
table_name: ChangelogTableName::Asset,
record_id: asset_id,
row_action: action,
store_id: row.map(|r| r.store_id).unwrap_or(None),
..Default::default()
};
ChangelogRepository::new(self.connection).insert(&row)
}
This will mean that the changelog record is created every time upsert_one
is called.