Node.js Database Integration 4 — Questions and Answers
Question 1: What does `client.release()` do in the `pg` pool API?
- Closes the database connection permanently
- Returns the client back to the pool for reuse (Correct answer)
- Commits any open transaction
- Clears the query result cache
Correct answer: Returns the client back to the pool for reuse
Calling `release()` signals that the client is done and can be reused by the pool for future queries.
Question 2: In Prisma, how do you run a raw SQL query?
- prisma.$rawQuery(sql)
- prisma.$queryRaw`SELECT ...` (Correct answer)
- prisma.raw(sql)
- prisma.execute(sql)
Correct answer: prisma.$queryRaw`SELECT ...`
`prisma.$queryRaw` uses a tagged template literal to safely execute raw SQL and return typed results.
Question 3: Which Sequelize method fetches a single record by its primary key?
- Model.findOne({ where: { id } })
- Model.findByPk(id) (Correct answer)
- Model.get(id)
- Model.fetch(id)
Correct answer: Model.findByPk(id)
`findByPk` is a shortcut that queries by primary key, equivalent to `findOne({ where: { id } })` but more explicit.
Question 4: What is the default behavior of Mongoose's `find()` when no documents match the query?
- Throws a DocumentNotFoundError
- Resolves with null
- Resolves with an empty array [] (Correct answer)
- Rejects the promise
Correct answer: Resolves with an empty array []
`find()` always resolves with an array; an empty array `[]` means no matches were found.
Question 5: In a SQLite database accessed via `better-sqlite3` in Node.js, what makes it different from most other Node.js DB drivers?
- It only supports async/await
- It is synchronous — queries block the event loop (Correct answer)
- It requires a separate server process
- It only works with WAL mode disabled
Correct answer: It is synchronous — queries block the event loop
`better-sqlite3` exposes a synchronous API intentionally, unlike async drivers, which simplifies code but blocks the event loop.
Question 6: Which MongoDB operator finds documents where an array field contains all of the specified values?
- $in
- $all (Correct answer)
- $elemMatch
- $contains
Correct answer: $all
`$all` matches documents where the array field contains every element in the provided array, regardless of order.
Question 7: In Knex.js, what is the correct way to start building a SELECT query for the `users` table?
- knex.select('users')
- knex('users').select('*') (Correct answer)
- knex.from('users').get()
- knex.table('users').fetch()
Correct answer: knex('users').select('*')
Knex uses a chainable builder syntax where you pass the table name to `knex()` and chain `.select()`, `.where()`, etc.
What does `client.release()` do in the `pg` pool API?