既存の監査ログ
clinic-dev
# 問い合わせ対応
エディタで確認用スクリプトを実行すると返信できます。
アドレス https://docs.internal.example/consultations/audit-log
相談3: 監査ログに理由がない
最終更新 ・同僚
この変更の業務上の理由は何か。
audit_logs テーブルは、変更前後の値と操作者を持ちます。業務上の理由は持ちません。監査ログで足りる領域と、イベントで残す領域を分けます。
cancelVisit は監査ログの ID を業務イベントの payload に入れます。payload は変更前後の値を持ちません。
実行結果の表の1行目は、業務イベントを導入する前の変更です。2行目と3行目は、変更前後の値が同じです。担当者は reason で2つを見分けられます。4行目の診察の完了は、まだ業務イベントを記録していないので、reason が null です。
監査ログそのものをイベントログから生成してしまう方法もあります。イベントから監査ログを生成できるなら、それが一番シンプルで楽です。
読み取り関数を見る
export const explainChange = (
db: DatabaseSync,
auditLogId: number,
): {
oldValue: string | null;
newValue: string | null;
operatedBy: string;
operatedAt: string;
eventName: string | null;
reason: string | null;
} | undefined =>
db.prepare(`
SELECT
audit_logs.old_value AS oldValue,
audit_logs.new_value AS newValue,
audit_logs.operated_by AS operatedBy,
audit_logs.operated_at AS operatedAt,
business_events.event_name AS eventName,
business_events.payload ->> '$.reason' AS reason
FROM audit_logs
LEFT JOIN business_events ON business_events.payload ->> '$.auditLogId' = audit_logs.id
WHERE audit_logs.id = ?
`).get(auditLogId) as
| {
oldValue: string | null;
newValue: string | null;
operatedBy: string;
operatedAt: string;
eventName: string | null;
reason: string | null;
}
| undefined;
export const findChangesWithoutReason = (
db: DatabaseSync,
): {
auditLogId: number;
rowId: string;
oldValue: string | null;
newValue: string | null;
operatedBy: string;
operatedAt: string;
}[] =>
db.prepare(`
SELECT
audit_logs.id AS auditLogId,
audit_logs.row_id AS rowId,
audit_logs.old_value AS oldValue,
audit_logs.new_value AS newValue,
audit_logs.operated_by AS operatedBy,
audit_logs.operated_at AS operatedAt
FROM audit_logs
LEFT JOIN business_events ON business_events.payload ->> '$.auditLogId' = audit_logs.id
WHERE business_events.event_id IS NULL
ORDER BY audit_logs.id
`).all() as {
auditLogId: number;
rowId: string;
oldValue: string | null;
newValue: string | null;
operatedBy: string;
operatedAt: string;
}[]; 出力例を見る
| (index) | auditLogId | oldValue | newValue | operatedBy | operatedAt | eventName | reason |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 'reserved' | 'cancelled' | '受付スタッフ' | '2026-10-15T01:20:00.000Z' | null | null |
| 1 | 2 | 'reserved' | 'cancelled' | '受付スタッフ' | '2026-11-27T17:16:45.456Z' | 'VisitCancelled' | '飼い主の都合' |
| 2 | 3 | 'reserved' | 'cancelled' | '受付スタッフ' | '2026-11-27T17:16:45.457Z' | 'VisitCancelled' | '来院前に回復したため' |
| 3 | 4 | 'reserved' | 'completed' | '獣医師' | '2026-11-27T17:16:45.457Z' | null | null |
| (index) | auditLogId | rowId | oldValue | newValue | operatedBy | operatedAt |
|---|---|---|---|---|---|---|
| 0 | 1 | 'visit-4' | 'reserved' | 'cancelled' | '受付スタッフ' | '2026-10-15T01:20:00.000Z' |
| 1 | 4 | 'visit-3' | 'reserved' | 'completed' | '獣医師' | '2026-11-27T17:16:45.457Z' |
手元の Node で動かすとき
chapter-4-audit-log-done で node why-changed.ts を実行します。
実行環境を読み込んでいます
clinic_production(read-write) SQLite
Tables
-
idTEXT -
pet_nameTEXT -
statusTEXT
-
event_idINTEGER -
event_atTEXT -
event_nameTEXT -
payloadTEXT -
target_idTEXT -
target_typeTEXT -
target_stateTEXT
-
idINTEGER -
table_nameTEXT -
row_idTEXT -
column_nameTEXT -
old_valueTEXT -
new_valueTEXT -
operated_byTEXT -
operated_atTEXT
実行環境を読み込んでいます