@aws-amplify/appsync-modelgen-plugin
Version:
57 lines • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConnectedFieldsForHasOne = exports.processHasOneConnection = void 0;
const process_connections_1 = require("./process-connections");
const process_connections_v2_1 = require("./process-connections-v2");
const fieldUtils_1 = require("./fieldUtils");
const fieldUtils_2 = require("./fieldUtils");
function processHasOneConnection(field, model, modelMap, connectionDirective, isCustomPKEnabled = false, shouldUseFieldsInAssociatedWithInHasOne = false) {
const otherSide = modelMap[field.type];
const otherSideBelongsToField = (0, fieldUtils_2.getOtherSideBelongsToField)(model.name, otherSide);
if (field.isList || (otherSideBelongsToField && otherSideBelongsToField.isList)) {
throw new Error("A hasOne relationship should be 1:1, no lists");
}
const connectionFields = connectionDirective.arguments.fields || [];
const references = connectionDirective.arguments.references || [];
let associatedWithFields;
if (references.length > 0) {
const associatedWithNativeReferences = (0, process_connections_v2_1.getConnectedFieldForReferences)(field, model, otherSide, connectionDirective.name);
associatedWithFields = references.map((reference) => otherSide.fields.find((field) => reference === field.name));
return {
kind: process_connections_1.CodeGenConnectionType.HAS_ONE,
associatedWith: associatedWithFields[0],
associatedWithFields,
associatedWithNativeReferences,
connectedModel: otherSide,
isConnectingFieldAutoCreated: false,
};
}
else if (isCustomPKEnabled) {
associatedWithFields = getConnectedFieldsForHasOne(otherSideBelongsToField, otherSide, shouldUseFieldsInAssociatedWithInHasOne);
}
else {
const otherSideField = (0, process_connections_v2_1.getConnectedFieldV2)(field, model, otherSide, connectionDirective.name);
associatedWithFields = [otherSideField];
}
const isConnectingFieldAutoCreated = connectionFields.length === 0;
const targetNames = connectionFields.length > 0
? [...connectionFields]
: (isCustomPKEnabled
? (0, fieldUtils_1.getModelPrimaryKeyComponentFields)(otherSide).map(componentField => (0, process_connections_1.makeConnectionAttributeName)(model.name, field.name, componentField.name))
: [(0, process_connections_1.makeConnectionAttributeName)(model.name, field.name)]);
return {
kind: process_connections_1.CodeGenConnectionType.HAS_ONE,
associatedWith: associatedWithFields[0],
associatedWithFields,
connectedModel: otherSide,
isConnectingFieldAutoCreated,
targetName: targetNames[0],
targetNames,
};
}
exports.processHasOneConnection = processHasOneConnection;
function getConnectedFieldsForHasOne(otherSideBelongsToField, otherSide, shouldUseFieldsInAssociatedWithInHasOne = false) {
return (!shouldUseFieldsInAssociatedWithInHasOne && otherSideBelongsToField) ? [otherSideBelongsToField] : (0, fieldUtils_1.getModelPrimaryKeyComponentFields)(otherSide);
}
exports.getConnectedFieldsForHasOne = getConnectedFieldsForHasOne;
//# sourceMappingURL=process-has-one.js.map