Skip to content

Commit

Permalink
chore: improve code and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrxyy committed Dec 27, 2023
1 parent b55e87e commit 33769b2
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 79 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:debug": "sh node_modules/.bin/jest --runInBand --config ./test/jest-e2e.json --watch --debug",
"test:e2e": "jest --config ./test/jest-e2e.json --watch --debug"
},
"dependencies": {
"@nestjs/common": "^9.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/models/Querys/index.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import knex from 'knex';
import { generateDbdiagramDsl } from 'src/utils/knex/DB2DBML';
import { get, pick } from 'lodash';
import { Knex } from 'knex';
import exportSQL from 'src/utils/knex/export-sql';
import { executeSQLWithDisabledForeignKeys } from 'src/utils/knex/executeSQLWithDisabledForeignKeys';
import exportDsl from 'src/utils/knex/export-dsl';

function pureCode(raw: string): string {
const codeRegex = /```.*\n([\s\S]*?)\n```/;
Expand Down Expand Up @@ -175,11 +175,11 @@ export class QueriesService {
tableDict: {},
linkDict: {},
});
const ddl = exportSQL(
const ddl = exportDsl(
tableDict,
linkDict,
// get(dbConfig, 'config.newDbType'),
);
).split(/\n\s*\n/);
const { client, host, port, user, password, database, newDbName }: any =
dbConfig.config;
if (ddl && get(dbConfig.config, 'newDbName')) {
Expand Down
5 changes: 2 additions & 3 deletions src/models/Schema/index.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { InjectModel } from '@nestjs/sequelize';
import { executeRes } from 'src/utils/response/sequeilze';
import { SchemaLog } from './SchemaLog.model';
import { get, omit } from 'lodash';
import { async } from 'rxjs';
import export_dbml from 'src/utils/knex/export-dbml';
import { GET_SCHEMA_INFO } from 'src/utils/prompts/schema';
import exportDsl from 'src/utils/knex/export-dsl';

@Injectable()
export class SchemaService {
Expand Down Expand Up @@ -59,7 +58,7 @@ export class SchemaService {
linkDict: {},
};

const dbml = export_dbml(tableDict, linkDict);
const dbml = exportDsl(tableDict, linkDict, 'dbml');

const description = await GET_SCHEMA_INFO.run(dbml);

Expand Down
15 changes: 10 additions & 5 deletions src/utils/knex/export-dbml.ts → src/utils/knex/export-dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { readFileSync, writeFileSync } from 'fs';
* @param [databaseType=postgres] - The type of database you want to export to.
* @returns dbml string.
*/
const export_dbml = (
const exportDsl = (
tableDict: any,
linkDict: any,
databaseType: 'postgres' | 'mysql' | 'dbml' | 'mssql' | 'json' = 'mysql',
Expand All @@ -17,11 +17,16 @@ const export_dbml = (
note: '',
tables: Object.values(tableDict || {}).map((table: any) => {
return {
...table,
name: table.name,
note: table.note,
fields: table.fields.map((field) => {
return {
...field,
dbdefault: {
type: field.dbdefaultType,
value: field.dbdefault,
},
type: {
// To lower case because of typing 'BIGINT' with upper case and increment get wrong pg sql type when export
type_name: field.type.toLowerCase(),
Expand All @@ -39,9 +44,9 @@ const export_dbml = (
endpoints: ref.endpoints.map((endpoint) => {
return {
...endpoint,
tableName: tableDict[endpoint.id].name,
tableName: tableDict[endpoint.table].name,
fieldNames: [
tableDict[endpoint.id].fields.find(
tableDict[endpoint.table].fields.find(
(field) => field.id == endpoint.fieldId,
).name,
],
Expand All @@ -52,8 +57,8 @@ const export_dbml = (
};

const database = new Parser(undefined).parse(combined as any, 'json');
const dbml = ModelExporter.export(database, 'dbml', false);
const dbml = ModelExporter.export(database, databaseType, false);
return dbml;
};

export default export_dbml;
export default exportDsl;
62 changes: 0 additions & 62 deletions src/utils/knex/export-sql.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/app.e2e-spec.ts → test/app.e2e-spec-demo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { AppModule } from '../src/app.module';

describe('AppController (e2e)', () => {
let app: INestApplication;
Expand Down
Loading

0 comments on commit 33769b2

Please sign in to comment.