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 @@ -15,6 +15,7 @@ esbuild
path.resolve(__dirname, 'api', 'fe.js'),
path.resolve(__dirname, 'api', 're.js'),
path.resolve(__dirname, 'forward_engineering', 'api.js'),
path.resolve(__dirname, 'forward_engineering', 'dbtProvider.js'),
path.resolve(__dirname, 'reverse_engineering', 'api.js'),
],
bundle: true,
Expand Down
49 changes: 49 additions & 0 deletions forward_engineering/dbtProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @typedef {import('./types').ColumnDefinition} ColumnDefinition
* @typedef {import('./types').ConstraintDto} ConstraintDto
* @typedef {import('./types').JsonSchema} JsonSchema
*/

const columnHelper = require('./helpers/columnHelper');
const constraintHelper = require('./helpers/constraintHelper');

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

/**
* @param {{ columnDefinition: ColumnDefinition }}
* @returns {string}
*/
decorateType({ columnDefinition }) {
const type = columnHelper.getTypeByProperty([], '')(columnDefinition);
const isComplexType = /^(array|struct)/i.test(type);

return isComplexType ? type.replace(/<[\s\S]+>$/, '<>') : type;
}

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

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

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

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

const findName = (keyId, properties) => {
return Object.keys(properties).find(name => properties[name].GUID === keyId);
};
Expand Down Expand Up @@ -91,8 +97,104 @@ const getCheckConstraint = jsonSchema => {
return checkConstraint.filter(Boolean).join(',\n');
};

/**
* @param {{ jsonSchema: JsonSchema }}
* @returns {ConstraintDto[]}
*/
const getCompositePrimaryKeys = ({ jsonSchema }) => {
if (!Array.isArray(jsonSchema.primaryKey)) {
return [];
}

return jsonSchema.primaryKey
.filter(primaryKey => primaryKey.compositePrimaryKey?.length)
.map(primaryKey => ({
keyType: 'PRIMARY KEY',
name: primaryKey.constraintName,
columns: getKeys(primaryKey.compositePrimaryKey, jsonSchema),
}));
};

/**
* @param {{ jsonSchema: JsonSchema }}
* @returns {ConstraintDto[]}
*/
const getCompositeUniqueKeys = ({ jsonSchema }) => {
if (!Array.isArray(jsonSchema.uniqueKey)) {
return [];
}

return jsonSchema.uniqueKey
.filter(uniqueKey => uniqueKey.compositeUniqueKey?.length)
.map(uniqueKey => ({
keyType: 'UNIQUE',
name: uniqueKey.constraintName,
columns: getKeys(uniqueKey.compositeUniqueKey, jsonSchema),
}));
};

/**
* @param {{ columnDefinition: ColumnDefinition }}
* @returns {ConstraintDto | undefined}
*/
const getColumnPrimaryKeyConstraint = ({ columnDefinition }) => {
const isPrimaryKey = columnDefinition.primaryKey && !columnDefinition.compositePrimaryKey;

if (!isPrimaryKey) {
return;
}

return {
keyType: 'PRIMARY KEY',
};
};

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

return {
keyType: 'UNIQUE',
};
};

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

return {
keyType: 'CHECK',
expression: columnDefinition.check,
};
};

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

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

module.exports = {
getConstraintOpts,
getUniqueKeyStatement,
getCheckConstraint,
getCompositeUniqueKeys,
getCompositePrimaryKeys,
getColumnConstraints,
};
29 changes: 29 additions & 0 deletions forward_engineering/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export type ColumnDefinition = {
name: string;
type: string;
nullable: boolean;
isActivated: boolean;
length?: number;
precision?: number;
primaryKey?: boolean;
scale?: number;
timePrecision?: number;
unique?: boolean;
check?: string;
properties?: Record<string, ColumnDefinition>;
};

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

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

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

export type JsonSchema = Record<string, unknown>;
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@
},
"nestedCollections": false,
"disablePatternField": true,
"enableForwardEngineering": true,
"enableForwardEngineering": {
"jsonDocument": true,
"jsonSchema": true,
"excel": true,
"plugin": true,
"dbt": true
},
"disableMultipleTypes": false,
"enableReverseEngineering": true,
"enableComplexTypesNormalization": true,
Expand Down