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
CRUD operations with Sequelize
📖 Scenario: You are building a simple Express API to manage a list of books in a library. Each book has a title and an author. You will use Sequelize to interact with a database and perform CRUD operations.
🎯 Goal: Create an Express app with Sequelize that can create, read, update, and delete books from the database.
📋 What You'll Learn
Create a Sequelize model for Book with title and author fields
Set up an Express app with Sequelize connection
Implement CRUD routes: POST /books, GET /books, PUT /books/:id, DELETE /books/:id
Use async/await and proper Sequelize methods for database operations
💡 Why This Matters
🌍 Real World
APIs like this are common in web apps to manage data records such as books, users, or products.
💼 Career
Understanding Sequelize CRUD operations is essential for backend developers working with Node.js and relational databases.
Progress0 / 4 steps
1
Set up Sequelize and define the Book model
Create a Sequelize instance called sequelize connected to an SQLite database in memory. Then define a model called Book with fields title and author, both of type Sequelize.STRING.
Express
Hint
Use new Sequelize('sqlite::memory:') to create the connection. Use sequelize.define to create the model with title and author as string fields.
2
Set up Express app and sync database
Create an Express app called app. Add a line to sync the Sequelize models with the database using sequelize.sync().
Express
Hint
Import Express, create app with express(), and call sequelize.sync() to create tables. Use app.use(express.json()) to parse JSON bodies.
3
Create CRUD routes for books
Add four routes to app: POST /books to create a book using Book.create(), GET /books to get all books using Book.findAll(), PUT /books/:id to update a book by id using Book.update(), and DELETE /books/:id to delete a book by id using Book.destroy(). Use async functions and await for Sequelize calls.
Express
Hint
Use app.post, app.get, app.put, and app.delete with async functions. Use Sequelize methods create, findAll, update, and destroy with await.
4
Start the Express server
Add code to start the Express server on port 3000 using app.listen(3000). Add a callback that logs 'Server is running on port 3000'.
Express
Hint
Use app.listen(3000, () => { console.log('Server is running on port 3000') }) to start the server.
Practice
(1/5)
1. Which Sequelize method is used to add a new record to the database?
easy
A. update()
B. findAll()
C. create()
D. destroy()
Solution
Step 1: Understand CRUD operations
CRUD stands for Create, Read, Update, Delete. Each operation has a matching Sequelize method.
Step 2: Match method to Create operation
The create() method is used to add new records to the database.
Final Answer:
create() -> Option C
Quick Check:
Create = create() [OK]
Hint: Create means add new, so use create() method [OK]
Common Mistakes:
Confusing create() with findAll() which reads data
Using update() to add new records
Using destroy() which deletes records
2. Which of the following is the correct syntax to update a user's name to 'Alice' where id is 5 using Sequelize?
B. User.update({ status: 'active' }, { where: { lastLogin: { [Op.lt]: new Date('2023-01-01') } } })
C. User.update('active', { lastLogin: { [Op.lt]: new Date('2023-01-01') } })
D. User.update({ status: 'active' }, { lastLogin: { [Op.lt]: new Date('2023-01-01') } })
Solution
Step 1: Understand update method parameters
The first argument is the values to update, the second is an options object with a where clause to filter records.
Step 2: Check correct use of Op.lt and date object
User.update({ status: 'active' }, { where: { lastLogin: { [Op.lt]: new Date('2023-01-01') } } }) correctly uses [Op.lt] with a Date object inside the where clause.
Step 3: Verify argument order and structure
The other options have incorrect argument order or missing where wrapper.
Final Answer:
User.update({ status: 'active' }, { where: { lastLogin: { [Op.lt]: new Date('2023-01-01') } } }) -> Option B
Quick Check:
update(values, { where: condition }) with Op.lt date [OK]
Hint: Update needs values first, then where with Op.lt date [OK]
Common Mistakes:
Swapping values and where arguments
Using string instead of Date object for date comparison