diff --git a/forward_engineering/configs/templates.js b/forward_engineering/configs/templates.js index 062efea..c524579 100644 --- a/forward_engineering/configs/templates.js +++ b/forward_engineering/configs/templates.js @@ -34,7 +34,7 @@ module.exports = { 'CREATE${orReplace}PROCEDURE ${name} (${arguments})\nAS $$\n${statement}\n$$ LANGUAGE plpgsql${securityMode}${configurationParameter};\n', columnDefinition: - '"${name}" ${type}${default}${encoding}${distKey}${sortKey}${notNull}${unique}${primaryKey}${inlineConstraints}${references}', + '"${name}" ${type}${default}${identity}${encoding}${distKey}${sortKey}${notNull}${unique}${primaryKey}${inlineConstraints}${references}', compoundSortKey: '${sortStyle} SORTKEY (${keys})', compoundUniqueKey: 'UNIQUE (${keys})', diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index 8fbc2ee..6196586 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -30,11 +30,13 @@ module.exports = (baseProvider, options, app) => { parseProps, getRowFormat, getStoredAs, + getIdentityDefinition, } = require('./helpers/general')(app); const { decorateType, getDefault, getQuota, + getIdentity, getUri, getARN, getSourceSchemaNameForExternalSchema, @@ -307,6 +309,7 @@ module.exports = (baseProvider, options, app) => { default: !_.isUndefined(columnDefinition.default) ? ' DEFAULT ' + getDefault(columnDefinition.type, columnDefinition.default) : '', + identity: getIdentity(columnDefinition.identity), distKey: columnDefinition.distKey ? ' DISTKEY' : '', sortKey: columnDefinition.sortKey ? ' SORTKEY' : '', primaryKey: columnDefinition.primaryKey ? ' PRIMARY KEY' : '', @@ -487,7 +490,8 @@ module.exports = (baseProvider, options, app) => { sortKey: jsonSchema.sortKey && !jsonSchema.compositeSortKey, primaryKey: columnDefinition.primaryKey && !jsonSchema.compositePrimaryKey, encoding: jsonSchema.encoding, - references: '', + identity: getIdentityDefinition(jsonSchema), + reference: '', }; }, diff --git a/forward_engineering/helpers/columnDefinitionHelper.js b/forward_engineering/helpers/columnDefinitionHelper.js index 2e6b4ec..166c5ed 100644 --- a/forward_engineering/helpers/columnDefinitionHelper.js +++ b/forward_engineering/helpers/columnDefinitionHelper.js @@ -1,4 +1,4 @@ -const _ = require('lodash'); +const { toUpper, toLower, chain, partition, get } = require('lodash'); const { commentIfDeactivated } = require('./commentDeactivatedHelper'); module.exports = app => { @@ -7,23 +7,23 @@ module.exports = app => { const { toString } = require('./general')(app); const decorateType = (type, columnDefinition) => { - type = _.toUpper(type); + type = toUpper(type); let resultType = type; if (isTimestamp(type)) { - if (columnDefinition.timePrecision && !_.isNaN(columnDefinition.timePrecision)) { + if (columnDefinition.timePrecision && !Number.isNaN(columnDefinition.timePrecision)) { resultType = `${type}(${columnDefinition.timePrecision})`; } } if (['VARCHAR', 'CHAR', 'CHARACTER', 'VARBYTE'].includes(type)) { - if (columnDefinition.length && !_.isNaN(columnDefinition.length)) { + if (columnDefinition.length && !Number.isNaN(columnDefinition.length)) { resultType = `${type}(${columnDefinition.length})`; } } if (['NUMBER', 'DECIMAL', 'NUMERIC'].includes(type)) { - if (!isNaN(columnDefinition.scale) && !isNaN(columnDefinition.precision)) { + if (!Number.isNaN(columnDefinition.scale) && !Number.isNaN(columnDefinition.precision)) { resultType = `${type}(${Number(columnDefinition.precision)},${Number(columnDefinition.scale)})`; } } @@ -33,23 +33,42 @@ module.exports = app => { const isString = type => ['VARCHAR', 'NVARCHAR', 'TEXT', 'CHAR', 'CHARACTER VARYING', 'BPCHAR', 'CHARACTER', 'NCHAR'].includes( - _.toUpper(type), + toUpper(type), ); - const isTimestamp = type => ['TIME', 'TIMESTAMP', 'TIMESTAMPTZ', 'TIMETZ'].includes(_.toUpper(type)); + const isTimestamp = type => ['TIME', 'TIMESTAMP', 'TIMESTAMPTZ', 'TIMETZ'].includes(toUpper(type)); const escapeString = str => str.replace(/^'([\S\s]+)'$/, '$1').replace(/'/g, "''"); const getDefault = (type, defaultValue) => { const constantsValues = ['current_timestamp', 'current_user', 'null']; - if (isString(type) && !constantsValues.includes(_.toLower(defaultValue))) { + if (isString(type) && !constantsValues.includes(toLower(defaultValue))) { return `'${escapeString(String(defaultValue))}'`; - } else if (_.toUpper(type) === 'BOOLEAN') { - return _.toUpper(defaultValue); - } else { - return defaultValue; + } else if (toUpper(type) === 'BOOLEAN') { + return toUpper(defaultValue); } + + return defaultValue; + }; + + const hasValue = value => value !== '' && value !== null && value !== undefined; + + const getIdentity = identity => { + if (!identity) { + return ''; + } + + const prefix = identity.generateIdentity === 'by default' ? 'GENERATED BY DEFAULT AS IDENTITY' : 'IDENTITY'; + + if (!hasValue(identity.seed) && !hasValue(identity.step)) { + return ` ${prefix}`; + } + + const seed = Number(get(identity, 'seed', 0)); + const step = Number(get(identity, 'step', 1)); + + return ` ${prefix}(${seed}, ${step})`; }; const getQuota = (quotaSize, isUnlimited) => { @@ -92,7 +111,7 @@ module.exports = app => { }; const getColumnComments = (tableName, columnDefinitions) => { - return _.chain(columnDefinitions) + return chain(columnDefinitions) .filter('comment') .map(columnData => { const comment = assignTemplates(templates.comment, { @@ -110,7 +129,7 @@ module.exports = app => { const createColumnsStatements = columns => columns.map(column => column.statement).join(',\n\t'); const getColumnsDefinitions = (columns, isParentActivated) => { - const [activatedColumns, deactivatedColumns] = _.partition( + const [activatedColumns, deactivatedColumns] = partition( columns, column => !isParentActivated || column?.isActivated, ); @@ -126,6 +145,7 @@ module.exports = app => { return { getQuota, + getIdentity, getSourceSchemaNameForExternalSchema, getUri, getARN, diff --git a/forward_engineering/helpers/general.js b/forward_engineering/helpers/general.js index 5195899..5ab8a17 100644 --- a/forward_engineering/helpers/general.js +++ b/forward_engineering/helpers/general.js @@ -219,6 +219,18 @@ module.exports = app => { .join(', '); }; + const getIdentityDefinition = jsonSchema => { + if (jsonSchema.defaultOption !== 'Identity') { + return null; + } + + return { + generateIdentity: jsonSchema.generateIdentity, + seed: jsonSchema.identity?.seed, + step: jsonSchema.identity?.step, + }; + }; + const getRowFormat = tableData => { if (tableData.rowFormatType === ROW_FORMAT_TYPES.DELIMITED && tableData.rowFormatDelimited) { return `\nROW FORMAT DELIMITED ${tableData.rowFormatDelimited}`; @@ -276,5 +288,6 @@ module.exports = app => { parseProps, getRowFormat, getStoredAs, + getIdentityDefinition, }; }; diff --git a/package-lock.json b/package-lock.json index 60e37b6..a8e4cf8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2958,6 +2958,7 @@ "dev": true, "hasInstallScript": true, "license": "MIT", + "peer": true, "bin": { "esbuild": "bin/esbuild" }, @@ -3713,6 +3714,7 @@ "integrity": "sha512-3mBv3CoPbh8dFbzfDGIWa2ytZjn2v+3EX4aKRXjIhsoGFzG8GCjfRirz3rwZf1wYbZzsNLTSgpw8VjQuWdp/jA==", "dev": true, "license": "MIT", + "peer": true, "bin": { "tsgolint": "bin/tsgolint.js" }, @@ -3802,6 +3804,7 @@ "integrity": "sha512-N2MylSdi48+5N/6S5j+maeHbUSIzzZ5uOcX5Hm4QpV8Dkb1HFjfAKTKX6yNPJQD9AhcT3ifHNB66tWTTJDi11Q==", "dev": true, "license": "MIT", + "peer": true, "bin": { "prettier": "bin/prettier.cjs" },