@graphile-contrib/pg-simplify-inflector
Advanced tools
Comparing version 4.0.0-alpha.0 to 5.0.0-beta.0
247
index.js
@@ -1,11 +0,1 @@ | ||
/* | ||
* This plugin removes the 'ByFooIdAndBarId' from the end of relations. | ||
* | ||
* If this results in a field conflict in your GraphQL schema, use smart | ||
* comments to rename the conflicting foreign key constraint: | ||
* | ||
* https://www.graphile.org/postgraphile/smart-comments/#renaming | ||
* | ||
*/ | ||
function fixCapitalisedPlural(fn) { | ||
@@ -25,2 +15,4 @@ return function(str) { | ||
pgSimplifyAllRows = true, | ||
pgShortPk = true, | ||
nodeIdFieldName = "nodeId", | ||
} | ||
@@ -32,8 +24,2 @@ ) { | ||
if (hasConnections && hasSimpleCollections && pgOmitListSuffix) { | ||
throw new Error( | ||
"Cannot omit -list suffix (`pgOmitListSuffix`) if both relay connections and simple collections are enabled." | ||
); | ||
} | ||
if ( | ||
@@ -45,2 +31,3 @@ hasSimpleCollections && | ||
) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
@@ -51,2 +38,7 @@ "You can simplify the inflector further by adding `{graphileBuildOptions: {pgOmitListSuffix: true}}` to the options passed to PostGraphile, however be aware that doing so will mean that later enabling relay connections will be a breaking change. To dismiss this message, set `pgOmitListSuffix` to false instead." | ||
const connectionSuffix = pgOmitListSuffix ? "-connection" : ""; | ||
const ConnectionSuffix = pgOmitListSuffix ? "Connection" : ""; | ||
const listSuffix = pgOmitListSuffix ? "" : "-list"; | ||
const ListSuffix = pgOmitListSuffix ? "" : "List"; | ||
builder.hook("inflection", oldInflection => { | ||
@@ -84,5 +76,12 @@ return { | ||
// Fix a naming bug | ||
deletedNodeId(table) { | ||
return this.camelCase( | ||
`deleted-${this.singularize(table.name)}-${nodeIdFieldName}` | ||
); | ||
}, | ||
getBaseName(columnName) { | ||
const matches = columnName.match( | ||
/^(.+?)(_row_id|_id|_uuid|_fk|RowId|Id|Uuid|UUID|Fk)$/ | ||
/^(.+?)(_row_id|_id|_uuid|_fk|_pk|RowId|Id|Uuid|UUID|Fk|Pk)$/ | ||
); | ||
@@ -142,3 +141,4 @@ if (matches) { | ||
return this.camelCase( | ||
this.distinctPluralize(this._singularizedTableName(table)) | ||
this.distinctPluralize(this._singularizedTableName(table)) + | ||
connectionSuffix | ||
); | ||
@@ -149,3 +149,3 @@ }, | ||
this.distinctPluralize(this._singularizedTableName(table)) + | ||
(pgOmitListSuffix ? "" : "-list") | ||
listSuffix | ||
); | ||
@@ -156,2 +156,16 @@ }, | ||
computedColumn(pseudoColumnName, proc, _table) { | ||
return proc.tags.fieldName | ||
? proc.tags.fieldName + (proc.returnsSet ? ConnectionSuffix : "") | ||
: this.camelCase( | ||
pseudoColumnName + (proc.returnsSet ? connectionSuffix : "") | ||
); | ||
}, | ||
computedColumnList(pseudoColumnName, proc, _table) { | ||
return proc.tags.fieldName | ||
? proc.tags.fieldName + ListSuffix | ||
: this.camelCase(pseudoColumnName + listSuffix); | ||
}, | ||
singleRelationByKeys(detailedKeys, table, _foreignTable, constraint) { | ||
@@ -206,6 +220,3 @@ if (constraint.tags.fieldName) { | ||
manyRelationByKeys(detailedKeys, table, _foreignTable, constraint) { | ||
if (constraint.tags.foreignFieldName) { | ||
return constraint.tags.foreignFieldName; | ||
} | ||
_manyRelationByKeysBase(detailedKeys, table, _foreignTable, _constraint) { | ||
const baseName = this.getBaseNameFromKeys(detailedKeys); | ||
@@ -225,25 +236,201 @@ const oppositeBaseName = baseName && this.getOppositeBaseName(baseName); | ||
} | ||
return oldInflection.manyRelationByKeys( | ||
return null; | ||
}, | ||
manyRelationByKeys(detailedKeys, table, foreignTable, constraint) { | ||
if (constraint.tags.foreignFieldName) { | ||
if (constraint.tags.foreignSimpleFieldName) { | ||
return constraint.tags.foreignFieldName; | ||
} else { | ||
return constraint.tags.foreignFieldName + ConnectionSuffix; | ||
} | ||
} | ||
const base = this._manyRelationByKeysBase( | ||
detailedKeys, | ||
table, | ||
_foreignTable, | ||
foreignTable, | ||
constraint | ||
); | ||
if (base) { | ||
return base + ConnectionSuffix; | ||
} | ||
return ( | ||
oldInflection.manyRelationByKeys( | ||
detailedKeys, | ||
table, | ||
foreignTable, | ||
constraint | ||
) + ConnectionSuffix | ||
); | ||
}, | ||
manyRelationByKeysSimple(detailedKeys, table, _foreignTable, constraint) { | ||
manyRelationByKeysSimple(detailedKeys, table, foreignTable, constraint) { | ||
if (constraint.tags.foreignSimpleFieldName) { | ||
return constraint.tags.foreignSimpleFieldName; | ||
} | ||
return this.camelCase( | ||
this.manyRelationByKeys( | ||
if (constraint.tags.foreignFieldName) { | ||
return constraint.tags.foreignFieldName + ListSuffix; | ||
} | ||
const base = this._manyRelationByKeysBase( | ||
detailedKeys, | ||
table, | ||
foreignTable, | ||
constraint | ||
); | ||
if (base) { | ||
return base + ListSuffix; | ||
} | ||
return ( | ||
oldInflection.manyRelationByKeys( | ||
detailedKeys, | ||
table, | ||
_foreignTable, | ||
foreignTable, | ||
constraint | ||
) + (pgOmitListSuffix ? "" : "-list") | ||
) + ListSuffix | ||
); | ||
}, | ||
functionQueryName(proc) { | ||
return this.camelCase( | ||
this._functionName(proc) + (proc.returnsSet ? connectionSuffix : "") | ||
); | ||
}, | ||
functionQueryNameList(proc) { | ||
return this.camelCase(this._functionName(proc) + listSuffix); | ||
}, | ||
...(pgShortPk | ||
? { | ||
tableNode(table) { | ||
return this.camelCase( | ||
`${this._singularizedTableName(table)}-by-${nodeIdFieldName}` | ||
); | ||
}, | ||
rowByUniqueKeys(detailedKeys, table, constraint) { | ||
if (constraint.tags.fieldName) { | ||
return constraint.tags.fieldName; | ||
} | ||
if (constraint.type === "p") { | ||
// Primary key, shorten! | ||
return this.camelCase(this._singularizedTableName(table)); | ||
} else { | ||
return this.camelCase( | ||
`${this._singularizedTableName(table)}-by-${detailedKeys | ||
.map(key => this.column(key)) | ||
.join("-and-")}` | ||
); | ||
} | ||
}, | ||
updateByKeys(detailedKeys, table, constraint) { | ||
if (constraint.tags.updateFieldName) { | ||
return constraint.tags.updateFieldName; | ||
} | ||
if (constraint.type === "p") { | ||
return this.camelCase( | ||
`update-${this._singularizedTableName(table)}` | ||
); | ||
} else { | ||
return this.camelCase( | ||
`update-${this._singularizedTableName( | ||
table | ||
)}-by-${detailedKeys | ||
.map(key => this.column(key)) | ||
.join("-and-")}` | ||
); | ||
} | ||
}, | ||
deleteByKeys(detailedKeys, table, constraint) { | ||
if (constraint.tags.deleteFieldName) { | ||
return constraint.tags.deleteFieldName; | ||
} | ||
if (constraint.type === "p") { | ||
// Primary key, shorten! | ||
return this.camelCase( | ||
`delete-${this._singularizedTableName(table)}` | ||
); | ||
} else { | ||
return this.camelCase( | ||
`delete-${this._singularizedTableName( | ||
table | ||
)}-by-${detailedKeys | ||
.map(key => this.column(key)) | ||
.join("-and-")}` | ||
); | ||
} | ||
}, | ||
updateByKeysInputType(detailedKeys, table, constraint) { | ||
if (constraint.tags.updateFieldName) { | ||
return this.upperCamelCase( | ||
`${constraint.tags.updateFieldName}-input` | ||
); | ||
} | ||
if (constraint.type === "p") { | ||
// Primary key, shorten! | ||
return this.upperCamelCase( | ||
`update-${this._singularizedTableName(table)}-input` | ||
); | ||
} else { | ||
return this.upperCamelCase( | ||
`update-${this._singularizedTableName( | ||
table | ||
)}-by-${detailedKeys | ||
.map(key => this.column(key)) | ||
.join("-and-")}-input` | ||
); | ||
} | ||
}, | ||
deleteByKeysInputType(detailedKeys, table, constraint) { | ||
if (constraint.tags.deleteFieldName) { | ||
return this.upperCamelCase( | ||
`${constraint.tags.deleteFieldName}-input` | ||
); | ||
} | ||
if (constraint.type === "p") { | ||
// Primary key, shorten! | ||
return this.upperCamelCase( | ||
`delete-${this._singularizedTableName(table)}-input` | ||
); | ||
} else { | ||
return this.upperCamelCase( | ||
`delete-${this._singularizedTableName( | ||
table | ||
)}-by-${detailedKeys | ||
.map(key => this.column(key)) | ||
.join("-and-")}-input` | ||
); | ||
} | ||
}, | ||
updateNode(table) { | ||
return this.camelCase( | ||
`update-${this._singularizedTableName( | ||
table | ||
)}-by-${nodeIdFieldName}` | ||
); | ||
}, | ||
deleteNode(table) { | ||
return this.camelCase( | ||
`delete-${this._singularizedTableName( | ||
table | ||
)}-by-${nodeIdFieldName}` | ||
); | ||
}, | ||
updateNodeInputType(table) { | ||
return this.upperCamelCase( | ||
`update-${this._singularizedTableName( | ||
table | ||
)}-by-${nodeIdFieldName}-input` | ||
); | ||
}, | ||
deleteNodeInputType(table) { | ||
return this.upperCamelCase( | ||
`delete-${this._singularizedTableName( | ||
table | ||
)}-by-${nodeIdFieldName}-input` | ||
); | ||
}, | ||
} | ||
: null), | ||
}; | ||
}); | ||
}; |
{ | ||
"name": "@graphile-contrib/pg-simplify-inflector", | ||
"version": "4.0.0-alpha.0", | ||
"version": "5.0.0-beta.0", | ||
"description": "Simplifies the graphile-build-pg inflector to trim the `ByFooIdAndBarId` from relations", | ||
@@ -31,3 +31,4 @@ "main": "index.js", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -34,0 +35,0 @@ "devDependencies": { |
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
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
20621
5
406