From 9b7b9e05cc030b61687c47a89237dd65ce6efb01 Mon Sep 17 00:00:00 2001 From: zarsa Date: Sun, 4 Jun 2023 14:20:08 +0430 Subject: [PATCH] add datasource and some settings --- db/data-source.ts | 19 ++++++++++ db/migrations/1685871985306-refactoring.ts | 43 ++++++++++++++++++++++ package.json | 8 +++- src/app.module.ts | 15 +------- src/entities/point-type.entity.ts | 2 +- 5 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 db/data-source.ts create mode 100644 db/migrations/1685871985306-refactoring.ts diff --git a/db/data-source.ts b/db/data-source.ts new file mode 100644 index 0000000..8583d83 --- /dev/null +++ b/db/data-source.ts @@ -0,0 +1,19 @@ +import { DataSource, DataSourceOptions } from 'typeorm'; + +export const dataSourceOptions: DataSourceOptions = { + type: 'mssql', + host: '193.176.240.223\\sql2017', + port: 1433, + username: 'sa', + password: 'Zar@Sql2017#1397', + database: 'TirebanClub', + entities: ['dist/**/*.entity{.ts,.js}'], + synchronize: true, + options: { + trustServerCertificate: true, + }, + migrations: ['dist/db/migrations/*.js'], + requestTimeout: 300000, +}; +const dataSource = new DataSource(dataSourceOptions); +export default dataSource; diff --git a/db/migrations/1685871985306-refactoring.ts b/db/migrations/1685871985306-refactoring.ts new file mode 100644 index 0000000..3f87f84 --- /dev/null +++ b/db/migrations/1685871985306-refactoring.ts @@ -0,0 +1,43 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class Refactoring1685871985306 implements MigrationInterface { + name = 'Refactoring1685871985306'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE "group" ("id" int NOT NULL IDENTITY(1,1), "name" nvarchar(255) NOT NULL, CONSTRAINT "PK_256aa0fda9b1de1a73ee0b7106b" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "person_groups_group" ("personId" int NOT NULL, "groupId" int NOT NULL, CONSTRAINT "PK_2ee7c7f82940eb58121d88a0176" PRIMARY KEY ("personId", "groupId"))`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_36e53832ea1beefb3de6cff976" ON "person_groups_group" ("personId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_267fc87e69a07e823de020a27e" ON "person_groups_group" ("groupId") `, + ); + await queryRunner.query( + `ALTER TABLE "person_groups_group" ADD CONSTRAINT "FK_36e53832ea1beefb3de6cff976b" FOREIGN KEY ("personId") REFERENCES "person"("id") ON DELETE CASCADE ON UPDATE CASCADE`, + ); + await queryRunner.query( + `ALTER TABLE "person_groups_group" ADD CONSTRAINT "FK_267fc87e69a07e823de020a27e7" FOREIGN KEY ("groupId") REFERENCES "group"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "person_groups_group" DROP CONSTRAINT "FK_267fc87e69a07e823de020a27e7"`, + ); + await queryRunner.query( + `ALTER TABLE "person_groups_group" DROP CONSTRAINT "FK_36e53832ea1beefb3de6cff976b"`, + ); + await queryRunner.query( + `DROP INDEX "IDX_267fc87e69a07e823de020a27e" ON "person_groups_group"`, + ); + await queryRunner.query( + `DROP INDEX "IDX_36e53832ea1beefb3de6cff976" ON "person_groups_group"`, + ); + await queryRunner.query(`DROP TABLE "person_groups_group"`); + await queryRunner.query(`DROP TABLE "group"`); + } +} diff --git a/package.json b/package.json index 75896d1..e6a2256 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,13 @@ "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:e2e": "jest --config ./test/jest-e2e.json", + "typeorm": "npm run build && npx typeorm -d dist/db/data-source.js", + "m:g": "npm run build && npx typeorm migration:generate ./db/migrations/refactoring -d dist/db/data-source.js", + "m:r": "npm run typeorm -- migration:run", +"migration:revert": "npm run typeorm -- migration:revert" + + }, "dependencies": { "@nestjs/common": "^9.0.0", diff --git a/src/app.module.ts b/src/app.module.ts index 1aa0249..cb651cd 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -5,22 +5,11 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { UsersModule } from './users/users.module'; import { PersonsModule } from './persons/persons.module'; import { GroupsModule } from './groups/groups.module'; +import { dataSourceOptions } from 'db/data-source'; @Module({ imports: [ - TypeOrmModule.forRoot({ - type: 'mssql', - host: '193.176.240.223\\sql2017', - port: 1433, - username: 'sa', - password: 'Zar@Sql2017#1397', - database: 'TirebanClub', - entities: ['dist/**/*.entity{.ts,.js}'], - synchronize: true, - options: { - trustServerCertificate: true, - }, - }), + TypeOrmModule.forRoot(dataSourceOptions), UsersModule, PersonsModule, GroupsModule, diff --git a/src/entities/point-type.entity.ts b/src/entities/point-type.entity.ts index 4539861..53b2daa 100644 --- a/src/entities/point-type.entity.ts +++ b/src/entities/point-type.entity.ts @@ -4,6 +4,6 @@ import { Column, Entity, PrimaryColumn } from 'typeorm'; export class PointType { @PrimaryColumn() code: number; - @Column({ length: 25, type: 'nvarchar' }) + @Column({ length: 50, type: 'nvarchar' }) name: string; }