Working with result sets in Connector/Node.js

MySQL 8.0 and the Document Store highlight brand new client tools such as the Shell and Connector/Node.js, both providing a JavaScript implementation of the X DevAPI. Although, for most cases, the public interface tends to be similar on both (barring some small details), there is one notable exception.

The way you handle result sets in a Node.js app using Connector/Node.js is a bit different from the way you do it using an interactive session or a non-interactive JS script in the Shell.

Executing an operation in the MySQL server using the Shell is a purely synchronous task from the user standpoint since the underlying code is C++ (multi-threaded) whereas, due to the way Node.js works (single-threaded with event loop), an application that uses Connector/Node.js has to do the same by resorting to some asynchronous construct.

In this specific case, Connector/Node.js uses Promises. So, every time we call execute(), the method returns back a Promise object which will eventually resolve to a Result instance. Currently, for retrieval operations such as collection.find(), and session.sql(), and to avoid buffering data at the client layer, that object only contains details about the operation status (generated warnings, number of affected items, etc.) and not really the result set data. So, it is expected that, at the same time, the application also provides a callback as the argument of  execute() which will be called for each element of the result set, and can be used itself to buffer data in some sort of application-level data-structure. In a way, this is somewhat similar to how a Node.js streaming API would work.

This is always the case, no matter the operation boundaries that are additionally defined like the number of results that are picked by the result set (with limit()) or the number of results that are skipped in that same result set (with offset()).

The following Connector/Node.js examples use the async/await syntax, which is available since Node.js v7.6.0.

Document Store

Assume you have a collection mySchema.myCollection like the following:

Using the Shell, you can retrieve all the documents in the collection with:

On the other hand, to retrieve those documents using Connector/Node.js, you will have to resort to something like:

Additional details can be found in the documentation.

Relational Tables

The behavior is the same with relational tables, with the added bonus that you can also retrieve details about the metadata of each column that is included in the result set.

So, assuming you have a table mySchema.myTable like the following:

Using the Shell, you can retrieve all the rows in the table with:

Retrieving column metadata can be done with:

Whereas, using Connector/Node.js, the column metadata is retrieved via an additional callback.

Additional details can be found in the documentation.


The raw SQL API is mostly similar to the relational table CRUD API, with a notable caveat. If you execute a SQL statement that leads to multiple result sets, for instance, calling a PROCEDURE that runs multiple queries, you will need to keep track of new result sets by checking every time the metadata callback is invoked.

Given the same table as before, assuming there is a procedure proc defined like:

Using the Shell, the result sets returned by calling the procedure can be handled like:

Whereas with Connector/Node.js, the same thing can be achieved with:

Handling single result set queries works pretty much the same as in the relational table CRUD example. Using the Shell, it’s as simple as:

Using Connector/Node.js, you can do the same with:

Final Thoughts

In summary, these are probably the only scenarios where the API diverges between Connector/Node.js and the Shell, which speaks volumes about the efficiency of a standard like the X DevAPI.

We understand this way of handling data is not exactly a common Node.js asynchronous pattern, but we believe it is nonetheless effective to prevent unnecessary buffering at the client layer and at giving a lot more control to the application developer, particularly with regards to handling multiple result sets, at least until a proper streaming solution is provided. Whether you agree or not, please give us your feedback, as we are always looking for improvements to the API and the overall developer experience.

If you want to learn more about Connector/Node.js and the X DevAPI, please check the following:

Make sure you also join our community Slack and come hang around at the #connectors channel:

MySQL Community on Slack