Skip to content

Commit

Permalink
Handle multiple PKs
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkiu committed Sep 20, 2022
1 parent abf12bb commit 547b33b
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 66 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@ schemaToErd('./schema_samples/sakila-schema.sql');

![sakila-schema.puml](puml_examples/sakila-schema.png)

## Source Codes

### Clone
```shell
$ git clone https://github.com/youngkiu/schema-to-erd.git
$ git submodule update --init --recursive
```

### Contribute
If an error occurs during use, please [open a new issue](https://github.com/youngkiu/schema-to-erd/issues) with the schema.sql file.
36 changes: 18 additions & 18 deletions dist/main.cjs

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions dist/main.mjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schema-to-erd",
"version": "1.2.9",
"version": "1.2.10",
"description": "Generate ERD UML file from Schema DDL file",
"type": "module",
"main": "dist/main.cjs",
Expand Down
4 changes: 4 additions & 0 deletions puml_examples/schema/oscommerce.puml
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,12 @@ customers_basket::customers_id --> customers::customers_id
customers_basket::products_id --> products::products_id
customers_basket_attributes::customers_id --> customers::customers_id
customers_basket_attributes::products_id --> products::products_id
customers_basket_attributes::products_options_id --> products_options::products_options_id
orders::customers_id --> customers::customers_id
orders_products::orders_id --> orders::orders_id
orders_products::products_id --> products::products_id
orders_status_history::orders_id --> orders::orders_id
orders_status_history::orders_status_id --> orders_status::orders_status_id
orders_products_attributes::orders_id --> orders::orders_id
orders_products_attributes::orders_products_id --> orders_products::orders_products_id
orders_products_download::orders_id --> orders::orders_id
Expand All @@ -461,6 +463,8 @@ orders_total::orders_id --> orders::orders_id
products::manufacturers_id --> manufacturers::manufacturers_id
products_attributes::products_id --> products::products_id
products_images::products_id --> products::products_id
products_options_values_to_products_options::products_options_id --> products_options::products_options_id
products_options_values_to_products_options::products_options_values_id --> products_options_values::products_options_values_id
reviews::products_id --> products::products_id
reviews::customers_id --> customers::customers_id
specials::products_id --> products::products_id
Expand Down
8 changes: 8 additions & 0 deletions puml_examples/schema/zencart.puml
Original file line number Diff line number Diff line change
Expand Up @@ -1009,23 +1009,31 @@ customers_basket::customers_id --> customers::customers_id
customers_basket::products_id --> products::products_id
customers_basket_attributes::customers_id --> customers::customers_id
customers_basket_attributes::products_id --> products::products_id
customers_basket_attributes::products_options_id --> products_options::products_options_id
ezpages::languages_id --> languages::languages_id
featured::products_id --> products::products_id
files_uploaded::customers_id --> customers::customers_id
orders::customers_id --> customers::customers_id
orders::paypal_ipn_id --> paypal::paypal_ipn_id
orders_products::orders_id --> orders::orders_id
orders_products::products_id --> products::products_id
orders_products_attributes::orders_id --> orders::orders_id
orders_products_attributes::orders_products_id --> orders_products::orders_products_id
orders_products_attributes::products_options_id --> products_options::products_options_id
orders_products_attributes::products_options_values_id --> products_options_values::products_options_values_id
orders_products_download::orders_id --> orders::orders_id
orders_products_download::orders_products_id --> orders_products::orders_products_id
orders_status_history::orders_id --> orders::orders_id
orders_status_history::orders_status_id --> orders_status::orders_status_id
orders_total::orders_id --> orders::orders_id
paypal_payment_status_history::paypal_ipn_id --> paypal::paypal_ipn_id
product_music_extra::record_company_id --> record_company::record_company_id
product_music_extra::music_genre_id --> music_genre::music_genre_id
products::manufacturers_id --> manufacturers::manufacturers_id
products_attributes::products_id --> products::products_id
products_discount_quantity::products_id --> products::products_id
products_options_values_to_products_options::products_options_id --> products_options::products_options_id
products_options_values_to_products_options::products_options_values_id --> products_options_values::products_options_values_id
reviews::products_id --> products::products_id
reviews::customers_id --> customers::customers_id
specials::products_id --> products::products_id
Expand Down
2 changes: 1 addition & 1 deletion src/parse_ddl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Parser } from 'sql-ddl-to-json-schema';
import assert from 'assert';
import _ from 'lodash';
import config from './unparsable_token.json' assert { type: "json" };
import config from './unparsable_token.json' assert { type: 'json' };

const parser = new Parser('mysql');

Expand Down
45 changes: 20 additions & 25 deletions src/plantuml_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,29 @@ function _generateRelation(tableName, columnNames, primaryKeys, allPKs) {
}

export default function(tableColumns) {
const entities = Object.entries(tableColumns)
.reduce(
(acc, [tableName, { columnNames, primaryKeys }]) => (
acc + _generateEntity(tableName, columnNames, primaryKeys)
),
'',
);
const allPKs = Object.entries(tableColumns)
.reduce(
(acc, [tableName, { primaryKeys }]) => {
if (primaryKeys.length !== 1) {
return acc;
}

const primaryKey = primaryKeys[0];
const entities = Object.entries(tableColumns).reduce(
(acc, [tableName, { columnNames, primaryKeys }]) => (
acc + _generateEntity(tableName, columnNames, primaryKeys)
),
'',
);
const allPKs = Object.entries(tableColumns).reduce(
(accTable, [tableName, { primaryKeys }]) => primaryKeys.reduce(
(accPk, primaryKey) => {
const primaryKeyName = primaryKey.startsWith(tableName) ? primaryKey : `${tableName}_${primaryKey}`;
const foreignKey = { tableName, columnName: primaryKey };
return { ...acc, [primaryKeyName]: foreignKey };
return { ...accPk, [primaryKeyName]: foreignKey };
},
{},
);
const relations = Object.entries(tableColumns)
.reduce(
(acc, [tableName, { columnNames, primaryKeys }]) => [
...acc, ..._generateRelation(tableName, columnNames, primaryKeys, allPKs),
],
[],
);
accTable,
),
{},
);
const relations = Object.entries(tableColumns).reduce(
(acc, [tableName, { columnNames, primaryKeys }]) => [
...acc, ..._generateRelation(tableName, columnNames, primaryKeys, allPKs),
],
[],
);

// https://plantuml.com/ko/ie-diagram
return `@startuml
Expand Down
2 changes: 1 addition & 1 deletion test/schema_to_erd.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import schemaToErd from '../src/schema_to_erd.js';
import { schemaToErd } from '../src/main.js';
import { promises as fs } from 'fs';
import path from 'path';
import glob from 'glob';
Expand Down

0 comments on commit 547b33b

Please sign in to comment.