|
| 1 | +# Async PostgreSQL driver for Python written in Rust. |
| 2 | + |
| 3 | +Driver for PostgreSQL written fully in Rust and exposed to Python. |
| 4 | +*Normal documentation is in development.* |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +You can install package with `pip` or `poetry`. |
| 9 | + |
| 10 | +poetry: |
| 11 | +```bash |
| 12 | +> poetry add psql-rust-driver |
| 13 | +``` |
| 14 | +pip: |
| 15 | +```bash |
| 16 | +> pip install psql-rust-driver |
| 17 | +``` |
| 18 | + |
| 19 | +Or you can build it by yourself. To do it, install stable rust and [maturin](https://github.com/PyO3/maturin). |
| 20 | +``` |
| 21 | +> maturin develop --release |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | +Usage is as easy as possible. |
| 26 | +Create new instance of PSQLPool, startup it and start querying. |
| 27 | +```python |
| 28 | +from typing import Any |
| 29 | +import asyncio |
| 30 | + |
| 31 | +from rust_psql_driver import PSQLPool |
| 32 | + |
| 33 | + |
| 34 | +db_pool = PSQLPool( |
| 35 | + username="postgres", |
| 36 | + password="pg_password", |
| 37 | + host="localhost", |
| 38 | + port=5432, |
| 39 | + db_name="postgres", |
| 40 | + max_db_pool_size=2, |
| 41 | +) |
| 42 | + |
| 43 | +async def main() -> None: |
| 44 | + await db_pool.startup() |
| 45 | + |
| 46 | + res: list[dict[str, Any]] = await db_pool.execute( |
| 47 | + "SELECT * FROM users", |
| 48 | + ) |
| 49 | + |
| 50 | + print(res) |
| 51 | + # You don't need to close Database Pool by yourself, |
| 52 | + # rust does it instead. |
| 53 | + |
| 54 | +``` |
| 55 | + |
| 56 | +## Query parameters |
| 57 | +You can pass parameters into queries. |
| 58 | +Parameters can be passed in any `execute` method as the second parameter, it must be a list. |
| 59 | +Any placeholder must be marked with `$< num>`. |
| 60 | + |
| 61 | +```python |
| 62 | + res: list[dict[str, Any]] = await db_pool.execute( |
| 63 | + "SELECT * FROM users WHERE user_id = $1 AND first_name = $2", |
| 64 | + [100, "RustDriver"], |
| 65 | + ) |
| 66 | +``` |
| 67 | + |
| 68 | +Support all types from Python is in development, but there are support for all basic types. More you can find here: ... |
| 69 | + |
| 70 | +## Transactions |
| 71 | +Of course it's possible to use transactions with this driver. |
| 72 | +It's as easy as possible and sometimes it copies common functionality from PsycoPG and AsyncPG. |
| 73 | + |
| 74 | +### You can use transactions as async context managers |
| 75 | +By default async context manager only begins and commits transaction automatically. |
| 76 | +```python |
| 77 | +from typing import Any |
| 78 | +import asyncio |
| 79 | + |
| 80 | +from rust_psql_driver import PSQLPool, IsolationLevel |
| 81 | + |
| 82 | + |
| 83 | +db_pool = PSQLPool() |
| 84 | + |
| 85 | +async def main() -> None: |
| 86 | + await db_pool.startup() |
| 87 | + |
| 88 | + transaction = await db_pool.transaction( |
| 89 | + isolation_level=IsolationLevel.Serializable, |
| 90 | + ) |
| 91 | + |
| 92 | + async with transaction: |
| 93 | + res: list[dict[str, Any]] = await transaction.execute( |
| 94 | + "SELECT * FROM users", |
| 95 | + ) |
| 96 | + |
| 97 | + print(res) |
| 98 | + # You don't need to close Database Pool by yourself, |
| 99 | + # rust does it instead. |
| 100 | +``` |
| 101 | + |
| 102 | +### Or you can control transaction fully on your own. |
| 103 | +```python |
| 104 | +from typing import Any |
| 105 | +import asyncio |
| 106 | + |
| 107 | +from rust_psql_driver import PSQLPool, IsolationLevel |
| 108 | + |
| 109 | + |
| 110 | +db_pool = PSQLPool() |
| 111 | + |
| 112 | +async def main() -> None: |
| 113 | + await db_pool.startup() |
| 114 | + |
| 115 | + transaction = await db_pool.transaction( |
| 116 | + isolation_level=IsolationLevel.Serializable, |
| 117 | + ) |
| 118 | + |
| 119 | + await transaction.begin() |
| 120 | + res: list[dict[str, Any]] = await transaction.execute( |
| 121 | + "INSERT INTO users VALUES ($1)", |
| 122 | + ["Some data"], |
| 123 | + ) |
| 124 | + # You must commit the transaction by your own |
| 125 | + # or your changes will be vanished. |
| 126 | + await transaction.commit() |
| 127 | + |
| 128 | + print(res) |
| 129 | + # You don't need to close Database Pool by yourself, |
| 130 | + # rust does it instead. |
| 131 | +``` |
| 132 | + |
| 133 | +## Extra Types |
| 134 | +Sometimes it's impossible to identify which type user tries to pass as a argument. But Rust is a strongly typed programming language so we have to help. |
| 135 | + |
| 136 | +| Extra Type in Python | Type in PostgreSQL | Type in Rust | |
| 137 | +| ------------- | ------------- | ------------- |
| 138 | +| SmallInt | SmallInt | i16 | |
| 139 | +| Integer | Integer | i32 | |
| 140 | +| BigInt | BigInt | i64 | |
| 141 | +| PyUUID | UUID | Uuid | |
| 142 | +| PyJSON | JSON, JSONB | Value | |
| 143 | + |
| 144 | +```python |
| 145 | +from typing import Any |
| 146 | +import asyncio |
| 147 | +import uuid |
| 148 | + |
| 149 | +from rust_psql_driver import PSQLPool |
| 150 | + |
| 151 | +from psql_rust_driver.extra_types import ( |
| 152 | + SmallInt, |
| 153 | + Integer, |
| 154 | + BigInt, |
| 155 | + PyUUID, |
| 156 | + PyJSON, |
| 157 | +) |
| 158 | + |
| 159 | + |
| 160 | +db_pool = PSQLPool() |
| 161 | + |
| 162 | +async def main() -> None: |
| 163 | + await db_pool.startup() |
| 164 | + |
| 165 | + res: list[dict[str, Any]] = await db_pool.execute( |
| 166 | + "INSERT INTO users VALUES ($1, $2, $3, $4, $5)", |
| 167 | + [ |
| 168 | + SmallInt(100), |
| 169 | + Integer(10000), |
| 170 | + BigInt(9999999), |
| 171 | + PyUUID(uuid.uuid4().hex), |
| 172 | + PyJSON( |
| 173 | + [ |
| 174 | + {"we": "have"}, |
| 175 | + {"list": "of"}, |
| 176 | + {"dicts": True}, |
| 177 | + ], |
| 178 | + ) |
| 179 | + ] |
| 180 | + ) |
| 181 | + |
| 182 | + print(res) |
| 183 | + # You don't need to close Database Pool by yourself, |
| 184 | + # rust does it instead. |
| 185 | + |
| 186 | +``` |
0 commit comments