Skip to content

Commit

Permalink
update database schema type to bigint (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
danishjoseph authored Apr 26, 2024
1 parent dfe1c02 commit b65e376
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 39 deletions.
5 changes: 2 additions & 3 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
DB_HOST=""
DB_PORT=
DB_PORT=""
DB_USERNAME=""
DB_PASSWORD=""
DB_NAME=""
SYNCHRONIZE=
DB_NAME=""
24 changes: 24 additions & 0 deletions database/data-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DataSource, DataSourceOptions } from 'typeorm';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { config } from 'dotenv';

config({ path: '.env' });

const dataSourceOptions: DataSourceOptions = {
type: 'postgres',
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: ['**/*.entity.js'],
migrations: ['dist/database/migrations/*.js'],
synchronize: false,
migrationsRun: true,
};

export const typeOrmModuleOptions: TypeOrmModuleOptions = {
...dataSourceOptions,
};

export default new DataSource(dataSourceOptions);
115 changes: 115 additions & 0 deletions database/migrations/1714097701336-InitialSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class InitialSetup1714097701336 implements MigrationInterface {
name = 'InitialSetup1714097701336';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE "assets" (
"id" SERIAL NOT NULL,
"isin" character varying(50) NOT NULL,
"symbol" character varying(50),
"name" character varying(255) NOT NULL,
"assetExchangeCode" character varying(50),
"faceValue" double precision NOT NULL,
"industry" character varying(50),
"sector" character varying(50),
CONSTRAINT "UQ_adc14321968e73ca3b26444f36c" UNIQUE ("isin"),
CONSTRAINT "PK_da96729a8b113377cfb6a62439c" PRIMARY KEY ("id")
)
`);
await queryRunner.query(`
CREATE TABLE "exchange" (
"id" SERIAL NOT NULL,
"name" character varying NOT NULL,
"abbreviation" character varying NOT NULL,
CONSTRAINT "UQ_38d9819cca963bfebe00e5302f8" UNIQUE ("name", "abbreviation"),
CONSTRAINT "PK_cbd4568fcb476b57cebd8239895" PRIMARY KEY ("id")
)
`);
await queryRunner.query(`
CREATE TABLE "trading_data" (
"id" SERIAL NOT NULL,
"date" date NOT NULL,
"open" double precision NOT NULL,
"high" double precision NOT NULL,
"low" double precision NOT NULL,
"close" double precision NOT NULL,
"lastPrice" double precision NOT NULL,
"previousClose" double precision NOT NULL,
"volume" bigint NOT NULL,
"turnover" double precision NOT NULL,
"totalTrades" integer,
"assetExchangeId" integer,
CONSTRAINT "UQ_d09147975848a5d6acbd65d3a0e" UNIQUE ("date", "assetExchangeId"),
CONSTRAINT "PK_e2dc47f2c072c491710bf997109" PRIMARY KEY ("id")
)
`);
await queryRunner.query(`
CREATE TABLE "asset_exchange" (
"id" SERIAL NOT NULL,
"assetId" integer,
"exchangeId" integer,
CONSTRAINT "UQ_35a085d033767e92d957b3c92c3" UNIQUE ("assetId", "exchangeId"),
CONSTRAINT "PK_7b1798c8ec6843971061a3208ad" PRIMARY KEY ("id")
)
`);
await queryRunner.query(`
CREATE TABLE "delivery_data" (
"id" SERIAL NOT NULL,
"date" date NOT NULL,
"deliveryQuantity" integer,
"deliveryPercentage" integer,
"assetExchangeId" integer,
CONSTRAINT "UQ_85107d5b1a4952f5ab2d96709ba" UNIQUE ("date", "assetExchangeId"),
CONSTRAINT "PK_9a17a5fbddaca467af6b59c14aa" PRIMARY KEY ("id")
)
`);
await queryRunner.query(`
ALTER TABLE "trading_data"
ADD CONSTRAINT "FK_d7d64e2e052e4312dd41551f036" FOREIGN KEY ("assetExchangeId") REFERENCES "asset_exchange"("id") ON DELETE CASCADE ON UPDATE NO ACTION
`);
await queryRunner.query(`
ALTER TABLE "asset_exchange"
ADD CONSTRAINT "FK_9f520b83d4c501d2fa34b30c3ed" FOREIGN KEY ("assetId") REFERENCES "assets"("id") ON DELETE NO ACTION ON UPDATE NO ACTION
`);
await queryRunner.query(`
ALTER TABLE "asset_exchange"
ADD CONSTRAINT "FK_f384144c492cc368c4621e2d85a" FOREIGN KEY ("exchangeId") REFERENCES "exchange"("id") ON DELETE NO ACTION ON UPDATE NO ACTION
`);
await queryRunner.query(`
ALTER TABLE "delivery_data"
ADD CONSTRAINT "FK_df64a5eb621b3849fdc07770931" FOREIGN KEY ("assetExchangeId") REFERENCES "asset_exchange"("id") ON DELETE CASCADE ON UPDATE NO ACTION
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "delivery_data" DROP CONSTRAINT "FK_df64a5eb621b3849fdc07770931"
`);
await queryRunner.query(`
ALTER TABLE "asset_exchange" DROP CONSTRAINT "FK_f384144c492cc368c4621e2d85a"
`);
await queryRunner.query(`
ALTER TABLE "asset_exchange" DROP CONSTRAINT "FK_9f520b83d4c501d2fa34b30c3ed"
`);
await queryRunner.query(`
ALTER TABLE "trading_data" DROP CONSTRAINT "FK_d7d64e2e052e4312dd41551f036"
`);
await queryRunner.query(`
DROP TABLE "delivery_data"
`);
await queryRunner.query(`
DROP TABLE "asset_exchange"
`);
await queryRunner.query(`
DROP TABLE "trading_data"
`);
await queryRunner.query(`
DROP TABLE "exchange"
`);
await queryRunner.query(`
DROP TABLE "assets"
`);
}
}
25 changes: 25 additions & 0 deletions database/migrations/1714099179945-UpdateToBigInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class UpdateToBigInt1714099179945 implements MigrationInterface {
name = 'UpdateToBigInt1714099179945';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "delivery_data" DROP COLUMN "deliveryQuantity"
`);
await queryRunner.query(`
ALTER TABLE "delivery_data"
ADD "deliveryQuantity" bigint
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "delivery_data" DROP COLUMN "deliveryQuantity"
`);
await queryRunner.query(`
ALTER TABLE "delivery_data"
ADD "deliveryQuantity" integer
`);
}
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
"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": "yarn build && npx typeorm -d dist/database/data-source.js",
"migration:generate": "yarn typeorm migration:generate",
"migration:run": "yarn typeorm migration:run",
"migration:revert": "yarn typeorm migration:revert"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand Down
7 changes: 5 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { AssetManagementModule } from './asset-management/asset-management.module';
import { DatabaseModule } from './database.module';
import { DataSyncModule } from './data-sync/data-sync.module';
import { ScheduleModule } from '@nestjs/schedule';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeOrmModuleOptions } from 'database/data-source';

@Module({
imports: [
Expand All @@ -14,7 +15,9 @@ import { ScheduleModule } from '@nestjs/schedule';
isGlobal: true,
envFilePath: ['.env'],
}),
DatabaseModule,
TypeOrmModule.forRootAsync({
useFactory: () => typeOrmModuleOptions,
}),
AssetManagementModule,
DataSyncModule,
],
Expand Down
4 changes: 2 additions & 2 deletions src/asset-management/entities/delivery-data.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export class DeliveryData {
@Column({ type: 'date' })
date: Date;

@Column({ type: 'int', nullable: true })
deliveryQuantity: number;
@Column({ type: 'bigint', nullable: true })
deliveryQuantity: bigint;

@Column({ type: 'int', nullable: true })
deliveryPercentage: number;
Expand Down
2 changes: 1 addition & 1 deletion src/asset-management/entities/trading-data.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class TradingData {
previousClose: number;

@Column({ type: 'bigint', nullable: false })
volume: number;
volume: bigint;

@Column({ type: 'float', nullable: false })
turnover: number;
Expand Down
9 changes: 6 additions & 3 deletions src/asset-management/services/delivery-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ export class DeliveryDataService {

async saveDeliveryData(deliveryData: DeliveryDataDTO): Promise<InsertResult> {
await validateAndThrowError(deliveryData, 'deliveryDataDTO');
return this.deliveryDataRepository.upsert(deliveryData as DeliveryData, {
conflictPaths: { date: true, assetExchange: true },
});
return this.deliveryDataRepository.upsert(
deliveryData as unknown as DeliveryData,
{
conflictPaths: { date: true, assetExchange: true },
},
);
}
}
9 changes: 6 additions & 3 deletions src/asset-management/services/trading-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export class TradingDataService {

async saveTradingData(tradingData: TradingDataDTO): Promise<InsertResult> {
await validateAndThrowError(tradingData, 'TradingDataDTO');
return this.tradingDataRepository.upsert(tradingData as TradingData, {
conflictPaths: { date: true, assetExchange: true },
});
return this.tradingDataRepository.upsert(
tradingData as unknown as TradingData,
{
conflictPaths: { date: true, assetExchange: true },
},
);
}
}
24 changes: 0 additions & 24 deletions src/database.module.ts

This file was deleted.

0 comments on commit b65e376

Please sign in to comment.