Skip to content

Commit 4103d62

Browse files
authored
Update codegen for models and gel generate (#8870)
Similar in spirit to the minimal guide at geldata/gel-python#797 Will need to be greatly expanded in the future.
1 parent 63a1f99 commit 4103d62

File tree

1 file changed

+56
-23
lines changed

1 file changed

+56
-23
lines changed

docs/reference/using/python/api/codegen.rst

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,34 @@ Code Generation
66

77
.. py:currentmodule:: gel
88
9-
The ``gel-python`` package exposes a command-line tool to generate typesafe functions from ``*.edgeql`` files, using :py:mod:`dataclasses` for objects primarily.
9+
The ``gel-python`` package exposes a command-line tool to generate various kinds of type-safe code from EdgeQL queries and your schema:
10+
11+
- ``queries``: typesafe functions from ``*.edgeql`` files, using :py:mod:`dataclasses` for objects primarily.
12+
- ``models``: a programmatic query-builder and Pydantic-based models generator.
1013

1114
.. code-block:: bash
1215
13-
$ gel-py
16+
$ uvx gel generate py/queries
17+
$ uvx gel generate py/models
1418
15-
Or alternatively:
19+
The :gelcmd:`generate` commands supports the same set of :ref:`connection options <ref_cli_gel_connopts>` as the ``gel`` CLI.
1620

17-
.. code-block:: bash
21+
.. code-block::
22+
23+
-I, --instance <instance>
24+
--dsn <dsn>
25+
--credentials-file <path/to/credentials.json>
26+
-H, --host <host>
27+
-P, --port <port>
28+
-b, --branch <branch>
29+
-u, --user <user>
30+
--password
31+
--password-from-stdin
32+
--tls-ca-file <path/to/certificate>
33+
--tls-security <insecure | no_host_verification | strict | default>
1834
19-
$ python -m gel.codegen
35+
Queries
36+
=======
2037

2138
Consider a simple query that lives in a file called ``get_number.edgeql``:
2239

@@ -51,8 +68,8 @@ By default, the generated code uses an ``async`` API. The generator supports add
5168

5269
.. code-block:: bash
5370
54-
$ gel-py --target async # generate async function (default)
55-
$ gel-py --target blocking # generate blocking code
71+
$ gel generate py/queries --target async # generate async function (default)
72+
$ gel generate py/queries --target blocking # generate blocking code
5673
5774
The names of the generated files will differ accordingly: ``{query_filename}_{target}_edgeql.py``.
5875

@@ -63,26 +80,42 @@ It may be preferable to generate a single file containing all the generated func
6380

6481
.. code-block:: bash
6582
66-
$ gel-py --file
83+
$ gel generate py/queries --file
6784
6885
This generates a single file called ``generated_{target}_edgeql.py`` in the root of your project.
6986

70-
Connection
71-
~~~~~~~~~~
87+
Models
88+
======
7289

73-
The ``gel-py`` command supports the same set of :ref:`connection options <ref_cli_gel_connopts>` as the ``gel`` CLI.
90+
The ``models`` generator will generate Pydantic classes and a programmatic query builder. It reflects your full schema, as well as our standard library into functions and Pydantic classes which we've enhanced to make a truly powerful type-safe programmatic data layer.
7491

75-
.. code-block::
92+
.. code-block:: python
7693
77-
-I, --instance <instance>
78-
--dsn <dsn>
79-
--credentials-file <path/to/credentials.json>
80-
-H, --host <host>
81-
-P, --port <port>
82-
-b, --branch <branch>
83-
-u, --user <user>
84-
--password
85-
--password-from-stdin
86-
--tls-ca-file <path/to/certificate>
87-
--tls-security <insecure | no_host_verification | strict | default>
94+
import datetime
95+
from models import User, std
96+
from gel import create_client
97+
98+
def main():
99+
client = create_client()
100+
101+
# Create a new User instance and save it to the database
102+
bob = User(name='Bob', dob=datetime.date(1984, 3, 1))
103+
client.save(bob)
104+
105+
# Select all Users
106+
users = client.query(User)
107+
108+
# Select all users with names like "Bob"
109+
bob_like = client.query(User.filter(lambda u: std.ilike(u.name, '%bob%')))
110+
111+
# Update an object
112+
bob.name = 'Robert'
113+
client.save(bob)
114+
115+
# Delete an object
116+
client.execute(User.filter(id=bob.id).delete())
117+
118+
client.close()
88119
120+
if __name__ == '__main__':
121+
main()

0 commit comments

Comments
 (0)