@ graphile/ 简化- inflection
这个预设简化了后Graphile schema中的字段名;例如. allUsers
变得简单 users, (中文(简体) ), User.postsByAuthorId 变得简单 User.posts,以及
Post.userByAuthorId 变得简单 Post.author。 。 。 .
把这个预设加进你的计划 几乎肯定是一个突破性的变化,所以 在你运送任何东西之前!
为所有 PostGraphile 用户推荐此预设 具有兼容性 数据库计划。 此预设默认无法启用的主要原因是 因为它增加了多重关系试图和 使用同一个字段名称,并且因为有些人命名了他们的主要密钥 外国密钥列会引发冲突。 冲突可能是 通过谨慎使用命名惯例或专门标记 和他们喜欢的名字有冲突的关系.
数据库计划不兼容
此插件假设您的主要密钥和外国密钥列为:
- 名称
id, (中文(简体) ),uuid,或pk;或 ;或 - 名称结束于
_id, (中文(简体) ),_uuid,或_pk;或 ;或 - 名称结束于
Id, (中文(简体) ),Uuid,或Pk。 。 。 .
如果您的数据库计划使用不同的命名惯例(特别是 您在表格本身之后指定主密钥,或者在表格本身之后指定单数版本 它,没有前缀或后缀),那么你可能会收到很多命名 如果您使用此插件, 则冲突 。 如果您受此影响, 此插件是 可能不适合你(但可以随意叉出叉子并更改交友日期) 您的计程车命名会议。但您可以使用 隐藏主键和外键的“后置/预设/中继”预设 减少这些冲突的来源.
自定义
此预设只是一个文件, 简单复制到您的工程中并自定义它 如你所见.
或者,你可以 写入您自己的插件。 。 。 .
变化 :
鉴于这些表格:
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.beveragesBeverage.companyByCompanyId👉Beverage.companyBeverage.companyByDistributorId👉Beverage.distributorCompany.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 命名会议,但您可以重命名
以一个聪明的评论:
comment on constraint "beverages_distributor_id_fkey" on "beverages" is
E'@foreignFieldName distributedBeverages';
或带有自定义折射插件 :
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)
);
},
},
},
};
安装 :
yarn add @graphile/simplify-inflection
或者说
npm install --save @graphile/simplify-inflection
用法 :
将预设添加到您的图形配置文件, 在预设下 :
// 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,例如:
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如果失败了,我们就会退后
到默认的(动词) ; 由您添加聪明的评论
或习惯上用来取代这些.
处理实地冲突:
在大多数情况下,冲突错误将指导你如何解决这些问题 使用 智能评论。 。 。 .
智能标记
@listSuffix
@listSuffix 允许您在单个实体的基础上推翻默认命名,
压倒性 pgOmitListSuffix。例如,与 pgOmitListSuffix: true,你吗
可应用 @listSuffix include 拥有 -List 后缀附于
生成该表格的简单收藏,并删除 -Connection 后缀
从中继连接。 何时 pgOmitListSuffix 没有 true,您可以使用
@listSuffix omit 有选择地省略 -List 简单收藏的后缀
附加 -Connection 改为中继连接.
若为 @listSuffix 设置,唯一有效的值是 "omit" 财务报告和审定财务报表 "include"任意
其他值将造成错误.
| 列表后缀省略 | @ listSnix 包含 | |
|---|---|---|
| 中继连接 | 连线 | 企业 |
| 简单收藏 | 企业 | 公司列表 |
注:
@listSuffix使用时将无效果@foreignSimpleFieldName。 。 。 .
适用于:
- 图表
- 外国主要制约因素
- 返回计算列函数
SETOF <record type>