Skip to content

Commit 02641e2

Browse files
committed
Add information about the new Bemi Django package
1 parent bc3cdc0 commit 02641e2

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

docs/docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ keywords: ['Bemi Changelog', 'Bemi New Features', 'Postgres Audit Trails', 'Chan
1717
* Create PG publications limited to specific tables with selective tracking
1818
* [Bemi Core](https://github.com/BemiHQ/bemi)
1919
* Allow customizing parsed change attributes with an override function
20+
* [Bemi Django](https://github.com/BemiHQ/bemi-django)
21+
* Create a new Python package to allow passing application context with data changes
2022
* [Bemi Prisma](https://github.com/BemiHQ/bemi-prisma)
2123
* Fix compatibility with Prisma v5.20+ driver adapter
2224

docs/docs/home.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ This allows automatically enhancing low-level database changes with application-
6464
#### Python
6565

6666
* **[SQLAlchemy](/orms/sqlalchemy)**
67+
* **[Django](/orms/django)**
6768

6869
## Architecture overview
6970

docs/docs/orms/django.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: Bemi Django Integration - Automate Context-Aware Audit Trails with PostgreSQL
3+
sidebar_label: Django
4+
hide_title: true
5+
description: Discover how Bemi integrates with Django and PostgreSQL to automatically track database changes, providing robust audit trails for your applications. Learn how to install and use the Bemi Django Python package for enhanced data tracking.
6+
image: 'img/social-card.png'
7+
keywords: [Bemi, Django, PostgreSQL, database auditing, data tracking, context-aware audit, application context, audit log, audit trail, data versioning]
8+
---
9+
10+
# Django
11+
12+
<a class="github-button" href="https://github.com/BemiHQ/bemi-django" data-size="large" data-show-count="true" aria-label="Star BemiHQ/bemi-django on GitHub">BemiHQ/bemi-django</a>
13+
<br />
14+
<br />
15+
16+
[Bemi](https://bemi.io) plugs into [Django](https://github.com/django/django) and PostgreSQL to track database changes automatically. It unlocks robust context-aware audit trails and time travel querying inside your application.
17+
18+
This Python package is a recommended Django integration, enabling you to pass application-specific context when performing database changes. This can include context such as the 'where' (API endpoint, worker, etc.), 'who' (user, cron job, etc.), and 'how' behind a change, thereby enriching the information captured by Bemi.
19+
20+
See this [example repo](https://github.com/BemiHQ/bemi-django-example) as a Django Todo app that automatically tracks and contextualized all changes.
21+
22+
## Prerequisites
23+
24+
- PostgreSQL 14+
25+
- Django
26+
27+
## Installation
28+
29+
1. Install the Python package
30+
31+
```sh
32+
pip install bemi-django
33+
```
34+
35+
2. Run the migration add lightweight [PostgreSQL triggers](https://www.postgresql.org/docs/current/plpgsql-trigger.html) for passing application context with all data changes into PostgreSQL replication log
36+
37+
```sh
38+
python manage.py migrate bemi
39+
```
40+
41+
## Usage
42+
43+
Add the Bemi middleware to your Django project app and add the path to a custom `get_bemi_context` function to automatically pass application context with all tracked database changes made within an HTTP request:
44+
45+
```py
46+
# settings.py
47+
48+
INSTALLED_APPS = [
49+
# Your other apps
50+
'bemi',
51+
]
52+
53+
MIDDLEWARE = [
54+
# Your other middlewares
55+
'bemi.BemiMiddleware',
56+
]
57+
58+
BEMI_CONTEXT_FUNCTION = 'your_project.utils.get_bemi_context'
59+
```
60+
61+
Now you can easily specify custom application context:
62+
63+
```py
64+
# utils.py
65+
66+
def get_bemi_context(request):
67+
# Return any custom context dict
68+
return {
69+
'user_id': request.user.id,
70+
'method': request.method,
71+
'path': request.path,
72+
}
73+
```
74+
75+
Application context:
76+
77+
* Is bound to the current execution thread within an HTTP request.
78+
* Is used only with `INSERT`, `UPDATE`, `DELETE` SQL queries performed via Django. Otherwise, it is a no-op.
79+
* Is passed directly into PG [Write-Ahead Log](https://www.postgresql.org/docs/current/wal-intro.html) with data changes without affecting the structure of the database and SQL queries.
80+
81+
Application context will automatically include the original SQL query that performed data changes, which is generally useful for troubleshooting purposes.
82+
83+
## Database connection
84+
85+
Connect your PostgreSQL source database on [bemi.io](https://bemi.io) to start ingesting and storing all data changes stitched together with application-specific context. The database connection details can be securely configured through the [dashboard UI](https://dashboard.bemi.io/log-in?ref=django) in a few seconds.
86+
87+
![dashboard](/img/dashboard.png)
88+
89+
Once your destination PostgreSQL database has been fully provisioned, you'll see a "Connected" status. You can now test the connection after making database changes in your connected source database:
90+
91+
```
92+
psql postgres://[USERNAME]:[PASSWORD]@[HOSTNAME]:5432/[DATABASE] -c 'SELECT "primary_key", "table", "operation", "before", "after", "context", "committed_at" FROM changes;'
93+
94+
primary_key | table | operation | before | after | context | committed_at
95+
-------------+-------+-----------+----------------------------------------------------+-----------------------------------------------------+-------------------------------------------------------------------------------------------+------------------------
96+
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
97+
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
98+
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
99+
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
100+
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
101+
```
102+
103+
See [Destination Database](/postgresql/destination-database) for more details.
104+
105+
## License
106+
107+
Distributed under the terms of the [LGPL-3.0](https://github.com/BemiHQ/bemi-django/blob/main/LICENSE).
108+
If you need to modify and distribute the code, please release it to contribute back to the open-source community.
109+
hide_title: true

docs/docusaurus.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ const config: Config = {
119119
label: "MikroORM",
120120
href: "https://github.com/BemiHQ/bemi-mikro-orm",
121121
},
122+
{
123+
label: "Django",
124+
href: "https://github.com/BemiHQ/bemi-django",
125+
},
122126
],
123127
},
124128
{

docs/sidebars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const sidebars: SidebarsConfig = {
6767
collapsed: true,
6868
items: [
6969
'orms/sqlalchemy',
70+
'orms/django',
7071
],
7172
},
7273
],

0 commit comments

Comments
 (0)