Skip to content

Commit edd3276

Browse files
authored
Merge pull request #38 from funktechno/f/updates
F/updates openapi enum support and comments
2 parents ce0815a + 5828f0f commit edd3276

File tree

11 files changed

+1215
-500
lines changed

11 files changed

+1215
-500
lines changed

dist/nosql-ts.js

Lines changed: 367 additions & 140 deletions
Large diffs are not rendered by default.

dist/nosql-ts.min.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/nosql.js

Lines changed: 367 additions & 140 deletions
Large diffs are not rendered by default.

dist/nosql.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sql.js

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ const sharedUtils_1 = require("./utils/sharedUtils");
898898
const constants_1 = require("./utils/constants");
899899
/**
900900
* SQL Tools Plugin for importing diagrams from SQL DDL and exporting to SQL.
901-
* Version: 0.0.4
901+
* Version: 0.0.5
902902
*/
903903
Draw.loadPlugin(function (ui) {
904904
// export sql methods
@@ -1035,7 +1035,6 @@ Draw.loadPlugin(function (ui) {
10351035
tableCell.insert(rowCell);
10361036
tableCell.geometry.height += 26;
10371037
}
1038-
rowCell = rowCell;
10391038
}
10401039
;
10411040
function parseSql(text, type) {
@@ -1229,9 +1228,16 @@ Draw.loadPlugin(function (ui) {
12291228
},{"./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){
12301229
"use strict";
12311230
Object.defineProperty(exports, "__esModule", { value: true });
1232-
exports.pluginVersion = void 0;
1231+
exports.validEnumTypes = exports.enumKeyword = exports.formatKeyword = exports.commentColumnQuantifiers = exports.pluginVersion = void 0;
12331232
// export sql methods
1234-
exports.pluginVersion = "0.0.4";
1233+
exports.pluginVersion = "0.0.5";
1234+
exports.commentColumnQuantifiers = {
1235+
Start: "/**",
1236+
End: "*/",
1237+
};
1238+
exports.formatKeyword = "@format";
1239+
exports.enumKeyword = "enum";
1240+
exports.validEnumTypes = ["string", "number", "integer", "boolean"];
12351241

12361242
},{}],6:[function(require,module,exports){
12371243
"use strict";
@@ -1241,7 +1247,12 @@ exports.removeHtml = removeHtml;
12411247
exports.dbTypeEnds = dbTypeEnds;
12421248
exports.RemoveNameQuantifiers = RemoveNameQuantifiers;
12431249
exports.getDbLabel = getDbLabel;
1250+
exports.entityName = entityName;
1251+
exports.getCommentIndexes = getCommentIndexes;
12441252
exports.getMermaidDiagramDb = getMermaidDiagramDb;
1253+
exports.GenerateDatabaseModel = GenerateDatabaseModel;
1254+
exports.generateComment = generateComment;
1255+
const constants_1 = require("./constants");
12451256
/**
12461257
* return text quantifiers for dialect
12471258
* @returns json
@@ -1307,6 +1318,34 @@ function getDbLabel(label, columnQuantifiers) {
13071318
};
13081319
return attribute;
13091320
}
1321+
function entityName(description, format) {
1322+
let result = "";
1323+
if (description) {
1324+
result += `${description}`;
1325+
}
1326+
if (format) {
1327+
result += ` @format ${format}`;
1328+
}
1329+
if (result) {
1330+
result = result.trim();
1331+
result = `/** ${result} */`;
1332+
}
1333+
return result;
1334+
}
1335+
function getCommentIndexes(result) {
1336+
let hasComment = false;
1337+
if (result.indexOf(constants_1.commentColumnQuantifiers.Start) !== -1 && result.indexOf(constants_1.commentColumnQuantifiers.End) !== -1) {
1338+
hasComment = true;
1339+
}
1340+
const beforeIndex = hasComment ? result.indexOf(constants_1.commentColumnQuantifiers.Start) : -1;
1341+
const firstSpaceIndex = hasComment ? result.indexOf(constants_1.commentColumnQuantifiers.Start) + constants_1.commentColumnQuantifiers.Start.length : -1;
1342+
const lastSpaceIndex = hasComment ? result.indexOf(constants_1.commentColumnQuantifiers.End) - 1 : -1;
1343+
return {
1344+
beforeStart: beforeIndex,
1345+
start: firstSpaceIndex,
1346+
end: lastSpaceIndex
1347+
};
1348+
}
13101349
/**
13111350
* generate db from drawio graph models
13121351
* @param ui
@@ -1319,16 +1358,43 @@ function getMermaidDiagramDb(ui, type) {
13191358
// only difference is entities is an array rather than object to allow duplicate tables
13201359
const entities = {};
13211360
const relationships = [];
1361+
// TODO: support for ts and openapi enum
13221362
// build models
13231363
for (const key in model.cells) {
13241364
if (Object.hasOwnProperty.call(model.cells, key)) {
13251365
const mxcell = model.cells[key];
13261366
if (mxcell.mxObjectId.indexOf("mxCell") !== -1) {
13271367
if (mxcell.style && mxcell.style.trim().startsWith("swimlane;")) {
1368+
let entityName = mxcell.value.toString();
1369+
let description = "";
1370+
let formatValue = "";
1371+
if ((entityName === null || entityName === void 0 ? void 0 : entityName.includes(constants_1.commentColumnQuantifiers.Start)) &&
1372+
(entityName === null || entityName === void 0 ? void 0 : entityName.includes(constants_1.commentColumnQuantifiers.End))) {
1373+
let result = entityName.toString();
1374+
const commentIndexes = getCommentIndexes(result);
1375+
const firstSpaceIndex = commentIndexes.start;
1376+
const lastSpaceIndex = commentIndexes.end;
1377+
entityName = result.substring(0, commentIndexes.beforeStart);
1378+
result = result.substring(firstSpaceIndex, lastSpaceIndex);
1379+
if (result.indexOf(constants_1.formatKeyword) !== -1) {
1380+
const formatIndex = result.indexOf(constants_1.formatKeyword);
1381+
formatValue = result.substring(formatIndex + constants_1.formatKeyword.length).trim();
1382+
result = result.substring(0, formatIndex);
1383+
}
1384+
if (result) {
1385+
description = result;
1386+
}
1387+
// decription = attribute.attributeType?.replace("/**", "").replace("*/", "");
1388+
}
13281389
const entity = {
1329-
name: RemoveNameQuantifiers(mxcell.value),
1390+
name: RemoveNameQuantifiers(entityName),
13301391
attributes: [],
13311392
};
1393+
const comment = generateComment(description, formatValue);
1394+
if (comment) {
1395+
entity.name += comment;
1396+
}
1397+
// const comment =
13321398
for (let c = 0; c < mxcell.children.length; c++) {
13331399
const col = mxcell.children[c];
13341400
if (col.mxObjectId.indexOf("mxCell") !== -1) {
@@ -1482,6 +1548,10 @@ function getMermaidDiagramDb(ui, type) {
14821548
}
14831549
}
14841550
}
1551+
const db = GenerateDatabaseModel(entities, relationships);
1552+
return db;
1553+
}
1554+
function GenerateDatabaseModel(entities, relationships) {
14851555
class DatabaseModel {
14861556
constructor(entities, relationships) {
14871557
this.entities = entities;
@@ -1497,5 +1567,19 @@ function getMermaidDiagramDb(ui, type) {
14971567
const db = new DatabaseModel(entities, relationships);
14981568
return db;
14991569
}
1570+
function generateComment(description, formatValue) {
1571+
let result = "";
1572+
if (description) {
1573+
result += `${description}`;
1574+
}
1575+
if (formatValue) {
1576+
result += ` @format ${formatValue}`;
1577+
}
1578+
if (result) {
1579+
result = result.trim();
1580+
result = `${constants_1.commentColumnQuantifiers.Start} ${result} ${constants_1.commentColumnQuantifiers.End}`;
1581+
}
1582+
return result;
1583+
}
15001584

1501-
},{}]},{},[4]);
1585+
},{"./constants":5}]},{},[4]);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sqltooling-drawio",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "plugins for sql tooling in drawio",
55
"main": "index.js",
66
"engines": {

src/nosql-ts.ts

Lines changed: 5 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { convertTypeScriptToCoreTypes } from "core-types-ts/dist/lib/ts-to-core-
88
import { convertCoreTypesToTypeScript } from "core-types-ts";
99
import { GetColumnQuantifiers, RemoveNameQuantifiers, dbTypeEnds, getDbLabel, getMermaidDiagramDb } from "./utils/sharedUtils";
1010
import { pluginVersion } from "./utils/constants";
11-
import { dbToOpenApi } from "./utils/nosqlUtils";
11+
import { ConvertOpenApiToDatabaseModel, dbToOpenApi, GeneratePropertyModel } from "./utils/nosqlUtils";
1212

1313
declare const window: Customwindow;
1414

@@ -281,7 +281,9 @@ export interface Child {
281281
const data = JSON.parse(text);
282282
const { data: doc } = convertOpenApiToCoreTypes( data );
283283
const { data: jsonSchema } = convertCoreTypesToJsonSchema( doc );
284-
openApi = jsonSchemaDocumentToOpenApi( jsonSchema, openApiOptions );
284+
// was losing format option, just going to check if exception thrown here
285+
jsonSchemaDocumentToOpenApi( jsonSchema, openApiOptions );
286+
openApi = data;
285287
} else if(type == "ts"){
286288
// serialize typescript classes to openapi spec
287289
const { data: doc } = convertTypeScriptToCoreTypes( text );
@@ -290,81 +292,7 @@ export interface Child {
290292
}
291293
const schemas = openApi?.components?.schemas;
292294
if(schemas){
293-
const models: DatabaseModel = {
294-
Dialect: "nosql",
295-
TableList: [],
296-
PrimaryKeyList: [],
297-
ForeignKeyList: [],
298-
};
299-
300-
for (const key in schemas) {
301-
if (Object.prototype.hasOwnProperty.call(schemas, key)) {
302-
const schema = schemas[key] as JSONSchema4;
303-
const tableModel: TableModel = {
304-
Name: dbTypeEnds(key),
305-
Properties: [],
306-
};
307-
for (const propertyKey in schema.properties) {
308-
if (Object.prototype.hasOwnProperty.call(schema.properties, propertyKey)) {
309-
const property = schema.properties[propertyKey];
310-
const propertyModel: PropertyModel = GeneratePropertyModel(key, propertyKey, property);
311-
if(propertyModel.ColumnProperties.includes("object") ||
312-
propertyModel.ColumnProperties.includes("array")) {
313-
let refName: string| null | undefined= null;
314-
if(property.$ref) {
315-
refName = property.$ref.split("/").pop();
316-
} else if(property.items && typeof property.items == "object") {
317-
refName = (property.items as JSONSchema4).$ref?.split("/").pop();
318-
}
319-
if(refName) {
320-
321-
const primaryKeyModel: ForeignKeyModel = {
322-
PrimaryKeyTableName: dbTypeEnds(key),
323-
ReferencesTableName: dbTypeEnds(refName),
324-
PrimaryKeyName: dbTypeEnds(propertyKey),
325-
// should just point to first property in uml table
326-
ReferencesPropertyName: "",
327-
IsDestination: false
328-
};
329-
const foreignKeyModel: ForeignKeyModel = {
330-
ReferencesTableName: dbTypeEnds(key),
331-
PrimaryKeyTableName: dbTypeEnds(refName),
332-
ReferencesPropertyName: dbTypeEnds(propertyKey),
333-
// should just point to first property in uml table
334-
PrimaryKeyName: "",
335-
IsDestination: true
336-
};
337-
models.ForeignKeyList.push(foreignKeyModel);
338-
models.ForeignKeyList.push(primaryKeyModel);
339-
propertyModel.IsForeignKey = true;
340-
}
341-
}
342-
343-
tableModel.Properties.push(propertyModel);
344-
}
345-
}
346-
347-
models.TableList.push(tableModel);
348-
}
349-
}
350-
for (let i = 0; i < models.ForeignKeyList.length; i++) {
351-
const fk = models.ForeignKeyList[i];
352-
if(!fk.ReferencesPropertyName){
353-
// match to first entry
354-
const property = models.TableList.find(t => t.Name == fk.ReferencesTableName)?.Properties[0];
355-
if(property){
356-
models.ForeignKeyList[i].ReferencesPropertyName = property.Name;
357-
}
358-
}
359-
if(!fk.PrimaryKeyName){
360-
// match to first entry
361-
const property = models.TableList.find(t => t.Name == fk.PrimaryKeyTableName)?.Properties[0];
362-
if(property){
363-
models.ForeignKeyList[i].PrimaryKeyName = property.Name;
364-
}
365-
}
366-
367-
}
295+
const models = ConvertOpenApiToDatabaseModel(schemas);
368296
foreignKeyList = models.ForeignKeyList;
369297
primaryKeyList = models.PrimaryKeyList;
370298
tableList = models.TableList;
@@ -562,20 +490,4 @@ export interface Child {
562490
}
563491
}
564492
});
565-
// TODO: may need to make recursive for when schema property items is array
566-
function GeneratePropertyModel(tableName: string, propertyName: string, property: JSONSchema4): PropertyModel {
567-
let columnProperties = (property.type ?? "object").toString();
568-
if(property.nullable) {
569-
columnProperties += " nullable";
570-
}
571-
const result: PropertyModel = {
572-
Name: dbTypeEnds(propertyName),
573-
IsPrimaryKey: false,
574-
IsForeignKey: false,
575-
ColumnProperties: columnProperties,
576-
TableName: dbTypeEnds(tableName),
577-
ForeignKey: [],
578-
};
579-
return result;
580-
}
581493

0 commit comments

Comments
 (0)