Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working uml generation from ts and openapi #18

Merged
merged 1 commit into from
Jun 24, 2024
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 .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ jobs:
- run: npm ci
- run: npm test
- run: npm run build:client
- run: npm run build:client:nosql:legacy
- run: npm run build:client:nosql
97 changes: 49 additions & 48 deletions dist/nosql.js
Original file line number Diff line number Diff line change
Expand Up @@ -200828,10 +200828,11 @@ Draw.loadPlugin(function (ui) {
mxUtils.br(divGenSQL);
divGenSQL.appendChild(sqlInputGenSQL);
const theMenuExportAs = ui.menus.get("exportAs");
let buttonLabel = "tonosql=To TS";
let buttonLabel = "tonosql=To NoSQL";
// vscode extension support
// FIXME: not compatible with vscode getting unexpected syntax error
if (!(theMenuExportAs && !window.VsCodeApi)) {
buttonLabel = "tonosql=Export As TS";
buttonLabel = "tonosql=Export As NoSQL";
}
// Extends Extras menu
mxResources.parse(buttonLabel);
Expand Down Expand Up @@ -201087,7 +201088,7 @@ Draw.loadPlugin(function (ui) {
const parser = new generate_sql_ddl_1.DbParser(type, db);
// generate sql
let sql = parser.getSQLDataDefinition();
sql = `/*\n\tGenerated in drawio\n\tDatabase: ${type}\n\tPlugin: sql\n\tVersion: ${pluginVersion}\n*/\n\n` + sql;
sql = `/*\n\tGenerated in drawio\n\tDatabase: ${type}\n\tPlugin: nosql\n\tVersion: ${pluginVersion}\n*/\n\n` + sql;
sql = sql.trim();
// update sql value in text area
sqlInputGenSQL.value = sql;
Expand Down Expand Up @@ -201137,7 +201138,7 @@ Draw.loadPlugin(function (ui) {
const sqlInputFromNOSQL = document.createElement("textarea");
sqlInputFromNOSQL.style.height = "200px";
sqlInputFromNOSQL.style.width = "100%";
const defaultReset = `/*\n\tDrawio default value\n\tPlugin: sql\n\tVersion: ${pluginVersion}\n*/\n\n
const defaultReset = `/*\n\tDrawio default value\n\tPlugin: nosql\n\tVersion: ${pluginVersion}\n*/\n\n
export interface WeatherForecast {
/** @format date-time */
date?: string;
Expand All @@ -201146,8 +201147,8 @@ export interface WeatherForecast {
/** @format int32 */
temperatureF?: number;
summary?: string | null;
nestedProp: string[]
child[]?: Child
nestedProp: string[];
children?: Child[];
}

export interface Child {
Expand All @@ -201158,8 +201159,8 @@ export interface Child {
{
"openapi": "3.0.0",
"info": {
"title": "My API",
"version": "1.0.0",
"title": "nosql plugin sample",
"version": ${pluginVersion},
"x-comment": "Generated by core-types-json-schema (https://github.com/grantila/core-types-json-schema)"
},
"paths": {},
Expand Down Expand Up @@ -201230,7 +201231,7 @@ export interface Child {
divFromNOSQL.appendChild(sqlInputFromNOSQL);
var graph = ui.editor.graph;
// Extends Extras menu
mxResources.parse("fromNoSql=From TS");
mxResources.parse("fromNoSql=From NoSQL");
const wndFromNOSQL = new mxWindow(mxResources.get("fromNoSql"), divFromNOSQL, document.body.offsetWidth - 480, 140, 320, 320, true, true);
wndFromNOSQL.destroyOnClose = false;
wndFromNOSQL.setMaximizable(false);
Expand Down Expand Up @@ -201259,87 +201260,86 @@ export interface Child {
}
;
function parseFromInput(text, type) {
var _a;
var _a, _b, _c;
// reset values
cells = [];
tableCell = null;
rowCell = null;
// load parser
// const parser = new SqlSimpleParser(type);
// const user: object = JSON.parse(text);
debugger;
// const reader = getTypeScriptReader( );
// const writer = getOpenApiWriter( { format: "json", title: "My API", version: "v1" } );
// // getTypeScriptWriter
// // getOpenApiReader
// const { convert } = makeConverter( reader, writer );
// convert({ data: text })
// .then( ( { data } ) => {
// debugger;
// const models: any | null = null;
// if(models){
// foreignKeyList = models.ForeignKeyList;
// primaryKeyList = models.PrimaryKeyList;
// tableList = models.TableList;
// exportedTables = tableList.length;
// }
// //Create Table in UI
// CreateTableUI(type);
// });
try {
let openApi = null;
const openApiOptions = {
title: "nosql",
title: "nosql default options",
version: pluginVersion
};
if (type == "openapi") {
// should already be a json, but going to serialize to openapi for validation
const data = JSON.parse(text);
const { data: doc } = (0, core_types_json_schema_1.convertOpenApiToCoreTypes)(data);
const { data: jsonSchema } = (0, core_types_json_schema_1.convertCoreTypesToJsonSchema)(doc);
openApi = (0, core_types_json_schema_1.jsonSchemaDocumentToOpenApi)(jsonSchema, openApiOptions);
}
else if (type == "ts") {
// serialize typescript classes to openapi spec
const { data: doc } = (0, ts_to_core_types_1.convertTypeScriptToCoreTypes)(text);
const { data: jsonSchema } = (0, core_types_json_schema_1.convertCoreTypesToJsonSchema)(doc);
openApi = (0, core_types_json_schema_1.jsonSchemaDocumentToOpenApi)(jsonSchema, openApiOptions);
}
debugger;
const schemas = (_a = openApi === null || openApi === void 0 ? void 0 : openApi.components) === null || _a === void 0 ? void 0 : _a.schemas;
if (schemas) {
const stringOpen = JSON.stringify(openApi, null, 2);
const models = {
Dialect: "nosql",
TableList: [],
PrimaryKeyList: [],
ForeignKeyList: [],
};
// const schemas = openApi.components?.schemas;
for (const key in schemas) {
if (Object.prototype.hasOwnProperty.call(schemas, key)) {
const schema = schemas[key];
const tableModel = {
Name: key,
Properties: [],
};
// const properties = (.properties;
for (const propertyKey in schema.properties) {
if (Object.prototype.hasOwnProperty.call(schema.properties, propertyKey)) {
const property = schema.properties[propertyKey];
const propertyModel = GeneratePropertyModel(key, propertyKey, property);
// {
// Name: propertyKey,
// IsPrimaryKey: false,
// IsForeignKey: false,
// ColumnProperties: property.type ?? "string",
// TableName: key,
// ForeignKey: [],
// };
tableModel.Properties.push(propertyModel);
if (propertyModel.ColumnProperties.includes("object") ||
propertyModel.ColumnProperties.includes("array")) {
let refName = null;
if (property.$ref) {
refName = property.$ref.split("/").pop();
}
else if (property.items && typeof property.items == "object") {
refName = (_b = property.items.$ref) === null || _b === void 0 ? void 0 : _b.split("/").pop();
}
if (refName) {
const foreignKeyModel = {
ReferencesTableName: key,
PrimaryKeyTableName: refName,
ReferencesPropertyName: propertyKey,
// should just point to first property in uml table
PrimaryKeyName: "",
IsDestination: true
};
models.ForeignKeyList.push(foreignKeyModel);
}
}
}
}
models.TableList.push(tableModel);
}
}
for (let i = 0; i < models.ForeignKeyList.length; i++) {
const fk = models.ForeignKeyList[i];
if (!fk.PrimaryKeyName) {
// match to first entry
const property = (_c = models.TableList.find(t => t.Name == fk.PrimaryKeyTableName)) === null || _c === void 0 ? void 0 : _c.Properties[0];
if (property) {
models.ForeignKeyList[i].PrimaryKeyName = property.Name;
}
}
}
foreignKeyList = models.ForeignKeyList;
primaryKeyList = models.PrimaryKeyList;
tableList = models.TableList;
Expand Down Expand Up @@ -201450,14 +201450,14 @@ export interface Child {
}
;
mxUtils.br(divFromNOSQL);
const resetBtnFromNOSQL = mxUtils.button(mxResources.get("reset"), function () {
const resetBtnFromNOSQL = mxUtils.button(mxResources.get("Reset TS"), function () {
sqlInputFromNOSQL.value = defaultReset;
});
resetBtnFromNOSQL.style.marginTop = "8px";
resetBtnFromNOSQL.style.marginRight = "4px";
resetBtnFromNOSQL.style.padding = "4px";
divFromNOSQL.appendChild(resetBtnFromNOSQL);
const resetOpenAPIBtnFromNOSQL = mxUtils.button(mxResources.get("resetOpenAPI"), function () {
const resetOpenAPIBtnFromNOSQL = mxUtils.button("Reset OpenAPI", function () {
sqlInputFromNOSQL.value = defaultResetOpenApi;
});
resetOpenAPIBtnFromNOSQL.style.marginTop = "8px";
Expand Down Expand Up @@ -201515,9 +201515,10 @@ export interface Child {
}
}
});
// TODO: may need to make recursive for when schema property items is array
function GeneratePropertyModel(tableName, propertyName, property) {
var _a;
let columnProperties = ((_a = property.type) !== null && _a !== void 0 ? _a : "string").toString();
let columnProperties = ((_a = property.type) !== null && _a !== void 0 ? _a : "object").toString();
if (property.nullable) {
columnProperties += " nullable";
}
Expand Down
378 changes: 378 additions & 0 deletions dist/nosql.min.js

Large diffs are not rendered by default.

80 changes: 30 additions & 50 deletions src/nosql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ Draw.loadPlugin(function(ui) {
mxUtils.br(divGenSQL);
divGenSQL.appendChild(sqlInputGenSQL);
const theMenuExportAs = ui.menus.get("exportAs");
let buttonLabel = "tonosql=To TS";
let buttonLabel = "tonosql=To NoSQL";
// vscode extension support
// FIXME: not compatible with vscode getting unexpected syntax error
if(!(theMenuExportAs && !window.VsCodeApi)) {
buttonLabel = "tonosql=Export As TS";
buttonLabel = "tonosql=Export As NoSQL";
}
// Extends Extras menu
mxResources.parse(buttonLabel);
Expand Down Expand Up @@ -334,7 +335,7 @@ Draw.loadPlugin(function(ui) {
const parser = new DbParser(type as string, db);
// generate sql
let sql = parser.getSQLDataDefinition();
sql = `/*\n\tGenerated in drawio\n\tDatabase: ${type}\n\tPlugin: sql\n\tVersion: ${pluginVersion}\n*/\n\n` + sql;
sql = `/*\n\tGenerated in drawio\n\tDatabase: ${type}\n\tPlugin: nosql\n\tVersion: ${pluginVersion}\n*/\n\n` + sql;
sql = sql.trim();
// update sql value in text area
sqlInputGenSQL.value = sql;
Expand Down Expand Up @@ -397,7 +398,7 @@ Draw.loadPlugin(function(ui) {
const sqlInputFromNOSQL = document.createElement("textarea");
sqlInputFromNOSQL.style.height = "200px";
sqlInputFromNOSQL.style.width = "100%";
const defaultReset = `/*\n\tDrawio default value\n\tPlugin: sql\n\tVersion: ${pluginVersion}\n*/\n\n
const defaultReset = `/*\n\tDrawio default value\n\tPlugin: nosql\n\tVersion: ${pluginVersion}\n*/\n\n
export interface WeatherForecast {
/** @format date-time */
date?: string;
Expand All @@ -420,8 +421,8 @@ export interface Child {
{
"openapi": "3.0.0",
"info": {
"title": "My API",
"version": "1.0.0",
"title": "nosql plugin sample",
"version": ${pluginVersion},
"x-comment": "Generated by core-types-json-schema (https://github.com/grantila/core-types-json-schema)"
},
"paths": {},
Expand Down Expand Up @@ -495,7 +496,7 @@ export interface Child {
var graph = ui.editor.graph;

// Extends Extras menu
mxResources.parse("fromNoSql=From TS");
mxResources.parse("fromNoSql=From NoSQL");

const wndFromNOSQL = new mxWindow(mxResources.get("fromNoSql"), divFromNOSQL, document.body.offsetWidth - 480, 140,
320, 320, true, true);
Expand Down Expand Up @@ -541,58 +542,26 @@ export interface Child {
cells = [];
tableCell = null;
rowCell = null;
// load parser
// const parser = new SqlSimpleParser(type);
// const user: object = JSON.parse(text);
debugger;
// const reader = getTypeScriptReader( );

// const writer = getOpenApiWriter( { format: "json", title: "My API", version: "v1" } );
// // getTypeScriptWriter
// // getOpenApiReader

// const { convert } = makeConverter( reader, writer );
// convert({ data: text })
// .then( ( { data } ) => {
// debugger;
// const models: any | null = null;

// if(models){
// foreignKeyList = models.ForeignKeyList;
// primaryKeyList = models.PrimaryKeyList;
// tableList = models.TableList;
// exportedTables = tableList.length;
// }

// //Create Table in UI
// CreateTableUI(type);
// });
try {
let openApi: PartialOpenApiSchema|null = null;
const openApiOptions: JsonSchemaDocumentToOpenApiOptions = {
title: "nosql",
title: "nosql default options",
version: pluginVersion
};
if(type == "openapi"){
// should already be a json, but going to serialize to openapi for validation
const data = JSON.parse(text);
const { data: doc } = convertOpenApiToCoreTypes( data );
const { data: jsonSchema } = convertCoreTypesToJsonSchema( doc );

openApi = jsonSchemaDocumentToOpenApi( jsonSchema, openApiOptions );
} else if(type == "ts"){

// serialize typescript classes to openapi spec
const { data: doc } = convertTypeScriptToCoreTypes( text );


const { data: jsonSchema } = convertCoreTypesToJsonSchema( doc );

openApi = jsonSchemaDocumentToOpenApi( jsonSchema, openApiOptions );
}
debugger;
const schemas = openApi?.components?.schemas;
if(schemas){

const stringOpen = JSON.stringify(openApi, null, 2);
const models: DatabaseModel = {
Dialect: "nosql",
TableList: [],
Expand All @@ -613,7 +582,8 @@ export interface Child {
const propertyModel: PropertyModel = GeneratePropertyModel(key, propertyKey, property);
tableModel.Properties.push(propertyModel);

if(propertyModel.ColumnProperties.includes("object")) {
if(propertyModel.ColumnProperties.includes("object") ||
propertyModel.ColumnProperties.includes("array")) {
let refName: string| null | undefined= null;
if(property.$ref) {
refName = property.$ref.split("/").pop();
Expand All @@ -622,11 +592,11 @@ export interface Child {
}
if(refName) {
const foreignKeyModel: ForeignKeyModel = {
PrimaryKeyTableName: key,
ReferencesTableName: refName,
PrimaryKeyName: propertyKey,
ReferencesTableName: key,
PrimaryKeyTableName: refName,
ReferencesPropertyName: propertyKey,
// should just point to first property in uml table
ReferencesPropertyName: "",
PrimaryKeyName: "",
IsDestination: true
};
models.ForeignKeyList.push(foreignKeyModel);
Expand All @@ -638,7 +608,17 @@ export interface Child {
models.TableList.push(tableModel);
}
}
debugger;
for (let i = 0; i < models.ForeignKeyList.length; i++) {
const fk = models.ForeignKeyList[i];
if(!fk.PrimaryKeyName){
// match to first entry
const property = models.TableList.find(t => t.Name == fk.PrimaryKeyTableName)?.Properties[0];
if(property){
models.ForeignKeyList[i].PrimaryKeyName = property.Name;
}
}

}
foreignKeyList = models.ForeignKeyList;
primaryKeyList = models.PrimaryKeyList;
tableList = models.TableList;
Expand Down Expand Up @@ -761,7 +741,7 @@ export interface Child {

mxUtils.br(divFromNOSQL);

const resetBtnFromNOSQL = mxUtils.button(mxResources.get("reset"), function() {
const resetBtnFromNOSQL = mxUtils.button(mxResources.get("Reset TS"), function() {
sqlInputFromNOSQL.value = defaultReset;
});

Expand All @@ -770,7 +750,7 @@ export interface Child {
resetBtnFromNOSQL.style.padding = "4px";
divFromNOSQL.appendChild(resetBtnFromNOSQL);

const resetOpenAPIBtnFromNOSQL = mxUtils.button(mxResources.get("resetOpenAPI"), function() {
const resetOpenAPIBtnFromNOSQL = mxUtils.button("Reset OpenAPI", function() {
sqlInputFromNOSQL.value = defaultResetOpenApi;
});

Expand Down
Loading