diff --git a/.gitignore b/.gitignore index 227bcb9..e5736f1 100644 --- a/.gitignore +++ b/.gitignore @@ -117,3 +117,4 @@ template2/ erdiadb.json **/.DS_Store .npmrc +git-short.sh diff --git a/examples/schema-type/License.ts b/examples/schema-type/License.ts index 4da5432..c8c0f06 100644 --- a/examples/schema-type/License.ts +++ b/examples/schema-type/License.ts @@ -7,6 +7,7 @@ export interface ILicense { title: string; code: string; description: string; + weight: number; expire: Date; } @@ -30,6 +31,10 @@ export const License = new EntitySchema({ comment: 'organization title', charset: 'utf8mb4', }, + weight: { + type: 'double precision', + comment: 'sort weight', + }, code: { type: 'varchar', length: 200, diff --git a/examples/schema-type/Organization.ts b/examples/schema-type/Organization.ts index cfea301..c254d88 100644 --- a/examples/schema-type/Organization.ts +++ b/examples/schema-type/Organization.ts @@ -5,6 +5,7 @@ export interface IOrganization { id: number; title: string; description: string; + supports: { name: string; year: number }[]; expire: Date; } @@ -33,6 +34,10 @@ export const Organization = new EntitySchema 'CURRENT_TIMESTAMP', diff --git a/examples/schema-type/factory.ts b/examples/schema-type/factory.ts index 9dcf59c..0fcdbe6 100644 --- a/examples/schema-type/factory.ts +++ b/examples/schema-type/factory.ts @@ -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(), }; @@ -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(), }; diff --git a/package.json b/package.json index 7da9c4b..d7a6d98 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" + ] } } diff --git a/src/template/html/mermaid-script.ts b/src/template/html/mermaid-script.ts index a0db770..366eaa5 100644 --- a/src/template/html/mermaid-script.ts +++ b/src/template/html/mermaid-script.ts @@ -1,4 +1,4 @@ -const mermaidScript = ` +const mermaidScript = ` `; export default mermaidScript; diff --git a/src/typeorm/columns/__tests__/column.tools.test.ts b/src/typeorm/columns/__tests__/column.tools.test.ts index d69d169..14bb19f 100644 --- a/src/typeorm/columns/__tests__/column.tools.test.ts +++ b/src/typeorm/columns/__tests__/column.tools.test.ts @@ -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'); }); }); diff --git a/src/typeorm/columns/__tests__/expects/expect.getColumnRecord-pass.json b/src/typeorm/columns/__tests__/expects/expect.getColumnRecord-pass.json index 383004c..17b63fe 100644 --- a/src/typeorm/columns/__tests__/expects/expect.getColumnRecord-pass.json +++ b/src/typeorm/columns/__tests__/expects/expect.getColumnRecord-pass.json @@ -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
line2
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" diff --git a/src/typeorm/columns/getColumnType.ts b/src/typeorm/columns/getColumnType.ts index 7ea7f66..a110b2a 100644 --- a/src/typeorm/columns/getColumnType.ts +++ b/src/typeorm/columns/getColumnType.ts @@ -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: Pick, 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; }