Add ability to add newtype wrappers to generated types
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 45/100
調査の方向性
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:
- 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.)
- 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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
cornucopia-rs/cornucopia のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
cornucopia-rs/cornucopia#334 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 76/100
cornucopia-rs/cornucopia#333 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
cornucopia-rs/cornucopia#309 · コメント 2 件 ·
-
configuration feature
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
cornucopia-rs/cornucopia#292 ·
-
codegen feature
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
cornucopia-rs/cornucopia#291 ·
cornucopia-rs/cornucopia の issue をすべて見る
似ている issue
-
area:models enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
bug frontend ui
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
aosama/astronomical#1973 ·
メンテナーはふだん 1 日以内に返信