Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication and more.

 

라고 공식 홈페이지 첫 화면에 나와있습니다... 영알못인 저는 구글 번역기에 넣어보겠습니다.

 

Sequelize는 Postgres, MySQL, MariaDB, SQLite 및 Microsoft SQL Server를위한 Promise 기반 Node.js ORM입니다.

견고한 트랜잭션 지원, 관계, eager 및 lazy로드, 읽기 복제 등을 제공합니다.

 

ORM = Object Relational Mappings

 

뭔 말인지 모르겠지만, 쉽게 말해 Sequelize는 Node.js에서 DB 서버를 생성할 수 있도록 해주는 것 같습니다.

 

Sequelize 설치

 

npm 또는 yarn을 이용해서 설치할 수 있습니다.

 

npm install sequelize (yarn add sequelize)

 

database 연결하기

 

const { Sequelize } = require('sequelize');

const sequelize = new Sequelize('database', 'username', 'password', etc_config);

 

[Sequelize 특징]

Sequelize에서 제공하는 대부분의 메서드는 비동기식이므로 Promise를 반환합니다.

그러므로, Promise API (예 : then, catch, finally 사용)를 즉시 사용할 수 있습니다.

Posted by sungho88
,

router.get('/', (req,res,next)=> {

try {

const user = await User.findOne({

...

});

res.render('login', {

user

})

} catch (error) {

console.error(error);

next(error);

}

})

 

이와 같은 데이터베이스에 접속하는 코드를 실행하였는데, 에러가 발생했다.

너무나 많이 발생하는 에러라 이젠 외워지기 시작했다.

이참에 블로그에 정리해두려고 글을 작성한다.

 

이것은 await를 사용하기 위해서는 async를 반드시 함수 앞에 붙여줘야 한다. 바늘과 실처럼...

 

router.get('/', async (req,res,next)=> {

try {

const user = await User.findOne({

...

});

res.render('login', {

user

})

} catch (error) {

console.error(error);

next(error);

}

})

Posted by sungho88
,

<배열>

기본 변수 할당

var foo = ["one", "two", "three"];

var [one, two, three] = foo;

console.log(one); // "one"

console.log(two); // "two"

console.log(three); // "three"

 

선언에서 분리한 할당

var a, b; [a, b] = [1, 2];

console.log(a); // 1

console.log(b); // 2

 

기본값 추가하기

var a, b;

[a=5, b=7] = [1];

console.log(a); // 1

console.log(b); // 7

 

변수 값 교환하기

var a = 1; var b = 3; [a, b] = [b, a]; console.log(a); // 3 console.log(b); // 1

 

함수가 반환한 배열 분석

function f() { return [1, 2]; } var a, b; [a, b] = f(); console.log(a); // 1 console.log(b); // 2

 

일부 반환 값 무시하기

function f() {

    return [1, 2, 3];

}

 

var [a, , b] = f();

console.log(a); // 1

console.log(b); // 3

 

변수에 배열의 나머지를 할당하기

—> 배열을 구조 분해할 경우, 나머지 구문을 이용해 분해하고 남은 부분을 하나의 변수에 할당.

var [a, ...b] = [1, 2, 3]; console.log(a); // 1 console.log(b); // [2, 3]

Posted by sungho88
,