Search code examples
javascriptnode.jsmysql-connectormysql-x-devapi

How to get raw SQL query from MySQL X DevAPI SQL CRUD functions?


I am using MySQL X DevAPI. That is working fine for relational tables.

// Working with Relational Tables
var mysqlx = require('@mysql/xdevapi');
var myTable;

// Connect to server using a connection URL
mysqlx
  .getSession({
    user: 'user',
    password: 'password',
    host: 'localhost',
    port: 33060
  })
  .then(function (session) {
    // Accessing an existing table
    myTable = session.getSchema('test').getTable('my_table');

    // Insert SQL Table data
    return myTable
      .insert(['name', 'birthday', 'age'])
      .values(['Laurie', '2000-5-27', 19])
      .execute()
  })
  .then(function () {
    // Find a row in the SQL Table
    return myTable
        .select(['_id', 'name', 'birthday'])
        .where('name like :name && age < :age)')
        .bind('name', 'L%')
        .bind('age', 30)
        .execute();
  })
  .then(function (myResult) {
    console.log(myResult.fetchAll());
  });

In the above code how can I see raw SQL query generated from .insert() and .select() CRUD functions which is finally getting executed?


Solution

  • When using the CRUD counterpart of the X DevAPI, the client neither generates nor sends SQL statements to the server. Instead it sends a Mysqlx.Crud.* protobuf message, which is converted to SQL in the server by the X Plugin.

    You can check the SQL being executed in the MySQL server by enabling the general log.

    Disclaimer: I'm the lead developer of the MySQL X DevAPI Connector for Node.js