Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Rust proc macro to generate Rust code from Prisma schema files.

License

NotificationsYou must be signed in to change notification settings

ShaunSHamilton/prisma-rust-schema

Repository files navigation

Usage

Note

Currently, this package is not being updated on the crates.io registry, because this crate depends on a crate on GitHub.

  1. Install the library
cargo add --git https://github.com/ShaunSHamilton/prisma-rust-schema.git
  1. Use the proc-macro:
use prisma_rust_schema::import_types;// Relative to `Cargo.toml`import_types!("./prisma/schema.prisma");// Or, use import options:import_types!(    schema_paths =["./prisma/schema.prisma"],    derive =[Debug,Clone, serde::Deserialize, serde::Serialize],// Optional, defaults to no derive    include =["User","Post"],// Optional, defaults to all models    prefix ="MyPrefix",// Optional, defaults to no prefix    patch =[structMyPrefixUser{        existing_field:MyPrefixPost}]);// If `schema_path` implements `IntoUrl`, it is fetched.import_types!("https://raw.githubusercontent.com/ShaunSHamilton/prisma-rust-schema/refs/heads/master/prisma/schema.prisma");

Options

OptionExampleDescription
@prs.rename = <new_name>@prs.rename = usernameRename the field in the generated Rust struct.
@prs.skip@prs.skipSkip the field in the generated Rust struct.
@prs.type = <type_override>@prs.type = usizeOverride the type of the field in the generated Rust struct.
@prs.visibility = <visibility>@prs.visibility = publicOverride the visibility (public, private, protected) of the field in the generated Rust struct.
@prs.derive = <trait>@prs.derive = Debug,Clone,serde::DeserializeFully-qualified, comma-separated derive attributes for the generated Rust struct.

Example

/// User model documentation/// @prs.visibility = protected/// @prs.derive = Debug,Clone,serde::Deserialize,serde::SerializemodelUser {/// User ID/// @prs.rename = `user_id`/// @prs.type = `usize`idInt@id@default(autoincrement())/// User name/// @prs.skipnameString?/// User emailsemailsString[]/// User ageageInt?@default(0)}/// Post model with only defaults/// @prs.derive = Debug,Clone,serde::Deserialize,serde::Serializemodelpost {idInt@id@default(autoincrement())titleStringcontentJsonpublishedBoolean@default(false)publishedAtDateTime?@default(now())}

Becomes:

#[doc ="User model documentation"]#[derive(Debug,Clone, serde::Deserialize, serde::Serialize)]pub(crate)structUser{#[doc ="User ID"]#[serde(rename ="user")]pub(crate)user_id:usize,#[doc ="User emails"]pub(crate)emails:Vec<String>,/// User agepub(crate)age:Option<i32>,}#[doc ="Post model with only defaults"]#[derive(Debug,Clone, serde::Deserialize, serde::Serialize)]pubstructPost{pubid:i32,pubtitle:String,pubcontent: serde_json::Value,pubpublished:bool,pubpublished_at:Option<chrono::DateTime<chrono::Utc>>,}

Constraints

This package is tested to work withprisma@^6. It does work withprisma@^5 but there are no native types such as@db.ObjectId. So,@prs.type must be used, otherwise the type will be the.prisma type.

VersionPrisma SchemaRust Type
5.x
model User {  id @db.ObjectId}
pub struct User {    id: String}
6.x
model User {  id @db.ObjectId}
pub struct User {    id: bson::oid::ObjectId}

Currently, it is up tothe user to ensure all types have valid derive attributes. Specifically, if therename attribute is needed, thenserde::Deserialize andserde::Serialize must be used. The generator will not add them automatically.

About

A Rust proc macro to generate Rust code from Prisma schema files.

Resources

License

Contributing

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp