1// In main.rs2modprisma;3useprisma::PrismaClient;45#[tokio::main]6asyncfnmain()->Result<(),Box<dynstd::error::Error>>{7// Create client (reads connection string from schema.prisma)8let client =prisma::new_client().await?;910// Or specify custom URL at runtime11// let client = prisma::new_client_with_url("file:./my-database.db").await?;1213// Now use the client for queries14Ok(())15}
3.3. CRUD Operations
3.3.1. CREATE
Create a single record
1let counter = client
2.counter()3.create(40,// value (required field)5"My Counter".to_string(),// name (required field)6vec![// optional fields7prisma::counter::description::set(Some("A test counter".to_string())),8prisma::counter::is_active::set(true),9prisma::counter::multiplier::set(Some(2.5)),10]11)12.exec()13.await?;
1let counters = client
2.counter()3.find_many(vec![])4.skip(10)5.take(5)// Limit to 5 results6.exec()7.await?;
Select specific fields only
1let names = client
2.counter()3.find_many(vec![])4.select(prisma::counter::select!({5 id
6 name
7}))8.exec()9.await?;1011for counter in names {12println!("ID: {}, Name: {}", counter.id, counter.name);13}
3.3.3. UPDATE
Update a single record
1let updated = client
2.counter()3.update(4prisma::counter::id::equals(1),// which record5vec![// what to update6prisma::counter::value::increment(1),7prisma::counter::name::set("Updated Name".to_string()),8prisma::counter::description::set(Some("New description".to_string())),9]10)11.exec()12.await?;1314println!("New value: {}", updated.value);
1let counter = client
2.counter()3.upsert(4prisma::counter::id::equals(1),// find by this5prisma::counter::create(0,"New".to_string(),vec![]),// create if not found6vec![prisma::counter::value::increment(1)]// update if found7)8.exec()9.await?;
1prisma::counter::value::equals(10)2prisma::counter::value::lt(10)// less than3prisma::counter::value::lte(10)// less than or equal4prisma::counter::value::gt(10)// greater than5prisma::counter::value::gte(10)// greater than or equal6prisma::counter::value::in_vec(vec![1,2,3,4])7prisma::counter::value::not_in_vec(vec![5,6])
1let count = client
2.counter()3.count(vec![4prisma::counter::is_active::equals(true)5])6.exec()7.await?;89println!("Total active counters: {}", count);
4.6. Transactions
Execute multiple operations atomically
1let result = client
2._transaction()3.run(|tx|asyncmove{4// Create user5let user = tx
6.user()7.create("user@example.com".to_string(),"John".to_string(),vec![])8.exec()9.await?;1011// Create counter linked to user12let counter = tx
13.counter()14.create(0,"User's Counter".to_string(),vec![])15.exec()16.await?;1718Ok((user, counter))19})20.await?;2122println!("Created user {} and counter {}", result.0.id, result.1.id);
4.7. Raw SQL Queries
Raw query with type safety
1useprisma_client_rust::raw;23let counters:Vec<prisma::counter::Data>= client
4._query_raw(raw!(5"SELECT * FROM counter WHERE value > {}",6107))8.exec()9.await?;
Raw execute (INSERT/UPDATE/DELETE)
1let affected = client
2._execute_raw(raw!(3"UPDATE counter SET value = value + 1 WHERE is_active = {}",4true5))6.exec()7.await?;89println!("Updated {} rows", affected);
4.8. Left Joins over Association Table
Suppose that we have the following tables:
Then to get all scripts belonging to a folder:
1pubfnget_all_scripts_of_folder(&self, folder_id:i32)->Vec<prisma::shell_script::Data>{2let runtime =tokio::runtime::Runtime::new().unwrap();34let scripts = runtime
5.block_on(asyncmove{6// Query the folder with all related scripts through the junction table7self.db
8.scripts_folder()9.find_unique(prisma::scripts_folder::id::equals(folder_id))10.with(11prisma::scripts_folder::rel_scriptsfolder_shellscript::fetch(vec![])12.with(prisma::rel_scriptsfolder_shellscript::shell_script::fetch()),13)14.exec()15.await16})17.unwrap();1819// Extract the shell scripts from the joined data20ifletSome(folder)= scripts {21 folder
22.rel_scriptsfolder_shellscript
23.unwrap_or_default()24.into_iter()25.filter_map(|rel| rel.shell_script.map(|s|*s))26.collect()27}else{28vec![]29}30}
.with() means left join;
.fetch() follows from what we declared in schema.prisma; Do we fetch a list? or fetch a single item?