Home  >  Article  >  Web Front-end  >  SQL Sequelize || By Munisekhar Udavalapati || MySQL || SQL

SQL Sequelize || By Munisekhar Udavalapati || MySQL || SQL

Patricia Arquette
Patricia ArquetteOriginal
2024-10-12 14:30:30442browse

SQL Sequelize || By Munisekhar Udavalapati || MySQL || SQL

Installing sequelize

npm install --save sequelize

You can also install MySQL. to use this command

npm install --save mysql2

Connecting to the database

javaScript const {Sequelize} =require('sequlize');
const sequelize =new Sequelize('database','username','password',{
host:'localhost',
dialect;
});

Testing the connection

javaScript try{
await sequlize.authenticate();
console.log('Connection success');
}catch(err){
console.error('Unable to connect to the database',err);
}

Model

Models are reprent's table from the database

const Sequelize=require('sequelize');
const sequelize=require('../util/db.js'); //database connection locations
const User=sequelize.define(
 'User', //table name 
  {  
    id:{
     type:Sequlize.INTEGER,
     autoIncrement:true,
     primaryKey:true
     },
     name:{
       type:Sequelize.STRING,
       allowNull: false,
     },
     email:{
        type:Sequelize.STRING,
        allowNull:false,
        unique:true
     }
  }

module.exports=User;

The above is the detailed content of SQL Sequelize || By Munisekhar Udavalapati || MySQL || SQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn