Skip to content

Commit c97e20f

Browse files
HCK-10160: enable dbt feature (#40)
* HCK-10160: add dbt provider * HCK-10160: enable dbt feature
1 parent d3b2bab commit c97e20f

7 files changed

Lines changed: 188 additions & 6 deletions

File tree

esbuild.package.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ esbuild
1313
entryPoints: [
1414
path.resolve(__dirname, 'forward_engineering', 'api.js'),
1515
path.resolve(__dirname, 'forward_engineering', 'ddlProvider.js'),
16+
path.resolve(__dirname, 'forward_engineering', 'dbtProvider.js'),
1617
path.resolve(__dirname, 'reverse_engineering', 'api.js'),
1718
],
1819
bundle: true,

forward_engineering/dbtProvider.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @typedef {import('./types').AppInstance} AppInstance
3+
* @typedef {import('./types').ColumnDefinition} ColumnDefinition
4+
* @typedef {import('./types').JsonSchema} JsonSchema
5+
* @typedef {import('./types').ConstraintDto} ConstraintDto
6+
*/
7+
const { toLower, toUpper, identity } = require('lodash');
8+
9+
const types = require('./configs/types');
10+
const defaultTypes = require('./configs/defaultTypes');
11+
const getKeyHelper = require('./helpers/keyHelper');
12+
const getColumnDefinitionHelper = require('./helpers/columnDefinitionHelper');
13+
14+
class DbtProvider {
15+
/**
16+
* @returns {DbtProvider}
17+
*/
18+
static createDbtProvider() {
19+
return new DbtProvider();
20+
}
21+
22+
/**
23+
* @param {string} type
24+
* @returns {string | undefined}
25+
*/
26+
getDefaultType(type) {
27+
return defaultTypes[type];
28+
}
29+
30+
/**
31+
* @returns {Record<string, object>}
32+
*/
33+
getTypesDescriptors() {
34+
return types;
35+
}
36+
37+
/**
38+
* @param {string} type
39+
* @returns {boolean}
40+
*/
41+
hasType(type) {
42+
return Object.keys(types).map(toLower).includes(toLower(type));
43+
}
44+
45+
/**
46+
* @param {{ type: string; columnDefinition: ColumnDefinition }}
47+
* @returns {string}
48+
*/
49+
decorateType({ type, columnDefinition }) {
50+
const columnDefinitionHelper = getColumnDefinitionHelper(identity);
51+
52+
return columnDefinitionHelper.decorateType(toUpper(type), columnDefinition);
53+
}
54+
55+
/**
56+
* @param {{ jsonSchema: JsonSchema }}
57+
* @returns {ConstraintDto[]}
58+
*/
59+
getCompositeKeyConstraints({ jsonSchema }) {
60+
const keyHelper = getKeyHelper(identity);
61+
62+
return keyHelper.getCompositeKeyConstraints({ jsonSchema });
63+
}
64+
65+
/**
66+
* @param {{ columnDefinition: ColumnDefinition }}
67+
* @returns {ConstraintDto[]}
68+
*/
69+
getColumnConstraints({ columnDefinition }) {
70+
const keyHelper = getKeyHelper(identity);
71+
72+
return keyHelper.getColumnConstraints({ columnDefinition });
73+
}
74+
}
75+
76+
module.exports = DbtProvider;

forward_engineering/ddlProvider.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const _ = require('lodash');
12
const defaultTypes = require('./configs/defaultTypes');
23
const types = require('./configs/types');
34
const templates = require('./configs/templates');
@@ -6,7 +7,6 @@ const dropStatementProxy = require('./helpers/dropStatementProxy');
67
const { joinActivatedAndDeactivatedStatements } = require('./utils/joinActivatedAndDeactivatedStatements');
78

89
module.exports = (baseProvider, options, app) => {
9-
const _ = app.require('lodash');
1010
const {
1111
tab,
1212
commentIfDeactivated,
@@ -19,7 +19,7 @@ module.exports = (baseProvider, options, app) => {
1919
} = app.require('@hackolade/ddl-fe-utils').general;
2020
const { assignTemplates, compareGroupItems } = app.require('@hackolade/ddl-fe-utils');
2121
const { decorateDefault, decorateType, canBeNational, getSign, createGeneratedColumn, canHaveAutoIncrement } =
22-
require('./helpers/columnDefinitionHelper')(_, wrap);
22+
require('./helpers/columnDefinitionHelper')(wrap);
2323
const { getTableName, getTableOptions, getPartitions, getViewData, getCharacteristics, escapeQuotes } =
2424
require('./helpers/general')(_, wrap);
2525
const { generateConstraintsString, foreignKeysToString, foreignActiveKeysToString, createKeyConstraint } =
@@ -31,7 +31,7 @@ module.exports = (baseProvider, options, app) => {
3131
assignTemplates,
3232
escapeQuotes,
3333
});
34-
const keyHelper = require('./helpers/keyHelper')(_, clean);
34+
const keyHelper = require('./helpers/keyHelper')(clean);
3535
const { processIndexKeyName } = require('./helpers/indexHelper')(wrap);
3636
const additionalOptions = getAdditionalOptions(options.additionalOptions);
3737

forward_engineering/helpers/columnDefinitionHelper.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
module.exports = (_, wrap) => {
1+
const _ = require('lodash');
2+
3+
module.exports = wrap => {
24
const addLength = (type, length) => {
35
return `${type}(${length})`;
46
};

forward_engineering/helpers/keyHelper.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
module.exports = (_, clean) => {
1+
/**
2+
* @typedef {import('../types').ColumnDefinition} ColumnDefinition
3+
* @typedef {import('../types').JsonSchema} JsonSchema
4+
* @typedef {import('../types').ConstraintDto} ConstraintDto
5+
*/
6+
7+
const _ = require('lodash');
8+
9+
module.exports = clean => {
210
const mapProperties = (jsonSchema, iteratee) => {
311
return Object.entries(jsonSchema.properties).map(iteratee);
412
};
@@ -166,11 +174,67 @@ module.exports = (_, clean) => {
166174
];
167175
};
168176

177+
/**
178+
* @param {{ jsonSchema: JsonSchema }}
179+
* @returns {ConstraintDto[]}
180+
*/
181+
const getCompositeKeyConstraints = ({ jsonSchema }) => {
182+
const compositePrimaryKeys = getCompositePrimaryKeys(jsonSchema);
183+
const compositeUniqueKeys = getCompositeUniqueKeys(jsonSchema);
184+
185+
return [...compositePrimaryKeys, ...compositeUniqueKeys];
186+
};
187+
188+
/**
189+
* @param {{ columnDefinition: ColumnDefinition }}
190+
* @returns {ConstraintDto | undefined}
191+
*/
192+
const getPrimaryKeyConstraint = ({ columnDefinition }) => {
193+
if (!isPrimaryKey(columnDefinition)) {
194+
return;
195+
}
196+
197+
return hydratePrimaryKeyOptions(columnDefinition.primaryKeyOptions ?? {}, '', columnDefinition.isActivated);
198+
};
199+
200+
/**
201+
* @param {{ columnDefinition: ColumnDefinition }}
202+
* @returns {ConstraintDto[]}
203+
*/
204+
const getUniqueKeyConstraints = ({ columnDefinition }) => {
205+
if (!isUniqueKey(columnDefinition)) {
206+
return [];
207+
}
208+
209+
if (isInlineUnique(columnDefinition)) {
210+
const constraint = hydrateUniqueOptions({}, '', columnDefinition.isActivated);
211+
212+
return [constraint];
213+
}
214+
215+
return columnDefinition.uniqueKeyOptions.map(uniqueKeyOption => {
216+
return hydrateUniqueOptions(uniqueKeyOption, '', columnDefinition.isActivated);
217+
});
218+
};
219+
220+
/**
221+
* @param {{ columnDefinition: ColumnDefinition }}
222+
* @returns {ConstraintDto[]}
223+
*/
224+
const getColumnConstraints = ({ columnDefinition }) => {
225+
const primaryKeyConstraint = getPrimaryKeyConstraint({ columnDefinition });
226+
const uniqueKeyConstraints = getUniqueKeyConstraints({ columnDefinition });
227+
228+
return [primaryKeyConstraint, ...uniqueKeyConstraints].filter(Boolean);
229+
};
230+
169231
return {
170232
getTableKeyConstraints,
171233
isInlineUnique,
172234
isInlinePrimaryKey,
173235
hydratePrimaryKeyOptions,
174236
hydrateUniqueOptions,
237+
getColumnConstraints,
238+
getCompositeKeyConstraints,
175239
};
176240
};

forward_engineering/types.d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export type ColumnDefinition = {
2+
name: string;
3+
type: string;
4+
isActivated: boolean;
5+
length?: number;
6+
precision?: number;
7+
primaryKey?: boolean;
8+
scale?: number;
9+
timePrecision?: number;
10+
unique?: boolean;
11+
primaryKeyOptions?: { GUID: string; constraintName: string };
12+
uniqueKeyOptions?: Array<{ GUID: string; constraintName: string }>;
13+
};
14+
15+
export type AppInstance = {
16+
require: (packageName: string) => unknown;
17+
general: object;
18+
}
19+
20+
export type ConstraintDtoColumn = {
21+
name: string;
22+
isActivated: boolean;
23+
};
24+
25+
export type KeyType = 'PRIMARY KEY' | 'UNIQUE';
26+
27+
export type ConstraintDto = {
28+
keyType: KeyType;
29+
name: string;
30+
columns?: ConstraintDtoColumn[];
31+
};
32+
33+
export type JsonSchema = Record<string, unknown>;

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
"nestedCollections": false,
2020
"disablePatternField": true,
2121
"disableMultipleTypes": true,
22-
"enableForwardEngineering": true,
22+
"enableForwardEngineering": {
23+
"jsonDocument": true,
24+
"jsonSchema": true,
25+
"excel": true,
26+
"plugin": true,
27+
"dbt": true
28+
},
2329
"disableReverseEngineering": false,
2430
"disableChoices": true,
2531
"enableJsonType": true,

0 commit comments

Comments
 (0)