Ad-hoc MLE JavaScript in Oracle Database 23c: Database Actions

Occasionally it makes sense to run MLE JavaScript in an ad-hoc fashion. I typically do that to test something before wrapping the JavaScript code into a module. There are many ways to achieve this goal:

This post explains how to use Database Actions to write ad-hoc MLE JavaScript snippets.

Prerequisites

The techniques described in this article require an Oracle Database 23c instance and Database Actions. The easiest way to get to a working setup might be the use of containers. You can grab both an Oracle Database 23c Free container as well as ORDS (needed for Database Actions) from https://container-registry.oracle.com. Either click on the database tile or use the shortcuts for the respective latest versions:

The following assumes that you successfully instantiated the containers and completed the installation of ORDS in FREEPDB1. You also need an ORDS-enabled account (mine is called EMILY). The account must be granted execute dynamic MLE system privilege, execute on SYS.JAVASCRIPT and create MLE for use with modules. If these don’t sound familiar please have a look at the JavaScript Developer’s Guide, chapter 9. Any additional grants not affecting MLE need to be provided as well.

Writing ad-hoc JavaScript using Database Actions

Once you enabled your schema for use with Database Actions (see enable schema in the ORDS package), log in with your account. At the time of writing the following versions were the most current ones:

  • Oracle Database Free 23.3.0
  • ORDS 23.4.0

From the launchpad, shown above, click on the MLE JS tile to enter the JavaScript editor. Instead of the default module editor, switch to the Snippet tab (at the top of the window).

Broadly speaking there are two different scenarios:

  • You want to write some JavaScript code without referring to any module other than those built-in
  • You want to invoke functions exported from a MLE module

Let’s look at these in more detail.

Calling built-in modules and ad-hoc scripting

The global scope contains a number of objects that have been placed there for your convenience. Interacting with the SODA API or database driver for example doesn’t require you to import anything, for example:

The green Play button executes the code snippet. The Output pane at the bottom shows both the results of the calls to console.log() as well as the time it took to execute the snippet.

Calling functionality exported by your MLE modules

If you would like to invoke functionality exported by MLE modules stored in your schema, the example requires a little modification. Let’s assume you want to create faker-js/faker in your schema as FAKERJS_MODULE. In that case you’d ensure that using the module is compliant with your company’s policy and the project’s license, and only after confirming that it is safe to use fakerjs, you proceed.

Once everyone signed off on the idea of using a third-party, open-source module in your application you can download the ESM (ECMA-Script) version of the module. I used version 8.4.1 from jsdelivr. Rather than staging the file on the database server I can import it directly into Database Actions. Switch back to the (Module) Editor and click on the little globe icon, enter the module’s URL and load the module:

A few moments later the module will be avaialble for use. Just like in all previous examples it is necessary to create a MLE environment. You can create the environment by switching from Modules to Environments in the tree view to the left, then click the ... next to the search and create the environment. Change the name first, then, from the list of available modules, select FAKERJS_MODULE and click on the > to move it to the right. If you like, click the pencil icon next to the import name and change the import name. In this example the import name has been defined as fakerjs.

Make sure you create the environment by clicking on the Create button at the bottom of the wizard. Back in the Snippets tab you can start writing some code using the API provided by fakerjs. This little snippet has been used in previous examples:

(async() => {
    const { faker } = await import ("fakerjs");

    console.log(
        faker.lorem.paragraphs(5)
    )
})();

Note that just as with node’s Read-Eval-Print-Loop (REPL) server, you need to use dynamic import statements in the code.

Before you can hit the green Play button, you need to assign the FAKERJS_ENV to snippet’s execution. Select it from the Environment drop down. Should you not see the environment yet, click on the refresh button right next to the drop-down menu, which should make the environment appear. Once the environment is selected you can enjoy some beautiful text placeholders :)

That’s it; Happy Testing!