Node.js Database Integration 3 — Questions and Answers
Question 1: In Prisma, what command regenerates the Prisma Client after modifying `schema.prisma`?
- prisma migrate dev
- prisma db push
- prisma generate (Correct answer)
- prisma sync
Correct answer: prisma generate
`prisma generate` reads the schema and regenerates the type-safe Prisma Client code.
Question 2: What is the correct way to handle transactions in node-postgres (`pg`) using a client from a pool?
- pool.transaction(async (client) => { ... })
- Acquire a client, run BEGIN/COMMIT/ROLLBACK manually, then release (Correct answer)
- Use pool.query() with 'TRANSACTION' flag
- Wrap queries in try/catch only
Correct answer: Acquire a client, run BEGIN/COMMIT/ROLLBACK manually, then release
Transactions require a dedicated client; you issue BEGIN, run queries, then COMMIT or ROLLBACK, and always release the client.
Question 3: In Sequelize, what does `sync({ force: true })` do?
- Syncs only new tables without dropping existing ones
- Drops and recreates all tables, destroying existing data (Correct answer)
- Adds missing columns without dropping tables
- Validates models against the existing schema
Correct answer: Drops and recreates all tables, destroying existing data
`force: true` drops all tables and recreates them, wiping all data — dangerous in production.
Question 4: Which MongoDB aggregation stage is used to filter documents, similar to a WHERE clause?
- $group
- $project
- $match (Correct answer)
- $filter
Correct answer: $match
`$match` filters the document stream to pass only documents that match the specified condition.
Question 5: What does the `N+1 query problem` refer to in the context of ORMs?
- Running one query that returns N+1 rows unexpectedly
- Executing one query to fetch N records, then N additional queries for related data (Correct answer)
- A query that times out after N+1 retries
- An index that requires N+1 comparisons
Correct answer: Executing one query to fetch N records, then N additional queries for related data
N+1 occurs when you fetch N parent records and then issue one extra query per record to load its relations, totaling N+1 queries.
Question 6: In Mongoose, which option passed to `Schema` disables automatic `__v` versioning key?
- { versionKey: false } (Correct answer)
- { version: false }
- { versioning: 'off' }
- { __v: false }
Correct answer: { versionKey: false }
Setting `versionKey: false` in schema options prevents Mongoose from adding the `__v` field to documents.
Question 7: Which Redis data structure is best suited for implementing a job queue in Node.js?
- String
- Hash
- List (Correct answer)
- Set
Correct answer: List
Redis Lists support atomic `LPUSH`/`RPOP` (or blocking `BRPOP`) operations, making them ideal for FIFO queues.
In Prisma, what command regenerates the Prisma Client after modifying `schema.prisma`?