Skip to main content

EdgeDB inspired ORM for PostgreSQL

Project description

FastDTO

FastDTO is a Python EdgeDB inspired open source ORM framework designed to simplify database interactions by parsing user-defined SQL queries, generating Data Transfer Objects (DTOs), and creating Python functions to execute the queries and return the DTOs.

Installation

You can install FastDTO via pip:

pip install fastdto

or poetry:

poetry add fastdto

Getting started

First of all, you'll need to setup template directory as in alembic for working with your database schema and queries. You easily can do it, via CLI command:

fastdto init

For now you should have something like this in your project directory:

YourProjectName/
├── dbschema/
│   ├── scripts/
│   │   ├──...
│   ├── dbschema.py
│   ├── README.md
...

In dbschema.py file you need to define your database schema, you can use README.md file, in section Schema for more definitive guide, how to do it, or check examples.

Writing queries

Unlike other popular ORMs, in FastDTO, users describe SQL queries not in Python files using a query builder, but in pure SQL within separate .sql files. This is driven by the framework's philosophy to provide developers with complete freedom and control over SQL queries, shielding them from inefficient queries generated by query builders and other pitfalls of modern ORM frameworks.

Once you know the SQL query you need, you can create a file named your_query_name.sql in the db/schema/scripts/ directory.

An example of your project directory structure will now look like this:

YourProjectName/
├── dbschema/
│   ├── scripts/
│   │   ├──your_query_name.sql
│   ├── dbschema.py
│   ├── README.md
...

Generating functions

Now begins the most enjoyable part of our work. After the arduous process of writing and debugging the SQL query, we are ready to wrap it in Python. For this, you will need to run just one CLI command:

fastdto generate

After these magic words, the framework will generate an init.py file in the dbschema/scripts/ directory. This file will contain the ready-to-use DTOs and asynchronous Python functions.

As example, for this SQL query:

SELECT number, owner_id FROM cars

FastDTO will generate this pair DTO and function:

class MyQueryResult(FastDTOModel):
    number: int
    owner_id: int


async def my_query(
    executor: IAsyncExecutor,
) -> list[MyQueryResult]:
    result = await executor.execute(
        """
        SELECT cars.number AS number, cars.owner_id AS owner_id FROM cars AS cars
        """,
    )
    return [MyQueryResult.from_list(row) for row in result]

Let's break it down.

  1. MyQueryResult is Pydantic BaseModel subclass with just one extra method from_list, defined by FastDTOModel class.
  2. my_query is a async Python function that takes our query and executes its in database.
  3. IAsyncExecutor - interface with 1 method - execute, provided by FastDTO. You can use predefined executors like SQLAlchemy or write one by yourself.

Using functions

As for example, here the code snippet, how to use my_query function from above with SQLAlchemy:

import asyncio
from dbschema.scripts import my_query
from fastdto.connection.sqlalchemy import SqlAlchemyAsyncExecutor  # Predefined SQLAlchemy executor


async def main():
    from sqlalchemy.ext.asyncio import create_async_engine

    engine = create_async_engine(
        "postgresql+asyncpg://user:password@localhost:5432/test"
    )
    async with engine.connect() as conn:
        result = await my_query(
            executor=SqlAlchemyAsyncExecutor(conn),
        )
        print(result)
        # result = [MyQueryResult(number=1, owner_id=1), MyQueryResult(number=2, owner_id=2)]

asyncio.run(main())

License

FastDTO is distributed under the MIT license.

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

fastdto-0.2.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastdto-0.2.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file fastdto-0.2.0.tar.gz.

File metadata

  • Download URL: fastdto-0.2.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.6 Darwin/21.6.0

File hashes

Hashes for fastdto-0.2.0.tar.gz
Algorithm Hash digest
SHA256 21f42c58ffc1cca3d324908cd7a7c43715a27a6372b5dc072298e970aef39c3f
MD5 621a7ba4342d87febd30268c0b57596e
BLAKE2b-256 2f990a24bf0042fa59b0dece696182634d4e3bfade4652510f04ef7167170ba5

See more details on using hashes here.

File details

Details for the file fastdto-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fastdto-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.6 Darwin/21.6.0

File hashes

Hashes for fastdto-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf93eb71793f59db3af44c196a90a1daadd892f7542232f713e197ba5c782e82
MD5 926ed98d022e8b37ec721d59ee26e479
BLAKE2b-256 962517ced15b09d6d360a581d5e8c99b0c34dd413ed46dd71e82c8cb42effff8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page