Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions forward_engineering/dbtProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @typedef {import('./types').ColumnDefinition} ColumnDefinition
* @typedef {import('./types').JsonSchema} JsonSchema
* @typedef {import('./types').ConstraintDto} ConstraintDto
*/
const { toLower } = require('lodash');

const types = require('./configs/descriptors');
const defaultTypes = require('./configs/defaultTypes');
const { decorateType } = require('./ddlProvider/ddlHelpers/columnDefinitionHelper');
const { getCompositeKeyConstraints, getColumnConstraints } = require('./ddlProvider/ddlHelpers/keyHelper');

class DbtProvider {
/**
* @returns {DbtProvider}
*/
static createDbtProvider() {
return new DbtProvider();
}

/**
* @param {string} type
* @returns {string | undefined}
*/
getDefaultType(type) {
return defaultTypes[type];
}

/**
* @returns {Record<string, object>}
*/
getTypesDescriptors() {
return types;
}

/**
* @param {string} type
* @returns {boolean}
*/
hasType(type) {
return Object.keys(types).map(toLower).includes(toLower(type));
}

/**
* @param {{ type: string; columnDefinition: ColumnDefinition }}
* @returns {string}
*/
decorateType({ type, columnDefinition }) {
return decorateType(type, columnDefinition);
}

/**
* @param {{ jsonSchema: JsonSchema }}
* @returns {ConstraintDto[]}
*/
getCompositeKeyConstraints({ jsonSchema }) {
return getCompositeKeyConstraints({ jsonSchema });
}

/**
* @param {{ columnDefinition: ColumnDefinition; jsonSchema: JsonSchema }}
* @returns {ConstraintDto[]}
*/
getColumnConstraints({ columnDefinition, jsonSchema }) {
return getColumnConstraints({ columnDefinition, jsonSchema });
}
}

module.exports = DbtProvider;
63 changes: 63 additions & 0 deletions forward_engineering/ddlProvider/ddlHelpers/keyHelper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @typedef {import('../../types').ColumnDefinition} ColumnDefinition
* @typedef {import('../../types').JsonSchema} JsonSchema
* @typedef {import('../../types').ConstraintDto} ConstraintDto
*/

const _ = require('lodash');
const { clean } = require('../../utils/general');

Expand Down Expand Up @@ -185,6 +191,61 @@ const getTableKeyConstraints = (jsonSchema, dbVersion) => {
];
};

/**
* @param {{ jsonSchema: JsonSchema }}
* @returns {ConstraintDto[]}
*/
const getCompositeKeyConstraints = ({ jsonSchema }) => {
const compositePrimaryKeys = getCompositePrimaryKeys(jsonSchema);
const compositeUniqueKeys = getCompositeUniqueKeys(jsonSchema);

return [...compositePrimaryKeys, ...compositeUniqueKeys];
};

/**
* @param {{ columnDefinition: ColumnDefinition; jsonSchema: JsonSchema }}
* @returns {ConstraintDto | undefined}
*/
const getPrimaryKeyConstraint = ({ columnDefinition, jsonSchema }) => {
if (!isPrimaryKey(columnDefinition)) {
return;
}

return hydratePrimaryKeyOptions(
_.get(columnDefinition, 'primaryKeyOptions.[0]', {}),
'',
columnDefinition.isActivated,
jsonSchema,
);
};

/**
* @param {{ columnDefinition: ColumnDefinition; jsonSchema: JsonSchema }}
* @returns {ConstraintDto | undefined}
*/
const getUniqueKeyConstraint = ({ columnDefinition, jsonSchema }) => {
if (!isUniqueKey(columnDefinition)) {
return;
}

return hydrateUniqueOptions({
options: _.get(columnDefinition, 'uniqueKeyOptions.[0]', {}),
isActivated: columnDefinition.isActivated,
jsonSchema,
});
};

/**
* @param {{ columnDefinition: ColumnDefinition; jsonSchema: JsonSchema }}
* @returns {ConstraintDto[]}
*/
const getColumnConstraints = ({ columnDefinition, jsonSchema }) => {
const primaryKeyConstraint = getPrimaryKeyConstraint({ columnDefinition, jsonSchema });
const uniqueKeyConstraint = getUniqueKeyConstraint({ columnDefinition, jsonSchema });

return [primaryKeyConstraint, uniqueKeyConstraint].filter(Boolean);
};

module.exports = {
getTableKeyConstraints,
isInlineUnique,
Expand All @@ -193,4 +254,6 @@ module.exports = {
hydratePrimaryKeyOptions,
hydrateUniqueOptions,
getUniqueKeyType,
getCompositeKeyConstraints,
getColumnConstraints,
};
28 changes: 28 additions & 0 deletions forward_engineering/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export type ColumnDefinition = {
name: string;
type: string;
isActivated: boolean;
length?: number;
precision?: number;
primaryKey?: boolean;
scale?: number;
timePrecision?: number;
unique?: boolean;
primaryKeyOptions?: Array<{ GUID: string; constraintName: string }>
uniqueKeyOptions?: Array<{ GUID: string; constraintName: string }>
};

export type ConstraintDtoColumn = {
name: string;
isActivated: boolean;
};

export type KeyType = 'PRIMARY KEY' | 'UNIQUE';

export type ConstraintDto = {
keyType: KeyType;
name: string;
columns: ConstraintDtoColumn[];
};

export type JsonSchema = Record<string, unknown>;
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
"nestedCollections": false,
"disablePatternField": true,
"disableMultipleTypes": true,
"enableForwardEngineering": true,
"enableForwardEngineering": {
"jsonDocument": true,
"jsonSchema": true,
"excel": true,
"plugin": true,
"dbt": true
},
"disableReverseEngineering": false,
"disableChoices": true,
"enableJsonType": true,
Expand Down