Skip to content

wdi-infinity/amex-week_10-day_04-practice-make-perfect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

practice

Keep Practice !

build a blog database using sequelize !!

models

  1. Post
  2. Comment

Assocation

posts can have multiple comments ?

SEQUELIZE GUID

  1. Create a new project file
mkdir blog
cd blog
  1. Initiate an npm
npm init -y
  1. Install the project dependencies
npm install --save sequelize pg pg-hstore path
  1. Tell sequelize to create all the files inside db folder
mkdir db
touch .sequelizerc
  1. Edit .sequelizerc file
const path = require('path');

module.exports = {
  "config": path.resolve('./db/config', 'config.json'),
  "models-path": path.resolve('./db/models'),
  "seeders-path": path.resolve('./db/seeders'),
  "migrations-path": path.resolve('./db/migrations'),
}
  1. Generate Sequelize files
sequelize init
  1. make sure that all your files inside the db folders like this
db
├── config
│ └── config.json
├── migrations
├── models
│ └── index.js
├── seeders
  1. update the config file
// db/config/config.json

{
  "development": {
  "username": <your username>,
  "password": null,
  "database": "blog",
  "host": "127.0.0.1",
  "dialect": "postgres",
  "define": {
        "underscored": true
    }
  },
...
}
  1. Generate the models
// write the commands
  1. Update the models that sequelize generated:
  • Change the table name to make it all lower case postgres naming conventions
  • Add the assocation
  1. update the migration file Change the table name to make it all lower case postgres naming conventions and add the assocation field then change the updatedAt to updated_at and createdAt to created_at
// add post id in the comment table
...
 post_id: {
        type: Sequelize.INTEGER,
        onDelete: "CASCADE",
        references: {
          model: "posts",
          key: "id"
        }
      }
  ....
  1. create the database
createdb blog
  1. run the migration files
write the command
  1. create an index.js file to write your query
  • Create a new post
  • Create a new comment
  • List all the posts with thier comments

don't forget to require the modesl before running the queries const db = require("./db/models");

  1. what is the diffrent between running migration and using ?
db.sequelize.sync({force: true})

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published