Thanks to the awesome team developing new features for Oracle SQL Developer Command Line creating modules for Multilingual Engine (MLE) just got a lot easier. SQLcl 24.3.2 introduces the mle create-module command allowing you to create a MLE module from a file in the directory. I blogged about the feature in the past. SQLcl 24.4.1 raises the bar even further.
Background
A little while ago I wrote an article how to use GraphQL with MLE. The corresponding code can be found on my GitHub. Please have a look at the blog post if you like to learn more about the implementation details, although they don’t really matter much as I’m about to show you how to load MLE modules into the database that rely on a module bundler.
If you look closely at the files in the GitHub repository, you’ll notice a Rollup configuration file. As per its own documentation, Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application.
This is exactly what I need for my GraphQL code: I am combining the latest GraphQL reference implementation for JavaScript plus a few other tools installed locally into a single file, which is then loaded into the database. My own code’s entry point is going to be in graphQLQuery.js which allows me to run arbitrary GraphQL queries against the database.
Let’s look at the details, starting with the Rollup configuration file:
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
export default {
input: "src/graphQLQuery.js",
output: {
file: "dist/bundle.js",
format: "esm",
},
plugins: [nodeResolve(), commonjs()],
external: ["mle-js-oracledb", "oracledb"],
};
Additionally, I need to tell Rollup to use a couple of plugins – nodeResolve and commonjs it wouldn’t use by default to generate the output. I also tell it to exclude Oracle’s SQL driver, oracledb and the MLE type declarations.
The output file is generated in dist/bundle.js in ESM (ECMA Script Module) format, as required by MLE. Once generated, the module is loaded into the database using SQLcl’s mle create-module command.
SQLcl supports Rollup
Thankfully SQLcl now takes some of that work into its own hands by supporting Rollup out of the box. The latest version (24.4.1 at the time of writing) allows you use Rollup and optionally provide a Rollup configuration file. If the default Rollup options work for your code, you don’t need a config file; unfortunately, the defaults don’t work for me in this case. Don’t worry if you don’t recall all flags, simply use the tab-completion to list all the options:
SQL> version
Oracle SQLDeveloper Command-Line (SQLcl) version: 24.4.1.0 build: 24.4.1.042.1221
SQL> mle create-module [tab]
-bundler -filename -language -metafile -replace -verbose
-bundler-config -if-not-exists -metadata -module-name -schema -version
Let’s put this new feature to good use! After cloning my source code and changing into graphql-simplified I
- use
npm installto grab the libraries from NPM as per the project’spackage.json - use SQLcl to connect to the database schema I want to deploy the code to
- use the new
mle create-modulecommand to create the bundle and deploy it
SQL> mle create-module -bundler rollup -bundler-config rollup.config.mjs -filename src/graphQLQuery.js -module-name GRAPHQL_ENDPOINT_MODULE
MLE Module GRAPHQL_ENDPOINT_MODULE created
That’s it! There is no need to create bundle.js via npx rollup -c, SQLcl did all that work for you and loaded the bundle into the database. Now that the module has been loaded, use it as you would any other module.
What happens if Rollup is not installed?
If instead of the output above you received this …
SQL> mle create-module -bundler rollup -bundler-config rollup.config.mjs -filename src/graphQLQuery.js -module-name GRAPHQL_ENDPOINT_MODULE
Cannot run program "rollup": error=2, No such file or directory
… you probably didn’t have rollup in your path. There are many ways to fix this, from installing Rollup globally (generally not advisable) to writing a small shell script calling npx rollup in the project’s root directory and adding it to the path.
$ pwd
/home/martin/projects/javascript-blogposts/graphql-simplified
$ PATH=${PATH}:. /opt/oracle/sqlcl/bin/sql app_user@localhost/freepdb1
SQLcl: Release 24.4 Production on Thu Feb 13 09:27:03 2025
Copyright (c) 1982, 2025, Oracle. All rights reserved.
Password? (**********?) ***********
Connected to:
Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free
Version 23.6.0.24.10
SQL> mle create-module -bundler rollup -bundler-config rollup.config.mjs -filename src/graphQLQuery.js -module-name GRAPHQL_ENDPOINT_MODULE
MLE Module GRAPHQL_ENDPOINT_MODULE created
I don’t like the installation of tools globally, these setups tend to create a lot of problems. A local, project-only installation on the other hand has less potential to cause irk.
Testing
With the module created, it’s time to create the call specification, and use it. The GraphQL query relies on the LOCATIONS table, which is part of the Human Resources database sample schema.
create or replace function graphql_query(
p_query varchar2,
p_args json
) return json
as mle module GRAPHQL_ENDPOINT_MODULE
signature 'graphQLQuery';
/
select
json_serialize(
graphql_query(
'query locById($id: Int) { getLocationById(id: $id) { city country_id } }',
JSON('{id: 1000}')
)
pretty
) graphql_result
/
GRAPHQL_RESULT
--------------------------------------------------------------------------------
{
"data" :
{
"getLocationByID" :
{
"city" : "Roma",
"country_id" : "IT"
}
}
}
Summary
Over the past few releases, Oracle SQLcl has greatly improved support for loading MLE modules into the database. The ability to load MLE modules via a network connection is a game changer, especially for those of us who like to deploy (JavaScript) code via CI/CD pipelines. The addition of Rollup support is the icing on the cake.
Happy bundling!