Skip to content

This guide was created while i was learning the basics of Sequelize and provides a step-by-step approach to utilizing Sequelize as an ORM with Express and Node.js.

Notifications You must be signed in to change notification settings

faicalbounedjar/BlogApi_Node_Express_Sequelize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sequelize ORM Usage with Express and Node.js: A Comprehensive Guide

Introduction

Sequelize is an Object-Relational Mapping (ORM) tool that simplifies data interaction by treating database entities as JavaScript objects.

Getting Started

  1. Initialize Sequelize Project

    • Run sequelize init to set up a new Sequelize project.
  2. Configure Sequelize

    • Edit the configuration file (config/config.json) based on your requirements, specifying database details.
  3. Database Creation

    • Use sequelize db:create to create a database based on the configured settings.
  4. Generate a Model

    • Create a model using sequelize model:generate --name <name> and specify attributes using --attributes <name>:<type>.
  5. Synchronizing the Model

    • Use sequelize.sync() within an asynchronous function to sync the model with the database. Use sequelize.sync({ alter|<force>: true }) to modify the existing model.
  6. Production Use and Migrations

    • In production, use sequelize db:migrate instead of sequelize db:create to manage database changes. Use sequelize.authenticate() instead of sync for authentication.
  7. Database Cleanup

    • To drop the database, run sequelize db:drop.
  8. Data Privacy and toJSON

    • Ensure data privacy by customizing the JSON output using toJSON in the model. Hide sensitive information and redefine the output format.
  9. Associations

    • Define relationships between models using associations. Use belongsTo for N->1 and hasMany for 1<-N relationships.
  10. Fetching Associated Models

    • Utilize { include: Model } in findAll to retrieve the full associated model with the current one.
    • For aliasing and specific associations, use { include: [{ model: User, as: 'user' }] } and add the alias using as.

Conclusion

This guide was created while learning the basics of Sequelize and provides a step-by-step approach to utilizing Sequelize as an ORM with Express and Node.js, enhancing data interaction and management within your applications.

About

This guide was created while i was learning the basics of Sequelize and provides a step-by-step approach to utilizing Sequelize as an ORM with Express and Node.js.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published