Check mysql tabel for new data nodejs

Here is an example of how you can check a MySQL table for new data using Node.js:

const mysql = require('mysql');

// Create a connection to the database
const db = mysql.createConnection({
  host: 'your_host',
  user: 'your_user',
  password: 'your_password',
  database: 'your_database'
});

// Connect to the database
db.connect((err) => {
  if (err) {
    console.error('error connecting:', err);
    return;
  }
  console.log('connected as id ' + db.threadId);

  // Query the database to get the latest data
  db.query('SELECT * FROM your_table ORDER BY id DESC LIMIT 1', (err, results) => {
    if (err) {
      console.error('error running query:', err);
      return;
    }
    const latestData = results[0];

    // Check if there is new data
    db.query('SELECT * FROM your_table WHERE id >?', latestData.id, (err, results) => {
      if (err) {
        console.error('error running query:', err);
        return;
      }
      const newData = results;

      // Process the new data
      console.log('New data:', newData);

      // Close the connection
      db.end();
    });
  });
});

This code creates a connection to a MySQL database using the mysql package, and then queries the database to get the latest data from a table. It then checks if there is new data by querying the database again with a condition that filters out any rows with an id less than or equal to the latest id. If there is new data, it is processed and logged to the console.

You can also use mysql-promise package which is a promise-based wrapper around the mysql package, it makes it easier to work with mysql in node.js.

const mysql = require('mysql-promise');

// Create a connection to the database
const db = mysql.create({ 
  host: 'your_host', 
  user: 'your_user', 
  password: 'your_password', 
  database: 'your_database' 
});

// Query the database to get the latest data
db.query('SELECT * FROM your_table ORDER BY id DESC LIMIT 1')
 .then((results) => {
    const latestData = results[0];

    // Check if there is new data
    db.query('SELECT * FROM your_table WHERE id >?', latestData.id)
     .then((results) => {
        const newData = results;

        // Process the new data
        console.log('New data:', newData);

        // Close the connection
        db.end();
      })
     .catch((err) => {
        console.error('error running query:', err);
      });
  })
 .catch((err) => {
    console.error('error running query:', err);
  });

You can also use async/await syntax to make the code look more synchronous

async function checkForNewData() {
  const db = mysql.create({ 
    host: 'your_host', 
    user: 'your_user', 
    password: 'your_password', 
    database: 'your_database' 
  });

  const latestData = await db.query('SELECT * FROM your_table ORDER BY id DESC LIMIT 1');
  const newData = await db.query('SELECT * FROM your_table WHERE id >?', latestData[0].id);

  console.log('New data:', newData);

  db.end();
}

checkForNewData();

Please note that you should replace your_host, your_user, your_password, your_database and your_table with your actual database credentials and table name.