# @graphile/simplify-inflection 中文文档

规范地址：https://npmcn.com/package/%40graphile/simplify-inflection/

版本：9\.0\.1
npm 元数据更新时间：2026-10-30T13:05:18.623Z
中文译文更新时间：2026-11-16T22:49:06.104Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40graphile/simplify-inflection
原文对照：https://npmcn.com/package/%40graphile/simplify-inflection/?lang=original
项目主页：https://github.com/graphile/crystal#readme
源码仓库：https://github.com/graphile/crystal

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：50.8 kB，7 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2023-02-03T16:47:47.367Z
- 最新版本发布：2026-10-30T13:05:18.307Z
- 近 30 天下载量：175600（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：45（近 12 个月 26 个）

## 中文简介

简化图形- 构建/ 图形- 构建- pg 插入来修剪 `ByFooIdAndBarId` 从关系等

## 安装

```sh
npm install @graphile/simplify-inflection
```

也可以使用：`pnpm add @graphile/simplify-inflection`、`yarn add @graphile/simplify-inflection`、`bun add @graphile/simplify-inflection`

## README

# @ graphile/ 简化- inflection

[![GitHub Sponsors](https://img.shields.io/github/sponsors/benjie?color=ff69b4&label=github%20sponsors)](https://github.com/sponsors/benjie) [![Discord chat room](https://img.shields.io/discord/489127045289476126.svg)](http://discord.gg/graphile) [![Follow](https://img.shields.io/badge/BSky-@Graphile.org-006aff.svg)](https://bsky.app/profile/graphile.org) [![Follow](https://img.shields.io/badge/Mastodon-@Graphile.fosstodon.org-6364ff.svg)](https://fosstodon.org/@graphile)

这个预设简化了后Graphile schema中的字段名;例如. `allUsers` 变得简单 `users`, (中文(简体) ), `User.postsByAuthorId` 变得简单 `User.posts`,以及 `Post.userByAuthorId` 变得简单 `Post.author`。 。 。 .

**把这个预设加进你的计划 几乎肯定是一个突破性的变化,所以 在你运送任何东西之前!**

**为所有 PostGraphile 用户推荐此预设** 具有兼容性 数据库计划。 此预设默认无法启用的主要原因是 因为它增加了多重关系试图和 使用同一个字段名称,并且因为有些人命名了他们的主要密钥 外国密钥列会引发冲突。 冲突可能是 通过谨慎使用命名惯例或专门标记 和他们喜欢的名字有冲突的关系.

## 数据库计划不兼容

此插件假设您的主要密钥和外国密钥列为:

-   名称 `id`, (中文(简体) ), `uuid`,或 `pk`;或 ;或
-   名称结束于 `_id`, (中文(简体) ), `_uuid`,或 `_pk`;或 ;或
-   名称结束于 `Id`, (中文(简体) ), `Uuid`,或 `Pk`。 。 。 .

如果您的数据库计划使用不同的命名惯例(特别是 您在表格本身之后指定主密钥,或者在表格本身之后指定单数版本 它,没有前缀或后缀),那么你可能会收到很多命名 如果您使用此插件, 则冲突 。 如果您受此影响, 此插件是 可能不适合你(但可以随意叉出叉子并更改交友日期) 您的计程车命名会议。但您可以使用 隐藏主键和外键的“后置/预设/中继”预设 减少这些冲突的来源.

## 自定义

此预设只是一个文件, 简单复制到您的工程中并自定义它 如你所见.

或者,你可以 [写入您自己的插件](https://postgraphile.org/postgraphile/next/inflection)。 。 。 .

## 变化 :

鉴于这些表格:

```sql
create table companies (
  id serial primary key,
  name text not null
);
create table beverages (
  id serial primary key,
  company_id int not null references companies,
  distributor_id int references companies,
  name text not null
);
```

-   `Query.allCompanies` 👉 `Query.companies` (通过 `pgSimplifyAllRows = false`(中文(简体) )
-   `Query.allBeverages` 👉 `Query.beverages`
-   `Beverage.companyByCompanyId` 👉 `Beverage.company`
-   `Beverage.companyByDistributorId` 👉 `Beverage.distributor`
-   `Company.beveragesByCompanyId` 👉 `Company.beverages` (因为 `company_id` 栏后 `[table_name]_id` 命名公约)
-   所有更新的突变现在接受 `patch` 改为 `companyPatch` 编号 : `beveragePatch` (通过 `pgSimplifyPatch = false`(中文(简体) )
-   如果您喜欢列表, 您可以使用 `pgOmitListSuffix = true` 以省略 `List` 后缀,转而附加 `Connection` 连接
-   单数和复数同为一并的字段,一个不同的复数是 要求是强制拓扑("鱼")来避免冲突(例如. `singularize("fish") === pluralize("fish")`) (中文(简体) ).

说明: `Company.beveragesByDistributorId` 因为.. `distributor_id` 不遵循 `[table_name]_id` 命名会议,但您可以重命名 以一个聪明的评论:

```sql
comment on constraint "beverages_distributor_id_fkey" on "beverages" is
  E'@foreignFieldName distributedBeverages';
```

或带有自定义折射插件 :

```js
const plugin = {
  name: "MyCustomInflectionPlugin",
  version: "0.0.0",
  inflection: {
    replace: {
      getOppositeBaseName(previous, options, baseName) {
        return (
          {
            // These are the default opposites
            parent: "child",
            child: "parent",
            author: "authored",
            editor: "edited",
            reviewer: "reviewed",

            // 👇 Add/customise this line:
            distributor: "distributed",
          }[baseName] || previous(baseName)
        );
      },
    },
  },
};
```

## 安装 :

```bash
yarn add @graphile/simplify-inflection
```

或者说

```bash
npm install --save @graphile/simplify-inflection
```

## 用法 :

将预设添加到您的图形配置文件, 在预设下 :

```ts
// graphile.config.mjs or similar

import { PgSimplifyInflectionPreset } from "@graphile/simplify-inflection";

export default {
  extends: [
    // ... (PostGraphileAmber, etc)
    PgSimplifyInflectionPreset,
  ],

  schema: {
    // ...
    /*
     * Uncomment if you want simple collections to lose the 'List' suffix
     * (and connections to gain a 'Connection' suffix).
     */
    //pgOmitListSuffix: true,
    /*
     * Uncomment if you want 'userPatch' instead of 'patch' in update
     * mutations.
     */
    //pgSimplifyPatch: false,
    /*
     * Uncomment if you want 'allUsers' instead of 'users' at root level.
     */
    //pgSimplifyAllRows: false,
    /*
     * Uncomment if you want primary key queries and mutations to have
     * `ById` (or similar) suffix; and the `nodeId` queries/mutations
     * to lose their `ByNodeId` suffix.
     */
    // pgShortPk: false,
  },
};
```

## 命名您的外国密钥字段

将您的外国密钥命名为 `author_id` 或者说 `author_fk`,例如:

```sql
CREATE TABLE posts (
  id serial primary key,
  author_id int not null references users,
  ...
);
```

我们可以自动取出字段前缀 : `author` 调用关系 `author` 而不是默认 : `user`。这允许一个员额有一个 `author`, (中文(简体) ), `editor`, (中文(简体) ), `reviewer`等所有指向 `users`。 。 。 .

然而,反之也不是那么容易。 在用户类型上,我们不能叫 倒置所有这些不同的关系 `posts`。默认反射符引用 改为: `postsByAuthorId`, (中文(简体) ), `postsByEditorId`等 (中文(简体) ). 不过我们宁愿用 更短的名字,所以我们介绍一个新的缩写: `getOppositeBaseName`这个 折射器通过一个基数Name(没有 `_id`编号 :`_fk` 后缀,例如:. `author`, (中文(简体) ), `editor`, (中文(简体) ), `reviewer` ,并应当返回相反的情况 将准备用于目标类型的基准名称,例如:. `authoredPosts`, (中文(简体) ), `editedPosts`, (中文(简体) ), `reviewedPosts`如果失败了,我们就会退后 到默认的(动词) ; 由您添加聪明的评论 或习惯上用来取代这些.

## 处理实地冲突:

在大多数情况下,冲突错误将指导你如何解决这些问题 使用 [智能评论](https://www.graphile.org/postgraphile/smart-comments/)。 。 。 .

## 智能标记

### `@listSuffix`

`@listSuffix` 允许您在单个实体的基础上推翻默认命名, 压倒性 `pgOmitListSuffix`。例如,与 `pgOmitListSuffix: true`,你吗 可应用 `@listSuffix include` 拥有 `-List` 后缀附于 生成该表格的简单收藏,并删除 `-Connection` 后缀 从中继连接。 何时 `pgOmitListSuffix` 没有 `true`,您可以使用 `@listSuffix omit` 有选择地省略 `-List` 简单收藏的后缀 附加 `-Connection` 改为中继连接.

若为 `@listSuffix` 设置,唯一有效的值是 `"omit"` 财务报告和审定财务报表 `"include"`任意 其他值将造成错误.

|  | 列表后缀省略 | @ listSnix 包含 |
| --: | :-- | :-- |
| 中继连接 | 连线 | 企业 |
| 简单收藏 | 企业 | 公司列表 |

> 注: `@listSuffix` 使用时将无效果 `@foreignSimpleFieldName`。 。 。 .

适用于:

-   图表
-   外国主要制约因素
-   返回计算列函数 `SETOF <record type>`

## 常见问题

### @graphile/simplify-inflection 是什么？

简化图形- 构建/ 图形- 构建- pg 插入来修剪 ByFooIdAndBarId 从关系等

### 如何安装 @graphile/simplify-inflection？

在项目目录运行 npm install @graphile/simplify-inflection，或使用 pnpm add / yarn add / bun add。

### @graphile/simplify-inflection 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @graphile/simplify-inflection 支持 ESM 还是 CommonJS？

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

### @graphile/simplify-inflection 可以商用吗？

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

### @graphile/simplify-inflection 的下载量有多少？

根据 npm 官方下载统计，@graphile/simplify-inflection 在 2026/10/17 – 2026/11/15 这 30 天内被下载 175,600 次，日均约 5,853 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @graphile/simplify-inflection 还在维护吗？

最新版本 9\.0\.1 发布于 2026/10/30（17 天前）；至今共发布 45 个版本，其中过去 12 个月发布了 26 个；首次发布于 2023/02/04。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

