Skip to content

View the integration on GitHub

PgStack Audit plugs into Supabase JS and PostgreSQL to track database changes automatically. It supports audit trails with application context and historical queries in your application.

This package passes application-specific context with database changes through the Supabase JS integration. Context can include the 'where' (API endpoint, worker, etc.), 'who' (user, cron job, etc.), and 'how' behind a change.

  • Supabase JS
  1. Install the NPM package
Terminal window
npm install @bemi-db/supabase-js
  1. Generate a Supabase JS migration file to add a Database Function for passing application context with all data changes into PostgreSQL replication log
Terminal window
npx bemi migration new
  1. Run the Supabase JS migration
Terminal window
npx supabase migration up

You can specify custom application context that will be automatically passed with all data changes by calling setContext in your Supabase Edge Function:

import { setContext } from 'https://esm.sh/@bemi-db/supabase-js@0.1.0'
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.43.5'
Deno.serve(async (req) => {
const supabase = createClient(...)
const { method, url } = req
const endpoint = `/${url.split('/')[3]}`
const payload = method === 'POST' ? await req.json() : {}
// Customizable context
await setContext(supabase, { method, endpoint, payload })
// Your code that performs data changes
})

Application context:

  • Is bound to the current database session within an HTTP request.
  • Is used only with INSERT, UPDATE, DELETE SQL queries performed via Supabase JS.
  • Is passed directly into PG Write-Ahead Log with data changes without affecting the structure of the database and SQL queries.

Connect your PostgreSQL database to PgStack Audit using the quickstart. Configure logical replication using the guide for your hosting platform.

With the integration installed, make a change through your application and check the project's Audit view. The change should include the application context supplied by your integration. Changes made outside the integrated application are still captured, but do not receive that application's context.

Use the PgStack database connection details from your project settings to query changes with SQL or your ORM. Replace [USERNAME], [HOSTNAME], and [DATABASE] with those details:

Terminal window
psql postgres://[USERNAME]@[HOSTNAME]:5432/[DATABASE] -c \
'SELECT "primary_key", "table", "operation", "before", "after", "context", "committed_at" FROM changes;'

Example output:

primary_key | table | operation | before | after | context | committed_at
-------------+-------+-----------+----------------------------------------------------+-----------------------------------------------------+-------------------------------------------------------------------------------------------+------------------------
26 | todo | CREATE | {} | {"id": 26, "task": "Sleep", "is_completed": false} | {"user_id": 187234, "endpoint": "/todo", "method": "POST", "SQL": "INSERT INTO ..."} | 2023-12-11 17:09:09+00
27 | todo | CREATE | {} | {"id": 27, "task": "Eat", "is_completed": false} | {"user_id": 187234, "endpoint": "/todo", "method": "POST", "SQL": "INSERT INTO ..."} | 2023-12-11 17:09:11+00
28 | todo | CREATE | {} | {"id": 28, "task": "Repeat", "is_completed": false} | {"user_id": 187234, "endpoint": "/todo", "method": "POST", "SQL": "INSERT INTO ..."} | 2023-12-11 17:09:13+00
26 | todo | UPDATE | {"id": 26, "task": "Sleep", "is_completed": false} | {"id": 26, "task": "Sleep", "is_completed": true} | {"user_id": 187234, "endpoint": "/todo/complete", "method": "PUT", "SQL": "UPDATE ..."} | 2023-12-11 17:09:15+00
27 | todo | DELETE | {"id": 27, "task": "Eat", "is_completed": false} | {} | {"user_id": 187234, "endpoint": "/todo/27", "method": "DELETE", "SQL": "DELETE FROM ..."} | 2023-12-11 17:09:18+00

Distributed under the terms of the LGPL-3.0. If you need to modify and distribute the code, please release it to contribute back to the open-source community.