diff --git a/.eslintrc b/.eslintrc index e8e33f3..25ee729 100644 --- a/.eslintrc +++ b/.eslintrc @@ -22,6 +22,7 @@ "@typescript-eslint/no-explicit-any": "off", "no-multiple-empty-lines": "error", "no-use-before-define": "off", + "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/no-use-before-define": "off", "@typescript-eslint/no-inferrable-types": [ "warn", diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index ed10d11..45d720b 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -20,5 +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 + - run: npm run build:client:nosql:min diff --git a/README.md b/README.md index c97707b..1d2dd69 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,12 @@ # sqltooling-drawio -plugins for sql tooling in drawio +* 3rd party plugins for sql tooling in drawio +* contributions welcome ## Getting Started * see https://github.com/ariel-bentu/tam-drawio for multiple install options * download plugin file * [sql.js](https://raw.githubusercontent.com/funktechno/sqltooling-drawio/main/dist/sql.js) - import/export SQL DLLS - * [nosql.js](https://raw.githubusercontent.com/funktechno/sqltooling-drawio/main/dist/nosql.js) - import/export typescript interfaces (NOT vscode compatible) + * [nosql.js](https://raw.githubusercontent.com/funktechno/sqltooling-drawio/main/dist/nosql.js) - import/export typescript interfaces and openapi jsons (NOT vscode compatible) * or [nosql.min.js](https://raw.githubusercontent.com/funktechno/sqltooling-drawio/main/dist/nosql.min.js) * or clone project `git clone --branch main git@github.com:funktechno/sqltooling-drawio.git` and check `dist folder` * vscode [Draw.io Integration](https://marketplace.visualstudio.com/items?itemName=hediet.vscode-drawio) diff --git a/package.json b/package.json index d0ab7f3..1bed727 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,9 @@ "build:types": "tsc -p tsconfig.json", "build:help": "tsc --help", "build:client": "npm run mkdir:dist && browserify src/sql.ts -p [ tsify --noImplicitAny ] > dist/sql.js && npm run updateVersion:dist", - "build:client:nosql:legacy": "npm run mkdir:dist && browserify src/nosql.ts -p [ tsify --noImplicitAny ] > dist/nosql.js && npm run updateVersion:dist", - "build:client:nosql": "npm run mkdir:dist && gulp && npm run updateVersion:dist", + "build:client:nosql": "npm run mkdir:dist && browserify src/nosql.ts -p [ tsify --noImplicitAny ] > dist/nosql.js && npm run updateVersion:dist", + "build:client:nosql:manual": "browserify src/nosql.ts -p [ tsify --noImplicitAny ] > \"absolutePath\\drawio-desktop\\drawio\\src\\main\\webapp\\plugins\\sql.js\"", + "build:client:nosql:min": "npm run mkdir:dist && gulp && npm run updateVersion:dist", "mkdir:dist": "node ./build/createDistFolder.js", "updateVersion:dist": "node ./build/updateVersion.js" }, diff --git a/src/nosql.ts b/src/nosql.ts index 435c1ee..9f2774e 100644 --- a/src/nosql.ts +++ b/src/nosql.ts @@ -1,34 +1,11 @@ -console.log("starting nosql plugin"); -import { DbParser } from "@funktechno/little-mermaid-2-the-sql/lib/src/generate-sql-ddl"; import { DbDefinition, DbRelationshipDefinition } from "@funktechno/little-mermaid-2-the-sql/lib/src/types"; import { ColumnQuantifiers, TableAttribute, TableEntity } from "./types/sql-plugin-types"; -// import { SqlSimpleParser } from "@funktechno/sqlsimpleparser"; import { DatabaseModel, ForeignKeyModel, PrimaryKeyModel, PropertyModel, TableModel } from "@funktechno/sqlsimpleparser/lib/types"; -import { JSONSchema4 } from "json-schema"; -// import { createRequire } from 'module'; -// const require = createRequire(import.meta.url); - -// // sibling-module.js is a CommonJS module. -// const siblingModule = require('./sibling-module'); - - -// import { -// getTypeScriptReader, -// getOpenApiWriter, -// makeConverter, -// getTypeScriptWriter, -// getOpenApiReader, -// } from "typeconv"; -// import { convertTypeScriptToCoreTypes } from "core-types-ts"; +import { JSONSchema4, JSONSchema4TypeName } from "json-schema"; import { convertCoreTypesToJsonSchema, convertOpenApiToCoreTypes, jsonSchemaDocumentToOpenApi } from "core-types-json-schema"; import { JsonSchemaDocumentToOpenApiOptions, PartialOpenApiSchema } from "openapi-json-schema"; import { convertTypeScriptToCoreTypes } from "core-types-ts/dist/lib/ts-to-core-types"; -// import { convertCoreTypesToJsonSchema } from "core-types-json-schema/dist/lib/core-types-to-json-schema"; -// import { -// JsonSchemaDocumentToOpenApiOptions, -// jsonSchemaDocumentToOpenApi, -// openApiToJsonSchema, -// } from "openapi-json-schema"; +import { convertCoreTypesToTypeScript } from "core-types-ts"; declare const window: Customwindow; @@ -36,10 +13,7 @@ declare const window: Customwindow; * SQL Tools Plugin for importing and exporting typescript interfaces. * Version: */ - Draw.loadPlugin(function(ui) { - console.log("loading nosql plugin"); - // export sql methods const pluginVersion = ""; @@ -53,7 +27,7 @@ Draw.loadPlugin(function(ui) { const sqlInputGenSQL = document.createElement("textarea"); sqlInputGenSQL.style.height = "200px"; sqlInputGenSQL.style.width = "100%"; - const sqlExportDefault = "-- click a database type button"; + const sqlExportDefault = "-- click a nosql type button"; sqlInputGenSQL.value = sqlExportDefault; mxUtils.br(divGenSQL); divGenSQL.appendChild(sqlInputGenSQL); @@ -327,21 +301,75 @@ Draw.loadPlugin(function(ui) { return db; } - function generateSql(type: "ts" | "openapi" | undefined) { - + function generateNoSql(type: "ts" | "openapi" | undefined) { // get diagram model const db = getMermaidDiagramDb(type); - // load parser - const parser = new DbParser(type as string, db); - // generate sql - let sql = parser.getSQLDataDefinition(); - 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; - // TODO: use selection as well? - const modelSelected = ui.editor.graph.getSelectionModel(); + const openapi = dbToOpenApi(db); + let result = ""; + if(type == "ts"){ + + const { data: doc } = convertOpenApiToCoreTypes( openapi ); + const { data: sourceCode } = convertCoreTypesToTypeScript( doc ); + result = `/*\n\tGenerated in drawio\n\tDatabase: ${type}\n\tPlugin: nosql\n\tVersion: ${pluginVersion}\n*/\n\n` + result; + result += sourceCode; + + } else if(type == "openapi"){ + result = JSON.stringify(openapi, null, 2); + } + sqlInputGenSQL.value = result; }; + /** + * convert db to openapi + * @param db + * @returns + */ + function dbToOpenApi(db: DbDefinition):PartialOpenApiSchema { + const result: PartialOpenApiSchema = { + openapi: "3.0.0", + info: { + // drawio file name? + title: "drawio nosql export", + version: pluginVersion, + "x-comment": "Generated by from drawio uml using plugin nosql" + }, + paths: {}, + components: { + schemas: {} + } + }; + const schema: JSONSchema4 = {}; + const entities = db.getEntities(); + for (const key in entities) { + if (Object.prototype.hasOwnProperty.call(entities, key)) { + const entity = entities[key]; + if(schema[key]){ + continue; + } + schema[key] = { + type: "object", + title: key, + additionalProperties: false, + properties: {} + }; + for (let p = 0; p < entity.attributes.length; p++) { + const attribute = entity.attributes[p]; + const propName = attribute.attributeName; + if(!propName || schema[key].properties[propName]){ + continue; + } + const attType = attribute.attributeType?.split(" ") ?? []; + const property: JSONSchema4 = { + title: `${key}.${propName}`, + nullable: attribute.attributeType?.includes("nullable") ?? false, + type: (attType[0] ?? "string") as JSONSchema4TypeName + }; + schema[key].properties[attribute.attributeName!] = property; + } + } + } + result.components!.schemas = schema; + return result; + } mxUtils.br(divGenSQL); @@ -354,8 +382,16 @@ Draw.loadPlugin(function(ui) { resetBtnGenSQL.style.padding = "4px"; divGenSQL.appendChild(resetBtnGenSQL); - const btnGenSQL_ts = mxUtils.button("TS", function() { - generateSql("ts"); + let btnGenSQL_ts = mxUtils.button("TS", function() { + generateNoSql("ts"); + }); + + btnGenSQL_ts.style.marginTop = "8px"; + btnGenSQL_ts.style.padding = "4px"; + divGenSQL.appendChild(btnGenSQL_ts); + + btnGenSQL_ts = mxUtils.button("OpenAPI", function() { + generateNoSql("openapi"); }); btnGenSQL_ts.style.marginTop = "8px"; @@ -859,3 +895,4 @@ function dbTypeEnds(label: string): string { // } return `${char1}${label}${char2}`; } +