Skip to content

Commit

Permalink
Merge pull request #38 from funktechno/f/updates
Browse files Browse the repository at this point in the history
F/updates openapi enum support and comments
  • Loading branch information
lastlink authored Jul 6, 2024
2 parents ce0815a + 5828f0f commit edd3276
Show file tree
Hide file tree
Showing 11 changed files with 1,215 additions and 500 deletions.
507 changes: 367 additions & 140 deletions dist/nosql-ts.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions dist/nosql-ts.min.js

Large diffs are not rendered by default.

507 changes: 367 additions & 140 deletions dist/nosql.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/nosql.min.js

Large diffs are not rendered by default.

96 changes: 90 additions & 6 deletions dist/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ const sharedUtils_1 = require("./utils/sharedUtils");
const constants_1 = require("./utils/constants");
/**
* SQL Tools Plugin for importing diagrams from SQL DDL and exporting to SQL.
* Version: 0.0.4
* Version: 0.0.5
*/
Draw.loadPlugin(function (ui) {
// export sql methods
Expand Down Expand Up @@ -1035,7 +1035,6 @@ Draw.loadPlugin(function (ui) {
tableCell.insert(rowCell);
tableCell.geometry.height += 26;
}
rowCell = rowCell;
}
;
function parseSql(text, type) {
Expand Down Expand Up @@ -1229,9 +1228,16 @@ Draw.loadPlugin(function (ui) {
},{"./utils/constants":5,"./utils/sharedUtils":6,"@funktechno/little-mermaid-2-the-sql/lib/src/generate-sql-ddl":1,"@funktechno/sqlsimpleparser":3}],5:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pluginVersion = void 0;
exports.validEnumTypes = exports.enumKeyword = exports.formatKeyword = exports.commentColumnQuantifiers = exports.pluginVersion = void 0;
// export sql methods
exports.pluginVersion = "0.0.4";
exports.pluginVersion = "0.0.5";
exports.commentColumnQuantifiers = {
Start: "/**",
End: "*/",
};
exports.formatKeyword = "@format";
exports.enumKeyword = "enum";
exports.validEnumTypes = ["string", "number", "integer", "boolean"];

},{}],6:[function(require,module,exports){
"use strict";
Expand All @@ -1241,7 +1247,12 @@ exports.removeHtml = removeHtml;
exports.dbTypeEnds = dbTypeEnds;
exports.RemoveNameQuantifiers = RemoveNameQuantifiers;
exports.getDbLabel = getDbLabel;
exports.entityName = entityName;
exports.getCommentIndexes = getCommentIndexes;
exports.getMermaidDiagramDb = getMermaidDiagramDb;
exports.GenerateDatabaseModel = GenerateDatabaseModel;
exports.generateComment = generateComment;
const constants_1 = require("./constants");
/**
* return text quantifiers for dialect
* @returns json
Expand Down Expand Up @@ -1307,6 +1318,34 @@ function getDbLabel(label, columnQuantifiers) {
};
return attribute;
}
function entityName(description, format) {
let result = "";
if (description) {
result += `${description}`;
}
if (format) {
result += ` @format ${format}`;
}
if (result) {
result = result.trim();
result = `/** ${result} */`;
}
return result;
}
function getCommentIndexes(result) {
let hasComment = false;
if (result.indexOf(constants_1.commentColumnQuantifiers.Start) !== -1 && result.indexOf(constants_1.commentColumnQuantifiers.End) !== -1) {
hasComment = true;
}
const beforeIndex = hasComment ? result.indexOf(constants_1.commentColumnQuantifiers.Start) : -1;
const firstSpaceIndex = hasComment ? result.indexOf(constants_1.commentColumnQuantifiers.Start) + constants_1.commentColumnQuantifiers.Start.length : -1;
const lastSpaceIndex = hasComment ? result.indexOf(constants_1.commentColumnQuantifiers.End) - 1 : -1;
return {
beforeStart: beforeIndex,
start: firstSpaceIndex,
end: lastSpaceIndex
};
}
/**
* generate db from drawio graph models
* @param ui
Expand All @@ -1319,16 +1358,43 @@ function getMermaidDiagramDb(ui, type) {
// only difference is entities is an array rather than object to allow duplicate tables
const entities = {};
const relationships = [];
// TODO: support for ts and openapi enum
// build models
for (const key in model.cells) {
if (Object.hasOwnProperty.call(model.cells, key)) {
const mxcell = model.cells[key];
if (mxcell.mxObjectId.indexOf("mxCell") !== -1) {
if (mxcell.style && mxcell.style.trim().startsWith("swimlane;")) {
let entityName = mxcell.value.toString();
let description = "";
let formatValue = "";
if ((entityName === null || entityName === void 0 ? void 0 : entityName.includes(constants_1.commentColumnQuantifiers.Start)) &&
(entityName === null || entityName === void 0 ? void 0 : entityName.includes(constants_1.commentColumnQuantifiers.End))) {
let result = entityName.toString();
const commentIndexes = getCommentIndexes(result);
const firstSpaceIndex = commentIndexes.start;
const lastSpaceIndex = commentIndexes.end;
entityName = result.substring(0, commentIndexes.beforeStart);
result = result.substring(firstSpaceIndex, lastSpaceIndex);
if (result.indexOf(constants_1.formatKeyword) !== -1) {
const formatIndex = result.indexOf(constants_1.formatKeyword);
formatValue = result.substring(formatIndex + constants_1.formatKeyword.length).trim();
result = result.substring(0, formatIndex);
}
if (result) {
description = result;
}
// decription = attribute.attributeType?.replace("/**", "").replace("*/", "");
}
const entity = {
name: RemoveNameQuantifiers(mxcell.value),
name: RemoveNameQuantifiers(entityName),
attributes: [],
};
const comment = generateComment(description, formatValue);
if (comment) {
entity.name += comment;
}
// const comment =
for (let c = 0; c < mxcell.children.length; c++) {
const col = mxcell.children[c];
if (col.mxObjectId.indexOf("mxCell") !== -1) {
Expand Down Expand Up @@ -1482,6 +1548,10 @@ function getMermaidDiagramDb(ui, type) {
}
}
}
const db = GenerateDatabaseModel(entities, relationships);
return db;
}
function GenerateDatabaseModel(entities, relationships) {
class DatabaseModel {
constructor(entities, relationships) {
this.entities = entities;
Expand All @@ -1497,5 +1567,19 @@ function getMermaidDiagramDb(ui, type) {
const db = new DatabaseModel(entities, relationships);
return db;
}
function generateComment(description, formatValue) {
let result = "";
if (description) {
result += `${description}`;
}
if (formatValue) {
result += ` @format ${formatValue}`;
}
if (result) {
result = result.trim();
result = `${constants_1.commentColumnQuantifiers.Start} ${result} ${constants_1.commentColumnQuantifiers.End}`;
}
return result;
}

},{}]},{},[4]);
},{"./constants":5}]},{},[4]);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sqltooling-drawio",
"version": "0.0.4",
"version": "0.0.5",
"description": "plugins for sql tooling in drawio",
"main": "index.js",
"engines": {
Expand Down
98 changes: 5 additions & 93 deletions src/nosql-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { convertTypeScriptToCoreTypes } from "core-types-ts/dist/lib/ts-to-core-
import { convertCoreTypesToTypeScript } from "core-types-ts";
import { GetColumnQuantifiers, RemoveNameQuantifiers, dbTypeEnds, getDbLabel, getMermaidDiagramDb } from "./utils/sharedUtils";

Check warning on line 9 in src/nosql-ts.ts

View workflow job for this annotation

GitHub Actions / tests

'RemoveNameQuantifiers' is defined but never used
import { pluginVersion } from "./utils/constants";
import { dbToOpenApi } from "./utils/nosqlUtils";
import { ConvertOpenApiToDatabaseModel, dbToOpenApi, GeneratePropertyModel } from "./utils/nosqlUtils";

Check warning on line 11 in src/nosql-ts.ts

View workflow job for this annotation

GitHub Actions / tests

'GeneratePropertyModel' is defined but never used

declare const window: Customwindow;

Expand Down Expand Up @@ -281,7 +281,9 @@ export interface Child {
const data = JSON.parse(text);
const { data: doc } = convertOpenApiToCoreTypes( data );
const { data: jsonSchema } = convertCoreTypesToJsonSchema( doc );
openApi = jsonSchemaDocumentToOpenApi( jsonSchema, openApiOptions );
// was losing format option, just going to check if exception thrown here
jsonSchemaDocumentToOpenApi( jsonSchema, openApiOptions );
openApi = data;
} else if(type == "ts"){
// serialize typescript classes to openapi spec
const { data: doc } = convertTypeScriptToCoreTypes( text );
Expand All @@ -290,81 +292,7 @@ export interface Child {
}
const schemas = openApi?.components?.schemas;
if(schemas){
const models: DatabaseModel = {
Dialect: "nosql",
TableList: [],
PrimaryKeyList: [],
ForeignKeyList: [],
};

for (const key in schemas) {
if (Object.prototype.hasOwnProperty.call(schemas, key)) {
const schema = schemas[key] as JSONSchema4;
const tableModel: TableModel = {
Name: dbTypeEnds(key),
Properties: [],
};
for (const propertyKey in schema.properties) {
if (Object.prototype.hasOwnProperty.call(schema.properties, propertyKey)) {
const property = schema.properties[propertyKey];
const propertyModel: PropertyModel = GeneratePropertyModel(key, propertyKey, property);
if(propertyModel.ColumnProperties.includes("object") ||
propertyModel.ColumnProperties.includes("array")) {
let refName: string| null | undefined= null;
if(property.$ref) {
refName = property.$ref.split("/").pop();
} else if(property.items && typeof property.items == "object") {
refName = (property.items as JSONSchema4).$ref?.split("/").pop();
}
if(refName) {

const primaryKeyModel: ForeignKeyModel = {
PrimaryKeyTableName: dbTypeEnds(key),
ReferencesTableName: dbTypeEnds(refName),
PrimaryKeyName: dbTypeEnds(propertyKey),
// should just point to first property in uml table
ReferencesPropertyName: "",
IsDestination: false
};
const foreignKeyModel: ForeignKeyModel = {
ReferencesTableName: dbTypeEnds(key),
PrimaryKeyTableName: dbTypeEnds(refName),
ReferencesPropertyName: dbTypeEnds(propertyKey),
// should just point to first property in uml table
PrimaryKeyName: "",
IsDestination: true
};
models.ForeignKeyList.push(foreignKeyModel);
models.ForeignKeyList.push(primaryKeyModel);
propertyModel.IsForeignKey = true;
}
}

tableModel.Properties.push(propertyModel);
}
}

models.TableList.push(tableModel);
}
}
for (let i = 0; i < models.ForeignKeyList.length; i++) {
const fk = models.ForeignKeyList[i];
if(!fk.ReferencesPropertyName){
// match to first entry
const property = models.TableList.find(t => t.Name == fk.ReferencesTableName)?.Properties[0];
if(property){
models.ForeignKeyList[i].ReferencesPropertyName = property.Name;
}
}
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;
}
}

}
const models = ConvertOpenApiToDatabaseModel(schemas);
foreignKeyList = models.ForeignKeyList;
primaryKeyList = models.PrimaryKeyList;
tableList = models.TableList;
Expand Down Expand Up @@ -562,20 +490,4 @@ export interface Child {
}
}
});
// TODO: may need to make recursive for when schema property items is array
function GeneratePropertyModel(tableName: string, propertyName: string, property: JSONSchema4): PropertyModel {
let columnProperties = (property.type ?? "object").toString();
if(property.nullable) {
columnProperties += " nullable";
}
const result: PropertyModel = {
Name: dbTypeEnds(propertyName),
IsPrimaryKey: false,
IsForeignKey: false,
ColumnProperties: columnProperties,
TableName: dbTypeEnds(tableName),
ForeignKey: [],
};
return result;
}

Loading

0 comments on commit edd3276

Please sign in to comment.