MySQL Shell 9.1  /  ...  /  Example MySQL Shell Extension Objects

10.2.4 Example MySQL Shell Extension Objects

Example 10.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.

# Define a hello_world function that will be exposed by the global object 'demo'
def hello_world():
    print("Hello world!")

# Create an extension object where the hello_world function will be registered
plugin_obj = shell.create_extension_object()

shell.add_extension_object_member(plugin_obj, "helloWorld", hello_world,
                                   {"brief": "Prints 'Hello world!'", "parameters": []})

# Registering the 'demo' global object
shell.register_global("demo", plugin_obj, 
                        {"brief": "A demo plugin that showcases MySQL Shell's plugin feature."})

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 10.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()."]})

In JavaScript mode, the function is called like this:

mysql-js> tools.listTables(session, "world_x", {excludeViews: true})

In Python mode, the function is called like this:

mysql-py> tools.list_tables(session, "world_x", {"excludeViews": True})