node prisma+postgreSQL中的查询与过滤

先建一个Prisma Schema,用于后面查询与过滤的数据操作

javascript 复制代码
model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  createdAt DateTime @default(now())
  posts     Post[]
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean  @default(false)
  views     Int      @default(0)
  createdAt DateTime @default(now())
  authorId  Int
  author    User     @relation(fields: [authorId], references: [id])

  categories Category[]
}

model Category {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

some、every、none

在进行to-many操作时,任何支持where条件的prisma方法,都可以在针对关系字段的过滤中使用它们,比如:

findeMany()、findFirst()、update/updateMany()、delete/deleteMany()

javascript 复制代码
// ① some:至少有一篇已发布的文章
const usersWithAtLeastOnePublished = await prisma.user.findMany({
  where: {
    posts: {
      some: { published: true },
    },
  },
})

// ② every:所有文章都已发布(注意:没有任何文章的用户也会被匹配)
const usersAllPublished = await prisma.user.findMany({
  where: {
    posts: {
      every: { published: true },
    },
  },
})

// ③ none:没有任何已发布的文章
const usersWithNoPublished = await prisma.user.findMany({
  where: {
    posts: {
      none: { published: true },
    },
  },
})

过滤操作符

equals、not、in、notIn、lt、lte、gt、gte、contains、startsWith、endsWith、has、hasEvery、hasSome、isEmpty

复合条件

javascript 复制代码
{
  AND: [...],   // 所有条件都满足
  OR: [...],    // 任一条件满足
  NOT: {...},   // 条件不满足
}

一段代码示例:

javascript 复制代码
// AND(隐式):多个条件同时满足
const users1 = await prisma.user.findMany({
  where: {
    name: { not: null },
    posts: {
      some: { published: true },
    },
  },
})

// 显式 AND
const users2 = await prisma.user.findMany({
  where: {
    AND: [
      { name: { contains: 'a' } },
      { posts: { some: { views: { gt: 100 } } } },
    ],
  },
})

// OR:满足任意一个条件
const users3 = await prisma.user.findMany({
  where: {
    OR: [
      { email: { endsWith: '@example.com' } },
      { posts: { some: { published: true } } },
    ],
  },
})

// NOT:取反
const users4 = await prisma.user.findMany({
  where: {
    NOT: {
      posts: {
        some: { published: false },
      },
    },
  },
})

// 混合:AND + OR + NOT + 关系过滤
const users5 = await prisma.user.findMany({
  where: {
    AND: [
      { createdAt: { gte: new Date('2025-01-01') } },
      {
        OR: [
          { posts: { some: { views: { gt: 1000 } } } },
          { posts: { every: { published: true } } },
        ],
      },
      {
        NOT: { email: { endsWith: '@spam.com' } },
      },
    ],
  },
})

排序

javascript 复制代码
orderBy: { field: 'asc' }         // 单列升序
orderBy: { field: 'desc' }        // 单列降序
orderBy: [{ f1: 'asc' }, { f2: 'desc' }]  // 多列排序

总结示例

javascript 复制代码
const users = await prisma.user.findMany({
  where: {
    AND: [
      { name: { in: ['Alice', 'Bob', 'Charlie'] } },
      { posts: { some: { published: true } } },
    ],
  },
  orderBy: [
    { createdAt: 'desc' },
    { name: 'asc' },
  ],
  include: {
    posts: {
      where: { published: true },
      orderBy: { createdAt: 'desc' },
      take: 1,
    },
  },
})
相关推荐
右耳朵猫AI1 小时前
Node.js周刊2026W37 | 三处进程崩溃修复、fs 内置 glob、Workers 模块注册表、Vitest 5.0
javascript·后端·node.js
右耳朵猫AI1 小时前
Web前端周刊2026W37 | Shopify 转原生、React 编译 Rust 化、Vitest 5.0、Rslib 1.0
前端·javascript·react.js·typescript·node.js
秋秋小事1 小时前
node prisma中的crud
node.js
梦帮科技2 小时前
从提示词到可复现音乐工作流:RNOISE 2.0 的 Prompt Engineering 架构
人工智能·python·mysql·ci/cd·架构·node.js·numpy
小静AI工程实验室3 小时前
JS 逆向接口 ID 变了?Python 与 Node.js 复现 JSON 大整数精度丢失
javascript·python·node.js
FungLeo12 小时前
成为全栈·Node 后端篇·辅助接口:相邻、相关、目录、统计与搜索
node.js·成为全栈
抓不住时间的沙18 小时前
Butterfly主题 5.7 导航栏添加相册同时设置相册入口密码
css·python·node.js
FungLeo19 小时前
成为全栈·Node 后端篇·通知系统:事件消费与已读态管理
node.js·成为全栈·通知系统·事件消费·已读状态管理
西瓜太郎4991 天前
API Key 轮换不该靠“瞬间替换”:用双 Key 灰度避免线上中断
node.js·api