node postgreSQL的select与include

select与include

select:选择特定字段,可以嵌套select使用,减少数据量

include:包含关联数据,返回完整的关联对象

两者都是用于返回一些数据,只是返回的数据不同。select返回指定字段,可以选标量字段,也可以选择关系字段,而include不能选标量字段,只能包含关系。

注意:使用时select与include不能平级在同一层使用

使用上的一个方式:关系内部可以继续使用where、orderBy、select、include

准备一份数据,代码如下:

javascript 复制代码
// seed.js
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()

async function main() {
  await prisma.post.deleteMany()
  await prisma.user.deleteMany()

  await prisma.user.create({
    data: {
      email: 'alice@example.com',
      name: 'Alice',
      password: 'hashed_pwd_1',
      posts: {
        create: [
          { title: 'Hello Prisma', content: 'first post', published: true },
          { title: 'Draft post', content: 'not ready', published: false },
        ],
      },
    },
  })
}

main().finally(() => prisma.$disconnect())

使用select与include,代码如下:

javascript 复制代码
// index.js
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()

async function main() {
  // ---------- 1. 什么都不加:只有 User 自身字段 ----------
  const plain = await prisma.user.findMany()
  console.log('1. 默认:', JSON.stringify(plain, null, 2))

  // ---------- 2. include:User 全部字段 + posts ----------
  const withInclude = await prisma.user.findMany({
    include: { posts: true },
  })
  console.log('2. include:', JSON.stringify(withInclude, null, 2))

  // ---------- 3. select:只要列出的字段 ----------
  const withSelect = await prisma.user.findMany({
    select: {
      id: true,
      name: true,
      posts: true, // 关联关系也可以写在 select 里
    },
  })
  console.log('3. select:', JSON.stringify(withSelect, null, 2))
}

main().finally(() => prisma.$disconnect())

看下下面数据输出的区别如下:

默认(没有select与include),输出如下:

javascript 复制代码
[
  {
    "id": 1,
    "email": "alice@example.com",
    "name": "Alice",
    "password": "hashed_pwd_1",
    "createdAt": "2024-05-01T10:00:00.000Z"
  }
]

使用select,输出如下:

javascript 复制代码
[
  {
    "id": 1,
    "name": "Alice",
    "posts": [
      {
        "id": 1,
        "title": "Hello Prisma",
        "content": "first post",
        "published": true,
        "authorId": 1
      },
      {
        "id": 2,
        "title": "Draft post",
        "content": "not ready",
        "published": false,
        "authorId": 1
      }
    ]
  }
]

使用include,代码结果如下:

javascript 复制代码
[
  {
    "id": 1,
    "email": "alice@example.com",
    "name": "Alice",
    "password": "hashed_pwd_1",
    "createdAt": "2024-05-01T10:00:00.000Z",
    "posts": [
      {
        "id": 1,
        "title": "Hello Prisma",
        "content": "first post",
        "published": true,
        "authorId": 1
      },
      {
        "id": 2,
        "title": "Draft post",
        "content": "not ready",
        "published": false,
        "authorId": 1
      }
    ]
  }
]

一般在实际的生产项目中会混合嵌套使用,比如像下面的代码:

javascript 复制代码
// include 里嵌 select(限制关联字段)
const a = await prisma.user.findMany({
  include: {
    posts: {
      select: { title: true, published: true },
    },
  },
})
// 结果:User 全字段 + posts: [{ title, published }, ...]

// select 里嵌 include
const b = await prisma.user.findMany({
  select: {
    id: true,
    posts: {
      include: { author: { select: { email: true } } },
    },
  },
})
// 结果:只有 id + posts(全字段 + author.email)

// 嵌套里加 where / orderBy / take
const c = await prisma.user.findMany({
  select: {
    id: true,
    posts: {
      where: { published: true },
      orderBy: { id: 'desc' },
      take: 5,
      select: { title: true },
    },
  },
})
相关推荐
FungLeo6 小时前
成为全栈·Go 后端篇·契约一致性与差分测试:两套实现如何互相校验
golang·node.js·go·合约测试·差分测试·成为全栈·api兼容
月夏11 小时前
前端第一次接自己的后端:mock 切真接口踩了 2 个坑,组件只改了一行 import
前端·后端·node.js
SEO_juper11 小时前
Node.js 页面主题聚焦度检测:title 和正文各说各的,AI 也读不懂
开发语言·人工智能·node.js·seo·外贸独立站
SEO_juper1 天前
Node.js 重定向链检测工具:301 跳了几跳、有没有成环,一次看清
xml·开发语言·python·node.js·json·bash
king13598745501 天前
AI Agent 端云协同选型:Node.js 任务成本算例与权限检查清单(2027—2028 展望)
人工智能·ai·node.js·ai agent
居里先生7521 天前
PDF 批注到底存在文件的哪里?从 /Annots 讲到导出验证
node.js
MasonYyp2 天前
安装npm依赖仓库Verdaccio
前端·npm·node.js
编程的一拳超人3 天前
【元宝搜索开发者征文大赛】四语实战:Java/Python/Go/Node.js 构建 AI 搜索应用全攻略
java·python·golang·node.js·agent
lucas_AI3 天前
今年 2 月就修好的漏洞,10 月才炸:HTTP2Shell 把 Node、Python 一锅端
node.js
SEO_juper4 天前
Node.js 全站死链检测工具:从 sitemap 出发一次找全 404
开发语言·人工智能·python·node.js·seo·外贸独立站