Skip to content

Commit

Permalink
[api] fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
woojiahao committed Mar 11, 2024
1 parent ee117d1 commit a90a64d
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 51 deletions.
1 change: 1 addition & 0 deletions api/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ module.exports = {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-empty-function': 'off',
},
};
10 changes: 5 additions & 5 deletions api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DatabaseModule } from './database/database.module';
import { ConfigModule } from "@nestjs/config";
import { ConfigModule } from '@nestjs/config';
import { UsersController } from './users/users.controller';
import { UsersModule } from './users/users.module';
import * as Joi from 'joi';
Expand All @@ -12,13 +12,13 @@ import * as Joi from 'joi';
ConfigModule.forRoot({
isGlobal: true,
validationSchema: Joi.object({
DATABASE_URL: Joi.string()
})
DATABASE_URL: Joi.string(),
}),
}),
DatabaseModule,
UsersModule
UsersModule,
],
controllers: [AppController, UsersController],
providers: [AppService],
})
export class AppModule { }
export class AppModule {}
4 changes: 2 additions & 2 deletions api/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const envVars = {
'db_url': 'DATABASE_URL'
}
db_url: 'DATABASE_URL',
};
6 changes: 3 additions & 3 deletions api/src/database/database.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';
import { databaseProviders } from './database.provider';

@Module({
providers: [...databaseProviders],
exports: [...databaseProviders]
providers: [...databaseProviders],
exports: [...databaseProviders],
})
export class DatabaseModule { }
export class DatabaseModule {}
30 changes: 15 additions & 15 deletions api/src/database/database.provider.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Provider } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Client } from "pg";
import { envVars } from "src/constants";
import { Provider } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Client } from 'pg';
import { envVars } from 'src/constants';

export const databaseProviders: Provider[] = [
{
provide: 'DATABASE_CONNECTION',
inject: [ConfigService],
useFactory: async (config: ConfigService): Promise<Client> => {
const databaseUrl = config.getOrThrow<string>(envVars.db_url);
const client = new Client({ connectionString: databaseUrl });
await client.connect();
return client;
}
}
]
{
provide: 'DATABASE_CONNECTION',
inject: [ConfigService],
useFactory: async (config: ConfigService): Promise<Client> => {
const databaseUrl = config.getOrThrow<string>(envVars.db_url);
const client = new Client({ connectionString: databaseUrl });
await client.connect();
return client;
},
},
];
4 changes: 1 addition & 3 deletions api/src/users/dtos/userVM.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export class UserVM {

}
export class UserVM {}
24 changes: 14 additions & 10 deletions api/src/users/users.entity.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@

export class User {
constructor(id: string, email: string, password_hash: string | null, username: string) { }
constructor(
id: string,
email: string,
password_hash: string | null,
username: string,
) {}

static fromRows(rows: any[]): User[] {
const users: User[] = [];
for (const [id, email, password_hash, username] of rows) {
const user = new User(id, email, password_hash && null, username);
users.push(user);
}
return users;
static fromRows(rows: any[]): User[] {
const users: User[] = [];
for (const [id, email, password_hash, username] of rows) {
const user = new User(id, email, password_hash && null, username);
users.push(user);
}
}
return users;
}
}
8 changes: 4 additions & 4 deletions api/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { UsersRepository } from './users.repository';
import { DatabaseModule } from 'src/database/database.module';

@Module({
controllers: [UsersController],
providers: [UsersService, UsersRepository],
imports: [DatabaseModule]
controllers: [UsersController],
providers: [UsersService, UsersRepository],
imports: [DatabaseModule],
})
export class UsersModule { }
export class UsersModule {}
18 changes: 9 additions & 9 deletions api/src/users/users.repository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Inject, Injectable } from "@nestjs/common";
import { Client } from "pg";
import { User } from "./users.entity";
import { Inject, Injectable } from '@nestjs/common';
import { Client } from 'pg';
import { User } from './users.entity';

@Injectable()
export class UsersRepository {
constructor(@Inject("DATABASE_CONNECTION") private client: Client) { }
constructor(@Inject('DATABASE_CONNECTION') private client: Client) {}

async getUsers(): Promise<User[]> {
const result = await this.client.query("select * from users;");
return User.fromRows(result.rows);
}
}
async getUsers(): Promise<User[]> {
const result = await this.client.query('select * from users;');
return User.fromRows(result.rows);
}
}

0 comments on commit a90a64d

Please sign in to comment.