-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to export SQLAlchemy models from Gel (EdgeDB). (#536)
Create separate intermediate objects to represent links with link properties (because ORMs tend to view them as such). Don't allow crazy names (not usual type of identifiers).Multi properties behave more like plain multi links than anything else (because they have a separate table with the property value in it), so they should be reflected like that establishing a relationship. Reflect Gel modules into Python modules. Handle backlink collisions for SQLAlchemy model generator. Add tests that setup a Gel database and then generate the SQLAlchemy models from it. The individual tests use a SQLAlchemy session to access the database using postgres protocol.Add a CLI for generating ORM models. Run `gel-orm --help` for more details
- Loading branch information
1 parent
7d50d0b
commit 645fce0
Showing
16 changed files
with
2,069 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[flake8] | ||
ignore = B008,B023,B306,E203,E402,E731,D100,D101,D102,D103,D104,D105,W503,W504,E252,F999,F541 | ||
exclude = .git,__pycache__,build,dist,.eggs | ||
exclude = .git,__pycache__,build,dist,.eggs,generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# | ||
# This source file is part of the EdgeDB open source project. | ||
# | ||
# Copyright 2024-present MagicStack Inc. and the EdgeDB authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
|
||
import argparse | ||
|
||
import gel | ||
|
||
from gel.codegen.generator import _get_conn_args | ||
from .introspection import get_schema_json | ||
from .sqla import ModelGenerator | ||
|
||
|
||
class ArgumentParser(argparse.ArgumentParser): | ||
def error(self, message): | ||
self.exit( | ||
2, | ||
f"error: {message:s}\n", | ||
) | ||
|
||
|
||
parser = ArgumentParser( | ||
description="Generate Python ORM code for accessing a Gel database." | ||
) | ||
parser.add_argument( | ||
"orm", | ||
choices=['sqlalchemy', 'django'], | ||
help="Pick which ORM to generate models for.", | ||
) | ||
parser.add_argument("--dsn") | ||
parser.add_argument("--credentials-file", metavar="PATH") | ||
parser.add_argument("-I", "--instance", metavar="NAME") | ||
parser.add_argument("-H", "--host") | ||
parser.add_argument("-P", "--port") | ||
parser.add_argument("-d", "--database", metavar="NAME") | ||
parser.add_argument("-u", "--user") | ||
parser.add_argument("--password") | ||
parser.add_argument("--password-from-stdin", action="store_true") | ||
parser.add_argument("--tls-ca-file", metavar="PATH") | ||
parser.add_argument( | ||
"--tls-security", | ||
choices=["default", "strict", "no_host_verification", "insecure"], | ||
) | ||
parser.add_argument( | ||
"--out", | ||
help="The output directory for the generated files.", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--mod", | ||
help="The fullname of the Python module corresponding to the output " | ||
"directory.", | ||
required=True, | ||
) | ||
|
||
|
||
def main(): | ||
args = parser.parse_args() | ||
# setup client | ||
client = gel.create_client(**_get_conn_args(args)) | ||
spec = get_schema_json(client) | ||
|
||
match args.orm: | ||
case 'sqlalchemy': | ||
gen = ModelGenerator( | ||
outdir=args.out, | ||
basemodule=args.mod, | ||
) | ||
gen.render_models(spec) | ||
case 'django': | ||
print('Not available yet. Coming soon!') |
Oops, something went wrong.