Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

tunamaguro/sqlc-rust-postgres

Repository files navigation

sqlc-rust-postgres

sqlc plugin for tokio_postgres and postgres

Important

This plugin is no longer maintained. Please use sqlc-gen-rust instead.

Usage

{
  "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"
              }
            ]
          }
        }
      ]
    }
  ]
}

Options

NOTE: This plugin supports json only.

db_crate

The supported values for db_crate are tokio_postgres, postgres, and deadpool_postgres. Default is tokio_postgres.

enum_derives

Strings added here will be included in the generated enum's derive attributes.

row_derives

Strings added here will be included in the generated XXXRow struct's derive attributes.

overrides

By default, this plugin does not support third-party crate types. If you wish to use them, add an entry here.

copy_types

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"
+              }
            ]

Setup develop environment

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

Update sqlc proto

Copy from https://github.com/sqlc-dev/sqlc/blob/main/protos/plugin/codegen.proto

About

sqlc plugin for `rust-postgres`

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •