Skip to content

Commit

Permalink
refactor: move db logic to nest, create ApiModule
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Mar 27, 2024
1 parent 9850f68 commit 44e6f9b
Show file tree
Hide file tree
Showing 24 changed files with 383 additions and 112 deletions.
19 changes: 19 additions & 0 deletions src/api/api.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Module } from '@nestjs/common';
import { DatabaseModule } from '../database/database.module';
import { PairLiquidityInfoHistoryController } from './pair-liquidity-info-history/pair-liquidity-info-history.controller';
import { PairLiquidityInfoHistoryService } from './pair-liquidity-info-history/pair-liquidity-info-history.service';
import { PairsController } from './pairs/pairs.controller';
import { TokensController } from './tokens/tokens.controller';
import { PairsService } from './pairs/pairs.service';
import { TokensService } from './tokens/tokens.service';

@Module({
imports: [DatabaseModule],
controllers: [
PairLiquidityInfoHistoryController,
PairsController,
TokensController,
],
providers: [PairLiquidityInfoHistoryService, PairsService, TokensService],
})
export class ApiModule {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { PairLiquidityInfoHistoryDbService } from '../../database/pair-liquidity-info-history-db.service';
import { PairLiquidityInfoHistoryDbService } from '../../database/pair-liquidity-info-history/pair-liquidity-info-history-db.service';
import { Pair, PairLiquidityInfoHistory } from '@prisma/client';
import { OrderQueryEnum } from '../../dto';
import { ContractAddress } from '../../lib/utils';
Expand Down
10 changes: 0 additions & 10 deletions src/api/pairs/pairs.module.ts

This file was deleted.

26 changes: 18 additions & 8 deletions src/api/pairs/pairs.service.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
import { Injectable } from '@nestjs/common';
import * as dal from '../../dal';
import { presentInvalidTokens } from '../../lib/utils';
import { PairDbService } from '../../database/pair/pair-db.service';

@Injectable()
export class PairsService {
constructor(private readonly pairDbService: PairDbService) {}
async getAllPairs(onlyListed?: boolean) {
return dal.pair.getAll(presentInvalidTokens, onlyListed);
return this.pairDbService.getAllWithCondition(
presentInvalidTokens,
onlyListed,
);
}

async getAllPairsWithLiquidityInfo(onlyListed?: boolean) {
return dal.pair.getAllWithLiquidityInfo(presentInvalidTokens, onlyListed);
return this.pairDbService.getAllWithLiquidityInfo(
presentInvalidTokens,
onlyListed,
);
}

async getCountStats() {
return {
all: await dal.pair.count(presentInvalidTokens),
synced: await dal.pair.count(presentInvalidTokens, 'synchronized'),
listed: await dal.pair.count(presentInvalidTokens, 'listed'),
all: await this.pairDbService.count(presentInvalidTokens),
synced: await this.pairDbService.count(
presentInvalidTokens,
'synchronized',
),
listed: await this.pairDbService.count(presentInvalidTokens, 'listed'),
};
}

async getPair(address: string) {
return dal.pair.getOne(address);
return this.pairDbService.getOne(address);
}

async getTopHeight() {
return dal.pair.getTopHeight();
return this.pairDbService.getTopHeight();
}
}
10 changes: 0 additions & 10 deletions src/api/tokens/tokens.module.ts

This file was deleted.

19 changes: 10 additions & 9 deletions src/api/tokens/tokens.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { Injectable } from '@nestjs/common';
import * as dal from '../../dal';
import { Pair, Token, PairLiquidityInfo } from '@prisma/client';
import { Pair, PairLiquidityInfo, Token } from '@prisma/client';
import { ContractAddress, presentInvalidTokens } from '../../lib/utils';
import { TokenDbService } from '../../database/token/token-db.service';

@Injectable()
export class TokensService {
constructor(private readonly tokenDbService: TokenDbService) {}
async getCount(onlyListed?: boolean) {
return dal.token.count(presentInvalidTokens, onlyListed);
return this.tokenDbService.count(presentInvalidTokens, onlyListed);
}
async getAllTokens(): Promise<Token[]> {
return dal.token.getAll(presentInvalidTokens);
return this.tokenDbService.getAll(presentInvalidTokens);
}
async getListedTokens(): Promise<Token[]> {
return dal.token.getListed();
return this.tokenDbService.getListed();
}

async listToken(address: ContractAddress) {
return dal.token.updateListedValue(address, true);
return this.tokenDbService.updateListedValue(address, true);
}

async unlistToken(address: ContractAddress) {
return dal.token.updateListedValue(address, false);
return this.tokenDbService.updateListedValue(address, false);
}

async getToken(address: ContractAddress): Promise<
Expand All @@ -30,7 +31,7 @@ export class TokensService {
})
| null
> {
return dal.token.getByAddressWithPairs(address);
return this.tokenDbService.getByAddressWithPairs(address);
}

async getTokenWithPairsInfo(address: ContractAddress): Promise<
Expand All @@ -46,6 +47,6 @@ export class TokensService {
})
| null
> {
return dal.token.getByAddressWithPairsAndLiquidity(address);
return this.tokenDbService.getByAddressWithPairsAndLiquidity(address);
}
}
14 changes: 2 additions & 12 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import { Module } from '@nestjs/common';
import { PairsModule } from './api/pairs/pairs.module';
import { TokensModule } from './api/tokens/tokens.module';
import { AppController } from './app.controller';
import { DatabaseModule } from './database/database.module';
import { TasksModule } from './tasks/tasks.module';
import { TokensService } from './api/tokens/tokens.service';
import { PairsService } from './api/pairs/pairs.service';
import { ClientsModule } from './clients/clients.module';
import { PairLiquidityInfoHistoryModule } from './api/pair-liquidity-info-history/pair-liquidity-info-history.module';
import { ApiModule } from './api/api.module';

@Module({
imports: [
PairsModule,
TokensModule,
ClientsModule,
DatabaseModule,
TasksModule,
PairLiquidityInfoHistoryModule,
],
imports: [ApiModule, ClientsModule, DatabaseModule],
controllers: [AppController],
providers: [TokensService, PairsService],
})
Expand Down
9 changes: 6 additions & 3 deletions src/database/database.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
import { PairDbService } from './pair-db.service';
import { PairLiquidityInfoHistoryDbService } from './pair-liquidity-info-history-db.service';
import { PairLiquidityInfoHistoryErrorDbService } from './pair-liquidity-info-history-error-db.service';
import { PairDbService } from './pair/pair-db.service';
import { PairLiquidityInfoHistoryDbService } from './pair-liquidity-info-history/pair-liquidity-info-history-db.service';
import { PairLiquidityInfoHistoryErrorDbService } from './pair-liquidity-info-history-error/pair-liquidity-info-history-error-db.service';
import { TokenDbService } from './token/token-db.service';

@Module({
providers: [
PrismaService,
PairDbService,
PairLiquidityInfoHistoryDbService,
PairLiquidityInfoHistoryErrorDbService,
TokenDbService,
],
exports: [
PairDbService,
PairLiquidityInfoHistoryDbService,
PairLiquidityInfoHistoryErrorDbService,
TokenDbService,
],
})
export class DatabaseModule {}
19 changes: 0 additions & 19 deletions src/database/pair-db.service.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from './prisma.service';
import { PrismaService } from '../prisma.service';
import { Pair, PairLiquidityInfoHistoryError, Token } from '@prisma/client';
import { PairLiquidityInfoHistoryErrorDbService } from './pair-liquidity-info-history-error-db.service';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from './prisma.service';
import { PrismaService } from '../prisma.service';
import { PairLiquidityInfoHistoryError } from '@prisma/client';

@Injectable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PairLiquidityInfoHistoryDbService } from './pair-liquidity-info-history-db.service';
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from './prisma.service';
import { PrismaService } from '../prisma.service';
import { Pair, PairLiquidityInfoHistory, Token } from '@prisma/client';
import { OrderQueryEnum } from '../dto';
import { ContractAddress } from '../lib/utils';
import { OrderQueryEnum } from '../../dto';
import { ContractAddress } from '../../lib/utils';

const token1: Token = {
id: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from './prisma.service';
import { PrismaService } from '../prisma.service';
import { PairLiquidityInfoHistory } from '@prisma/client';
import { OrderQueryEnum } from '../dto';
import { ContractAddress } from '../lib/utils';
import { OrderQueryEnum } from '../../dto';
import { ContractAddress } from '../../lib/utils';

@Injectable()
export class PairLiquidityInfoHistoryDbService {
Expand Down
Loading

0 comments on commit 44e6f9b

Please sign in to comment.