Bird
Raised Fist0
Expressframework~20 mins

Sequelize ORM setup in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Sequelize ORM Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Sequelize model definition produce?
Consider this Sequelize model definition for a User. What will be the data type of the 'age' column in the database?
Express
const User = sequelize.define('User', {
  name: {
    type: Sequelize.STRING,
    allowNull: false
  },
  age: {
    type: Sequelize.INTEGER,
    allowNull: true
  }
});
AA column named 'age' with string type that cannot be null
BA column named 'age' with integer type that can be null
CA column named 'age' with boolean type that can be null
DA column named 'age' with date type that cannot be null
Attempts:
2 left
💡 Hint
Look at the 'type' and 'allowNull' properties in the model definition.
📝 Syntax
intermediate
2:00remaining
Which option correctly initializes Sequelize with SQLite?
You want to create a Sequelize instance using SQLite as the database. Which code snippet correctly does this?
Aconst sequelize = new Sequelize('sqlite', 'memory');
Bconst sequelize = new Sequelize('sqlite::memory:');
Cconst sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:' });
Dconst sequelize = new Sequelize('sqlite://memory');
Attempts:
2 left
💡 Hint
Check the Sequelize docs for SQLite connection strings.
🔧 Debug
advanced
2:00remaining
Why does this Sequelize sync code throw an error?
Given this code snippet, why does calling sequelize.sync() throw a TypeError?
Express
import { Sequelize } from 'sequelize';

const sequelize = Sequelize('sqlite::memory:');

await sequelize.sync();
ABecause 'await' cannot be used outside async functions
BBecause 'sync' is not a method on Sequelize instances
CBecause SQLite is not supported by Sequelize
DBecause Sequelize must be called with 'new' keyword to create an instance
Attempts:
2 left
💡 Hint
Check how Sequelize instances are created.
state_output
advanced
2:00remaining
What is the output after creating and saving a User instance?
Given this code, what will be logged to the console?
Express
const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory:');

const User = sequelize.define('User', {
  username: DataTypes.STRING
});

(async () => {
  await sequelize.sync();
  const user = User.build({ username: 'alice' });
  await user.save();
  console.log(user.id);
})();
A1
Bundefined
Cnull
DThrows an error because 'id' is not defined
Attempts:
2 left
💡 Hint
Sequelize auto-generates primary keys named 'id' by default.
🧠 Conceptual
expert
2:00remaining
Which option best describes Sequelize's 'sync' method behavior?
What does calling sequelize.sync({ force: true }) do in a Sequelize setup?
AOnly creates tables if they do not exist, without dropping anything
BUpdates existing tables without dropping them
CDrops all tables and recreates them according to model definitions
DDeletes all data from tables but keeps the structure intact
Attempts:
2 left
💡 Hint
Check the meaning of 'force' in Sequelize sync options.

Practice

(1/5)
1. What is the main purpose of Sequelize in an Express application?
easy
A. To connect and interact with databases using JavaScript objects
B. To handle HTTP requests and routing
C. To style the frontend components
D. To manage user authentication

Solution

  1. Step 1: Understand Sequelize's role

    Sequelize is an ORM that helps connect your app to databases using JavaScript objects.
  2. Step 2: Compare with other Express tasks

    Handling HTTP requests or styling is done by other tools, not Sequelize.
  3. Final Answer:

    To connect and interact with databases using JavaScript objects -> Option A
  4. Quick Check:

    Sequelize = Database connection [OK]
Hint: Sequelize = database ORM, not routing or styling [OK]
Common Mistakes:
  • Confusing Sequelize with Express routing
  • Thinking Sequelize manages frontend styling
  • Assuming Sequelize handles authentication directly
2. Which of the following is the correct way to create a Sequelize instance for a PostgreSQL database named 'mydb' with user 'user' and password 'pass'?
easy
A. const sequelize = new Sequelize({ database: 'mydb', username: 'user', password: 'pass' });
B. const sequelize = Sequelize.connect('mydb', 'user', 'pass', { dialect: 'postgres' });
C. const sequelize = new Sequelize('mydb', 'user', 'pass', { dialect: 'postgres' });
D. const sequelize = new Sequelize('postgres://user:pass@localhost/mydb');

Solution

  1. Step 1: Recall Sequelize constructor syntax

    The Sequelize constructor takes database name, username, password, and options including dialect.
  2. Step 2: Check each option

    const sequelize = new Sequelize('mydb', 'user', 'pass', { dialect: 'postgres' }); matches the correct syntax. const sequelize = Sequelize.connect('mydb', 'user', 'pass', { dialect: 'postgres' }); uses a non-existent connect method. const sequelize = new Sequelize({ database: 'mydb', username: 'user', password: 'pass' }); uses wrong parameter structure. const sequelize = new Sequelize('postgres://user:pass@localhost/mydb'); is valid but uses a connection string, which is not asked here.
  3. Final Answer:

    const sequelize = new Sequelize('mydb', 'user', 'pass', { dialect: 'postgres' }); -> Option C
  4. Quick Check:

    Sequelize constructor = new Sequelize(db, user, pass, options) [OK]
Hint: Sequelize constructor uses new Sequelize(db, user, pass, options) [OK]
Common Mistakes:
  • Using Sequelize.connect instead of new Sequelize
  • Passing options incorrectly
  • Confusing connection string with parameters
3. Given the following Sequelize model definition, what will be the output of console.log(User.name);?
const User = sequelize.define('User', {
  name: {
    type: Sequelize.STRING,
    allowNull: false
  }});
medium
A. 'User'
B. undefined
C. Sequelize.STRING
D. An error because 'name' is a field, not a property

Solution

  1. Step 1: Understand model definition

    The first argument 'User' is the model name and is accessible as User.name property.
  2. Step 2: Check what User.name returns

    User.name returns the model name string 'User', not the field value or type.
  3. Final Answer:

    'User' -> Option A
  4. Quick Check:

    Model name = User.name = 'User' [OK]
Hint: Model name is stored in User.name, not fields [OK]
Common Mistakes:
  • Confusing field 'name' with model name
  • Expecting field value instead of model name
  • Assuming User.name is undefined
4. You wrote this code to sync your Sequelize models but get an error:
await sequelize.sync({ force: true });
console.log('Database synced!');
What is the most likely cause of the error?
medium
A. Sequelize instance not created
B. Missing async function wrapper for await
C. force option is invalid
D. Incorrect sync method name

Solution

  1. Step 1: Check usage of await

    Using await requires the code to be inside an async function.
  2. Step 2: Verify other options

    sync is correct method, force is valid option, and Sequelize instance must exist before this code.
  3. Final Answer:

    Missing async function wrapper for await -> Option B
  4. Quick Check:

    await needs async function [OK]
Hint: Always use await inside async functions [OK]
Common Mistakes:
  • Using await outside async function
  • Misspelling sync method
  • Misunderstanding force option
5. You want to set up Sequelize in your Express app to connect to a MySQL database named 'shop' with username 'root' and password 'pass' using separate parameters for database, username, password, and options, and define a 'Product' model with fields 'title' (string) and 'price' (decimal). Which code snippet correctly sets up Sequelize and defines this model?
hard
A. const sequelize = new Sequelize('mysql://root:pass@localhost/shop'); const Product = sequelize.define('Product', { title: Sequelize.STRING, price: Sequelize.DECIMAL });
B. const sequelize = new Sequelize('shop', 'root', 'pass', { dialect: 'mysql' }); const Product = sequelize.define('Product', { title: 'string', price: 'decimal' });
C. const sequelize = new Sequelize('mysql://root:pass@localhost/shop'); const Product = sequelize.define('Product', { title: { type: 'string' }, price: { type: 'decimal' } });
D. const sequelize = new Sequelize('shop', 'root', 'pass', { dialect: 'mysql' }); const Product = sequelize.define('Product', { title: { type: Sequelize.STRING }, price: { type: Sequelize.DECIMAL } });

Solution

  1. Step 1: Check Sequelize instance creation

    const sequelize = new Sequelize('shop', 'root', 'pass', { dialect: 'mysql' }); const Product = sequelize.define('Product', { title: { type: Sequelize.STRING }, price: { type: Sequelize.DECIMAL } }); correctly uses new Sequelize with database, user, password, and dialect options for MySQL using separate parameters.
  2. Step 2: Verify model field definitions

    const sequelize = new Sequelize('shop', 'root', 'pass', { dialect: 'mysql' }); const Product = sequelize.define('Product', { title: { type: Sequelize.STRING }, price: { type: Sequelize.DECIMAL } }); defines fields with type property using Sequelize.STRING and Sequelize.DECIMAL, which is correct syntax.
  3. Step 3: Identify errors in other options

    Options A and D use connection string instead of separate parameters. const sequelize = new Sequelize('shop', 'root', 'pass', { dialect: 'mysql' }); const Product = sequelize.define('Product', { title: 'string', price: 'decimal' }); uses separate parameters but incorrect field definitions with string literals instead of Sequelize data types.
  4. Final Answer:

    Correct Sequelize setup and model definition with proper data types -> Option D
  5. Quick Check:

    Sequelize setup + model fields = const sequelize = new Sequelize('shop', 'root', 'pass', { dialect: 'mysql' }); const Product = sequelize.define('Product', { title: { type: Sequelize.STRING }, price: { type: Sequelize.DECIMAL } }); [OK]
Hint: Use Sequelize data types with { type: Sequelize.TYPE } syntax [OK]
Common Mistakes:
  • Using string literals instead of Sequelize data types
  • Incorrect Sequelize constructor parameters
  • Omitting type property in model fields