Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
build
build
.env
2 changes: 2 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DB_CONNECTION_STRING=
PORT=
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "ISC",
"description": "",
"dependencies": {
"dotenv": "^16.4.5",
"fastify": "^5.0.0",
"pg": "^8.13.1",
"pg-format": "^1.0.4"
Expand Down
2 changes: 1 addition & 1 deletion requests.http
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@host = http://localhost:4400
@host = http://localhost:4440

###

Expand Down
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export const CONFIG = {
dbConnectionString: process.env.DB_CONNECTION_STRING!,
port: Number(process.env.PORT!)
} as const
6 changes: 3 additions & 3 deletions src/controller/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export default function createApp(options = {}, dependencies: Dependencies) {

const app = fastify(options)

app.get('/api/pets', async () => {
app.get('/api/pets', async (request, reply) => {
const pets = await petService.getAll();
return pets;
return {pets, message: 'success'};
})

type PostPetsRoute = {
Body: PetToCreate;
Body: PetToCreate
}
app.post<PostPetsRoute>('/api/pets', async (request, reply) => {
const { body: petToCreate } = request;
Expand Down
15 changes: 9 additions & 6 deletions src/db.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import {Pool} from 'pg';
import { Pool } from 'pg';

export type DbClient = {
query: <RowType>(query: string, params?: any[]) => Promise<RowType[] | RowType>;
}

export function createPgClient(): DbClient {
const pool = new Pool({
connectionString: 'postgres://postgres:dbpassword@localhost:5400/pets'
});
type PgClientOptions = {
connectionString: string
}
export function createPgClient({ connectionString }:PgClientOptions): DbClient {
console.log('DB_CONNECTION_STRING', connectionString);


const pool = new Pool({ connectionString });
return {
async query(sql: string, params?: any[]) {
const result = await pool.query(sql, params);
return result.rows;
}

}
}
6 changes: 4 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import createApp from "./controller/app";
import { createPgClient } from "./db";
import 'dotenv/config';
import { CONFIG } from './config';

const PORT = 4400;
const PORT = CONFIG.port;

const dbClient = createPgClient();
const dbClient = createPgClient({ connectionString: CONFIG.dbConnectionString });

const options = {
logger: {
Expand Down
2 changes: 1 addition & 1 deletion tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Right now the user can provide any kind of body for the POST request. Ensure tha

## Task 2: Determines the reply's body

Add a desired output schema (deserialization) to the `GET /api/pets` and `POST /api/pets`.
Add a desired output schema (serialization) to the `GET /api/pets` and `POST /api/pets`.

- What do you think what is the advantage of this?

Expand Down