Home > Web Front-end > JS Tutorial > body text

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

Patricia Arquette
Release: 2024-10-12 14:30:30
Original
440 people have browsed it

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;
Copy after login

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!

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template