TypeScript + promise-mysql の導入編
index:
概要:
前の TypeScript + node/express 関連で
promise-mysql でDB操作編となります
参考のコード
参考
https://github.com/CodeFoodPixels/node-promise-mysql
https://github.com/CodeFoodPixels/node-promise-mysql/blob/master/examples/connection/query.js
promise-mysql の追加
npm install promise-mysql --save npm install @types/mysql --save
mysql操作
・select ,insert など
https://github.com/kuc-arc-f/ts_mysql_1/blob/master/test.ts
/******************************** * *********************************/ function get_table(): void{ let connection :mysql.Connection; mysql.createConnection({ host: "localhost", user: "db_user", password: "password", database: "vue1" }) .then((conn) => { connection = conn; return connection.query('SELECT * FROM books'); }).then((results: any[]) => { // console.log(results ); results.forEach(result => { console.log(result); }); connection.end(); }); } /******************************** * *********************************/ function add_table(title: string): void{ let sql = ` INSERT INTO books (title ,createdAt ) VALUES ('${ title }', now() ) `; let connection :mysql.Connection; mysql.createConnection({ host: "localhost", user: "db_user", password: "password", database: "vue1" }) .then((conn) => { connection = conn; connection.query( sql ) connection.end(); }); }