-
You have some tables in your database that you want to audit. You want to know who changed what and when. You want to be able to query this data easily and see the changes over time.
-
You want to be able to subscribe to changes in the database and be notified in real-time.
- Audit
- A generic audit table, with triggers on the tables you want to audit, that will automatically insert a row into the audit table when a row is inserted, updated, or deleted.
- Fully typed objects using TypeScript / ZOD / ORM Drizzle
- SQL code is defined here:
- Drizzle schema:
- Example query usage:
- Notifications
- using NOTIFY/LISTEN to subscribe to changes in the database and be notified in real-time. https://www.postgresql.org/docs/current/sql-notify.html
- Fully typed events with payload using TypeScript / ZOD / ORM Drizzle
- Notify triggers (SQL):
- Typescript / Zod code and function to listen to notifications:
Example repository integrating:
- https://orm.drizzle.team/docs/overview
- https://zod.dev/
- https://www.postgresql.org/
- https://github.com/supabase/supa_audit
- https://www.postgresql.org/docs/current/sql-notify.html
To install dependencies:
bun install
To run:
bun run dev
This example projects implements 3 tables:
- users
- posts (with a foreign key to users)
- comments (with a foreign key to users and posts)
- record_version (stored in a audit table)
Server:
- http://localhost:3000 -> list all posts
- http://localhost:3000/stream -> stream all audit table changes
- http://localhost:3000/generate -> generate random posts and comments
http://localhost:3000/create-post -> create a post
http://localhost:3000/create-comment -> create a comment
http://localhost:3000/update-post -> update a random post
http://localhost:3000/update-comment -> update a random comment