Prima Client Rust
An ergonomic and autogenerated database client that utilises Prisma schemas.
Installation
-
Install the
prisma-client-rust
CLI
cargo install prisma-client-rust
-
Install
prisma-client-rust
- If you have
cargo-edit
installed, runcargo add prisma-client-rust
- If not, add
prisma-client-rust = "1.0.0'
as a dependency inCargo.toml
- If you have
-
Add
prisma-client-rust
as a generator to your Prisma Schema
generator client {
provider = "prisma-client-rust"
// The folder that files should be generated to, relative to your schema
output = "./db"
}
- Generate rust module with
prisma-client-rust generate
- Include the generated module in your code and connect a new Prisma client
// Name of the module will be the folder specified in the generator's 'output'
use
// Any async runtime can be used, tokio is just an example
async
Queries
The following examples use this schema:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-rust"
binaryTargets = ["native"]
output = "../src/db"
}
model User {
username String @id
displayName String
posts Post[]
comments Comment[]
}
model Post {
id String @id
content String
comments Comment[] @relation()
User User? @relation(fields: [userUsername], references: [username])
userUsername String?
}
model Comment {
id String @id
postId String
post Post @relation(fields: [postId], references: [id])
User User? @relation(fields: [userUsername], references: [username])
userUsername String?
}
Find
;
async
Create
;
async
Delete
;
async