@devoxa/prisma-relay-cursor-connection

中文文档
3.1.3发布于 2025/12/08MITCommonJS近 30 天下载 13万

Extend Prisma's findMany method to support Relay Cursor Connections

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. General Usage
  4. Type-Safe Arguments
  5. Custom Cursors
  6. Custom Edges & Nodes
  7. Resolve information
  8. Contributing
  9. Contributors
  10. License

prisma-relay-cursor-connection

Extend Prisma's findMany method to support Relay Cursor Connections

Package Version Build Status Code Coverage

Installation • Usage • Contributing • Contributors • License


Installation

yarn add @devoxa/prisma-relay-cursor-connection

This module has a peer dependency on @prisma/client. You can check the supported versions in the package.json (peerDependencies).

Usage

General Usage

This module validates the connection arguments to make sure they work with Prisma. The following combinations are supported:

  • {} All resources
  • { first: number } The first X resources
  • { first: number, after: string } The first X resources after the id Y
  • { last: number } The last X resources
  • { last: number, before: string } The last X resources before the id Y

Two cases need to be checked in your code if you are passing in user-provided data to prevent the user from reading out too many resources at once:

  • One of first | last has to be defined
  • first | last have to be below a reasonable maximum (e.g. 100)
import {
  findManyCursorConnection,
  ConnectionArguments,
} from '@devoxa/prisma-relay-cursor-connection'

const result = await findManyCursorConnection(
  (args) => client.todo.findMany(args),
  () => client.todo.count(),
  { first: 5, after: '5c11e0fa-fd6b-44ee-9016-0809ee2f2b9a' } // typeof ConnectionArguments
)

Type-Safe Arguments

You can also use additional FindManyArgs while keeping type safety intact:

import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'

const baseArgs = {
  select: { id: true, isCompleted: true },
  where: { isCompleted: true },
}

const result = await findManyCursorConnection(
  (args) => client.todo.findMany({ ...args, ...baseArgs }),
  () => client.todo.count({ where: baseArgs.where }),
  { last: 5, before: '5c11e0fa-fd6b-44ee-9016-0809ee2f2b9a' }
)

// Type error: Property text does not exist
result.edges[0].node.text

Custom Cursors

By default, the cursor is the id field of your model. If you would like to use a different field, a compound index, or handle encoding/decoding, you can pass the following options:

import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'

const result = await findManyCursorConnection(
  (args) => client.todo.findMany(args),
  () => client.todo.count(),
  { first: 5, after: 'eyJpZCI6MTZ9' },
  {
    getCursor: (record) => ({ id: record.id }),
    encodeCursor: (cursor) => Buffer.from(JSON.stringify(cursor)).toString('base64'),
    decodeCursor: (cursor) => JSON.parse(Buffer.from(cursor, 'base64').toString('ascii')),
  }
)

You can find more examples for custom cursors in the unit tests.

Custom Edges & Nodes

By default, the edge consists of the cursor and the node. If you would like to add additional fields to the edge or the node, you can pass the following option:

import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'

const result = await findManyCursorConnection(
  (args) => client.todo.findMany(args),
  () => client.todo.count(),
  { first: 5, after: 'eyJpZCI6MTZ9' },
  {
    recordToEdge: (record) => ({
      node: { ...record, extraNodeField: 'Foo' },
      extraEdgeField: 'Bar',
    }),
  }
)

Out-of-the box this will have the revised types inferred for you.

Resolve information

You can pass GraphQL resolve information into the options to automatically remove extra Prisma queries for fields that are not present in your GraphQL query. This is mainly useful if you are not using totalCount for your pagination logic or you only want to query totalCount without any edges.

import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'
import { GraphQLResolveInfo } from 'graphql'

const resolveInfo: GraphQLResolveInfo = {
  // ...
}

const result = await findManyCursorConnection(
  (args) => client.todo.findMany(args),
  () => client.todo.count(),
  { first: 5, after: '5c11e0fa-fd6b-44ee-9016-0809ee2f2b9a' },
  { resolveInfo }
)

Contributing

# Setup the test database
yarn prisma migrate dev --preview-feature

# Run the tests
yarn test

Contributors

Thanks goes to these wonderful people (emoji key):

David Reeß
David Reeß

💻 📖 ⚠️
Sean Matheson
Sean Matheson

💻 ⚠️
Marc
Marc

💻
Jeong Seong Dae
Jeong Seong Dae

💻 ⚠️
Ahmet Uysal
Ahmet Uysal

💻 ⚠️
Nick Randall
Nick Randall

💻
Igor Urminček
Igor Urminček

📖
Alex Schrimpf
Alex Schrimpf

💻
Marina Riera
Marina Riera

💻
Kervin Vasquez
Kervin Vasquez

💻 ⚠️
Jeff Gu Kang
Jeff Gu Kang

📖
Orta Therox
Orta Therox

💻 📖 ⚠️
Kareem-Medhat
Kareem-Medhat

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT

版本与发布记录

@devoxa/prisma-relay-cursor-connection 共发布 20 个版本,首次发布于 2020/06/18;过去 12 个月发布了 1 个版本。

版本发布日期距今
3.1.3latest2025/12/0811 个月前
3.1.22024/11/291.9 年前
3.1.12023/11/212 年前
3.1.02023/08/023 年前
3.0.02023/07/293 年前
2.2.42023/07/253 年前

关于 @devoxa/prisma-relay-cursor-connection 的常见问题

@devoxa/prisma-relay-cursor-connection 是什么?

Extend Prisma's findMany method to support Relay Cursor Connections

如何安装 @devoxa/prisma-relay-cursor-connection?

在项目目录运行 npm install @devoxa/prisma-relay-cursor-connection,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@prisma/client(>=2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@devoxa/prisma-relay-cursor-connection 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/devoxa__prisma-relay-cursor-connection 是否存在。

@devoxa/prisma-relay-cursor-connection 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@devoxa/prisma-relay-cursor-connection 可以商用吗?

@devoxa/prisma-relay-cursor-connection 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@devoxa/prisma-relay-cursor-connection 的下载量有多少?

根据 npm 官方下载统计,@devoxa/prisma-relay-cursor-connection 在 2026/10/12 – 2026/11/10 这 30 天内被下载 129,598 次,日均约 4,320 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@devoxa/prisma-relay-cursor-connection 还在维护吗?

最新版本 3.1.3 发布于 2025/12/08(11 个月前);至今共发布 20 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/06/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。