graphile-utils
Version:
Utilities to help with building graphile-build plugins
62 lines • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.entityIsIdentifiedBy = exports.isProcedure = exports.isClass = exports.isConstraint = exports.isAttribute = void 0;
const parseIdentifierParts_1 = require("./parseIdentifierParts");
function isAttribute(obj) {
return obj.kind === "attribute";
}
exports.isAttribute = isAttribute;
function isConstraint(obj) {
return obj.kind === "constraint";
}
exports.isConstraint = isConstraint;
function isClass(obj) {
return obj.kind === "class";
}
exports.isClass = isClass;
function isProcedure(obj) {
return obj.kind === "procedure";
}
exports.isProcedure = isProcedure;
function entityIsIdentifiedBy(obj, identifier, build) {
const parts = (0, parseIdentifierParts_1.default)(identifier);
if (parts.length === 1) {
const [expectedName] = parts;
return obj.name === expectedName;
}
else if (parts.length === 2) {
const [parentName, expectedName] = parts;
if (isAttribute(obj) || isConstraint(obj)) {
// Parent is a table
const klass = build.pgIntrospectionResultsByKind.class.find((kls) => kls.id === obj.classId);
return obj.name === expectedName && !!klass && klass.name === parentName;
}
else if (isClass(obj) || isProcedure(obj)) {
// Parent is a schema
return obj.name === expectedName && obj.namespaceName === parentName;
}
else {
throw new Error(`Type '${obj.kind}' not supported by makeSmartCommentsPlugin`);
}
}
else if (parts.length === 3) {
const [grandparentName, parentName, expectedName] = parts;
if (isAttribute(obj) || isConstraint(obj)) {
// Parent is a table, grandparent is a schema
const klass = build.pgIntrospectionResultsByKind.class.find((kls) => kls.id === obj.classId);
return (obj.name === expectedName &&
!!klass &&
klass.name === parentName &&
klass.namespaceName === grandparentName);
}
else {
// Parent is a schema; grandparent doesn't make sense
throw new Error(`Identifier '${identifier}' does not make sense for a '${obj.kind}' entity`);
}
}
else {
throw new Error(`makeSmartCommentsPlugin did not know how to interpret match '${identifier}'`);
}
}
exports.entityIsIdentifiedBy = entityIsIdentifiedBy;
//# sourceMappingURL=introspectionHelpers.js.map