Movatterモバイル変換


[0]ホーム

URL:


Getting Started
Setup

Setup

If you have completed theinstallation steps and setup thecargo prisma <command> alias,you are ready to add the Prisma Client Rust generator to yourPrisma schema (opens in a new tab).

A common file layout is to give the schema and migrations their own folder:

Cargo.tomlsrc/    main.rsprisma/    schema.prisma    migrations/

Below is an example of a schema located atprisma/schema.prisma.It uses a SQLite database and generates the client atsrc/prisma.rs:

prisma/schema.prisma
datasourcedb {    provider="sqlite"    url="file:dev.db"}generatorclient {// Corresponds to the cargo alias created earlier    provider="cargoprisma"// The location to generate the client. Is relative to the position of the schema    output="../src/prisma.rs"}modelUser {    idString@id    displayNameString}

Next, runcargo prisma generate to generate the client that will be used in your Rust code.If you haverustfmt installed,the generated code will be formatted for easier exploration and debugging.

💡

The generated client must not be checked into source control.It cannot be transferred between devices or operating systems.You will need to re-generate it wherever you build your project.If using git, add it to your.gitignore file.

Creating the Client

First, make sure you are using theTokio (opens in a new tab) async runtime.Other runtimes have not been tested, but since thePrisma Engines (opens in a new tab) use it there is likely no other option.

PrismaClient::_builder() provides a builder API for customising the generated client.The simplest use of this is calling.build() directly on the builder.Using the above schema for reference,this builder creates a client in amain.rs file right next toprisma.rs:

src/main.rs
// Stops the client from outputting a huge number of warnings during compilation.#[allow(warnings, unused)]mod prisma;use prisma::PrismaClient;use prisma_client_rust::NewClientError;#[tokio::main]asyncfnmain() {let client:Result<PrismaClient,NewClientError>=PrismaClient::_builder().build().await;}

Thewith_url builder method can be used to customise which database the client connects to.In most cases it is recommended to control this with an environment variable in your schema,but for some cases (eg. desktop apps with multiple databases) environment variables cannot be customised.

Naming Clashes

Rust has areserved set of keywords (opens in a new tab) that cannot be used as names in your code.If you name a model or field something that after conversion tosnake_case will be a restricted keyword,it will be prefixed withr# in the generated client instead of just failing to generate.

InstallationStructure

[8]ページ先頭

©2009-2025 Movatter.jp