Skip to content

Commit

Permalink
Add a CLI for generating ORM models.
Browse files Browse the repository at this point in the history
Run `gel-orm --help` for more details.
  • Loading branch information
vpetrovykh committed Nov 27, 2024
1 parent 76f4ada commit ae0d597
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
86 changes: 86 additions & 0 deletions gel/orm/cli.py
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(
"--dir",
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.dir,
basemodule=args.mod,
)
gen.render_models(spec)
case 'django':
print('Not available yet. Coming soon!')
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def finalize_options(self):
"console_scripts": [
"edgedb-py=gel.codegen.cli:main",
"gel-py=gel.codegen.cli:main",
"gel-orm=gel.orm.cli:main",
]
}
)

0 comments on commit ae0d597

Please sign in to comment.