Drill 20: Improve it — Databases with Postgres
Add one improvement and explain the benefit.
What you will be able to do
- Add one improvement and explain the benefit.
Practice task
Add one improvement and explain the benefit.
import { Pool } from 'pg'
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
max: 10,
})
type Course = {
id: string
title: string
minutes: number
}
async function findCourse(id: string): Promise<Course | null> {
const result = await pool.query<Course>(
'select id, title, minutes from courses where id = $1',
[id],
)
return result.rows[0] ?? null
}
async function createCourse(course: Course): Promise<Course> {
const client = await pool.connect()
try {
await client.query('begin')
await client.query(
'insert into courses (id, title, minutes) values ($1, $2, $3)',
[course.id, course.title, course.minutes],
)
await client.query('commit')
return course
} catch (error) {
await client.query('rollback')
throw error
} finally {
client.release()
}
}Your turn
EasyWrite your answer
Add one improvement and explain the benefit.
TEXT · starter
Your answer…Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.