Skip to content

Commit

Permalink
fix(mermaid): fixed mermaid ER Diagram
Browse files Browse the repository at this point in the history
- fixed mermaid ER Diagram syntax error when using TypeORM-only types that contain spaces
  • Loading branch information
imjuni committed Mar 12, 2024
1 parent f65aca8 commit 5cdf167
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ template2/
erdiadb.json
**/.DS_Store
.npmrc
git-short.sh
5 changes: 5 additions & 0 deletions examples/schema-type/License.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface ILicense {
title: string;
code: string;
description: string;
weight: number;
expire: Date;
}

Expand All @@ -30,6 +31,10 @@ export const License = new EntitySchema<ILicense & ILicenseRelation>({
comment: 'organization title',
charset: 'utf8mb4',
},
weight: {
type: 'double precision',
comment: 'sort weight',
},
code: {
type: 'varchar',
length: 200,
Expand Down
5 changes: 5 additions & 0 deletions examples/schema-type/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface IOrganization {
id: number;
title: string;
description: string;
supports: { name: string; year: number }[];
expire: Date;
}

Expand Down Expand Up @@ -33,6 +34,10 @@ export const Organization = new EntitySchema<IOrganization & IOrganizationRelati
comment: 'organization description',
charset: 'utf8mb4',
},
supports: {
type: 'simple-json',
comment: 'organization supports other organization',
},
expire: {
type: 'datetime',
default: () => 'CURRENT_TIMESTAMP',
Expand Down
2 changes: 2 additions & 0 deletions examples/schema-type/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function organizationFactory({
id: value.id ?? 0,
title: value.title ?? '',
description: value.description ?? '',
supports: [{ name: 'ironman', year: 1970 }],
expire: value.expire ?? new Date(),
};

Expand All @@ -54,6 +55,7 @@ function licenseFactory({ value: nullableValue }: { entity: 'license'; value?: P
code: value.code ?? '',
title: value.title ?? '',
description: value.description ?? '',
weight: 0.3,
expire: value.expire ?? new Date(),
};

Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"lint": "eslint --cache .",
"lint-staged": "lint-staged",
"prettier": "prettier --write src/**/*.ts",
"pub": "npm run bundle && RELEASE_MODE=true NODE_ENV=production npm publish --registry http://localhost:8901",
"unpub": "npm unpublish erdia@$npm_package_version --registry http://localhost:8901",
"pub": "npm run bundle && cross-env RELEASE_MODE=true NODE_ENV=production npm publish --registry http://localhost:8901",
"unpub": "npm unpublish $npm_package_name@$npm_package_version --registry http://localhost:8901",
"pub:prod": "npm run bundle && cross-env RELEASE_MODE=true npm publish --access=public",
"clean:dts": "rimraf dist/cjs/src dist/esm/src dist/src dist/examples",
"typeorm": "typeorm-ts-node-commonjs",
Expand Down Expand Up @@ -155,5 +155,13 @@
"type-fest": "^4.2.0",
"typeorm": "^0.3.17",
"yargs": "^17.7.2"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"cross-env NODE_ENV=production eslint --cache"
],
"*.{js,jsx,ts,tsx},*.json,.{eslintrc.cjs,prettierrc}": [
"cross-env NODE_ENV=production prettier --ignore-path .eslintignore --parser json --write"
]
}
}
2 changes: 1 addition & 1 deletion src/template/html/mermaid-script.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mermaidScript = `<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.3.1/mermaid.min.js"></script>
const mermaidScript = `<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.9.0/mermaid.min.js"></script>
<script>mermaid.initialize({ startOnLoad: true });</script>`;

export default mermaidScript;
12 changes: 6 additions & 6 deletions src/typeorm/columns/__tests__/column.tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,32 @@ describe('getColumnAttributeKey', () => {

describe('getColumnType', () => {
test('numeric-type', () => {
const columnType = getColumnType({ type: 'bigint', length: '0' });
const columnType = getColumnType({ type: 'bigint', length: '0', isPrimary: false, isNullable: true });
expect(columnType).toEqual('bigint');
});

test('function-type without length + undefined', () => {
const columnType = getColumnType({ type: Boolean, length: '200' });
const columnType = getColumnType({ type: Boolean, length: '200', isPrimary: false, isNullable: true });
expect(columnType).toEqual('boolean');
});

test('function-type with length', () => {
const columnType = getColumnType({ type: Boolean, length: '200' }, true);
const columnType = getColumnType({ type: Boolean, length: '200', isPrimary: false, isNullable: true }, true);
expect(columnType).toEqual('boolean(200)');
});

test('function-type without length', () => {
const columnType = getColumnType({ type: Boolean, length: '200' }, false);
const columnType = getColumnType({ type: Boolean, length: '200', isPrimary: false, isNullable: true }, false);
expect(columnType).toEqual('boolean');
});

test('string-type with length', () => {
const columnType = getColumnType({ type: 'varchar', length: '200' }, true);
const columnType = getColumnType({ type: 'varchar', length: '200', isPrimary: false, isNullable: true }, true);
expect(columnType).toEqual('varchar(200)');
});

test('string-type without length', () => {
const columnType = getColumnType({ type: 'varchar', length: '200' }, false);
const columnType = getColumnType({ type: 'varchar', length: '200', isPrimary: false, isNullable: true }, false);
expect(columnType).toEqual('varchar');
});
});
Original file line number Diff line number Diff line change
@@ -1,77 +1,82 @@
[
{
"$kind": "column",
"name": "firstName",
"version": "1.0.0",
"createdAt": "2023-01-01T11:22:33.000+09:00",
"updatedAt": "2023-01-02T11:22:33.000+09:00",
"change": "add",
"entity": "tbl_user",
"name": "firstName",
"dbName": "first_name",
"attributeKey": [],
"isNullable": "",
"columnType": "varchar",
"columnType": "*varchar",
"charset": "utf8mb4",
"columnTypeWithLength": "varchar(256)",
"columnTypeWithLength": "*varchar(256)",
"comment": "user firstname",
"weight": 743020.17
"weight": 785020.17
},
{
"$kind": "column",
"name": "id",
"version": "1.0.0",
"createdAt": "2023-01-01T11:22:33.000+09:00",
"updatedAt": "2023-01-02T11:22:33.000+09:00",
"change": "add",
"entity": "tbl_user",
"name": "id",
"dbName": "id",
"attributeKey": [
"PK"
],
"isNullable": "",
"columnType": "int",
"columnType": "*int",
"charset": "",
"columnTypeWithLength": "int",
"columnTypeWithLength": "*int",
"comment": "",
"weight": 20331017.22
"weight": 20373017.22
},
{
"$kind": "column",
"name": "isActive",
"version": "1.0.0",
"createdAt": "2023-01-01T11:22:33.000+09:00",
"updatedAt": "2023-01-02T11:22:33.000+09:00",
"change": "add",
"entity": "tbl_user",
"name": "isActive",
"dbName": "is_active",
"attributeKey": [],
"isNullable": "",
"columnType": "boolean",
"columnType": "*boolean",
"charset": "",
"columnTypeWithLength": "boolean",
"columnTypeWithLength": "*boolean",
"comment": "line1<br />line2<br />line3",
"weight": 736017.07
"weight": 778017.07
},
{
"$kind": "column",
"name": "lastName",
"version": "1.0.0",
"createdAt": "2023-01-01T11:22:33.000+09:00",
"updatedAt": "2023-01-02T11:22:33.000+09:00",
"change": "add",
"entity": "tbl_user",
"name": "lastName",
"dbName": "last_name",
"attributeKey": [],
"isNullable": "",
"columnType": "varchar",
"columnType": "*varchar",
"charset": "utf8mb4",
"columnTypeWithLength": "varchar(256)",
"columnTypeWithLength": "*varchar(256)",
"comment": "",
"weight": 743014.25
"weight": 785014.25
},
{
"$kind": "column",
"name": "photo",
"version": "1.0.0",
"createdAt": "2023-01-01T11:22:33.000+09:00",
"updatedAt": "2023-01-02T11:22:33.000+09:00",
"change": "add",
"entity": "tbl_user",
"name": "photo",
"dbName": "photo_id",
"attributeKey": [
"FK"
Expand Down
20 changes: 15 additions & 5 deletions src/typeorm/columns/getColumnType.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import getIsNullable from '#/typeorm/columns/getIsNullable';
import { isTrue } from 'my-easy-fp';
import type { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';

export default function getColumnType(
columnMetadata: Pick<ColumnMetadata, 'type' | 'length'>,
columnMetadata: Pick<ColumnMetadata, 'type' | 'length' | 'isNullable' | 'isPrimary'>,
includeLength?: boolean,
) {
const nullable = getIsNullable(columnMetadata);
if (typeof columnMetadata.type === 'function') {
if (isTrue(includeLength ?? false) && columnMetadata.length !== '') {
return `${columnMetadata.type.name.toString().toLowerCase()}(${columnMetadata.length})`;
const name = columnMetadata.type.name.toString().toLowerCase().replace(/\s/g, '-');
const withNullable = nullable === 'nullable' ? name : `*${name}`;
return `${withNullable}(${columnMetadata.length})`;
}

return columnMetadata.type.name.toString().toLowerCase();
const name = columnMetadata.type.name.toString().toLowerCase().replace(/\s/g, '-');
const withNullable = nullable === 'nullable' ? name : `*${name}`;
return withNullable;
}

if (isTrue(includeLength ?? false) && columnMetadata.length !== '') {
return `${columnMetadata.type.toString()}(${columnMetadata.length})`;
const name = columnMetadata.type.toString().replace(/\s/g, '-');
const withNullable = nullable === 'nullable' ? name : `*${name}`;
return `${withNullable}(${columnMetadata.length})`;
}

return columnMetadata.type.toString();
const name = columnMetadata.type.toString().replace(/\s/g, '-');
const withNullable = nullable === 'nullable' ? name : `*${name}`;
return withNullable;
}

0 comments on commit 5cdf167

Please sign in to comment.