6.2.4 Example MySQL Shell Extension Objects

Example 6.1 Creating and Registering Extension Objects - Python

This example creates a function hello_world()
which is made available through the user-defined MySQL Shell
global object demo. The code creates a new
extension object and adds the hello_world()
function to it as a member, then registers the extension object
as the MySQL Shell global object demo.

Note that the member name is specified in camel case in the
shell.add_extension_object_member() function.
When you call the member in Python mode, use snake case for the
member name, and MySQL Shell automatically handles the
conversion. In JavaScript mode, the function is called like
this:

mysql-js> demo.helloWorld()

In Python mode, the function is called like this:

mysql-py> demo.hello_world()

Example 6.2 Creating and Registering Extension Objects - JavaScript

This example creates an extension object with the function
listTables() as a member, and registers it
directly as the MySQL Shell global object
tools:

// Define a listTables function that will be exposed by the global object tools
function listTables(session, schemaName, options) {
...
}
// Create an extension object and add the listTables function to it as a member
var object = shell.createExtensionObject()
shell.addExtensionObjectMember(object, "listTables", listTables,
{
brief:"Retrieves the tables from a given schema.",
details: ["Retrieves the tables of the schema named schemaName.",
"If excludeCollections is true, the collection tables will not be returned"],
parameters:
[
{
name: "session",
type: "object",
class: "Session",
brief: "An X Protocol session object."
},
{
name: "schemaName",
type: "string",
brief: "The name of the schema from which the table list will be pulled."
},
{
name: "options",
type: "dictionary",
brief: "Additional options that affect the function behavior.",
options: [
{
name: "excludeViews",
type: "bool",
brief: "If set to true, the views will not be included on the list, default is false",
},
{
name: "excludeCollections",
type: "bool",
brief: "If set to true, the collections will not be included on the list, default is false",
}
]
},
]
});
// Register the extension object as the global object "tools"
shell.registerGlobal("tools", object, {brief:"Global object for ExampleCom administrator tools",
details:[
"Global object to access homegrown ExampleCom administrator tools.",
"Add new tools to this global object as members with shell.addExtensionObjectMember()."]})