diff --git a/forward_engineering/helpers/generalHelper.js b/forward_engineering/helpers/generalHelper.js index a516431..71b3ec4 100644 --- a/forward_engineering/helpers/generalHelper.js +++ b/forward_engineering/helpers/generalHelper.js @@ -173,7 +173,7 @@ const commentDeactivatedStatement = (statement, isActivated = true, isParentActi } const retrieveIsItemActivated = (itemConfig) => { - const value = retrivePropertyFromConfig(itemConfig, 0, "isActivated", true); + const value = itemConfig?.[0]?.["isActivated"]; if (value === undefined) { return true; diff --git a/forward_engineering/helpers/updateHelpers/viewHelper.js b/forward_engineering/helpers/updateHelpers/viewHelper.js index d419136..762707e 100644 --- a/forward_engineering/helpers/updateHelpers/viewHelper.js +++ b/forward_engineering/helpers/updateHelpers/viewHelper.js @@ -76,7 +76,10 @@ const getDropView = child => { const getAlterView = role => { const viewData = [role]; - const optionScript = getOptionsScript(role.compMod.collectionData?.collectionRefsDefinitionsMap || {}, viewData); + const optionScript = getOptionsScript({ + collectionRefsDefinitionsMap: role.compMod.collectionData?.collectionRefsDefinitionsMap || {}, + viewData + }); const viewName = role.name || role.code; const keyspaceName = role.compMod.keyspaceName; if (optionScript) { diff --git a/forward_engineering/helpers/viewHelper.js b/forward_engineering/helpers/viewHelper.js index d399f40..b43ee99 100644 --- a/forward_engineering/helpers/viewHelper.js +++ b/forward_engineering/helpers/viewHelper.js @@ -8,19 +8,44 @@ const { getOptions, getPrimaryKeyList } = require('./tableHelper'); const setDependencies = ({ lodash }) => _ = lodash; -const getColumnNames = (collectionRefsDefinitionsMap, columns) => { - return _.uniq(Object.keys(columns).map(name => { - const id = _.get(columns, [name, 'GUID']); +const columnsToName = column => column.name; + +const joinColumns = (columns = [], isParentActivated) => { + let [activatedColumns, deactivatedColumns] = _.partition(columns, column => _.get(column, 'isActivated', true)); + deactivatedColumns = deactivatedColumns.map(columnsToName).join(', '); + activatedColumns = activatedColumns.map(columnsToName).join(', '); + const getDeactivatedStatement = columns => commentDeactivatedStatement(columns, false, isParentActivated, 'INLINE'); + if (!deactivatedColumns.length) { + return activatedColumns; + } else if (!activatedColumns.length) { + return getDeactivatedStatement(deactivatedColumns); + } + return activatedColumns + ' ' + getDeactivatedStatement(', ' + deactivatedColumns); +}; - const itemDataId = Object.keys(collectionRefsDefinitionsMap).find(viewFieldId => { - const definitionData = collectionRefsDefinitionsMap[viewFieldId]; +const getColumn = (collectionRefsDefinitionsMap, id, columns = {}) => { + const itemData = collectionRefsDefinitionsMap[id]; - return definitionData.definitionId === id; - }); - const itemData = collectionRefsDefinitionsMap[itemDataId]; + return Object.values(columns).find(column => column.GUID === itemData?.definitionId) || {}; +}; - return `"${_.get(itemData, 'name', name)}"`; - })).filter(_.identity); +const getColumnNames = (collectionRefsDefinitionsMap, columns) => { + return _.uniqBy( + Object.keys(columns).map(name => { + const id = _.get(columns, [name, 'GUID']); + + const itemDataId = Object.keys(collectionRefsDefinitionsMap).find(viewFieldId => { + const definitionData = collectionRefsDefinitionsMap[viewFieldId]; + + return definitionData.definitionId === id; + }); + const itemData = collectionRefsDefinitionsMap[itemDataId]; + const columnName = `"${_.get(itemData, 'name', name)}"`; + const isActivated = _.get(columns[name], 'isActivated'); + + return { name: columnName, isActivated }; + }), 'name' + ).filter(_.identity); }; const getViewColumns = (collectionRefsDefinitionsMap, properties) => { @@ -41,67 +66,79 @@ const getViewColumns = (collectionRefsDefinitionsMap, properties) => { }, {}); }; -const getWhereStatement = columnsNames => { - if (_.isEmpty(columnsNames)) { +const getWhereStatement = (columns = [], isParentActivated) => { + const [activatedColumns, deactivatedColumns] = _.partition(columns, column => _.get(column, 'isActivated', true)) + if (_.isEmpty(columns) || deactivatedColumns.length === columns.length) { return ''; } - return 'WHERE ' + columnsNames - .reduce((statement, name) => { + + const generateNewStatement = name => `AND ${name} IS NOT NULL`; + const deactivatedColumnsScript = deactivatedColumns.map(column => generateNewStatement(column.name)).join(' '); + const commentDeactivatedColumnsScript = commentDeactivatedStatement(deactivatedColumnsScript, false, isParentActivated, 'INLINE'); + const activatedColumnsScript = [...activatedColumns] + .reduce((statement, column) => { if (!statement) { - return `${name} IS NOT NULL`; + return `${column.name} IS NOT NULL`; } - return `${statement} AND ${name} IS NOT NULL`; + return `${statement} ${generateNewStatement(column.name)}`; }, ''); + + return `WHERE ${activatedColumnsScript}${deactivatedColumnsScript ? ' ' + commentDeactivatedColumnsScript : ''}`; }; -const getNamesByIds = (collectionRefsDefinitionsMap, ids) => { +const getNamesByIds = (collectionRefsDefinitionsMap, ids, columns = {}) => { return ids.reduce((hash, id) => { const name = _.get(collectionRefsDefinitionsMap, [id, 'name']); + const column = getColumn(collectionRefsDefinitionsMap, id, columns); + const isActivated = column.isActivated; if (!name) { return hash; } return Object.assign({}, hash, { - [id]: { name: name } + [id]: { name: name, isActivated } }); }, {}); }; -const getClusteringKeyData = (collectionRefsDefinitionsMap, viewData) => { +const getClusteringKeyData = (collectionRefsDefinitionsMap, viewData, columns) => { const clusteringKeys = retrivePropertyFromConfig(viewData, 0, 'compositeClusteringKey', []); const clusteringKeysHash = getNamesByIds( collectionRefsDefinitionsMap, - clusteringKeys.map(key => key.keyId) + clusteringKeys.map(key => key.keyId), + columns ); return { clusteringKeys, clusteringKeysHash }; }; -const getPrimaryKeysNames = (collectionRefsDefinitionsMap, viewData) => { +const getPrimaryKeys = (collectionRefsDefinitionsMap, viewData, columns) => { return [ - ...getCompositeKeysNames(collectionRefsDefinitionsMap, viewData, 'compositePartitionKey'), - ...getCompositeKeysNames(collectionRefsDefinitionsMap, viewData, 'compositeClusteringKey'), + ...getCompositeKeys({ collectionRefsDefinitionsMap, viewData, key: 'compositePartitionKey', columns }), + ...getCompositeKeys({ collectionRefsDefinitionsMap, viewData, key: 'compositeClusteringKey', columns }), ]; }; -const getCompositeKeysNames = (collectionRefsDefinitionsMap, viewData, key) => { +const getCompositeKeys = ({ collectionRefsDefinitionsMap, viewData, key, columns }) => { const partitionKeys = retrivePropertyFromConfig(viewData, 0, key, []); const partitionKeysHash = getNamesByIds( collectionRefsDefinitionsMap, - partitionKeys.map(key => key.keyId) + partitionKeys.map(key => key.keyId), + columns ); - return _.values(partitionKeysHash).filter(_.identity).map(field => `"${field.name}"`); + return _.values(partitionKeysHash).filter(_.identity).map(field => ({ ...field, name: `"${field.name}"` })); }; -const getPrimaryKeyScript = (collectionRefsDefinitionsMap, viewData, isParentActivated) => { +const getPrimaryKeyScript = ({ collectionRefsDefinitionsMap, viewData, isParentActivated, columns }) => { const partitionKeys = retrivePropertyFromConfig(viewData, 0, 'compositePartitionKey', []); const partitionKeysHash = getNamesByIds( collectionRefsDefinitionsMap, - partitionKeys.map(key => key.keyId) + partitionKeys.map(key => key.keyId), + columns ); - const clusteringKeyData = getClusteringKeyData(collectionRefsDefinitionsMap, viewData); + const clusteringKeyData = getClusteringKeyData(collectionRefsDefinitionsMap, viewData, columns); const keysList = getPrimaryKeyList(partitionKeysHash, clusteringKeyData.clusteringKeysHash, isParentActivated); if (!keysList) { @@ -113,9 +150,9 @@ const getPrimaryKeyScript = (collectionRefsDefinitionsMap, viewData, isParentAct const addTab = script => _.trim(script || '').replace(/ /g, ' '); -const getOptionsScript = (collectionRefsDefinitionsMap, viewData) => { +const getOptionsScript = ({ collectionRefsDefinitionsMap, viewData, columns, isParentActivated }) => { setDependencies(dependencies); - const clusteringKeyData = getClusteringKeyData(collectionRefsDefinitionsMap, viewData); + const clusteringKeyData = getClusteringKeyData(collectionRefsDefinitionsMap, viewData, columns); const tableComment = retrivePropertyFromConfig(viewData, 0, 'comments', ''); const tableOptions = retrivePropertyFromConfig(viewData, 0, 'tableOptions', ''); @@ -124,11 +161,20 @@ const getOptionsScript = (collectionRefsDefinitionsMap, viewData) => { clusteringKeyData.clusteringKeysHash, '', tableOptions, - tableComment + tableComment, + isParentActivated )); }; -const getEmptyViewScript = ({ viewData, entitySchema, entityData, entityName, isViewChildrenActivated }) => { +const getEmptyViewScript = ({ + viewData, + entitySchema, + entityData, + entityName, + isParentActivated, + collectionRefsDefinitionsMap, + columns +}) => { let script = []; const entityColumns = entitySchema?.properties || {}; @@ -140,11 +186,16 @@ const getEmptyViewScript = ({ viewData, entitySchema, entityData, entityName, is [field.GUID]: field, }; }, {}); - const primaryKeyScript = getPrimaryKeyScript(entityFieldsMap, entityData, isViewChildrenActivated); - const optionsScript = getOptionsScript({}, viewData); + const primaryKeyScript = getPrimaryKeyScript({ entityFieldsMap, entityData, isParentActivated, columns }); + const optionsScript = getOptionsScript({ + collectionRefsDefinitionsMap: {}, + viewData, + columns, + isParentActivated + }); script.push(`AS SELECT * FROM ${entityName}`); - script.push(getWhereStatement(getPrimaryKeysNames(collectionRefsDefinitionsMap, entityData))); + script.push(getWhereStatement(getPrimaryKeys(collectionRefsDefinitionsMap, entityData, columns), isParentActivated)); if (primaryKeyScript) { script.push(primaryKeyScript); @@ -181,28 +232,36 @@ module.exports = { const name = bucketName ? `"${bucketName}"."${viewName}"` : `"${viewName}"`; const isViewActivated = retrieveIsItemActivated(entityData) && retrieveIsItemActivated(viewData); - const isViewChildrenActivated = isKeyspaceActivated && isViewActivated; + const isParentActivated = isKeyspaceActivated && isViewActivated; - const primaryKeyScript = getPrimaryKeyScript(collectionRefsDefinitionsMap, viewData, isViewChildrenActivated); - const optionsScript = getOptionsScript(collectionRefsDefinitionsMap, viewData); + const primaryKeyScript = getPrimaryKeyScript({ collectionRefsDefinitionsMap, viewData, isParentActivated, columns }); + const optionsScript = getOptionsScript({ collectionRefsDefinitionsMap, viewData, columns, isParentActivated }); script.push(`CREATE MATERIALIZED VIEW ${ifNotExist? `IF NOT EXISTS `:``}${name}`); if (_.isEmpty(columns)) { - script.concat(getEmptyViewScript({ + script = script.concat(getEmptyViewScript({ viewData, entitySchema, entityData, entityName: tableName, - isViewChildrenActivated, + isParentActivated, + collectionRefsDefinitionsMap, + columns })); } else { - const columnsNames = getColumnNames(collectionRefsDefinitionsMap, columns); - script.push(`AS SELECT ${columnsNames.join(', ')}`); + const columnsData = getColumnNames(collectionRefsDefinitionsMap, columns); + const joinedColumns = joinColumns(columnsData, isParentActivated); + script.push(`AS SELECT ${joinedColumns}`); script.push(`FROM ${tableName}`); - script.push(getWhereStatement(_.uniq([ - ...getPrimaryKeysNames(collectionRefsDefinitionsMap, viewData), - ...getCompositeKeysNames(collectionRefsDefinitionsMap, viewData, 'whereClause') - ]))); + script.push( + getWhereStatement( + _.uniq([ + ...getPrimaryKeys(collectionRefsDefinitionsMap, viewData, columns), + ...getCompositeKeys({ collectionRefsDefinitionsMap, viewData, key: 'whereClause', columns }) + ]), + isParentActivated + ) + ); if (primaryKeyScript) { script.push(primaryKeyScript); }