使用 Prisma 来获取当前 post 的前后两个帖子,你可以按照以下步骤进行操作:

  1. 确保已经安装了 Prisma,并且已经设置了数据库连接。

  2. 在你的 Prisma schema 文件中,确保你有一个名为 Post 的模型,包含了你帖子的相关字段。

  3. 在你的代码文件中,导入 PrismaClient,并创建一个 PrismaClient 实例:

const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
  1. 使用 Prisma 的查询方法来获取前后两个帖子。你可以使用 findUnique 方法来获取当前帖子的详细信息,然后使用 findFirst 和 findLast 方法来获取前一个和后一个帖子的信息。以下是一个示例代码:
async function getAdjacentPosts(postId) {
  const currentPost = await prisma.post.findUnique({
    where: { id: postId },
  });

  const previousPost = await prisma.post.findFirst({
    where: { id: { lt: currentPost.id } },
    orderBy: { id: 'desc' },
  });

  const nextPost = await prisma.post.findFirst({
    where: { id: { gt: currentPost.id } },
    orderBy: { id: 'asc' },
  });

  return {
    previousPost,
    currentPost,
    nextPost,
  };
}

// Usage
getAdjacentPosts('postId').then(result => {
  console.log(result);
});

在上面的代码中,postId 是当前帖子的 ID。getAdjacentPosts 函数会返回一个包含前一个帖子、当前帖子和后一个帖子的对象。

你可以根据你的需求进行修改和适应。请确保在使用之前,你已经正确地配置了 Prisma 和数据库连接

prismajs 获取当前 post 的前后两个共 4 个帖子