Add ability to add newtype wrappers to generated types

オープン
#330 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
45/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
postgresql, rust
領域
databases

調査の方向性

Start by reading the schema, query, and configuration parsing and code generation entry points to see how database column types become generated Rust types. The issue proposes supporting custom newtype wrappers across a table's generated queries, but does not name specific files or tests. Done means a wrapper can be configured and used consistently in generated query types.

索引モデルが issue の本文から書いたものです。

説明

Describe the problem

For example, if we have two tables in our database called Foo and Bar, respectively, where each of them has int4 ids, there is nothing preventing us from writing code that takes the id from a Foo and uses it in the context of a Bar.

In other contexts with Rust code we can solve this by adding a thin newtype wrapper around i32 like pub struct FooId(pub i32) and use that instead of i32 in the context of a Foo (and analogously for Bar). That way, the type system can guarantee that we don't confuse them.

Some other examples of when this could be useful:

  1. an Email struct that is a thin wrapper around String, but that enforces the invariants expected of an email, so the type system encodes that requirement for us. (The way I'd expect this to be implemented is that there is a TryFrom impl for the Email struct so that application code needs to handle a string not being a valid email, but the FromSql impl for Email is infallible, i.e. we assume that all writes to the database match the invariant we establish in the application code.)
  2. a PasswordHash struct that has methods on it for validating whether a provided string matches the hash, so that authentication could be done in a very idiomatic way on the User type returned by Cornucopia.
Describe the solution you'd like

It would be very nice to be able to add newtype wrappers to the schema, to the queries, or to the config. Generally, I'd like to be able to specify some custom type ty to use in place of some column across an entire table. All of the generated queries that involve this table should then use ty in place of the column.

Alternatives considered

No response

Additional context

I searched the issues and didn't find anything on this; please let me know if this is something you'd be interested in! I'd be happy to contribute code changes if this is a feature that you would be interested in having.

I've been playing around with other ways of interacting with SQL databases in Rust for the last few months, and I think that they could all use work on this pattern. It'd be a huge quality of life improvement to be able to use these patterns in my code when interacting with databases. I otherwise really like the approach that Cornucopia takes in general, so it would make me very happy to help with this if desired!

Importance

Would make my life easier

主要言語
Rust
スター
1.2k
フォーク
77
平均マージ
5日 12時間
マージ済み PR(30日)
1

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cornucopia-rs/cornucopia のほかの issue

cornucopia-rs/cornucopia の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。