Concept Flow - CRUD operations with Sequelize
Start
Create Model Instance
Read Data
Update Data
Delete Data
End
This flow shows the four main steps of CRUD with Sequelize: create, read, update, and delete data in the database.
const user = await User.create({ name: 'Ana' }); const foundUser = await User.findByPk(user.id); await foundUser.update({ name: 'Anna' }); await foundUser.destroy();
| Step | Action | Input | Sequelize Method | Result | Database State |
|---|---|---|---|---|---|
| 1 | Create user | { name: 'Ana' } | User.create | User instance with id=1 | User table has 1 record: { id:1, name:'Ana' } |
| 2 | Read user by ID | id=1 | User.findByPk | User instance with id=1, name='Ana' | No change |
| 3 | Update user name | { name: 'Anna' } | foundUser.update | User instance updated to name='Anna' | User table record updated: { id:1, name:'Anna' } |
| 4 | Delete user | none | foundUser.destroy | User instance deleted | User table is empty |
| 5 | Exit | No more actions | N/A | End of CRUD operations | Final database state: empty user table |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 |
|---|---|---|---|---|---|
| user | undefined | { id:1, name:'Ana' } | { id:1, name:'Ana' } | { id:1, name:'Anna' } | { id:1, name:'Anna' } (deleted) |
| foundUser | undefined | undefined | { id:1, name:'Ana' } | { id:1, name:'Anna' } | { id:1, name:'Anna' } (deleted) |
CRUD with Sequelize:
- Create: Model.create({data}) adds a record.
- Read: Model.findByPk(id) fetches by primary key.
- Update: instance.update({data}) changes fields.
- Delete: instance.destroy() removes record.
Each step changes database state accordingly.