React to database changes
Patterns 4 · 3:49
A change tracking integration publishes every insert, update and delete in a database table into a channel, and a watcher route reacts to each one. The database administrator first turns on the database's own change feature.
What it covers
- Scaffolding the change tracking with
weikio init --kind change-tracking - The filled document for PostgreSQL and
public.invoice, with its placeholders from variables and a secret - The channel
invoice-changes, made withweikio channel create, where each change is one stored event - The watcher route, started by the kamelet
weikio-event-source, with its log step - Three changes to invoice 1041, read back with
weikio events showandweikio logs - The agent
devas the leading copy, and each change on the admin UI's Changes tab
Commands in this video
Terminal
$ weikio init invoice-tracking --kind change-tracking
$ weikio channel create sandbox/invoice-changes
$ weikio apply -f ./invoice-tracking
$ weikio deploy sandbox/invoice-tracking --env devtest
$ weikio run ./invoice-watcher --no-logs
$ psql -d erp -c "INSERT INTO invoice (id, customer, amount, status) VALUES (1041, 'Lakeside Bakery', 640, 'open')"
$ weikio events show sandbox/invoice-changes --seq 1
$ psql -d erp -c "UPDATE invoice SET status = 'paid' WHERE id = 1041"
$ psql -d erp -c "DELETE FROM invoice WHERE id = 1041"
$ weikio logs sandbox/invoice-watcher --since 10mTranscript
- 0:01An invoice changes in the database.
- 0:03Other systems must hear of every change.
- 0:12The three changes to invoice 1041 left no rows.
- 0:16The database's administrator switches on its change feature.
- 0:23The tools are a change tracking, a channel, and the kamelet weikio-event-source.
- 0:33The change tracking is an integration of its own kind.
- 0:36The command wrote its scaffold, and the tracking publishes every insert,
- 0:41update and delete into a channel.
- 0:44The scaffold names SQL Server, and the filled document names postgres,
- 0:50public.invoice and another channel, invoice-changes.
- 0:56The placeholders read variables and a secret:
- 0:59erp-db-host, erp-db-user, erp-db-password.
- 1:07A channel is a hub service that keeps its events.
- 1:11Every change the tracking publishes is one stored event.
- 1:15It holds an op letter, c for create, u for update,
- 1:19d for delete, and the row before and after.
- 1:22The command created the channel sandbox/invoice-changes.
- 1:29The kamelet weikio-event-source starts the watcher's route on the channel.
- 1:34The route sees each stored event as it lands.
- 1:37Its file shows the kamelet, the channel and the log step.
- 1:41The log step prints each change's operation, invoice and status.
- 1:48The apply sends the folder to the hub.
- 1:52The change tracking is created with no warning, because the channel exists.
- 1:57The deploy answers with one agent running.
- 2:02The watcher runs, from the kamelet weikio-event-source.
- 2:11The database's own tool inserts invoice 1041.
- 2:19Two seconds later, the channel holds one stored event.
- 2:23It carries op c, before null and after the new row.
- 2:32The tool updates the row for invoice 1041.
- 2:35Then the row is removed.
- 2:42The channel holds three stored events.
- 2:49weikio logs reads ten minutes of the watcher's log.
- 2:53The received lines read op c, u, d.
- 3:03The change tracking runs one copy at a time, dev is the leader.
- 3:10Its Changes lists each change for invoice 1041.
- 3:16The event holds op u, open in before and paid in after.
- 3:25The change tracking publishes every insert,
- 3:28update and delete of the table into a channel.
- 3:31The channel invoice-changes keeps each one as a stored event.
- 3:35The kamelet weikio-event-source starts the watcher's route,
- 3:39and the route sees each event.
Next in this track
Patterns 5 · 2:58 One copy at a time Watched