{"id":20253531,"url":"https://github.com/graphile/pg-simplify-inflector","last_synced_at":"2025-03-23T02:12:58.051Z","repository":{"id":42048579,"uuid":"138903573","full_name":"graphile/pg-simplify-inflector","owner":"graphile","description":"Simplifies the graphile-build-pg inflector to trim the `ByFooIdAndBarId` from relations","archived":false,"fork":false,"pushed_at":"2024-09-17T10:35:52.000Z","size":368,"stargazers_count":89,"open_issues_count":1,"forks_count":9,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-15T03:11:21.405Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/graphile.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"Benjie"}},"created_at":"2018-06-27T15:59:18.000Z","updated_at":"2025-02-24T12:44:30.000Z","dependencies_parsed_at":"2024-09-18T18:46:48.237Z","dependency_job_id":null,"html_url":"https://github.com/graphile/pg-simplify-inflector","commit_stats":null,"previous_names":["graphile-contrib/pg-simplify-inflector"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile%2Fpg-simplify-inflector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile%2Fpg-simplify-inflector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile%2Fpg-simplify-inflector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile%2Fpg-simplify-inflector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphile","download_url":"https://codeload.github.com/graphile/pg-simplify-inflector/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245044584,"owners_count":20551902,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T10:25:28.070Z","updated_at":"2025-03-23T02:12:58.028Z","avatar_url":"https://github.com/graphile.png","language":"JavaScript","funding_links":["https://github.com/sponsors/Benjie"],"categories":[],"sub_categories":[],"readme":"# @graphile-contrib/pg-simplify-inflector\n\nThis plugin simplifies field names in the PostGraphile schema; e.g. `allUsers`\nbecomes simply `users`, `User.postsByAuthorId` becomes simply `User.posts`, and\n`Post.userByAuthorId` becomes simply `Post.author`.\n\n**Adding this plugin to your schema is almost certainly a breaking change, so do\nit before you ship anything!** This is the primary reason this isn't enabled by\ndefault in PostGraphile.\n\n_This plugin is recommended for all PostGraphile users._\n\n## Customising\n\nThis plugin is implemented as a single JS file that does not need to be compiled\nat all - you can simply copy it into your project and customise it as you see\nfit.\n\nAlternatively, you can\n[write your own inflection plugin](https://www.graphile.org/postgraphile/inflection/).\n\n## Changes:\n\nGiven these tables:\n\n```sql\ncreate table companies (\n  id serial primary key,\n  name text not null\n);\ncreate table beverages (\n  id serial primary key,\n  company_id int not null references companies,\n  distributor_id int references companies,\n  name text not null\n);\n```\n\n- `Query.allCompanies` 👉 `Query.companies` (disable via\n  `pgSimplifyAllRows = false`)\n- `Query.allBeverages` 👉 `Query.beverages`\n- `Beverage.companyByCompanyId` 👉 `Beverage.company`\n- `Beverage.companyByDistributorId` 👉 `Beverage.distributor`\n- `Company.beveragesByCompanyId` 👉 `Company.beverages` (because the\n  `company_id` column follows the `[table_name]_id` naming convention)\n- All update mutations now accept `patch` instead of `companyPatch` /\n  `beveragePatch` (disable via `pgSimplifyPatch = false`)\n- If you are using `pgSimpleCollections = \"only\"` then you can set\n  `pgOmitListSuffix = true` to omit the `List` suffix\n- Fields where the singular and plural are the same and a distinct plural is\n  required are force-pluralised (\"fishes\") to avoid conflicts (e.g.\n  `singularize(\"fish\") === pluralize(\"fish\")`).\n\nNote: `Company.beveragesByDistributorId` will remain, because `distributor_id`\ndoes not follow the `[table_name]_id` naming convention, but you could rename\nthis yourself with a smart comment:\n\n```sql\ncomment on constraint \"beverages_distributor_id_fkey\" on \"beverages\" is\n  E'@foreignFieldName distributedBeverages';\n```\n\nor with a custom inflector:\n\n```js\nmodule.exports = makeAddInflectorsPlugin(\n  {\n    getOppositeBaseName(baseName) {\n      return (\n        {\n          // These are the default opposites\n          parent: \"child\",\n          child: \"parent\",\n          author: \"authored\",\n          editor: \"edited\",\n          reviewer: \"reviewed\",\n\n          // 👇 Add/customise this line:\n          distributor: \"distributed\",\n        }[baseName] || null\n      );\n    },\n  },\n  true\n);\n```\n\n## Installation:\n\n```bash\nyarn add @graphile-contrib/pg-simplify-inflector\n```\n\nor\n\n```bash\nnpm install --save @graphile-contrib/pg-simplify-inflector\n```\n\n## Usage:\n\nCLI:\n\n```bash\npostgraphile --append-plugins @graphile-contrib/pg-simplify-inflector\n```\n\nLibrary:\n\n```js\nconst PgSimplifyInflectorPlugin = require(\"@graphile-contrib/pg-simplify-inflector\");\n\n// ...\n\napp.use(\n  postgraphile(process.env.AUTH_DATABASE_URL, \"app_public\", {\n    appendPlugins: [PgSimplifyInflectorPlugin],\n\n    // Optional customisation\n    graphileBuildOptions: {\n      /*\n       * Uncomment if you want simple collections to lose the 'List' suffix\n       * (and connections to gain a 'Connection' suffix).\n       */\n      //pgOmitListSuffix: true,\n      /*\n       * Uncomment if you want 'userPatch' instead of 'patch' in update\n       * mutations.\n       */\n      //pgSimplifyPatch: false,\n      /*\n       * Uncomment if you want 'allUsers' instead of 'users' at root level.\n       */\n      //pgSimplifyAllRows: false,\n      /*\n       * Uncomment if you want primary key queries and mutations to have\n       * `ById` (or similar) suffix; and the `nodeId` queries/mutations\n       * to lose their `ByNodeId` suffix.\n       */\n      // pgShortPk: true,\n    },\n    // ... other settings ...\n  })\n);\n```\n\n## Naming your foreign key fields\n\nBy naming your foreign key along the lines of `author_id` or `author_fk`, e.g.:\n\n```sql\nCREATE TABLE posts (\n  id serial primary key,\n  author_id int not null references users,\n  ...\n);\n```\n\nWe can automatically extract the field prefix: `author` and call the relation\n`author` rather than the default: `user`. This allows for a post to have an\n`author`, `editor`, `reviewer`, etc. all which point to `users`.\n\nThe reverse, however, is not so easy. On the User type, we can't call the\nreverse of all these different relations `posts`. The default inflector refers\nto these as `postsByAuthorId`, `postsByEditorId`, etc. However we'd rather use\nshorter names, so we introduce a new inflector: `getOppositeBaseName`. This\ninflector is passed a baseName (the part without the `_id`/`_fk` suffix, e.g.\n`author`, `editor`, `reviewer` above) and should return the opposite of that\nbase name which will be prepended to the target type to produce, e.g.\n`authoredPosts`, `editedPosts`, `reviewedPosts`. Failing this, we just fall back\nto the default (verbose) inflector; it will be up to you to add smart comments\nor a custom inflector to override these.\n\n## Handling field conflicts:\n\nIn most cases, the conflict errors will guide you on how to fix these issues\nusing [smart comments](https://www.graphile.org/postgraphile/smart-comments/).\n\n## Smart Tags\n\n### `@foreignSimpleFieldName`\n\n`@foreignSimpleFieldName` was added to override the naming of the foreign-side\nof a one-to-many relationship's simple collections field (if you're using simple\ncollections). By default we'll take the `@foreignFieldName` and add the \"list\nsuffix\" (\"List\" by default, but \"\" if `pgOmitListSuffix` is set), but if you\nprefer you can override it entirely with `@foreignSimpleFieldName`. If you set\n`@foreignSimpleFieldName` and you're using `simpleCollections 'both'` then you\nshould also set `@foreignFieldName` explicitly or unexpected things may occur.\n\nApplies to:\n\n- foreign key constraints\n\n### `@listSuffix`\n\n`@listSuffix` allows you to override the default naming on a per-entity basis,\noverriding `pgOmitListSuffix`. For example, with `pgOmitListSuffix: true`, you\ncan apply `@listSuffix include` to have the `-List` suffix appended to the\nsimple collection generated for that table, and remove the `-Connection` suffix\nfrom the Relay connection. When `pgOmitListSuffix` is not `true`, you can use\n`@listSuffix omit` to selectively omit the `-List` suffix on simple collections\nand append `-Connection` to the Relay connection instead.\n\nIf `@listSuffix` is set, the only valid values are `\"omit\"` and `\"include\"`. Any\nother value will cause an error.\n\n|                   | @listSuffix omit    | @listSuffix include |\n| ----------------: | :------------------ | :------------------ |\n|  Relay Connection | companiesConnection | companies           |\n| Simple Collection | companies           | companiesList       |\n\n\u003e NOTE: `@listSuffix` will have no effect when using `@foreignSimpleFieldName`.\n\nApplies to:\n\n- tables\n- foreign key constraints\n- computed column functions returning `SETOF \u003crecord type\u003e`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphile%2Fpg-simplify-inflector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphile%2Fpg-simplify-inflector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphile%2Fpg-simplify-inflector/lists"}