WIP => Making Sense of ORM Chaos
Keely
Posted on August 3, 2021
Getting lost in the sauce that is setting EVERYTHING up for ORM projects.
Node.js is the language to build and talk to a server. Express is a framework to make that easier. Bootstrap is to CSS as Express is to Node.js.
I'm working through setting up an ecommerce backend by modifying starter code and configuring a working Express.js API to use Sequelize to interact with a MySQL database. S
So I'll need to do a few things in the set up.
The Set Up
Add npm files to package.json and update with name of project (include sequelize, express, dotenv, mysql2)
Look through the package json to see what's already been set up for commands to run - run seed, nodemon
npm i sequelize
npm i express
npm i dotenv
npm i mysql2
these were all included in the code provided in wustl
Copy server file (express)
Create connection folder and copy over connection.js file
Create .env file and update db name for project
Set up database
mysql -u root -p
SOURCE schema.sql
quit
npm run seed
run server
node server.js
The Data
When actually starting to code, we need to start by setting up the Models. But first, let's visualize the data by working our way back from Routes.
In the database, each table will require it's own model and route. These are all brought together using "index.js" located in each folder.
So the main route index pulls all the routes together under /api to be used in the server file. We are expected to get JSON back.
[localhost:3002/api]
The router folder index creates the paths for each individual route set up: /travellers, /locations, /trips.
[localhost:3002/api/travellers]
And then the Models index links together the individual models traveller, location, trip
[localhost:3002/api/:traveller]
Seeds
We were provided with Seeds which gives us an example of each data table that should be created which can then help us build the models
Models
Set up Model folder and models
Add references snippets to Models
Create model index: Product, Category, Tag, ProductTag
- Define columns for each column (Add validation, include decimals)
- Extend Product model to set up fields and rules (need to figure out where this is used and then complete)
- Finish model index
Validation Scaffolding:
user.init({id: {}, username:{type:, validate: {}},sequelize...});
Indexing Models
.hasOne
Define a Driver as having one License to create a foreign key in the license table. Also define the association starting with License. If driver is deleted, cascade also deleted the associated License.
Driver.hasOne(License, {
foreignKey: 'driver_id',
onDelete: 'CASCADE'
.belongsTo
License.belongsTo(Driver, {
foreignKey: 'driver_id',
.hasMany
Define a Driver as having many Cars, thus creating a foreign key in the 'car' table.
Driver.hasMany(Car, {
foreignKey: 'driver_id',
onDelete: 'CASCADE', });
Car.belongsTo(Driver, {
foreignKey: 'driver_id', });
.belongsToMany
Define the third table needed to store the foreign keys
Location.belongsToMany(Traveller, {
through: {
model: Trip,
unique: false},
as: 'location_travellers'});
How to test these... run the seeds through the database.
Routes
Index has been set up for us: /categories, /products, /tags
To Dos:
- Category Routes & Insomnia set up
- Product Routes & Insomnia set up
- Tag Routes & Insomnia set up
GET
The purpose of the GET route is to find all the data and return as json so we can then use that data in other areas/services (ie. all locations). Can also bring in filtered data (ie. a single location).
Async Await scaffolding:
router.get('/', async (req, res) => {try{}catch(err){}}
I started to set up my first GET route in Insomnia to test, and ran into to do item in the body-parser. This is part of express middleware that needs to be configured.
Misc. Notes
What do the different status codes mean for each?
Are we supposed to push to github with our .env file included?
add to .gitignore >> the name of that file is there so we don't need to worry about it in this project
Documentation:
https://www.npmjs.com/package/sequelize
https://www.npmjs.com/package/express
https://www.npmjs.com/package/dotenv
https://www.npmjs.com/package/mysql2
Posted on August 3, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.