Introduction
EdgeDB DTO is a CLI tool for Python that generates pydantic dataclasses for edgeql queries (EdgeDB query language).
Table of Contents
- Python Code
- Features
- Installation
- Example pipeline
- CLI Options
- Dependencies
- Limitations
- Contributors
- License
Python Code
# main.py
from simple.queries.dto import InsertPerson
from edgedb import create_client
CLIENT = create_client()
def insert_three_people():
rob = InsertPerson(name="Rob")
alice = InsertPerson(name="Alice", best_friend=rob)
tom = InsertPerson(name="Tom", best_friend=alice)
tom.run(executor=CLIENT, transaction=True)
insert_three_people()
Features
- Mix and Match multiple Classes: Missed ORMs while using EdgeDB in Python? Look no further! edgedb-dto supports embedding DTOs in each other.
- Easy, intuitive and in 3 steps:
- Write .edgeql queries.
- run
edgedb-py && edgedb-dtoin the terminal - Import generated classes
- Validation using pydantic:
edgedb-dtogenerates pydantic dataclasses that you can modify as you wish to validate your input data. - Asynchronous and Synchronous Support: Supports both asynchronous and synchronous database calls.
Installation
To install EdgeDB DTO from PyPI, run:
pip install edgedb-dto
Example Pipeline
Define your database
Project Tree
Schema
We have the following simple schema:
# default.esdl
module default {
type Person{
required name: str{
constraint exclusive;
}
best_friend: Person;
}
}
EdgeQL Query
We also have the following simple .edgeql query:
# insert_person.edgeql
insert Person{
name := $name,
best_friend := $best_friend
} unless conflict on .name else (
select Person filter .name = $name
)
Generate DTO Classes
Let's do some Magic ✨
edgedb-py --no-skip-pydantic --target=blocking && edgedb-dto
PS : To do async calls just remove --target option (run edgedb-py --help for more context)
New Project Tree
Generated DTO Class
# insert_person_edgeql_dto.py
from ..insert_person_edgeql import InsertPersonResult, insert_person
@dataclass
class InsertPerson(DTO):
name: str
best_friend: DTO | uuid.UUID | None = None
def _query(self, **kwargs):
return insert_person(**kwargs)
def run(self, executor: Client, transaction: bool = False
) -> InsertPersonResult | None:
return self._run(executor, transaction)
Using our DTO Classes
Now let's populate our database with ease:
# main.py
from simple.queries.dto import InsertPerson
from edgedb import create_client
CLIENT = create_client()
def insert_three_people():
rob = InsertPerson(name="Rob")
alice = InsertPerson(name="Alice", best_friend=rob)
tom = InsertPerson(name="Tom", best_friend=alice)
tom.run(executor=CLIENT, transaction=True)
insert_three_people()
By calling run method on the tom DTO class, we managed to insert all of our data into the database in a single transaction.
CLI Options
--source-directory(-s) [Optional argument]: Source directory containing EdgeQL files.
Default: Edgedb-DTO will scan for the fiels generated by edgedb-py.--output-directory(-o) [Optional argument]: Output directory for generated DTO classes.
Default: the files will be generated in a folder nameddtoin the parent folder of the EdgeQL Python files.
Please note that it's better for type safety to not specify theoutput-directory
Dependencies
python: version=^3.11 is required to run this library.jinja2: For templating the DTO classes.pydantic: For data validation and settings management.edgedb: For interacting with the EdgeDB database.
Limitations
N + 1 Problem
edgedb-dto does not act like a query builder. So the more DTOs you link, the more back and forth calls are made to the database. In the last example, 3 queries to the database are made to complete the transaction.
Contributors
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for edgedb-dto 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| edgedb_dto-1.0.2.tar.gz | 10.9 kB | Without provenance | Details for edgedb_dto-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| edgedb_dto-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for edgedb_dto-1.0.2-py3-none-any.whl |
Total release size: 21.9 kB
Release files / edgedb_dto-1.0.2.tar.gz
| Download URL | edgedb_dto-1.0.2.tar.gz |
|---|---|
| Size | 10.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0c3f78b28a511a764347002e3dcca5bd543c751ccc0505b2c5bb11ffd0cd420d
|
|
BLAKE2b-256 checksum How to use checksums |
d87e2902b5df7988402f487d0440cf7d8c9d72d3454fdab907ad9d5a8f0c71ea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.9 Linux/5.15.154+
|
Release files / edgedb_dto-1.0.2-py3-none-any.whl
| Download URL | edgedb_dto-1.0.2-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
74750dc54db7baaaa2d4e4f53c8c8155270d5bad8200ad74a6495b656aa1a497
|
|
BLAKE2b-256 checksum How to use checksums |
cee22130aa4f28a2202e4e60225a533ba3bdf6616e557ad3c5ccc6e6c455fa00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.9 Linux/5.15.154+
|
Security for edgedb-dto 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.