No project description provided
Project description
pg-rls-sqlalchemy
Work in progress.
SQLAlchemy and Alembic support for Postgres features like:
- Row Level Security (RLS)
- Policies
Built on top of alembic_utils but provides a more usable interface and a few missing features
Installation
pip install pg-rls-sqlalchemy
OR
poetry add pgalchemy
Policy and Row Level Security
Using RLS BaseModel
Recommended most projects. This is for projects with majority of tables using RLS which will also be almost all new projects using this library.
from sqlalchemy.orm import declarative_base
from pgalchemy import Policy, PolicyType, PolicyCommands, rls_base
BaseModel = rls_base(declarative_base())
class MyModel(BaseModel):
...
Policy("pol_my_models_select_primary", on=MyModel, as_=PolicyType.PERMISSIVE, for_=PolicyCommands.SELECT, using="user_id == auth.uid()")
Policy("pol_my_models_delete_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.DELETE, using="user_id == auth.uid()")
Policy("pol_my_models_update_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.UPDATE, using="user_id == auth.uid()", with_check="user_id == auth.uid()")
Policy("pol_my_models_update_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.INSERT, with_check="user_id == auth.uid()")
Using RLS Decorator
Only intended for projects with majority of tables without RLS enabled. Usually only for existing projects with most tables not protected using RLS that are only using RLS for a niche use case
This is not recommended for other use cases as it makes it easy for a developer to forget to enable RLS and expose a security vulnerability.
from sqlalchemy.orm import declarative_base
from pgalchemy import , rls_base, Policy, PolicyType, PolicyCommands
BaseModel = declarative_base()
@rls()
# Equivalent to:
# @policy(Policy("pol_my_models_primary", as_=PolicyType.PERMISSIVE, for_=PolicyCommands.ALL, using="user_id == auth.uid()", with_check="user_id == auth.uid()"))
class MyModel(BaseModel):
...
Policy("pol_my_models_select_primary", on=MyModel, as_=PolicyType.PERMISSIVE, for_=PolicyCommands.SELECT, using="user_id == auth.uid()")
Policy("pol_my_models_delete_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.DELETE, using="user_id == auth.uid()")
Policy("pol_my_models_update_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.UPDATE, using="user_id == auth.uid()", with_check="user_id == auth.uid()")
Policy("pol_my_models_update_primary", on=MyModel,as_=PolicyType.PERMISSIVE, for_=PolicyCommands.INSERT, with_check="user_id == auth.uid()")
Functions
Using Python Functions
from pgalchemy.functions import sql_function
from pgalchemy.types import ReturnTypedExpression
@sql_function(schema='test')
def get_thing(id: int):
return ReturnTypedExpression[MyModel](
select(MyModel).where(MyModel.id == id)
)
Using SQL File with empty python function
from pgalchemy.functions import sql_function
@sql_function(schema='test', path='../functions/get_thing.sql')
def get_thing(id: int) -> MyModel:
pass
Using SQL File with metadata
import inspect
from pgalchemy.functions import Function
Function(
schema='test',
path='../functions/get_thing.sql',
returns=MyModel,
parameters=[inspect.Parameter(name='id', annotation=int)]
)
Views
Using Python Functions
from pgalchemy.views import sql_view
from pgalchemy.types import ReturnTypedExpression
@sql_view(schema='test')
def my_view():
return select(MyModel).where(MyModel.id == id)
Using SQL File
import inspect
from pgalchemy.views import View
View(
schema='test',
path='../views/my_view.sql',
)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pgalchemy-0.1.6.tar.gz.
File metadata
- Download URL: pgalchemy-0.1.6.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.1 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7bcd599ca1040cd1b42737dcd44bb6a0885a87dc8f1cf124ad810d1ccd01515
|
|
| MD5 |
650becd4acbad4f05bb8b0e7df57a73c
|
|
| BLAKE2b-256 |
863cfd6ca78a73168a9073fc741302784c57ba9e414f3bc1118b4cdf615cd4d6
|
File details
Details for the file pgalchemy-0.1.6-py3-none-any.whl.
File metadata
- Download URL: pgalchemy-0.1.6-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.1 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22c252165b1027bab0f63aa1cc2fac35d0601fb4ad02cb40e4de43fdd680a60c
|
|
| MD5 |
880d26161e29f3c39bb184ce54fa444a
|
|
| BLAKE2b-256 |
9f6d648b6594b93cad3d3da8c2dd82a86fc5cbb1e7c77af988a5ca09395c3ebd
|