3.3: Sequelize
- 1.ORMs allow us to query SQL databases using application languages such as JavaScript
- 2.Sequelize is the most popular JavaScript ORM
- 3.Understand basic Sequelize setup and usage
- 4.Models tell our apps what data they can access; Migrations tell our databases how to be structured
ORMs (Object-Relational Mappings) allow us to manage and query SQL databases using application languages like JavaScript without having to write SQL. This makes our applications more robust because it reduces human error from typos in SQL queries which are typically written as strings. ORMs are a layer on top of SQL, where ORMs translate application code to SQL before querying SQL DBs.
Sequelize is the most popular JavaScript ORM and we will use Sequelize in our applications during Bootcamp. The following sections follow official Sequelize tutorials. There is no need to remember all implementation details from the tutorials. Feel free to skim them, understand high-level concepts and refer back to the tutorials during implementation.
Introduction to Databases
Table Relationships
Sequelize Migrations, Seeders and Models
Setting up Sequelize in an Express App
Sequelize Migrations
Please checkout the finished code in this repository, ensure that you're on the
initial_setup
branch.Migrations | Sequelize
Sequelize official tutorial on Sequelize migrations
- 1.We will use migrations to set up our DB schema, the structure of our database, or the tables and their relationships. All companies use migrations to manage DB schema. Rocket considers migrations a core concept of Sequelize and ORMs in general.
- 2.Migrations at Rocket will only have "development" and "production" environments for simplicity. Tech teams in industry often have "test" environments for more robust testing between development and production.
- 3.We will use
model:generate
at Rocket to generate model and migration files ( This is done below) - 4.You may also use a command like this:
npx sequelize migration:generate --name products
- 5.Rocket will use
.sequelizerc
to specify Sequelize file and folder paths as per the example in "The.sequelizerc
file" section - 6.We will not use concepts from "Dynamic configuration" onward during Rocket's Bootcamp
Primary Migrations
Secondary Migrations
Tertiary Migrations
Migrations | Sequelize
Sequelize official tutorial on Sequelize Seeders
- 1.We use seeder files to setup the initial data that can be used to populate our database. All companies will likely seed dummy data so that developers can collaborate and work with similar data.
- 2.We will use
npx sequelize seed:generate --name products
command in order to generate our Seeder file - 3.Rocket will use seed files to populate initial data in our applications
Primary Seeders
Secondary Seeders
Tertiary Seeders
Migration Alteration
Seeder Alteration
Running Migrations and Seeders
Please checkout the finished code in this repository, ensure that you're on the
running_migrations_seeders
branch if you want to test out the migrations and seeders on your machine you will need to install the dependencies with the command npm install
after the installation you need to setup your database connections and .env
after this you can run the migrations, and seeders.
Model Basics | Sequelize
Sequelize official tutorial on Sequelize models
- 1.A Model is an abstraction that represents a table within your database, in Sequelize the Model is a class, the instances of this class represent the data stored.
- 2.We will
Extend the Sequelize
Model
to define models at Rocket - 3.We will use default table name inference for all Sequelize examples at Rocket, which automatically assumes table names are the pluralised form of model names
- 4.When passing in the second object that contains sequelize, we will pass two more key value pairs :
- 1.
modelName: '<lowercase-name-of-model'>
- 2.
underscored: true
- 5.We will not use
model.sync
to synchronise models with databases because that behaviour is not production-safe. We will instead use database migrations.
Require vs Import Statements
You may notice that Sequelize docs use
require
syntax to import modules. This is an older import syntax that Node.js still supports. Rocket recommends using import
syntax for all code we write, and to update file extensions to .cjs
(short for CommonJS) instead of .js
for files that use require
syntax. To enable import
syntax for all .js
files by default, Rocket has included a "type": "module"
setting in package.json
in all Rocket starter code.Model Instances | Sequelize
Sequelize official tutorial on Sequelize model instances
- 1.An instance of the Model class represents a row of data that is stored within the Sequelize database.
- 2.We will use the
create
method to create model instances at Rocket instead ofbuild
andsave
. - 3.Rocket recommends using the suggested way to log model instances with
.toJSON()
- 4.Rocket recommends using
update
to update model instances for precision instead ofset
andsave
Model Querying - Basics | Sequelize
Sequelize official tutorial on Sequelize model instances
- 1.Model Querying allows developers to preform CRUD like actions to our database, we can preform actions such as create, read update and delete on the database
- 2.We will use
Model.create()
to insert rows into our database instead ofModel.build()
andinstance.save()
- 3.We will rarely need to use syntax in "Advanced queries with functions (not just columns)" section, if ever
- 4.We will use
Model.bulkCreate
to seed data in our databases for Rocket exercises - 5.Limits and pagination will not be necessary until we have large amounts of data that slows down our apps when retrieved all at once
Model Querying - Finders | Sequelize
Sequelize official tutorial on Sequelize model finder methods
- 1.If you're not updating, inserting or deleting a value from your stored data, you're probably looking to list out some specific information, or even just all the data in your table, use the Model Query Finders to do this.
- 2.These are some of the most common Sequelize methods we will use in our apps
- 3.We can use the
Instance.findAll()
method to select data from the database to use within our application
Sequelize Models (1)
Sequelize Models (2)
Last modified 30d ago