Oracle Database 23ai running on Linux x86-64 and aarch64 allows you to write server-side JavaScript code, stored and executed directly within the database. Multilingual Engine (MLE), powered by GraalVM makes this possible. Developers can write their MLE modules in a way that is very similar to how PL/SQL developers write functions, procedures, and packages. It continues beyond there, though: it is possible to use 3rd party JavaScript modules from community repositories such as NPM, provided, of course, their licenses are compliant with your project and your organisation approves their use.
Intro and recap
Oracle Database 23ai introduced a couple of new Data Definition Language (DDL) statements to create MLE modules in the database. You use create MLE module for example to create a MLE module:
create MLE module my_module
language javascript as
import foo from 'bar';
export function baz() {
// imagine some code here :)
}
/
That approach has a small caveat: the mix of SQL and JavaScript makes it nearly impossible for any linter to work with the code. Unit testing is possible, but it might require additional work, including the use of SQL and/or PL/SQL—something not every JavaScript developer might be comfortable with.
New command to deploy MLE/JavaScript code
Wouldn’t it be nice if you could deploy a JavaScript file to the database without sandwiching the JavaScript code between DDL statements? Doing so would unlock
- linting
- local unit testing
- the use of formatting tools
- and many others
The latest Oracle SQL Developer Command Line (SQLcl) release – 24.3.2 – makes it very easy to deploy JavaScript files to the database. A couple of new commands have been introduced:
mle create-modulemle alter-module
You use the create-module command to load a JavaScript file into the database. The best thing about the command is that you don’t need to run SQLcl on the database server; all you need is a connection to the database. This makes the mle create-module ideally suited for Continuous Integration (CI) pipelines. Use alter-module to modify module properties.
Example
Here is an example of mle create-module in action; it picks up from an earlier post featuring the popular validator.js library. ‘validator.js’ has previously been featured in many demos and examples. This time, however, rather than using a directory (object) on the database server to store the JavaScript file and load it into the database as a BFILE, it will be loaded by SQLcl.
Start by downloading validator.js from your favourite CDN. Remember to pick the ECMA Script version and check license compatibility, security compliance, etc.
$ curl -Lo validator.js 'https://cdn.jsdelivr.net/npm/validator@13.12.0/+esm'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 120k 0 120k 0 0 1680k 0 --:--:-- --:--:-- --:--:-- 1695k
This doesn’t take long at all. Next, connect to your local development database and load the file into the database (mine is an Oracle Free instance running in a container):
$ ~/tools/sqlcl/bin/sql -V
SQLcl: Release 24.3.2.0 Production Build: 24.3.2.330.1718
$ ~/tools/sqlcl/bin/sql emily@localhost/freepdb1
SQLcl: Release 24.3 Production on Mon Dec 02 11:14:03 2024
Copyright (c) 1982, 2024, 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.5.0.24.07
SQL> mle create-module -language javascript -
2 -version '13.12.0' -
3 -filename /tmp/validator.js -
4 -module-name validator_module
MLE Module validator_module created
That’s it – the command created the module:
SQL> select
2 module_name,
3 version
4 from
5 user_mle_modules
6 where
7* module_name = 'VALIDATOR_MODULE';
MODULE_NAME VERSION
___________________ __________
VALIDATOR_MODULE 13.12.0
Summary
The ability to deploy MLE/JavaScript files via SQLcl is excellent news for all teams embracing Continuous Integration as part of their workflow. SQLcl is small and lightweight and can easily be integrated into a CI pipeline. Its ability to deploy JavaScript directly into the database is an excellent feature for JavaScript developers.
