@graphile-contrib/pg-simplify-inflector
Advanced tools
Comparing version 6.1.0 to 7.0.0-alpha.1
@@ -1,2 +0,3 @@ | ||
declare const PgSimplifyInflectorPlugin: import("graphile-build").Plugin; | ||
import type { Plugin } from "graphile-build"; | ||
declare const PgSimplifyInflectorPlugin: Plugin; | ||
export default PgSimplifyInflectorPlugin; |
121
index.js
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function fixCapitalisedPlural(fn) { | ||
@@ -9,3 +11,2 @@ return function (str) { | ||
} | ||
function fixChangePlural(fn) { | ||
@@ -23,3 +24,12 @@ return function (str) { | ||
} | ||
function isPrimaryKey(detailedKeys, table) { | ||
if (!table.primaryKeyConstraint) { | ||
return false; | ||
} | ||
const { keyAttributes } = table.primaryKeyConstraint; | ||
return ( | ||
detailedKeys.length === keyAttributes.length && | ||
detailedKeys.every((key, i) => key === keyAttributes[i]) | ||
); | ||
} | ||
function PgSimplifyInflectorPlugin( | ||
@@ -40,3 +50,2 @@ builder, | ||
pgSimpleCollections === "only" || pgSimpleCollections === "both"; | ||
if ( | ||
@@ -53,6 +62,3 @@ hasSimpleCollections && | ||
} | ||
/** @typedef {import('graphile-build-pg').PgEntity} PgEntity */ | ||
function omitListSuffix(/** @type PgEntity */ entity) { | ||
function omitListSuffix(entity) { | ||
const tag = entity.tags.listSuffix; | ||
@@ -66,23 +72,17 @@ if (tag == null) return !!pgOmitListSuffix; | ||
} | ||
function connectionSuffix(/** @type PgEntity */ entity) { | ||
function connectionSuffix(entity) { | ||
return omitListSuffix(entity) ? "-connection" : ""; | ||
} | ||
function ConnectionSuffix(/** @type PgEntity */ entity) { | ||
function ConnectionSuffix(entity) { | ||
return omitListSuffix(entity) ? "Connection" : ""; | ||
} | ||
function listSuffix(/** @type PgEntity */ entity) { | ||
function listSuffix(entity) { | ||
return omitListSuffix(entity) ? "" : "-list"; | ||
} | ||
function ListSuffix(/** @type PgEntity */ entity) { | ||
function ListSuffix(entity) { | ||
return omitListSuffix(entity) ? "" : "List"; | ||
} | ||
builder.hook("inflection", (oldInflection) => { | ||
return { | ||
...oldInflection, | ||
/* | ||
@@ -94,3 +94,2 @@ * This solves the issue with `blah-table1s` becoming `blahTable1S` | ||
upperCamelCase: fixCapitalisedPlural(oldInflection.upperCamelCase), | ||
/* | ||
@@ -102,3 +101,2 @@ * Pluralize/singularize only supports single words, so only run | ||
singularize: fixChangePlural(oldInflection.singularize), | ||
distinctPluralize(str) { | ||
@@ -124,3 +122,2 @@ const singular = this.singularize(str); | ||
}, | ||
// Fix a naming bug | ||
@@ -132,3 +129,2 @@ deletedNodeId(table) { | ||
}, | ||
getBaseName(columnName) { | ||
@@ -143,3 +139,2 @@ const matches = columnName.match( | ||
}, | ||
baseNameMatches(baseName, otherName) { | ||
@@ -149,3 +144,2 @@ const singularizedName = this.singularize(otherName); | ||
}, | ||
/* This is a good method to override. */ | ||
@@ -171,3 +165,2 @@ getOppositeBaseName(baseName) { | ||
}, | ||
getBaseNameFromKeys(detailedKeys) { | ||
@@ -191,3 +184,2 @@ if (detailedKeys.length === 1) { | ||
}, | ||
...(pgSimplifyPatch | ||
@@ -200,3 +192,2 @@ ? { | ||
: null), | ||
...(pgSimplifyAllRows | ||
@@ -218,3 +209,2 @@ ? { | ||
: null), | ||
computedColumn(pseudoColumnName, proc, _table) { | ||
@@ -228,3 +218,2 @@ return proc.tags.fieldName | ||
}, | ||
computedColumnList(pseudoColumnName, proc, _table) { | ||
@@ -235,4 +224,3 @@ return proc.tags.fieldName | ||
}, | ||
singleRelationByKeys(detailedKeys, table, _foreignTable, constraint) { | ||
singleRelationByKeys(detailedKeys, table, foreignTable, constraint) { | ||
if (constraint.tags.fieldName) { | ||
@@ -245,3 +233,3 @@ return constraint.tags.fieldName; | ||
} | ||
if (this.baseNameMatches(baseName, table.name)) { | ||
if (isPrimaryKey(detailedKeys, foreignTable)) { | ||
return this.camelCase(`${this._singularizedTableName(table)}`); | ||
@@ -252,7 +240,6 @@ } | ||
table, | ||
_foreignTable, | ||
foreignTable, | ||
constraint | ||
); | ||
}, | ||
singleRelationByKeysBackwards( | ||
@@ -271,9 +258,14 @@ detailedKeys, | ||
const baseName = this.getBaseNameFromKeys(detailedKeys); | ||
const oppositeBaseName = baseName && this.getOppositeBaseName(baseName); | ||
if (oppositeBaseName) { | ||
return this.camelCase( | ||
`${oppositeBaseName}-${this._singularizedTableName(table)}` | ||
); | ||
if (baseName) { | ||
const oppositeBaseName = this.getOppositeBaseName(baseName); | ||
if (oppositeBaseName) { | ||
return this.camelCase( | ||
`${oppositeBaseName}-${this._singularizedTableName(table)}` | ||
); | ||
} | ||
if (this.baseNameMatches(baseName, foreignTable.name)) { | ||
return this.camelCase(`${this._singularizedTableName(table)}`); | ||
} | ||
} | ||
if (this.baseNameMatches(baseName, foreignTable.name)) { | ||
if (isPrimaryKey(detailedKeys, table)) { | ||
return this.camelCase(`${this._singularizedTableName(table)}`); | ||
@@ -288,21 +280,21 @@ } | ||
}, | ||
_manyRelationByKeysBase(detailedKeys, table, _foreignTable, _constraint) { | ||
const baseName = this.getBaseNameFromKeys(detailedKeys); | ||
const oppositeBaseName = baseName && this.getOppositeBaseName(baseName); | ||
if (oppositeBaseName) { | ||
return this.camelCase( | ||
`${oppositeBaseName}-${this.distinctPluralize( | ||
this._singularizedTableName(table) | ||
)}` | ||
); | ||
if (baseName) { | ||
const oppositeBaseName = this.getOppositeBaseName(baseName); | ||
if (oppositeBaseName) { | ||
return this.camelCase( | ||
`${oppositeBaseName}-${this.distinctPluralize( | ||
this._singularizedTableName(table) | ||
)}` | ||
); | ||
} | ||
if (this.baseNameMatches(baseName, _foreignTable.name)) { | ||
return this.camelCase( | ||
`${this.distinctPluralize(this._singularizedTableName(table))}` | ||
); | ||
} | ||
} | ||
if (this.baseNameMatches(baseName, _foreignTable.name)) { | ||
return this.camelCase( | ||
`${this.distinctPluralize(this._singularizedTableName(table))}` | ||
); | ||
} | ||
return null; | ||
}, | ||
manyRelationByKeys(detailedKeys, table, foreignTable, constraint) { | ||
@@ -327,2 +319,9 @@ if (constraint.tags.foreignFieldName) { | ||
} | ||
if (isPrimaryKey(detailedKeys, table)) { | ||
return ( | ||
this.camelCase( | ||
`${this.distinctPluralize(this._singularizedTableName(table))}` | ||
) + ConnectionSuffix(constraint) | ||
); | ||
} | ||
return ( | ||
@@ -337,3 +336,2 @@ oldInflection.manyRelationByKeys( | ||
}, | ||
manyRelationByKeysSimple(detailedKeys, table, foreignTable, constraint) { | ||
@@ -355,2 +353,9 @@ if (constraint.tags.foreignSimpleFieldName) { | ||
} | ||
if (isPrimaryKey(detailedKeys, table)) { | ||
return ( | ||
this.camelCase( | ||
`${this.distinctPluralize(this._singularizedTableName(table))}` | ||
) + ListSuffix(constraint) | ||
); | ||
} | ||
return ( | ||
@@ -365,3 +370,2 @@ oldInflection.manyRelationByKeys( | ||
}, | ||
functionQueryName(proc) { | ||
@@ -376,3 +380,2 @@ return this.camelCase( | ||
}, | ||
...(pgShortPk | ||
@@ -394,5 +397,3 @@ ? { | ||
return this.camelCase( | ||
`${this._singularizedTableName( | ||
table | ||
)}-by-${detailedKeys | ||
`${this._singularizedTableName(table)}-by-${detailedKeys | ||
.map((key) => this.column(key)) | ||
@@ -403,3 +404,2 @@ .join("-and-")}` | ||
}, | ||
updateByKeys(detailedKeys, table, constraint) { | ||
@@ -517,3 +517,2 @@ if (constraint.tags.updateFieldName) { | ||
} | ||
module.exports = PgSimplifyInflectorPlugin; | ||
@@ -520,0 +519,0 @@ // Hacks for TypeScript/Babel import |
{ | ||
"name": "@graphile-contrib/pg-simplify-inflector", | ||
"version": "6.1.0", | ||
"version": "7.0.0-alpha.1", | ||
"description": "Simplifies the graphile-build-pg inflector to trim the `ByFooIdAndBarId` from relations", | ||
"main": "index.js", | ||
"scripts": { | ||
"prepack": "tsc && prettier --write index.js", | ||
"postgraphile": "postgraphile --append-plugins \"$(pwd)/index.js\"", | ||
@@ -37,5 +38,7 @@ "lint:fix": "yarn prettier --write", | ||
"devDependencies": { | ||
"postgraphile": "4.6.0", | ||
"prettier": "^2.0.4" | ||
"@types/node": "^15.12.5", | ||
"postgraphile": "^4.12.3", | ||
"prettier": "^2.3.2", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
@@ -199,5 +199,5 @@ # @graphile-contrib/pg-simplify-inflector | ||
overriding `pgOmitListSuffix`. For example, with `pgOmitListSuffix: true`, you | ||
can apply `@listSuffix include` to have the `-List` suffix appended to the simple | ||
collection generated for that table, and remove the `-Connection` suffix from the | ||
Relay connection. When `pgOmitListSuffix` is not `true`, you can use | ||
can apply `@listSuffix include` to have the `-List` suffix appended to the | ||
simple collection generated for that table, and remove the `-Connection` suffix | ||
from the Relay connection. When `pgOmitListSuffix` is not `true`, you can use | ||
`@listSuffix omit` to selectively omit the `-List` suffix on simple collections | ||
@@ -204,0 +204,0 @@ and append `-Connection` to the Relay connection instead. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
496
25527
4
4
1