MySQL Blog Archive
For the latest blogs go to blogs.oracle.com/mysql
MySQL Connector/Node.JS 1.0.4 now on npmjs.com

MySQL Connector/Node.JS 1.0.4 was recently released as a development milestone release. This is the first version available via npmjs.com. npmjs.com is the central registry for Node.JS packages. This and potential future official MySQL packages can be found using the @mysql organisation. For this to work we had to change the package name to contain that prefix and have chosen the name @mysql/xdevapi as this package provides the implementation of our X DevAPI for Node.JS and we have a free namespace for potential future libraries as part of our Connector/Node.JS product.

61

Given an existing Node.JS project we can easily install the library:

$ npm install --save @mysql/xdevapi
myapp@1.0.0 /home/johannes/experiments/myapp
└── @mysql/xdevapi@1.0.4

As presented with previous releases the implementation is built around Promises. After the installation it’s easy to ask for a session:

const xdevapi = require('@mysql/xdevapi');
const promise = xdevapi.getSession({
    host: 'localhost',
    port: 33060,
    dbUser: 'username',
    dbPassword: 'veryverysecret'
});

If the MySQL Server is running and the X Plugin was loaded this Promise object will resolve to a session, else it will be rejected.

As a new feature in 1.0.4 the connection parameters can also be provided in form of an URL. As the URL is defined in the X DevAPI it can be shared among applications using different languages and MySQL Connectors:

const xdevapi = require('@mysql/xdevapi');
const promise = xdevapi.getSession('mysqlx://username:veryverysecret@hostname');

Another new feature in MySQL Connector/Node.JS is support for view DDL operations. To create a view a CRUD select object can be used:

const myschema = session.getSchema('myschema');
const table = myschema.getTable('a_table');
const selection = table.select(['id', 'CONCAT(field1, field2)']).where('1 = 2');
const viewDefinition = myschema.createView('a_view');
const promise = viewDefinition.definedAs(selection).security(viewDefinition.INVOKER).execute();

In case the view isn’t needed anymore it can of course be dropped again:

const promise = session.getSchema('myschema').dropView('a_view');

For information purpose another addition are functions to gather row or document counts from tables or collections:

const promise1 = myschema.getTable('a_table').count();
const promise2 = myschema.getCollection('a_collection').count();

For the future there’s more to come. We’re working on different improvements to the API and performance. More on this in a later post.