Documentation Home
X DevAPI User Guide for MySQL Shell in JavaScript Mode
Download this Manual
PDF (US Ltr) - 1.2Mb
PDF (A4) - 1.2Mb

2.3 Working with a Session Object

All previous examples used the getSchema() or getDefaultSchema() methods of the Session object, which return a Schema object. You use this Schema object to access Collections and Tables. Most examples make use of the X DevAPI ability to chain all object constructions, enabling you to get to the Schema object in one line. For example:

schema = mysqlx.getSession(...).getSchema();

This object chain is equivalent to the following, with the difference that the intermediate step is omitted:

session = mysqlx.getSession(); 
schema = session.getSchema().

There is no requirement to always chain calls until you get a Schema object, neither is it always what you want. If you want to work with the Session object, for example, to call the Session object method getSchemas(), there is no need to navigate down to the Schema. For example:

session = mysqlx.getSession(); session.getSchemas().

In this example the mysqlx.getSession() function is used to open a Session. Then the Session.getSchemas() function is used to get a list of all available schemas and print them to the console.

// Connecting to MySQL and working with a Session
var mysqlx = require('mysqlx');

// Connect to a dedicated MySQL server using a connection URI
var mySession = mysqlx.getSession('user:password@localhost');

// Get a list of all available schemas
var schemaList = mySession.getSchemas();

print('Available schemas in this session:\n');

// Loop over all available schemas and print their name
for (index in schemaList) {
  print(schemaList[index].name + '\n');