Skip to content

Commit 77f852f

Browse files
HCK-10151: add dbt provider
1 parent 3cb89a0 commit 77f852f

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

forward_engineering/dbtProvider.js

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

forward_engineering/ddlProvider/ddlHelpers/keyHelper.js

+63
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* @typedef {import('../../types').ColumnDefinition} ColumnDefinition
3+
* @typedef {import('../../types').JsonSchema} JsonSchema
4+
* @typedef {import('../../types').ConstraintDto} ConstraintDto
5+
*/
6+
17
const _ = require('lodash');
28
const { clean } = require('../../utils/general');
39

@@ -185,6 +191,61 @@ const getTableKeyConstraints = (jsonSchema, dbVersion) => {
185191
];
186192
};
187193

194+
/**
195+
* @param {{ jsonSchema: JsonSchema }}
196+
* @returns {ConstraintDto[]}
197+
*/
198+
const getCompositeKeyConstraints = ({ jsonSchema }) => {
199+
const compositePrimaryKeys = getCompositePrimaryKeys(jsonSchema);
200+
const compositeUniqueKeys = getCompositeUniqueKeys(jsonSchema);
201+
202+
return [...compositePrimaryKeys, ...compositeUniqueKeys];
203+
};
204+
205+
/**
206+
* @param {{ columnDefinition: ColumnDefinition; jsonSchema: JsonSchema }}
207+
* @returns {ConstraintDto | undefined}
208+
*/
209+
const getPrimaryKeyConstraint = ({ columnDefinition, jsonSchema }) => {
210+
if (!isPrimaryKey(columnDefinition)) {
211+
return;
212+
}
213+
214+
return hydratePrimaryKeyOptions(
215+
_.get(columnDefinition, 'primaryKeyOptions.[0]', {}),
216+
'',
217+
columnDefinition.isActivated,
218+
jsonSchema,
219+
);
220+
};
221+
222+
/**
223+
* @param {{ columnDefinition: ColumnDefinition; jsonSchema: JsonSchema }}
224+
* @returns {ConstraintDto | undefined}
225+
*/
226+
const getUniqueKeyConstraint = ({ columnDefinition, jsonSchema }) => {
227+
if (!isUniqueKey(columnDefinition)) {
228+
return;
229+
}
230+
231+
return hydrateUniqueOptions({
232+
options: _.get(columnDefinition, 'uniqueKeyOptions.[0]', {}),
233+
isActivated: columnDefinition.isActivated,
234+
jsonSchema,
235+
});
236+
};
237+
238+
/**
239+
* @param {{ columnDefinition: ColumnDefinition; jsonSchema: JsonSchema }}
240+
* @returns {ConstraintDto[]}
241+
*/
242+
const getColumnConstraints = ({ columnDefinition, jsonSchema }) => {
243+
const primaryKeyConstraint = getPrimaryKeyConstraint({ columnDefinition, jsonSchema });
244+
const uniqueKeyConstraint = getUniqueKeyConstraint({ columnDefinition, jsonSchema });
245+
246+
return [primaryKeyConstraint, uniqueKeyConstraint].filter(Boolean);
247+
};
248+
188249
module.exports = {
189250
getTableKeyConstraints,
190251
isInlineUnique,
@@ -193,4 +254,6 @@ module.exports = {
193254
hydratePrimaryKeyOptions,
194255
hydrateUniqueOptions,
195256
getUniqueKeyType,
257+
getCompositeKeyConstraints,
258+
getColumnConstraints,
196259
};

forward_engineering/types.d.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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?: Array<{ GUID: string; constraintName: string }>
12+
uniqueKeyOptions?: Array<{ GUID: string; constraintName: string }>
13+
};
14+
15+
export type ConstraintDtoColumn = {
16+
name: string;
17+
isActivated: boolean;
18+
};
19+
20+
export type KeyType = 'PRIMARY KEY' | 'UNIQUE';
21+
22+
export type ConstraintDto = {
23+
keyType: KeyType;
24+
name: string;
25+
columns: ConstraintDtoColumn[];
26+
};
27+
28+
export type JsonSchema = Record<string, unknown>;

0 commit comments

Comments
 (0)