Bird
Raised Fist0
Node.jsframework~20 mins

ORM concept (Sequelize, Prisma overview) in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ORM Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What does ORM stand for and what is its main purpose?
Choose the correct definition and purpose of ORM in Node.js frameworks like Sequelize and Prisma.
AObject Reactive Model; it creates reactive objects for UI updates.
BOnline Resource Manager; it manages online resources like APIs and external services.
COperational Runtime Middleware; it handles middleware operations during runtime.
DObject-Relational Mapping; it helps convert data between incompatible systems by mapping objects in code to database tables.
Attempts:
2 left
💡 Hint
Think about how code objects relate to database tables.
❓ component_behavior
intermediate
2:00remaining
What will be the output of this Prisma query?
Given this Prisma client code, what will be the value of 'users' after execution?
Node.js
const users = await prisma.user.findMany({ where: { age: { gt: 18 } } });
AAn array of user objects where each user is older than 18.
BA single user object with age exactly 18.
CAn empty array because no users are returned without explicit select.
DA count of users older than 18.
Attempts:
2 left
💡 Hint
Look at the 'findMany' method and the 'where' filter.
📝 Syntax
advanced
2:00remaining
Which Sequelize model definition is syntactically correct?
Select the option that correctly defines a Sequelize model for a 'Product' with 'name' as string and 'price' as float.
Asequelize.define('Product', { name: DataTypes.STRING, price: DataTypes.FLOAT });
Bsequelize.define('Product', { name: STRING, price: FLOAT });
Csequelize.define('Product', { name: Sequelize.STRING, price: Sequelize.FLOAT });
Dsequelize.define('Product', { name: 'string', price: 'float' });
Attempts:
2 left
💡 Hint
Remember to use DataTypes for field types in Sequelize.
🔧 Debug
advanced
2:00remaining
Why does this Prisma query throw an error?
Consider this Prisma query code snippet. Why does it throw a runtime error? const user = await prisma.user.findUnique({ where: { email: 'test@example.com' } }); console.log(user.name.toUpperCase());
ABecause 'findUnique' requires an 'id' field, not 'email'.
BBecause 'user' can be null if no record matches, so accessing 'name' causes an error.
CBecause 'toUpperCase()' is not a valid method for strings in JavaScript.
DBecause Prisma client does not support 'findUnique' method.
Attempts:
2 left
💡 Hint
Think about what happens if no user matches the email.
❓ state_output
expert
3:00remaining
What is the final state of the database after this Sequelize transaction?
Given this Sequelize transaction code, what will be the final state of the 'Orders' table if an error occurs during the second create call? const t = await sequelize.transaction(); try { await Order.create({ item: 'Book', quantity: 1 }, { transaction: t }); await Order.create({ item: null, quantity: 2 }, { transaction: t }); // item cannot be null await t.commit(); } catch (error) { await t.rollback(); } What happens to the 'Orders' table?
ABoth orders are added, but the second has a null item field.
BOnly the first order is added because it was created before the error.
CNo new orders are added because the transaction is rolled back due to the error.
DThe transaction commits partially, adding the first order and ignoring the error.
Attempts:
2 left
💡 Hint
Remember how transactions work when an error occurs.

Practice

(1/5)
1. What is the main purpose of an ORM like Sequelize or Prisma in Node.js?
easy
A. To simplify database operations by using JavaScript code instead of raw SQL
B. To replace Node.js as a runtime environment
C. To create user interfaces for web applications
D. To manage server hardware resources

Solution

  1. Step 1: Understand what ORM stands for

    ORM means Object-Relational Mapping, which helps translate code into database queries.
  2. Step 2: Identify ORM's role in Node.js

    Sequelize and Prisma let developers write JavaScript code to interact with databases instead of writing SQL directly.
  3. Final Answer:

    To simplify database operations by using JavaScript code instead of raw SQL -> Option A
  4. Quick Check:

    ORM = Simplify database code [OK]
Hint: ORM means code replaces SQL for easier database work [OK]
Common Mistakes:
  • Thinking ORM replaces Node.js runtime
  • Confusing ORM with UI tools
  • Assuming ORM manages hardware
2. Which of the following is the correct way to define a model in Sequelize?
easy
A. createModel('User', { name: 'string' })
B. sequelize.define('User', { name: DataTypes.STRING })
C. new Model('User', { name: String })
D. Model.defineUser({ name: DataType.String })

Solution

  1. Step 1: Recall Sequelize model definition syntax

    Sequelize uses sequelize.define(modelName, attributes) to create models.
  2. Step 2: Match options with correct syntax

    sequelize.define('User', { name: DataTypes.STRING }) matches the correct method and uses DataTypes.STRING properly.
  3. Final Answer:

    sequelize.define('User', { name: DataTypes.STRING }) -> Option B
  4. Quick Check:

    Sequelize model = sequelize.define() [OK]
Hint: Sequelize models use sequelize.define() with DataTypes [OK]
Common Mistakes:
  • Using 'new Model' instead of sequelize.define
  • Incorrect DataTypes usage
  • Wrong method names like createModel
3. Given this Prisma schema snippet:
model Post {
  id    Int     @id @default(autoincrement())
  title String
  published Boolean @default(false)
}

What will be the default value of published when a new Post is created without specifying it?
medium
A. false
B. true
C. null
D. undefined

Solution

  1. Step 1: Read the Prisma model definition

    The field published is Boolean with @default(false), meaning it defaults to false.
  2. Step 2: Understand default behavior on creation

    If no value is given for published, Prisma sets it to the default, which is false.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    Prisma default Boolean = false [OK]
Hint: Look for @default() in Prisma schema for default values [OK]
Common Mistakes:
  • Assuming default is true without checking
  • Thinking default is null or undefined
  • Ignoring @default directive
4. You wrote this Sequelize code:
const User = sequelize.define('User', {
  username: Sequelize.STRING,
  age: Sequelize.INTEGER
});

But you get an error: Sequelize.STRING is not a constructor. What is the likely fix?
medium
A. Change Sequelize.STRING to 'string' (a string literal)
B. Remove the model definition and use raw SQL instead
C. Import DataTypes from Sequelize and use DataTypes.STRING instead
D. Rename 'User' to 'Users' in define method

Solution

  1. Step 1: Identify the cause of the error

    Sequelize.STRING is not a constructor because Sequelize exports DataTypes separately.
  2. Step 2: Correct usage of DataTypes

    You should import DataTypes from Sequelize and use DataTypes.STRING and DataTypes.INTEGER in model definitions.
  3. Final Answer:

    Import DataTypes from Sequelize and use DataTypes.STRING instead -> Option C
  4. Quick Check:

    Use DataTypes.STRING, not Sequelize.STRING [OK]
Hint: Use DataTypes from Sequelize for types, not Sequelize directly [OK]
Common Mistakes:
  • Using Sequelize.STRING instead of DataTypes.STRING
  • Trying to fix by renaming model
  • Switching to raw SQL unnecessarily
5. You want to fetch all users with their posts using Prisma. Which code snippet correctly fetches users including their posts?
hard
A. const users = await prisma.user.findAll({ include: ['posts'] });
B. const users = await prisma.user.findMany({ select: { posts: false } });
C. const users = await prisma.user.findMany({ where: { posts: null } });
D. const users = await prisma.user.findMany({ include: { posts: true } });

Solution

  1. Step 1: Recall Prisma syntax for including related models

    Prisma uses include: { relatedModel: true } to fetch related data.
  2. Step 2: Check each option for correct syntax

    const users = await prisma.user.findMany({ include: { posts: true } }); uses include: { posts: true } which is correct. const users = await prisma.user.findMany({ select: { posts: false } }); excludes posts, C filters by posts null, D uses wrong method name.
  3. Final Answer:

    const users = await prisma.user.findMany({ include: { posts: true } }); -> Option D
  4. Quick Check:

    Prisma include related = include: { posts: true } [OK]
Hint: Use include: { relatedModel: true } to fetch relations in Prisma [OK]
Common Mistakes:
  • Using findAll instead of findMany in Prisma
  • Using select to exclude instead of include to fetch
  • Wrong syntax for including relations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard