Feb 24, 2017 · 1 min read
Sequelize.js
ORM이란
Sequelize란
npm install sequelizeconst Sequelize = require('sequelize');const sequelize = new Sequelize(
'@@@', // 데이터베이스 이름
'@@@', // user 이름
'@@@', // password
{
'host': 'localhost',
'dialect': mysql // 다른 RDB를 설정해도 된다.
}
);sequelize
.authenticate()
.then(function(err){
console.log('Connection has been established successfully.');
.catch(function(err){
console.log('Unable to connect to the database:', err);
});
.authenticate() 함수를 통해 connection을 테스트할 수 있다.
var User = sequelize.define('user', {
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
}
});Model을 define할 때는 sequelize.define(‘name’, {attributes}, {options})으로 한다.
User.sync({force: true}).then(function(){
return User.create({
firstName: 'John',
lastName: 'Hancock'
});
});force가 true일 경우 이미 존재하는 테이블을 drop할 수 있다.
