MySQL Shell 8.4  /  ...  /  Creating User-Defined MySQL Shell Global Objects

10.2.1 Creating User-Defined MySQL Shell Global Objects

To create a new MySQL Shell global object to act as an entry point for your extension objects, first create a new top-level extension object using the built-in shell.createExtensionObject() function in JavaScript or shell.create_extension_object() in Python:

shell.createExtensionObject()

Then register this top-level extension object as a MySQL Shell global object by calling the shell.registerGlobal() method in JavaScript or shell.register_global() in Python. The syntax for the method is as follows:

shell.registerGlobal(name, object[, definition])

Where:

  • name is a string giving the name (and class) of the global object. The name must be a valid scripting identifier, so the first character must be a letter or underscore character, followed by any number of letters, numbers, or underscore characters. The name must be unique in your MySQL Shell installation, so it must not be the name of a built-in MySQL Shell global object (for example, db, dba, cluster, session, shell, util) and it must not be a name you have already used for a user-defined MySQL Shell global object. The examples below show how to check whether the name already exists before registering the global object.

    Important

    The name that you use to register the global object is used as-is when you access the object in both JavaScript and Python modes. It is therefore good practice to use a simple one-word name for the global object (for example, ext). If you register the global object with a complex name in camel case or snake case (for example, myCustomObject), when you use the global object, you must specify the name as it was registered. Only the names used for members are handled in a language-appropriate way.

  • object is the extension object that you are registering as a MySQL Shell global object. You can only register an extension object once.

  • definition is an optional dictionary with help information for the global object that is provided in the MySQL Shell help system. The dictionary contains the following keys:

    • brief (string, optional): A short description of the global object to be provided as help information.

    • details (list of strings, optional): A detailed description of the global object to be provided as help information.