Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const faker = require('faker');
- const mysql = require('mysql');
- const connection = mysql.createConnection({
- host : 'localhost',
- user : 'root',
- database : 'db_books'
- });
- const firstName = 'Bill';
- const lastName = 'Reinger';
- connection.connect();
- function getQuery(sql, params){
- return new Promise((resolve, reject) => {
- connection.query(sql,params,function (error, results, fields){
- if (error) {
- reject(error);
- }
- resolve(results);
- });
- });
- }
- let sql = 'SELECT * FROM `authors` WHERE first_name=? and last_name=? limit 1;';
- getQuery(sql,[firstName, lastName])
- .then(authors=>{
- sql = 'SELECT * FROM `books_authors` WHERE author_id=?;';
- return getQuery(sql,[authors[0].id]);
- })
- .then(books_authors=>{
- sql = 'SELECT * FROM `books` WHERE id IN (?);';
- const args = books_authors.map(b_a => b_a.book_id);
- return getQuery(sql,[args]);
- })
- .then(books => {
- console.log(books);
- })
- .catch(error => {
- console.log(error);
- })
- .finally(()=>{
- connection.end();
- });
- // let sql = 'SELECT * FROM `authors` WHERE first_name=? and last_name=? limit 1;';
- // connection.query(sql,[firstName, lastName], function (error, results, fields) {
- // if (error) throw error;
- // let authorId = results[0].id;
- // sql = 'SELECT * FROM `books_authors` WHERE author_id=?;';
- // connection.query(sql,[authorId], function (error, books_authors, fields) {
- // console.log(books_authors);
- // sql = 'SELECT * FROM `books` WHERE id IN (?);';
- // const args = books_authors.map(b_a => b_a.book_id);
- // connection.query(sql,[args],function (error, books, fields) {
- // if (error) throw error;
- // console.log(books);
- // connection.end();
- // });
- // });
- //
- //
- // console.log(authorId);
- // });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement