-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
54 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
export class UserVM { | ||
|
||
} | ||
export class UserVM {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |