diff --git a/forward_engineering/services/statements/getPrimaryKeySampleByStructure.js b/forward_engineering/services/statements/getPrimaryKeySampleByStructure.js index 82f2de4..9246a7f 100644 --- a/forward_engineering/services/statements/getPrimaryKeySampleByStructure.js +++ b/forward_engineering/services/statements/getPrimaryKeySampleByStructure.js @@ -3,6 +3,7 @@ * @typedef {import('../../../shared/types').UUID} UUID */ const RandExp = require('randexp'); +const { get, isEmpty, isPlainObject } = require('lodash'); const { PK_SEGMENT_TYPE } = require('../../../shared/constants'); /** @@ -10,7 +11,9 @@ const { PK_SEGMENT_TYPE } = require('../../../shared/constants'); * @returns {string} */ const getPrimaryKeySampleByStructure = ({ collection, jsonData }) => { - const keyField = Object.values(collection.properties || {}).find(field => field.primaryKeyStructure); + const keyField = Object.values(collection.properties || {}).find( + field => field.primaryKey && !isEmpty(field.primaryKeyStructure), + ); const primaryKeyStructure = keyField?.primaryKeyStructure; if (!Array.isArray(primaryKeyStructure)) { @@ -42,14 +45,11 @@ const getPrimaryKeySampleByStructure = ({ collection, jsonData }) => { * @returns {string} */ const getPrimaryKeyStructureFieldValue = ({ segment, collection, jsonData }) => { - const fieldNames = (segment.segmentKey || []).map(({ keyId }) => { - return findFieldNameById({ collection, id: keyId }); + const fieldNamePaths = (segment.segmentKey || []).map(({ keyId }) => { + return getFieldNamePath({ collection, keyId }); }); - return fieldNames - .filter(Boolean) - .map(fieldName => jsonData[fieldName]) - .join(''); + return fieldNamePaths.map(fieldNamePath => get(jsonData, fieldNamePath)).join(''); }; /** @@ -67,21 +67,41 @@ const getPrimaryKeyStructurePatternValue = ({ segment }) => { }; /** - * @param {{ collection: object, id: UUID }} - * @returns {string} + * @param {{ collection: object, keyId: UUID }} + * @returns {string[]} */ -const findFieldNameById = ({ collection, id }) => { - return Object.entries(collection.properties || {}).reduce((result, [fieldName, field]) => { - if (result) { - return result; - } +const getFieldNamePath = ({ collection, keyId }) => { + const properties = getCollectionProperties({ collection }); - if (field.GUID === id) { - return fieldName; + return Object.entries(properties).reduce((result, [fieldName, field]) => { + if (field.GUID === keyId) { + return [...result, fieldName]; } - return findFieldNameById({ collection: field, id }); - }, ''); + const namePath = getFieldNamePath({ collection: field, keyId }); + + return namePath.length ? [...result, fieldName, ...namePath] : result; + }, []); +}; + +/** + * @param {{ collection: object }} + * @returns {object} + */ +const getCollectionProperties = ({ collection }) => { + if (collection.properties) { + return collection.properties; + } + + if (Array.isArray(collection.items)) { + return collection.items.reduce((result, item, index) => ({ ...result, [index]: item }), {}); + } + + if (isPlainObject(collection.items)) { + return collection.items; + } + + return {}; }; module.exports = { diff --git a/forward_engineering/services/statements/insertStatements.js b/forward_engineering/services/statements/insertStatements.js index d27de31..d24febf 100644 --- a/forward_engineering/services/statements/insertStatements.js +++ b/forward_engineering/services/statements/insertStatements.js @@ -32,7 +32,7 @@ const getInsertScriptForCollection = ({ jsonData, collection }) => { } const keyPropertyName = Object.keys(collection?.properties ?? {}).find(propertyName => - Boolean(collection?.properties?.[propertyName]['']), + Boolean(collection?.properties?.[propertyName]?.primaryKey), ); const insertionPath = getKeySpaceReference(collection); diff --git a/properties_pane/field_level/fieldLevelConfig.json b/properties_pane/field_level/fieldLevelConfig.json index 0a15f64..937402e 100644 --- a/properties_pane/field_level/fieldLevelConfig.json +++ b/properties_pane/field_level/fieldLevelConfig.json @@ -140,6 +140,10 @@ making sure that you maintain a proper JSON format. "propertyKeyword": "primaryKeyStructure", "propertyTooltip": "For user-defined key structure, describe the different segments for the PK", "propertyType": "group", + "groupControl": true, + "templateOptions": { + "hasToggler": true + }, "structure": [ { "propertyName": "Segment type", @@ -175,6 +179,10 @@ making sure that you maintain a proper JSON format. "propertyType": "fieldList", "template": "orderedList", "parentType": "primaryKeyStructure", + "disabledItemStrategy": { + "type": "allowTypes", + "parameters": ["string", "number"] + }, "dependency": { "key": "segmentType", "value": "field" @@ -224,7 +232,7 @@ making sure that you maintain a proper JSON format. } ], "dependency": { - "key": "containerLevelKey", + "key": "primaryKey", "value": true } },