제대로����������� ������������������ 배우는����������� ������������������ Node.js����������� ������������������ 프로그래밍����������� ������������������ Ch.11����������� ������������������
Node����������� ������������������ 관계형����������� ������������������ ����������� ������������������ 데이터����������� ������������������ 베이스����������� ������������������ 바인딩
아꿈사����������� ������������������ Cecil
MySQL을����������� ������������������ 통합하기����������� ������������������ 위한����������� ������������������ 두가지����������� ������������������ 접근법
• JavaScript����������� ������������������ MySQL����������� ������������������ 클라이언트����������� ������������������ mysql(node-mysql)����������� ������������������
• Node.js의����������� ������������������ 관계형����������� ������������������ 데이터����������� ������������������ 베이스����������� ������������������ 연결을����������� ������������������ 위한����������� ������������������ 공통����������� ������������������ 프레임워크����������� ������������������ node-db(db-mysql)
예제에����������� ������������������ 사용할����������� ������������������ 기본����������� ������������������ 테이블����������� ������������������ 구조
• id����������� ������������������ -����������� ������������������ int(11),����������� ������������������ primary����������� ������������������ key,����������� ������������������ not����������� ������������������ null,����������� ������������������ auto����������� ������������������ increment����������� ������������������
• title����������� ������������������ -����������� ������������������ varchar(255),����������� ������������������ unique����������� ������������������ key,����������� ������������������ not����������� ������������������ null����������� ������������������
• text����������� ������������������ -����������� ������������������ text,����������� ������������������ nulls����������� ������������������ allowed����������� ������������������
• created����������� ������������������ -����������� ������������������ date����������� ������������������ time,����������� ������������������ nulls����������� ������������������ allowed
db-mysql• 설치����������� ������������������
• npm����������� ������������������ install����������� ������������������ db-mysql����������� ������������������ • 네이티브����������� ������������������ 모듈로����������� ������������������ MySQL����������� ������������������ 클라이언트����������� ������������������ 라이브러리가����������� ������������������ 설치되어����������� ������������������ 있어야����������� ������������������ 함����������� ������������������
• DB����������� ������������������ 연결����������� ������������������ ����������� ������������������ ����������� ������������������ var����������� ������������������ db����������� ������������������ =����������� ������������������ new����������� ������������������ mysql.Database({����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ hostname:����������� ������������������ ‘localhost’,����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ user:����������� ������������������ ‘username’,����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ password:����������� ������������������ ‘userpass’,����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ database:����������� ������������������ ‘databasenm});����������� ������������������
• Query����������� ������������������ • 체인화된����������� ������������������ 메서드����������� ������������������ 사용����������� ������������������ • 쿼리����������� ������������������ 문자열을����������� ������������������ 이용한����������� ������������������ 직접����������� ������������������ Query
체인화된����������� ������������������ 메서드
db.on('ready',����������� ������������������ function(server)����������� ������������������ {����������� ������������������
����������� ������������������ //����������� ������������������ 체인화된����������� ������������������ 메서드와����������� ������������������ 중첩����������� ������������������ 콜백을����������� ������������������ 사용하여����������� ������������������ 쿼리����������� ������������������ ����������� ������������������
����������� ������������������ this.query()����������� ������������������
����������� ������������������ ����������� ������������������ .select('*')����������� ������������������ ����������� ������������������ .from('nodetest2')����������� ������������������ ����������� ������������������ .where('id����������� ������������������ =����������� ������������������ 1').execute(function(error,����������� ������������������ rows,����������� ������������������ columns)����������� ������������������ {����������� ������������������
����������� ������������������ ����������� ������������������ ����������� ������������������ if����������� ������������������ (error)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ return����������� ������������������ console.log('ERROR:����������� ������������������ '����������� ������������������ +����������� ������������������ error);����������� ������������������
����������� ������������������ ����������� ������������������ ����������� ������������������ }����������� ������������������ console.log(rows);����������� ������������������ ����������� ������������������
����������� ������������������ console.log(columns);����������� ������������������
});
rows:����������� ������������������ 각����������� ������������������ Row에����������� ������������������ 대한����������� ������������������ 이름/값����������� ������������������ 쌍����������� ������������������ columns:����������� ������������������ 각����������� ������������������ Column에����������� ������������������ 대한����������� ������������������ 이름과����������� ������������������ Type
쿼리����������� ������������������ 문자열을����������� ������������������ 이용한����������� ������������������ 직접����������� ������������������ Query����������� ������������������
db.on('ready',����������� ������������������ function(server)����������� ������������������ {����������� ������������������
����������� ������������������ //����������� ������������������ 직접����������� ������������������ 쿼리����������� ������������������ 문자열과����������� ������������������ 이벤트를����������� ������������������ 사용하여����������� ������������������ 쿼리����������� ������������������
����������� ������������������ var����������� ������������������ qry����������� ������������������ =����������� ������������������ this.query();����������� ������������������
����������� ������������������ qry.execute('select����������� ������������������ *����������� ������������������ from����������� ������������������ nodetest2����������� ������������������ where����������� ������������������ id����������� ������������������ =����������� ������������������ 1');����������� ������������������
����������� ������������������ qry.on('success',����������� ������������������ function(rows,����������� ������������������ columns)����������� ������������������ {����������� ������������������ ����������� ������������������
����������� ������������������ ����������� ������������������ console.log(rows);����������� ������������������ //����������� ������������������ print����������� ������������������ out����������� ������������������ ����������� ������������������ returned����������� ������������������ rows����������� ������������������ ����������� ������������������
����������� ������������������ ����������� ������������������ console.log(columns);����������� ������������������ //����������� ������������������ print����������� ������������������ out����������� ������������������ returns����������� ������������������ columns����������� ������������������
����������� ������������������ });����������� ������������������ qry.on('error',����������� ������������������ function(error)����������� ������������������ {����������� ������������������
����������� ������������������ ����������� ������������������ console.log('Error:����������� ������������������ '����������� ������������������ +����������� ������������������ error);����������� ������������������ ����������� ������������������
����������� ������������������ });����������� ������������������
});
플레이스����������� ������������������ 홀더의����������� ������������������ 사용
qry.execute('update����������� ������������������ nodetest2����������� ������������������ set����������� ������������������ title����������� ������������������ =����������� ������������������ ?����������� ������������������ where����������� ������������������ id����������� ������������������ =����������� ������������������ ?',����������� ������������������ ����������� ������������������
["This����������� ������������������ was����������� ������������������ a����������� ������������������ better����������� ������������������ title",����������� ������������������ 1]);����������� ������������������
!
• 플레이스����������� ������������������ 홀더를����������� ������������������ 직접����������� ������������������ 쿼리����������� ������������������ 문자열뿐만����������� ������������������ 아니라����������� ������������������ 체인����������� ������������������ 메서드에서도����������� ������������������ 사용될����������� ������������������ 수����������� ������������������ 있음.����������� ������������������
• MySQL����������� ������������������ 함수를����������� ������������������ 사용할����������� ������������������ 때는����������� ������������������ 쿼리����������� ������������������ 문자열에����������� ������������������ 직접����������� ������������������ 넣어야����������� ������������������ 함.
직접����������� ������������������ Query로����������� ������������������ 데이터����������� ������������������ 베이스����������� ������������������ 업데이트
db.on('ready',����������� ������������������ function(server)����������� ������������������ {����������� ������������������
����������� ������������������ //����������� ������������������ 직접����������� ������������������ 문자열과����������� ������������������ 이벤트를����������� ������������������ 사용하여����������� ������������������ 쿼리����������� ������������������
����������� ������������������ var����������� ������������������ qry����������� ������������������ =����������� ������������������ this.query();����������� ������������������
����������� ������������������ qry.execute('insert����������� ������������������ into����������� ������������������ nodetest2����������� ������������������ (title,����������� ������������������ text,created)����������� ������������������ values(?,?,NOW())',����������� ������������������ ����������� ������������������
����������� ������������������ ����������� ������������������ ����������� ������������������ ['Third����������� ������������������ Entry','Third����������� ������������������ entry����������� ������������������ in����������� ������������������ series']);����������� ������������������
����������� ������������������ qry.on('success',����������� ������������������ function(result)����������� ������������������ {����������� ������������������ ����������� ������������������
����������� ������������������ ����������� ������������������ console.log(result);����������� ������������������
����������� ������������������ });����������� ������������������
����������� ������������������ qry.on('error',����������� ������������������ function(error)����������� ������������������ {����������� ������������������ ����������� ������������������
����������� ������������������ ����������� ������������������ console.log('Error:����������� ������������������ '����������� ������������������ +����������� ������������������ error);����������� ������������������
����������� ������������������ });����������� ������������������ ����������� ������������������
});
{����������� ������������������ id:����������� ������������������ 3,����������� ������������������ affected:����������� ������������������ 1,����������� ������������������ warning:����������� ������������������ 0����������� ������������������ }
체인����������� ������������������ 메서드로����������� ������������������ 데이터����������� ������������������ 베이스����������� ������������������ 업데이트db.on('ready',����������� ������������������ function(server)����������� ������������������ {����������� ������������������ ����������� ������������������ var����������� ������������������ qry����������� ������������������ =����������� ������������������ this.query();����������� ������������������ ����������� ������������������ ����������� ������������������ qry.insert(‘nodetest2',['title','text','created'],����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ['Fourth����������� ������������������ Entry',����������� ������������������ 'Fourth����������� ������������������ entry����������� ������������������ in����������� ������������������ series',����������� ������������������ 'NOW()']).execute(function(err,result)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ if����������� ������������������ (err)����������� ������������������ {����������� ������������������ …⋯����������� ������������������ }����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ else����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(result);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ var����������� ������������������ qry2����������� ������������������ =����������� ������������������ db.query();����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ qry2.update('nodetest2')����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ .set({title:����������� ������������������ 'Better����������� ������������������ title'}).where('id����������� ������������������ =����������� ������������������ ?',[4]).execute(function(err,����������� ������������������ result)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ if(err)����������� ������������������ {����������� ������������������ …⋯����������� ������������������ }����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ else����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(result);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ });����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }����������� ������������������ ����������� ������������������ ����������� ������������������ });����������� ������������������ });
node-mysql
• 설치����������� ������������������
• npm����������� ������������������ install����������� ������������������ mysql����������� ������������������
• MySQL����������� ������������������ 클라이언트����������� ������������������ 소프트웨어가����������� ������������������ 필요����������� ������������������ 없음.����������� ������������������
• DB����������� ������������������ 연결����������� ������������������
����������� ������������������ ����������� ������������������ var����������� ������������������ client����������� ������������������ =����������� ������������������ mysql.createClient({����������� ������������������ user:����������� ������������������ ‘username',����������� ������������������ ����������� ������������������
����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ password:����������� ������������������ 'password'}); ����������� ������������������ ����������� ������������������ client.query('USE����������� ������������������ nodetest2');
node-mysql을����������� ������������������ 사용한����������� ������������������ CRUDclient.query('INSERT����������� ������������������ INTO����������� ������������������ nodetest2����������� ������������������ '����������� ������������������ +����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ 'SET����������� ������������������ title����������� ������������������ =����������� ������������������ ?,����������� ������������������ text����������� ������������������ =����������� ������������������ ?,����������� ������������������ created����������� ������������������ =����������� ������������������ NOW()’,����������� ������������������ ����������� ������������������ ����������� ������������������ ['A����������� ������������������ seventh����������� ������������������ item',����������� ������������������ 'This����������� ������������������ is����������� ������������������ a����������� ������������������ seventh����������� ������������������ item'],����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ function(err,����������� ������������������ result)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ if����������� ������������������ (err)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(err);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }����������� ������������������ else����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ var����������� ������������������ id����������� ������������������ =����������� ������������������ result.insertId;����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(result.insertId);����������� ������������������ ����������� ������������������ //����������� ������������������ update ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ client.query('UPDATE����������� ������������������ nodetest2����������� ������������������ SET����������� ������������������ '����������� ������������������ +����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ 'title����������� ������������������ =����������� ������������������ ?����������� ������������������ WHERE����������� ������������������ ID����������� ������������������ =����������� ������������������ ?',����������� ������������������ ['New����������� ������������������ title',����������� ������������������ id],����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ function����������� ������������������ (err,����������� ������������������ result)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ if����������� ������������������ (err)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(err);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }����������� ������������������ else����������� ������������������ {
����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(result.affectedRows);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ //����������� ������������������ delete����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ client.query('DELETE����������� ������������������ FROM����������� ������������������ nodetest2����������� ������������������ WHERE����������� ������������������ id����������� ������������������ =����������� ������������������ ?',����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ [id],����������� ������������������ function(err,����������� ������������������ result)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ if(err)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(err);����������� ������������������ }else{����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(result.affectedRows);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ getData();����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }����������� ������������������ });����������� ������������������ }});����������� ������������������ }});����������� ������������������ function����������� ������������������ getData()����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ client.query('SELECT����������� ������������������ *����������� ������������������ FROM����������� ������������������ nodetest2����������� ������������������ ORDER����������� ������������������ BY����������� ������������������ id',����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ function(err,����������� ������������������ result,fields)����������� ������������������ {����������� ������������������ //����������� ������������������ fields는����������� ������������������ column����������� ������������������ 정보����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(result);����������� ������������������ console.log(fields);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ client.end();����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ });}
mysql-queues
•node-mysql����������� ������������������ 모듈가����������� ������������������ 연동됨.����������� ������������������
•중첩����������� ������������������ 쿼리를����������� ������������������ 실행할����������� ������������������ 필요����������� ������������������ 없이����������� ������������������ 다중����������� ������������������ 쿼리를����������� ������������������ 지원����������� ������������������
•트랜잭션����������� ������������������ 지원
다중����������� ������������������ 쿼리����������� ������������������ -����������� ������������������ mysql����������� ������������������ queuesqueues(client,����������� ������������������ true);����������� ������������������ //����������� ������������������ create����������� ������������������ queueq����������� ������������������ =����������� ������������������ client.createQueue();����������� ������������������ //����������� ������������������ do����������� ������������������ insertq.query('INSERT����������� ������������������ INTO����������� ������������������ nodetest2����������� ������������������ (title,����������� ������������������ text,����������� ������������������ created)����������� ������������������ '����������� ������������������ +’values(?,?,NOW())',����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ['Title����������� ������������������ for����������� ������������������ 8',����������� ������������������ 'Text����������� ������������������ for����������� ������������������ 8']);����������� ������������������ //����������� ������������������ updateq.query('UPDATE����������� ������������������ nodetest2����������� ������������������ SET����������� ������������������ title����������� ������������������ =����������� ������������������ ?����������� ������������������ WHERE����������� ������������������ title����������� ������������������ =����������� ������������������ ?’,����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ['New����������� ������������������ Title����������� ������������������ for����������� ������������������ 8','Title����������� ������������������ for����������� ������������������ 8']);����������� ������������������ ����������� ������������������ q.execute();����������� ������������������ //����������� ������������������ 이����������� ������������������ Select는����������� ������������������ 위의����������� ������������������ 쿼리가����������� ������������������ 다����������� ������������������ 처리되어야����������� ������������������ 실행됨,client.query('SELECT����������� ������������������ *����������� ������������������ FROM����������� ������������������ nodetest2����������� ������������������ ORDER����������� ������������������ BY����������� ������������������ ID',����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ function(err,����������� ������������������ result,����������� ������������������ fields)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ if����������� ������������������ (err)����������� ������������������ {����������� ������������������ console.log(err);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }else{����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ //����������� ������������������ should����������� ������������������ show����������� ������������������ all����������� ������������������ records,����������� ������������������ including����������� ������������������ newest����������� ������������������ console.log(result);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ client.end();����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }����������� ������������������ });
트랜잭션����������� ������������������ -����������� ������������������ mysql����������� ������������������ queuesqueues(client,����������� ������������������ true);����������� ������������������ //����������� ������������������ 트랜잭션����������� ������������������ 시작var����������� ������������������ trans����������� ������������������ =����������� ������������������ client.startTransaction();trans.query('INSERT����������� ������������������ INTO����������� ������������������ nodetest2����������� ������������������ (title,����������� ������������������ text,����������� ������������������ created)����������� ������������������ '����������� ������������������ +����������� ������������������ 'values(?,?,NOW())',����������� ������������������ ����������� ������������������ ['Title����������� ������������������ for����������� ������������������ 8',����������� ������������������ 'Text����������� ������������������ for����������� ������������������ 8'],����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ function(err,info)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ if����������� ������������������ (err)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ trans.rollback();����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }else{����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(info);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ trans.query('UPDATE����������� ������������������ nodetest2����������� ������������������ SET����������� ������������������ title����������� ������������������ =����������� ������������������ ?����������� ������������������ WHERE����������� ������������������ title����������� ������������������ =����������� ������������������ ?',����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ['Better����������� ������������������ Title����������� ������������������ for����������� ������������������ 8','Title����������� ������������������ for����������� ������������������ 8'],����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ function(err,info)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ if(err)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ trans.rollback();����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }else{����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ trans.commit();����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }});����������� ������������������ }});����������� ������������������ ����������� ������������������ trans.execute();����������� ������������������ //����������� ������������������ 트랙잭션이����������� ������������������ 완료될때까지����������� ������������������ 다음����������� ������������������ 쿼리를����������� ������������������ 실행이����������� ������������������ 안됨.
Sequelize
•node-mysql����������� ������������������ 모듈가����������� ������������������ 연동됨.����������� ������������������
•높은����������� ������������������ 수준의����������� ������������������ 추상화����������� ������������������ 제공.����������� ������������������ ORM(Object����������� ������������������ Relation����������� ������������������ Mapper)����������� ������������������
•모델����������� ������������������ 정의var����������� ������������������ Nodetest2����������� ������������������ =����������� ������������������ sequelize.define('nodetest2',����������� ������������������
{id����������� ������������������ :����������� ������������������ {type:����������� ������������������ Sequelize.INTEGER,����������� ������������������ primaryKey:����������� ������������������ true},title����������� ������������������ :����������� ������������������ {type:����������� ������������������ Sequelize.STRING,����������� ������������������ allowNull:����������� ������������������ false,����������� ������������������ unique:����������� ������������������ true},����������� ������������������ text����������� ������������������ :����������� ������������������ Sequelize.TEXT, created����������� ������������������ :����������� ������������������ Sequelize.DATE����������� ������������������ ����������� ������������������
});����������� ������������������ //����������� ������������������ 데이터����������� ������������������ 베이스에����������� ������������������ 적용.����������� ������������������ 테이블이����������� ������������������ 없는����������� ������������������ 경우����������� ������������������ 생성.����������� ������������������ Nodetest2.sync().error(function(err)����������� ������������������ {����������� ������������������
console.log(err);����������� ������������������ ����������� ������������������ });����������� ������������������
ORM����������� ������������������ 스타일의����������� ������������������ CRUD•데이터����������� ������������������ 베이스����������� ������������������ 쿼리가����������� ������������������ 아닌����������� ������������������ 객체의����������� ������������������ 핸들링
var����������� ������������������ test����������� ������������������ =����������� ������������������ Nodetest2.build(����������� ������������������ {����������� ������������������ title:����������� ������������������ 'New����������� ������������������ object',����������� ������������������ ����������� ������������������ text:����������� ������������������ 'Newest����������� ������������������ object����������� ������������������ in����������� ������������������ the����������� ������������������ data����������� ������������������ store’});����������� ������������������ ����������� ������������������ //����������� ������������������ 객체를����������� ������������������ 저장����������� ������������������ ����������� ������������������ test.save().success(����������� ������������������ ����������� ������������������ ����������� ������������������ function()����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ Nodetest2.find({where����������� ������������������ :����������� ������������������ {title:����������� ������������������ 'New����������� ������������������ object'}}).success(function(test)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ test.title����������� ������������������ =����������� ������������������ 'New����������� ������������������ object����������� ������������������ title';����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ test.save().error(function(err)����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ console.log(err);����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ });����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ test.save().success(function()����������� ������������������ {����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ …⋯����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ ����������� ������������������ }����������� ������������������ ����������� ������������������ …⋯
관계형에����������� ������������������ ORM으로
•ORM����������� ������������������ 자체����������� ������������������ 고유의����������� ������������������ 데이터����������� ������������������ 구조가����������� ������������������ 필요����������� ������������������
•ex)����������� ������������������ Widget����������� ������������������ 객체를����������� ������������������ 위한����������� ������������������ 테이블은����������� ������������������ widgets����������� ������������������ (복수형)����������� ������������������
•데이터����������� ������������������ 변경을����������� ������������������ 위한����������� ������������������ 추가����������� ������������������ 정보가����������� ������������������ 들어감����������� ������������������ ����������� ������������������
•기존의����������� ������������������ 데이터����������� ������������������ 구조에����������� ������������������ ORM을����������� ������������������ 적용할����������� ������������������ 때는����������� ������������������ 충분한����������� ������������������ 고려가����������� ������������������ 필요