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

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:

  • using DBMS_MLE directly
  • using APEX’s SQL Workshop
  • using Database Actions

This post demonstrates how to use APEX’s SQL Workshop.

Prerequisites

You need an APEX environment connecting to Oracle Database 23c Free. If you don’t have one yet, you might find the following post useful setting one up with very little effort: Fast-path to developing with Oracle APEX and Multilingual Engine. Since this is my lab environment I kept everything as simple as possible, something you wouldn’t do at work. In other words, don’t be surprised if you don’t see TLS encryption in the browser’s address bar.

Writing ad-hoc JavaScript in APEX

With your APEX environment available let’s dive into writing some MLE/JavaScript code. Broadly speaking there are two different scenarios:

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

Calling built-in modules and ad-hoc scripting

If you want to use the built-ins and do some calculations all you need to do is head over to SQL Workshop > SQL Commands and selecting JavaScript (MLE) from the language drop down. Then it’s up to you to code to your heart’s delight.

This is literally it.

Calling functionality exported by other MLE modules

If you look at the screenshot above you might notice an Environment drop-down. Assuming you want to call functionality from one of your own/a third party MLE module stored in the database, you will need to make use of that.

Begin by storing your MLE module in the database using the Object Browser. For the purpose of this demo I’ll use faker-js/faker 8.4.1. Don’t forget to grab the ESM (ECMAScript) version of the module, for example from cdn.jsdelivr.net.

If you want to follow the example 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

With the module saved, create a new MLE environment fakerjs_env mapping the import name fakerjs to the newly created fakerjs_module. Once that’s completed you can call any of faker’s functions.

Switch back to SQL Commands, then use JavaScript (MLE) from the language drop down and select fakerjs_env as the Environment. As with all ad-hoc JavaScript calling custom MLE modules you have to resort to dynamic imports, just as node’s REPL server:

> import oracledb from "oracledb";
import oracledb from "oracledb";
^^^^^^

Uncaught:
SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use 
dynamic import: const { default: oracledb } = await import("oracledb");

The code snippet therefore becomes this:

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

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

You can see it in action in the following screenshot:

It’s as simple as that! Happy coding!

Blog at WordPress.com.