I have a system that audits DB row add/edit/delete operations via a Postgres function with signature:
func_audit_record(p_table_name TEXT,
p_action_name TEXT,
p_old_row JSONB,
p_new_row JSONB,
p_edited_by INTEGER)
This function has been executed directly via other Postgres functions up until this point, but now I need to execute it via C# directly.
How do I go about mapping a C# data type to the Postgres JSONB data-type while maintaining the above signature?
public async Task AuditAddAsync(string tableName, string actionName, JsonDocument oldRow, JsonDocument newRow, int appUserId)
{
var obj = new
{
p_audit_table_name = tableName,
p_audit_action_name = actionName,
p_old_row_state_as_text = oldRow, //JsonDocument object is too complex here
p_new_row_state_as_text = newRow, //JsonDocument object is too complex here
p_edited_by_user_id = appUserId,
p_system_action = isSystemAction,
p_notes = auditNotes
};
await _dapperAdapter.ExecuteAsync("func_audit_record", obj);
}
I've tried with JsonDocument above (which seems to have the correct structure in the RootElement property), but that's too complex. Also, running JsonDocument.RootElement.ToString() throws an error since the function func_audit_record is expecting JSONB, not TEXT.
I really don't want to need to create a duplicate func_audit_record with TEXT parameters.
Ideas?