Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- update docs
- retrieve some more options from query
  • Loading branch information
devkral committed Aug 19, 2024
1 parent c02244d commit c21362b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
15 changes: 11 additions & 4 deletions databasez/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,17 @@ def extract_options(
if self.default_isolation_level is not None:
options.setdefault("isolation_level", self.default_isolation_level)
new_query_options = dict(database_url.options)
if "ssl" in new_query_options:
assert "ssl" not in options
ssl = typing.cast(str, new_query_options.pop("ssl"))
options["ssl"] = {"true": True, "false": False}.get(ssl.lower(), ssl.lower())
for param in ["ssl", "echo", "echo_pool"]:
if param in new_query_options:
assert param not in options
value = typing.cast(str, new_query_options.pop(param))
options[param] = value.lower() in {"true", ""}
if "pool_size" in new_query_options:
assert "pool_size" not in options
options["pool_size"] = int(new_query_options["pool_size"])
if "pool_recycle" in new_query_options:
assert "pool_recycle" not in options
options["pool_recycle"] = int(new_query_options["pool_recycle"])
return database_url.replace(options=new_query_options), options

def json_serializer(self, inp: dict) -> str:
Expand Down
20 changes: 16 additions & 4 deletions docs/connections-and-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,30 @@ and for configuring the connection pool.
# Use an SSL connection.
database = Database('postgresql+asyncpg://localhost/example?ssl=true')

# Use a connection pool of between 5-20 connections.
database = Database('mysql+aiomysql://localhost/example?min_size=5&max_size=20')
# Use an SSL connection and configure pool
database = Database('postgresql+asyncpg://localhost/example?ssl=true&pool_size=20')
```

You can also use keyword arguments to pass in any connection options.
Available keyword arguments may differ between database backends. Keywords can be used like in create_async_engine (most are passed through).
This means also the keyword extraction works like in sqlalchemy

```python
database = Database('postgresql+asyncpg://localhost/example', ssl=True, min_size=5, max_size=20)
database = Database('postgresql+asyncpg://localhost/example', ssl=True, pool_size=20)
```

Note: not all query values are morphed into kwargs arguments.
Options which will be transformed are in `databasez/sqlalchemy.py` in `extract_options`

Some transformed options are:

- ssl: enable ssl
- echo: enable echo
- echo_pool: enable echo for pool
- pool_size: maximal amount of connections
- pool_recycle: maximal duration a connection may live


## Connection options as a dictionary

**Databasez** also provides another way of passing the connection options by using dictionaries.
Expand All @@ -139,7 +151,7 @@ Databasez expects a python dictionary like object with the following structure.
"user": ...,
"password": ...,
"database": ...,
"options": {
"options": { # only query
"driver": ... # In case of MSSQL
"ssl": ...
}
Expand Down
20 changes: 6 additions & 14 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Databasez was built for Python 3.8+ and on the top of the newest **SQLAlchemy 2*
simple asyncio support for a range of databases.

### Special notes
This package couldn't exist without [Databases](https://www.encode.io/databasex/) and the continuous work
This package couldn't exist without [Databases](https://www.encode.io/databases/) and the continuous work
done by the amazing team behind it. For that reason, thank you!

## Installation
Expand All @@ -69,24 +69,16 @@ done by the amazing team behind it. For that reason, thank you!
$ pip install databasez
```

If you are interested in using the [test client](./test-client.md), you can also install:

```shell
$ pip install databasez[testing]
```
If you are interested in using the [test client](./test-client.md), you can just use it.

## What does databasez support at the moment

Databasez currently supports `sqlite`, `postgres`, `mysql` and `sql server`. More drivers can and
will be added in the future.
Databasez currently supports nearly all async drivers of sqlalchemy.

Database drivers supported are:
If this is not enough there are two special dialects with restricted features:

* [asyncpg][asyncpg] - For postgres.
* [aiomysql][aiomysql] - For MySQL/MariaDB.
* [asyncmy][asyncmy] - For MySQL/MariaDB.
* [aiosqlite][aiosqlite] - For SQLite.
* [aioodbc][aioodbc] - For MSSQL (SQL Server).
- jdbc: can load nearly any jdbc driver
- dbapi2: can load nearly any dbapi2 driver (python standard), async as well as sync.

### Driver installation

Expand Down
3 changes: 2 additions & 1 deletion docs_src/quickstart/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from databasez import Database

database = Database("sqlite+aiosqlite:///example.db")
# non captured arguments are passed through to create_async_engine
database = Database("sqlite+aiosqlite:///example.db", echo=True)
await database.connect()

# Create a table.
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ TEST_DATABASE_URLS = """
sqlite+aiosqlite:///testsuite.sqlite3,\
mysql+aiomysql://username:passwsss*1348394#@localhost:3306/testsuite,\
mysql+asyncmy://username:passwsss*1348394#@localhost:3306/testsuite,\
postgresql+psycopg://username:passwsss*1348394#@localhost:5432/testsuite,\
postgresql+psycopg://username:passwsss*1348394#@localhost:5432/testsuite&pool_size=5,\
postgresql+asyncpg://username:passwsss*1348394#@localhost:5432/testsuite,\
mssql+aioodbc://sa:Mssql123mssql-@localhost:1433/master?driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes&Encrypt=Optional\
mssql+aioodbc://sa:Mssql123mssql-@localhost:1433/master?driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes&Encrypt=Optional&pool_recycle=10\
"""

0 comments on commit c21362b

Please sign in to comment.