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
1 change: 1 addition & 0 deletions esbuild.package.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ esbuild
entryPoints: [
path.resolve(__dirname, 'forward_engineering', 'api.js'),
path.resolve(__dirname, 'forward_engineering', 'ddlProvider.js'),
path.resolve(__dirname, 'forward_engineering', 'dbtProvider.js'),
path.resolve(__dirname, 'reverse_engineering', 'api.js'),
],
bundle: true,
Expand Down
76 changes: 76 additions & 0 deletions forward_engineering/dbtProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @typedef {import('./types').ColumnDefinition} ColumnDefinition
* @typedef {import('./types').JsonSchema} JsonSchema
* @typedef {import('./types').ConstraintDto} ConstraintDto
*/
const { toLower, toUpper } = require('lodash');

const types = require('./configs/descriptors');
const defaultTypes = require('./configs/defaultTypes');
const { decorateType } = require('./ddlProvider/ddlHelpers/columnDefinitionHelpers/decorateType');
const getKeyHelper = 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 {{ columnDefinition: ColumnDefinition }}
* @returns {string}
*/
decorateType({ columnDefinition }) {
const type = toUpper(columnDefinition.mode || columnDefinition.childType || columnDefinition.type);
const dataType = decorateType(type, columnDefinition);

return dataType.trim();
}

/**
* @param {{ jsonSchema: JsonSchema }}
* @returns {ConstraintDto[]}
*/
getCompositeKeyConstraints({ jsonSchema }) {
const keyHelper = getKeyHelper(value => value);

return keyHelper.getCompositeKeyConstraints({ jsonSchema });
}

/**
* @param {{ columnDefinition: ColumnDefinition }}
* @returns {ConstraintDto[]}
*/
getColumnConstraints({ columnDefinition }) {
const keyHelper = getKeyHelper(value => value);

return keyHelper.getColumnConstraints({ columnDefinition });
}
}

module.exports = DbtProvider;
58 changes: 58 additions & 0 deletions forward_engineering/ddlProvider/ddlHelpers/keyHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = clean => {
const hydrateUniqueOptions = (options, columnName, isActivated) =>
clean({
keyType: 'UNIQUE',
name: options.constraintName,
columns: [
{
name: columnName,
Expand All @@ -50,6 +51,7 @@ module.exports = clean => {
const hydratePrimaryKeyOptions = (options, columnName, isActivated) =>
clean({
keyType: 'PRIMARY KEY',
name: options.constraintName,
columns: [
{
name: columnName,
Expand Down Expand Up @@ -135,10 +137,66 @@ module.exports = clean => {
];
};

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

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

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

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

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

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

/**
* @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);
};

return {
getTableKeyConstraints,
isInlineUnique,
isInlinePrimaryKey,
getKeys,
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 @@ -22,7 +22,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