sqlc plugin for tokio_postgres and postgres
Important
This plugin is no longer maintained. Please use sqlc-gen-rust instead.
{
"version": "2",
"plugins": [
{
"name": "rust-postgres",
"wasm": {
"url": "https://github.com/tunamaguro/sqlc-rust-postgres/releases/download/v0.1.4/sqlc-rust-postgres.wasm",
"sha256": "b66264569d0d703ebd9ebd90f98381c221c03b3f5255ca277be306b578b91d39"
}
}
],
"sql": [
{
"schema": "examples/custom_type/src/schema.sql",
"queries": "examples/custom_type/src/query.sql",
"engine": "postgresql",
"codegen": [
{
"out": "examples/custom_type/src",
"plugin": "rust-postgres",
"options": {
"db_crate": "tokio_postgres",
"enum_derives": [
"PartialEq"
],
"row_derives": [
"PartialEq"
],
"overrides": [
{
"db_type": "voiceactor",
"rs_type": "crate::VoiceActor"
},
{
"db_type": "money",
"rs_type": "postgres_money::Money"
},
{
"db_type": "pg_catalog.numeric",
"rs_type": "rust_decimal::Decimal"
}
]
}
}
]
}
]
}
NOTE: This plugin supports json only.
The supported values for db_crate
are tokio_postgres
, postgres
, and deadpool_postgres
. Default is tokio_postgres
.
- Example of
tokio_postgres
: https://github.com/tunamaguro/sqlc-rust-postgres/blob/main/examples/authors/src/queries.rs - Example of
postgres
: https://github.com/tunamaguro/sqlc-rust-postgres/blob/main/examples/jets/src/queries.rs - Example of
deadpool_postgres
: https://github.com/tunamaguro/sqlc-rust-postgres/blob/main/examples/ondeck/src/queries.rs
Strings added here will be included in the generated enum
's derive attributes.
Strings added here will be included in the generated XXXRow
struct's derive attributes.
By default, this plugin does not support third-party crate types. If you wish to use them, add an entry here.
Specifies additional types that should be passed by value instead of reference for better performance. Database-generated enums and primitive types (i32, i64, bool, etc.) are automatically optimized.
"copy_types": [
"postgres_money::Money",
"uuid::Uuid"
]
This optimization reduces function call overhead by avoiding unnecessary references for copy-cheap types.
When an unsupported DB type is encountered, you might see an error like:
$ sqlc generate
# package rust-postgres
error generating code: thread 'main' panicked at src/query.rs:308:17:
Cannot find rs_type that matches column type of `pg_catalog.numeric`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
In this case, add the following entry to overrides:
"overrides": [
{
"db_type": "voiceactor",
"rs_type": "crate::VoiceActor"
},
{
"db_type": "money",
"rs_type": "postgres_money::Money"
},
+ {
+ "db_type": "pg_catalog.numeric",
+ "rs_type": "rust_decimal::Decimal"
+ }
]
Install protoc
.
sudo apt-get install protobuf-compiler
Ref: https://docs.rs/prost-build/latest/prost_build/#sourcing-protoc
Install just and run setup
cargo install just
just setup-tools
Run sqlc
just generate
Copy from https://github.com/sqlc-dev/sqlc/blob/main/protos/plugin/codegen.proto