Node.js Database Integration 2 — Questions and Answers
Question 1: Which method in the `pg` (node-postgres) library is used to execute a parameterized query safely?
- client.query('SELECT * WHERE id = ' + id)
- client.query('SELECT * WHERE id = $1', [id]) (Correct answer)
- client.execute({id: id})
- client.run('SELECT * WHERE id = ?', id)
Correct answer: client.query('SELECT * WHERE id = $1', [id])
Parameterized queries use `$1, $2` placeholders with a values array to prevent SQL injection.
Question 2: In Mongoose, what does calling `.lean()` on a query do?
- Enables strict schema validation
- Returns plain JavaScript objects instead of Mongoose documents (Correct answer)
- Automatically populates all references
- Converts all fields to strings
Correct answer: Returns plain JavaScript objects instead of Mongoose documents
`.lean()` skips Mongoose document hydration, returning raw POJOs which are faster and lighter.
Question 3: What is the purpose of connection pooling in Node.js database drivers?
- To encrypt database connections
- To reuse existing connections instead of opening a new one per query (Correct answer)
- To load-balance queries across multiple databases
- To cache query results in memory
Correct answer: To reuse existing connections instead of opening a new one per query
Connection pooling maintains a set of open connections that can be reused, reducing the overhead of establishing new connections.
Question 4: Which Sequelize method is used to define an association where one User has many Posts?
- User.belongsTo(Post)
- User.hasMany(Post) (Correct answer)
- User.associate(Post)
- User.references(Post)
Correct answer: User.hasMany(Post)
`User.hasMany(Post)` creates a one-to-many association, adding a foreign key to the Posts table.
Question 5: In Redis with the `ioredis` client, which command stores a value with an expiration time of 60 seconds?
- client.set('key', 'value', 'EX', 60) (Correct answer)
- client.set('key', 'value', 'TTL', 60)
- client.setex('key', 'value', 60)
- client.put('key', 'value', {expire: 60})
Correct answer: client.set('key', 'value', 'EX', 60)
`SET key value EX 60` sets a key with a 60-second expiry; `setex` also works but takes args as `(key, seconds, value)`.
Question 6: What does the `{ upsert: true }` option do in a MongoDB `updateOne` call?
- Updates all matching documents
- Inserts a new document if no match is found (Correct answer)
- Replaces the entire document
- Returns the updated document
Correct answer: Inserts a new document if no match is found
Upsert combines update and insert: if no document matches the filter, a new one is created.
Question 7: Which Node.js ORM supports both SQL and NoSQL databases and uses a unified query API?
- Mongoose
- Sequelize
- Prisma (Correct answer)
- Knex
Correct answer: Prisma
Prisma supports PostgreSQL, MySQL, SQLite, MongoDB, and more through a unified schema and client API.
Which method in the `pg` (node-postgres) library is used to execute a parameterized query safely?