Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix docs, extract more options by default and bump version #44

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion databasez/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from databasez.core import Database, DatabaseURL

__version__ = "0.9.4"
__version__ = "0.9.5"

__all__ = ["Database", "DatabaseURL"]
18 changes: 14 additions & 4 deletions databasez/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,20 @@ 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", ""}
for param in ["pool_size", "max_overflow"]:
if param in new_query_options:
assert param not in options
options[param] = int(typing.cast(str, new_query_options.pop(param)))
if "pool_recycle" in new_query_options:
assert "pool_recycle" not in options
options["pool_recycle"] = float(
typing.cast(str, new_query_options.pop("pool_recycle"))
)
return database_url.replace(options=new_query_options), options

def json_serializer(self, inp: dict) -> str:
Expand Down
21 changes: 17 additions & 4 deletions docs/connections-and-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,31 @@ 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, int (former name: min_size).
- max_overflow: maximal amount of connections, int (former name: max_size).
- pool_recycle: maximal duration a connection may live, float.


## Connection options as a dictionary

**Databasez** also provides another way of passing the connection options by using dictionaries.
Expand All @@ -139,7 +152,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
4 changes: 4 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

## 0.9.5

### Changed

- Extract more options by default from query options.

### Fixed

- `disconnect_hook` was called too early.
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\
"""
Loading