Skip to content

AlexanderHott/sqlc-gen-better-python

 
 

Repository files navigation

sqlc-gen-better-python

Codecov Python Version from PEP 621 TOML Ruff

A WASM plugin for SQLC allowing the generation of Python code.

Note

This is currently being worked on. It is far from being ready for any kind of release, let alone a stable one.
Please wait for the v1 release; before that, this plugin is likely to not work.

Example Config

# filename: sqlc.yaml
version: "2"
plugins:
  - name: python
    wasm:
      url: https://github.com/rayakame/sqlc-gen-better-python/releases/download/v0.3.1/sqlc-gen-better-python.wasm
      sha256: 6e2a6d4d4a2320e32ebc25bf718342d97d2de75fc6fd00bf196ca3e5edca6ee8
sql:
  - engine: "postgresql"
    queries: "query.sql"
    schema: "schema.sql"
    codegen:
      - out: "app/db"
        plugin: python
        options:
          package: "db"
          emit_init_file: true
          sql_driver: "asyncpg"
          model_type: "msgspec"

More options at the sqlc config reference

Configuration Options

Name Type Required Description
package string yes The name of the package where the generated files will be located
emit_init_file bool yes If set to to false there will be no __init__.py file created in the package that you specified. Only set this to false if you know that you already have a __init__.py file otherwise the generated code wont work.
sql_driver string no The name of the sql driver you want to use. Defaults to aiosqlite. Valid options are listed here
model_type string no The model type you want to use. This can be one of dataclass, msgspec or attrs. Defaults to dataclass
initialisms list[string] no An array of initialisms to upper-case. For example, app_id becomes AppID. Defaults to ["id"].
emit_exact_table_names bool no If true, model names will mirror table names. Otherwise sqlc attempts to singularize plural table names.
emit_classes bool no If true, every query function will be put into a class called Querier. Otherwise every function will be a standalone function.
inflection_exclude_table_names list[string] no An array of table names that should not be turned singular. Only applies if emit_exact_table_names is false.
omit_unused_models bool no If set to true and there are models/tables that are not used in any query, they wont be turned into models.
docstrings string no If set, there will be docstrings generated in the selected format. This can be one of google, numpy, pep257 and none. none will not generate any docstrings.
docstrings_emit_sql bool no If set to false the SQL code for each query wont be included in the docstrings. This defaults to true but is not used when docstrings is not set or set to none
query_parameter_limit integer no Not yet implemented.
debug bool no If set to true, there will be debug logs generated into a log.txt file when executing sqlc generate. Defaults to false

Feature Support

Every sqlc macro is supported. The supported query commands depend on the SQL driver you are using, supported commands are listed below.

Every :batch* command is not supported by this plugin and probably will never be.

Prepared Queries are not planned for the near future, but will be implemented sooner or later

Note

Asyncpg only has very bad support until now. It doesn't support :execresult, :execrows and :execlastid

:exec :execresult :execrows :execlastid :many :one :copyfrom
aiosqlite yes yes yes yes yes yes no
sqlite3 yes yes yes yes yes yes no
asyncpg yes no no no yes yes no
psycopg2 no no no no no no no
mysql no no no no no no no

Development

A roadmap of what is planned & worked on can be found here.

Contributions are very welcome, for more information and help please read the contribution guidelines.

Changelog

Can be found here

Credits

Because of missing documentation about creating these plugins, this work is heavily inspired by:

Special thanks to tandemdude for answering my questions on discord.

About

SQLC plugin adding support for Python code generation

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 87.3%
  • Go 12.1%
  • Other 0.6%