Skip to content

Commit 1c20778

Browse files
authored
Merge pull request #40 from funktechno/f/updates
improved comment parsing
2 parents edd3276 + b974fae commit 1c20778

File tree

4 files changed

+124
-171
lines changed

4 files changed

+124
-171
lines changed

src/nosql-ts.ts

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { convertCoreTypesToTypeScript } from "core-types-ts";
99
import { GetColumnQuantifiers, RemoveNameQuantifiers, dbTypeEnds, getDbLabel, getMermaidDiagramDb } from "./utils/sharedUtils";
1010
import { pluginVersion } from "./utils/constants";
1111
import { ConvertOpenApiToDatabaseModel, dbToOpenApi, GeneratePropertyModel } from "./utils/nosqlUtils";
12+
import { defaultReset, defaultResetOpenApi } from "./utils/constants-nosql";
1213

1314
declare const window: Customwindow;
1415

@@ -128,96 +129,6 @@ Draw.loadPlugin(function(ui) {
128129
const sqlInputFromNOSQL = document.createElement("textarea");
129130
sqlInputFromNOSQL.style.height = "200px";
130131
sqlInputFromNOSQL.style.width = "100%";
131-
const defaultReset = `/*\n\tDrawio default value\n\tPlugin: nosql\n\tVersion: ${pluginVersion}\n*/\n\n
132-
export interface WeatherForecast {
133-
/** @format date-time */
134-
date?: string;
135-
/** @format int32 */
136-
temperatureC?: number;
137-
/** @format int32 */
138-
temperatureF?: number;
139-
summary?: string | null;
140-
nestedProp: string[];
141-
children?: Child[];
142-
}
143-
144-
export interface Child {
145-
name: string
146-
}
147-
`;
148-
149-
150-
const defaultResetOpenApi = `
151-
{
152-
"openapi": "3.0.0",
153-
"info": {
154-
"title": "nosql plugin sample",
155-
"version": "${pluginVersion}",
156-
"x-comment": "Generated by core-types-json-schema (https://github.com/grantila/core-types-json-schema)"
157-
},
158-
"paths": {},
159-
"components": {
160-
"schemas": {
161-
"WeatherForecast": {
162-
"properties": {
163-
"date": {
164-
"title": "WeatherForecast.date",
165-
"description": "@format date-time",
166-
"type": "string"
167-
},
168-
"temperatureC": {
169-
"title": "WeatherForecast.temperatureC",
170-
"description": "@format int32",
171-
"type": "number"
172-
},
173-
"temperatureF": {
174-
"title": "WeatherForecast.temperatureF",
175-
"description": "@format int32",
176-
"type": "number"
177-
},
178-
"summary": {
179-
"title": "WeatherForecast.summary",
180-
"nullable": true,
181-
"type": "string"
182-
},
183-
"nestedProp": {
184-
"items": {
185-
"title": "WeatherForecast.nestedProp.[]",
186-
"type": "string"
187-
},
188-
"title": "WeatherForecast.nestedProp",
189-
"type": "array"
190-
},
191-
"child": {
192-
"$ref": "#/components/schemas/Child",
193-
"title": "WeatherForecast.child"
194-
}
195-
},
196-
"required": [
197-
"nestedProp"
198-
],
199-
"additionalProperties": false,
200-
"title": "WeatherForecast",
201-
"type": "object"
202-
},
203-
"Child": {
204-
"properties": {
205-
"name": {
206-
"title": "Child.name",
207-
"type": "string"
208-
}
209-
},
210-
"required": [
211-
"name"
212-
],
213-
"additionalProperties": false,
214-
"title": "Child",
215-
"type": "object"
216-
}
217-
}
218-
}
219-
}
220-
`;
221132

222133
sqlInputFromNOSQL.value = defaultReset;
223134
mxUtils.br(divFromNOSQL);

src/nosql.ts

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { JsonSchemaDocumentToOpenApiOptions, PartialOpenApiSchema } from "openap
77
import { GetColumnQuantifiers, RemoveNameQuantifiers, dbTypeEnds, getDbLabel, getMermaidDiagramDb } from "./utils/sharedUtils";
88
import { pluginVersion } from "./utils/constants";
99
import { ConvertOpenApiToDatabaseModel, dbToOpenApi, GeneratePropertyModel } from "./utils/nosqlUtils";
10+
import { defaultResetOpenApi } from "./utils/constants-nosql";
1011

1112
declare const window: Customwindow;
1213

@@ -113,79 +114,6 @@ Draw.loadPlugin(function(ui) {
113114
const sqlInputFromNOSQL = document.createElement("textarea");
114115
sqlInputFromNOSQL.style.height = "200px";
115116
sqlInputFromNOSQL.style.width = "100%";
116-
117-
const defaultResetOpenApi = `
118-
{
119-
"openapi": "3.0.0",
120-
"info": {
121-
"title": "nosql plugin sample",
122-
"version": "${pluginVersion}",
123-
"x-comment": "Generated by core-types-json-schema (https://github.com/grantila/core-types-json-schema)"
124-
},
125-
"paths": {},
126-
"components": {
127-
"schemas": {
128-
"WeatherForecast": {
129-
"properties": {
130-
"date": {
131-
"title": "WeatherForecast.date",
132-
"description": "@format date-time",
133-
"type": "string"
134-
},
135-
"temperatureC": {
136-
"title": "WeatherForecast.temperatureC",
137-
"description": "@format int32",
138-
"type": "number"
139-
},
140-
"temperatureF": {
141-
"title": "WeatherForecast.temperatureF",
142-
"description": "@format int32",
143-
"type": "number"
144-
},
145-
"summary": {
146-
"title": "WeatherForecast.summary",
147-
"nullable": true,
148-
"type": "string"
149-
},
150-
"nestedProp": {
151-
"items": {
152-
"title": "WeatherForecast.nestedProp.[]",
153-
"type": "string"
154-
},
155-
"title": "WeatherForecast.nestedProp",
156-
"type": "array"
157-
},
158-
"child": {
159-
"$ref": "#/components/schemas/Child",
160-
"title": "WeatherForecast.child"
161-
}
162-
},
163-
"required": [
164-
"nestedProp"
165-
],
166-
"additionalProperties": false,
167-
"title": "WeatherForecast",
168-
"type": "object"
169-
},
170-
"Child": {
171-
"properties": {
172-
"name": {
173-
"title": "Child.name",
174-
"type": "string"
175-
}
176-
},
177-
"required": [
178-
"name"
179-
],
180-
"additionalProperties": false,
181-
"title": "Child",
182-
"type": "object"
183-
}
184-
}
185-
}
186-
}
187-
`;
188-
189117
sqlInputFromNOSQL.value = defaultResetOpenApi;
190118
mxUtils.br(divFromNOSQL);
191119
divFromNOSQL.appendChild(sqlInputFromNOSQL);

src/utils/constants-nosql.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { pluginVersion } from "./constants";
2+
3+
export const defaultReset = `/*\n\tDrawio default value\n\tPlugin: nosql\n\tVersion: ${pluginVersion}\n*/\n\n
4+
export interface WeatherForecast {
5+
/** @format date-time */
6+
date?: string;
7+
/** @format int32 */
8+
temperatureC?: number;
9+
/** @format int32 */
10+
temperatureF?: number;
11+
summary?: string | null;
12+
nestedProp: string[];
13+
children?: Child[];
14+
}
15+
16+
export interface Child {
17+
name: string
18+
}
19+
`;
20+
21+
export const defaultResetOpenApi = `
22+
{
23+
"openapi": "3.0.0",
24+
"info": {
25+
"title": "nosql plugin sample",
26+
"version": "${pluginVersion}",
27+
"x-comment": "Generated by core-types-json-schema (https://github.com/grantila/core-types-json-schema)"
28+
},
29+
"paths": {},
30+
"components": {
31+
"schemas": {
32+
"WeatherForecast": {
33+
"properties": {
34+
"date": {
35+
"title": "WeatherForecast.date",
36+
"description": "@format date-time",
37+
"type": "string"
38+
},
39+
"temperatureC": {
40+
"title": "WeatherForecast.temperatureC",
41+
"description": "@format int32",
42+
"type": "number"
43+
},
44+
"temperatureF": {
45+
"title": "WeatherForecast.temperatureF",
46+
"description": "@format int32",
47+
"type": "number"
48+
},
49+
"summary": {
50+
"title": "WeatherForecast.summary",
51+
"nullable": true,
52+
"type": "string"
53+
},
54+
"nestedProp": {
55+
"items": {
56+
"title": "WeatherForecast.nestedProp.[]",
57+
"type": "string"
58+
},
59+
"title": "WeatherForecast.nestedProp",
60+
"type": "array"
61+
},
62+
"child": {
63+
"$ref": "#/components/schemas/Child",
64+
"title": "WeatherForecast.child"
65+
}
66+
},
67+
"required": [
68+
"nestedProp"
69+
],
70+
"additionalProperties": false,
71+
"title": "WeatherForecast",
72+
"type": "object"
73+
},
74+
"Child": {
75+
"properties": {
76+
"name": {
77+
"title": "Child.name",
78+
"type": "string"
79+
}
80+
},
81+
"required": [
82+
"name"
83+
],
84+
"additionalProperties": false,
85+
"title": "Child",
86+
"type": "object"
87+
}
88+
}
89+
}
90+
}
91+
`;

src/utils/nosqlUtils.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export function dbToOpenApi(db: DatabaseModelResult): PartialOpenApiSchema {
153153
schema[schemaKey].type = type;
154154
} else {
155155
const property: JSONSchema4 = {
156-
title: `${key}.${propName}`,
156+
title: `${schemaKey}.${propName}`,
157157
nullable: attribute.attributeType?.includes("nullable") ?? false,
158158
type: type,
159159
};
@@ -211,9 +211,11 @@ export function ConvertOpenApiToDatabaseModel(
211211
PrimaryKeyList: [],
212212
ForeignKeyList: [],
213213
};
214+
const tableDict: Record<string, TableModel> = {};
214215
for (const key in schemas) {
215216
if (Object.prototype.hasOwnProperty.call(schemas, key)) {
216217
const schema = schemas[key] as JSONSchema4;
218+
const originalKey = dbTypeEnds(key);
217219
const tableModel: TableModel = {
218220
Name: dbTypeEnds(key),
219221
Properties: [],
@@ -250,7 +252,7 @@ export function ConvertOpenApiToDatabaseModel(
250252
) {
251253
const property = schema.properties[propertyKey];
252254
const propertyModel: PropertyModel = GeneratePropertyModel(
253-
key,
255+
tableModel.Name,
254256
propertyKey,
255257
property
256258
);
@@ -264,18 +266,27 @@ export function ConvertOpenApiToDatabaseModel(
264266
} else if (property.items && typeof property.items == "object") {
265267
refName = (property.items as JSONSchema4).$ref?.split("/").pop();
266268
}
269+
if(refName) {
270+
const refSchema:JSONSchema4|null = schemas[refName] as JSONSchema4;
271+
if(refSchema && !refSchema.enum){
272+
const comment = generateComment(refSchema.description, refSchema.format);
273+
if (comment) {
274+
refName = `${dbTypeEnds(refName)} ${comment}`;
275+
}
276+
}
277+
}
267278
if (refName) {
268279
const primaryKeyModel: ForeignKeyModel = {
269-
PrimaryKeyTableName: dbTypeEnds(key),
270-
ReferencesTableName: dbTypeEnds(refName),
280+
PrimaryKeyTableName: tableModel.Name,
281+
ReferencesTableName: refName,
271282
PrimaryKeyName: dbTypeEnds(propertyKey),
272283
// should just point to first property in uml table
273284
ReferencesPropertyName: "",
274285
IsDestination: false,
275286
};
276287
const foreignKeyModel: ForeignKeyModel = {
277-
ReferencesTableName: dbTypeEnds(key),
278-
PrimaryKeyTableName: dbTypeEnds(refName),
288+
ReferencesTableName: tableModel.Name,
289+
PrimaryKeyTableName: refName,
279290
ReferencesPropertyName: dbTypeEnds(propertyKey),
280291
// should just point to first property in uml table
281292
PrimaryKeyName: "",
@@ -292,24 +303,36 @@ export function ConvertOpenApiToDatabaseModel(
292303
}
293304

294305
models.TableList.push(tableModel);
306+
// may no longer be needed
307+
if(!tableDict[originalKey]) {
308+
tableDict[originalKey] = tableModel;
309+
}
295310
}
296311
}
297312
for (let i = 0; i < models.ForeignKeyList.length; i++) {
298313
const fk = models.ForeignKeyList[i];
299314
if (!fk.ReferencesPropertyName) {
300315
// match to first entry
301-
const property = models.TableList.find(
316+
let property = models.TableList.find(
302317
(t) => t.Name == fk.ReferencesTableName
303318
)?.Properties[0];
319+
if(!property) {
320+
// attempt a comment lookup
321+
property = tableDict[fk.ReferencesTableName]?.Properties[0];
322+
}
304323
if (property) {
305324
models.ForeignKeyList[i].ReferencesPropertyName = property.Name;
306325
}
307326
}
308327
if (!fk.PrimaryKeyName) {
309328
// match to first entry
310-
const property = models.TableList.find(
329+
let property = models.TableList.find(
311330
(t) => t.Name == fk.PrimaryKeyTableName
312331
)?.Properties[0];
332+
if(!property) {
333+
// attempt a comment lookup
334+
property = tableDict[fk.PrimaryKeyTableName]?.Properties[0];
335+
}
313336
if (property) {
314337
models.ForeignKeyList[i].PrimaryKeyName = property.Name;
315338
}

0 commit comments

Comments
 (0)