https://sequelize.org/

 

Sequelize

Sequelize is a modern TypeScript and Node.js ORM for Oracle, Postgres, MySQL, MariaDB, SQLite and SQL Server, and more. Featuring solid transaction support, relations, eager and lazy loading, read replication and more.

sequelize.org

1. 라이브러리 설치

npm i sequelize
npm i mysql2

2. DB 연결 (app.js)

const express = require('express');
const app = express();
const port = 3000;
const { Sequelize } = require('sequelize');

// database, username, password는 자신의 DB에 맞는 값을 입력해야 한다. 
// username: root (주로)
// password : 설정하지 않았으면 null 과 '' 중에 하나 입력
const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: 'mysql'
});

app.get('/', (req, res) => {
  res.send('Hello World!');
})

app.listen(port, async () => {
  try {
    await sequelize.authenticate();
    console.log('Connection has been established successfully.');
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
  console.log(`서버가 실행됩니다. http://localhost:${port}`);
})

 

+ Recent posts