title: Doubts with mysql, typescript and nodejs
body: <p>I'm new in Typescript and NodeJS.</p>
  <p>I'm trying to solve a problem using mysql and nodejs with typescript, but I'm having some difficulties.
  Basically what my program is going to do is receive data through a POST request, and with that data execute two queries in the database.
  Explaining better, the data that will come in this request, is the username and the query that he wants to launch in the database. In case of success (query executed correctly and does not contain an error), the database will make a successful insert in a table and in case of failure another insert will occur, but with failure in the same table.</p>
  <p>The problem I am having is that I want to return a response to the user in text format, in case of error or success. But I'm not sure how to return this to the user.</p>
  <p>My request code calls the function that executes the received query (string) and makes the necessary inserts:</p>
  <pre><code>import express from 'express';
  import mysql from 'mysql';
  import cors from 'cors';
  import config from './config/db';
  import test from './services/test';
  // import routes from './routes';
  
  const app = express();
  app.use(express.json());
  app.use(cors());
  
  app.get('/', (_, res) =&gt; {
    return res.json({ message: 'Hello World' });
  });
  
  app.post('/query', (req, res) =&gt; {
    const { author, query } = req.body;
  
    try {
      if (author &amp;&amp; query) {
        const connection = mysql.createConnection(config);
  
        const result = test({ author, query }, connection);
  
        console.log('Result endpoint is: ', result);
        return res.json(result);
      }
      return res.status(400).json({ error: 'Parameters wrong' });
    } catch (err) {
      return res.status(400).json({ error: err.message });
    }
  });
  
  app.listen(3333, () =&gt; {
    console.log(' Server runing');
  });
  
  </code></pre>
  <p>And the code of my function that executes the query and the inserts</p>
  <pre><code>import mysql from 'mysql';
  
  interface IQuery {
    author: string;
    query: string;
  }
  
  function insertQuery(
    { author, query }: IQuery,
    error: number,
    conn: mysql.Connection,
  ): void {
    conn.query(
      'INSERT INTO query_tracker (name, query, error) VALUES (?, ?, ?)',
      [author, query, error],
    );
  }
  
  function executeQuery(
    { author, query }: IQuery,
    conn: mysql.Connection,
  ): string {
    // create connection with DB
    console.log(' DB Connected');
  
    // execute statement from post
    conn.query(query, (err, results) =&gt; {
      if (err) {
        insertQuery({ author, query }, 0, conn);
        return err.sqlMessage;
        // eslint-disable-next-line no-else-return
      } else {
        insertQuery({ author, query }, 1, conn);
        return results.message;
      }
    });
  }
  
  export default executeQuery;
  </code></pre>