Skip to content

Commit

Permalink
Merge pull request #21 from funktechno/f/nosql
Browse files Browse the repository at this point in the history
F/nosql working typescript and nosql generation
  • Loading branch information
lastlink authored Jun 24, 2024
2 parents c3d0624 + ed11b08 commit c2feb44
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 48 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
123 changes: 80 additions & 43 deletions src/nosql.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
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;

/**
* SQL Tools Plugin for importing and exporting typescript interfaces.
* Version: <VERSION>
*/

Draw.loadPlugin(function(ui) {
console.log("loading nosql plugin");

// export sql methods
const pluginVersion = "<VERSION>";

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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";
Expand Down Expand Up @@ -859,3 +895,4 @@ function dbTypeEnds(label: string): string {
// }
return `${char1}${label}${char2}`;
}

0 comments on commit c2feb44

Please sign in to comment.