JavaScript Call Specifications

Version

23.2

Categories

JavaScript

Writing JavaScript modules and environments are the first steps towards the creation of your application. Once the JavaScript code is ready you can expose it to SQL and PL/SQL thanks to a so-called call specification. A JavaScript call specification consists of the following:

  • the module name

  • an (optional) reference to a environment

  • the (simplified) JavaScript function’s signature as per the module code

All client code, regardless whether it’s written in Java, Python, or even with node-oracledb, can access JavaScript stored procedures in the database.

The following example demonstrates

  1. The creation of a JavaScript module (hello_module) in the current user’s schema featuring a single function named hello()

  2. The addition of a call specification f_hello() exposing the JavaScript function to SQL and PL/SQL

  3. A sample invocation of the previously defined function

create or replace mle module hello_module
language javascript as

// JavaScript code to follow from here
/**
 * return a friendly greeting
 * @param {string} who - who should be greeted?
 * @returns {string}
 */
export function hello(who) {
    return 'hello ' + who;
}
/

create or replace function f_hello(
    p_who varchar2)
return varchar2
as mle module hello_module
signature 'hello';
/

select
    f_hello('JavaScript');
Result
SQL> create or replace mle module hello_module
  2  language javascript as
  3
  4  export function hello(who) {
  5
  6      return 'hello ' + who;
  7  }
  8  /

MLE module created.

SQL> create or replace function f_hello(
  2      p_who varchar2)
  3  return varchar2
  4  as mle module hello_module
  5  signature 'hello';
  6  /

Function created.

SQL> select
  2      hello('JavaScript');

HELLO('JAVASCRIPT')
-------------------------------------------------------------------------------
hello JavaScript

Benefits

JavaScript Call Specifications expose JavaScript code to SQL and PL/SQL allowing any programming language with a SQL driver to make use of it. In addition to standalone functions and procedures packages can be used to create a container for call specifications originating from the same JavaScript module.

Further information