0% found this document useful (0 votes)
9 views5 pages

Backend Setup with Sequelize and Express

The document provides a step-by-step guide for setting up a backend using Sequelize, Express, and PostgreSQL. It includes instructions for installing necessary packages, configuring the database connection, creating models and migrations, and setting up API controllers. Additionally, it covers seeding the database with sample data and running the server.

Uploaded by

k2268645
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Backend Setup with Sequelize and Express

The document provides a step-by-step guide for setting up a backend using Sequelize, Express, and PostgreSQL. It includes instructions for installing necessary packages, configuring the database connection, creating models and migrations, and setting up API controllers. Additionally, it covers seeding the database with sample data and running the server.

Uploaded by

k2268645
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Backend Setup Guide-

1. npm i sequelize dotenv express pg pg-hstore cors


2. npm i -g sequelize-cli
3. sequelize init
--- creates migrations, models, config and seeders folder
4. Now, edit [Link] name to [Link] first and then replace its contents to -

require('dotenv').config();
[Link] = {
development: {
username: [Link].DB_USERNAME,
password: [Link].DB_PASSWORD,
database: [Link].DB_NAME,
host: [Link].DB_HOST,
logging:true,
dialect: "postgres"
},
test: {
username: [Link].DB_USERNAME,
password: [Link].DB_PASSWORD,
database: [Link].DB_NAME,
host: [Link].DB_HOST,
dialect: "postgres"
},
production: {
username: [Link].DB_USERNAME,
password: [Link].DB_PASSWORD,
database: [Link].DB_NAME,
host: [Link].DB_HOST,
dialect: "postgres"
}
}

5. Also, change [Link] → [Link] in [Link] of models folder


6. Make a .env file like below –

DB_NAME=postgres
DB_USERNAME=postgres
DB_PASSWORD='shivani'
DB_HOST=localhost
PORT=4005

7. sequelize model:generate --name Book --atributes book_id:integer, book_name:string,


year_published:smallint
--This will create [Link] named file inside migrations folder and also a [Link] in
models folder
8. Now, go in [Link] file inside models folder and update it’s contents as –
'use strict';
const {
Model
} = require('sequelize');
[Link] = (sequelize, DataTypes) => {
class Book extends Model {
static associate(models) {
}
}
[Link]({
book_id:
{
type:[Link],
primaryKey:true,
autoIncrement:true
},
name: {
type:[Link],
allowNull:false
},
author:
{
type: [Link],
allowNull:false
},
year_published:
{
type:[Link],
allowNull:false
}
}, {
sequelize,
modelName: 'Book',
tableName:'books',
timestamps:false
});
return Book;
};
9. Now, go in [Link] file inside migrations folder and update it’s contents as –

'use strict';
/** @type {import('sequelize-cli').Migration} */
[Link] = {
async up(queryInterface, Sequelize) {
await [Link]('books', {

book_id: {
type: [Link],
allowNull: false,
autoIncrement: true,
primaryKey: true,

},
name: {
type: [Link],
allowNull:false
},
author: {
type: [Link],
allowNull:false
},
year_published: {
type: [Link],
allowNull:false
}
});
},
async down(queryInterface, Sequelize) {
await [Link]('books');
}
};

10. Now, make a [Link] file in root directory and keep it’s contents as –

const express=require('express');
const app=express();
const {Sequelize}=require('sequelize');
const path=require('path');
const cors =require('cors');

require('dotenv').config();
[Link](cors());
[Link]([Link]());
[Link]([Link]({extended:false}));

const booksController=require('./controllers/books_controller');
[Link]('/api/books',booksController);

[Link]([Link] || 4005, () =>{


[Link]('server is running on port 4005');
})
11. sequelize db:migrate
--This will migrate the table in the database

12. sequelize seed:generate --name books


--This will create a seeder file for adding data
you can add some dummy data like below if you want or leave it empty also,
the code is as below-

'use strict';

/** @type {import('sequelize-cli').Migration} */


[Link] = {
async up (queryInterface, Sequelize) {
await [Link]('books',[
{
name:'The Hobbit',
author:'J.R.R tolken ',
year_published:1937
},
{
name:'The Harry Poter',
author:'J.R.R rowlien ',
year_published:1925
},
{
name:'The Great Priyanshi',
author:'F. Scott ',
year_published:1935
},
{
name:'The Greate Shivaji ',
author:'George alkin ',
year_published:1949
}
])
},

async down (queryInterface, Sequelize) {

await [Link]('books', null, {});

13. sequelize db:seed:all


--This will save the dummy data in the database
14. Now, make controllers for Apis

[Link]
--get Api

const books=require('express').Router()
const db=require('../models')
const {Book}=db

[Link]('/',async(req,res)=>{
try{
const foundBooks=await [Link]()
[Link](200).json(foundBooks)
}
catch(err){
[Link](500).send("Server error")
[Link]([Link])
}
})
[Link]=books

15. You can also set up different folder for routes and mention routes there.

You might also like