读取数据
分页

分页

分页允许您指定从 find_firstfind_many 查询以及许多关联中返回的记录范围。

示例使用以下模式

generator client {
    provider = "cargo prisma"
    output = "src/prisma.rs"
}
 
model Post {
    id        String   @id @default(cuid())
    createdAt DateTime @default(now())
    updatedAt DateTime @updatedAt
    published Boolean
    title     String
    content   String?
    desc      String?
 
    comments Comment[]
}
 
model Comment {
    id        String   @id @default(cuid())
    createdAt DateTime @default(now())
    content   String
 
    post   Post   @relation(fields: [postID], references: [id])
    postID String
}

获取

use prisma::post;
 
let posts: Vec<post::Data> = client
    .post()
    .find_many(vec![post::title::contains("Title".to_string())])
    // Only the first 5 records will be returned
    .take(5)
    .exec()
    .await?;
 

跳过

use prisma::post;
 
let posts: Vec<post::Data> = client
    .post()
    .find_many(vec![post::title::contains("Title".to_string())])
    // The first 2 records will be skipped
    .skip(2)
    .exec()
    .await?;
 

游标

cursor唯一过滤器 作为其参数。

use prisma::post;
 
let posts: Vec<post::Data> = client
    .post()
    .find_many(vec![])
    .cursor(post::id::equals("abc".to_string()))
    .exec()
    .await?;

order_by 与游标分页结合使用时非常有用。

关联分页

上述方法可以链接到许多关联的 fetch 调用。

use prisma::post;
 
let posts: Vec<post::Data> = client
    .post()
    .find_many(vec![])
    .with(
        post::comments::fetch(vec![])
            .skip(10)
            .take(5)
            .cursor(comment::id::equals("abc".to_string())),
    )
    .exec()
    .await?;