Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/modules/categories/pages/index.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
= Categories

* xref:javascript/index.adoc[]
* xref:plsql/index.adoc[]
* xref:sql/index.adoc[]
4 changes: 4 additions & 0 deletions docs/modules/categories/pages/javascript/index.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= JavaScript

Oracle Database supports a rich set of languages for writing user-defined functions and stored procedures, including PL/SQL, Java, and C.
With Oracle Database Multilingual Engine (MLE), developers have the additional option to run JavaScript code starting with Oracle Database 23c.
71 changes: 71 additions & 0 deletions features/inline-javascript.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
= Inline JavaScript Procedures
:database-version: 23.2.0
:database-category: javascript

[[feature_summary]]

Inlined JavaScript procedures allow you to embed JavaScript code directly in the `CREATE FUNCTION` and `CREATE PROCEDURE` statements without the need of creating a link:javascript-modules.html[module] first.

If you want to implement a less complex JavaScript feature quickly, inlined procedures and functions are a good choice. The following example coverts seconds-since-epoch to an Oracle Date.

[source,sql]
[subs="verbatim"]
----
create or replace function epoch_to_Date (
P_EPOCH number
) return date
as mle language javascript
q'~
let d = new Date(0);
d.setUTCSeconds(P_EPOCH);

return d;
~';
/

select
to_char(
epoch_to_date(1684758614),
'yyyy-mm-dd hh24:mi:ss'
) the_date;

----

.Result
[source,sql]
[subs="verbatim"]
----
SQL> create or replace function epoch_to_date (
2 P_EPOCH number
3 ) return date
4 as mle language javascript
5 q'~
6 let d = new Date(0);
7 d.setUTCSeconds(P_EPOCH);
8
9 return d;
10 ~';
11 /

Function created.

SQL>
SQL> select
2 to_char(
3 epoch_to_date(1684758614),
4 'yyyy-mm-dd hh24:mi:ss'
5 ) the_date;

THE_DATE
-------------------
2023-05-22 12:30:14
----

== Benefits

Inline JavaScript functions and procedures are a convenient way of exposing functionality in JavaScript to SQL and PL/SQL. You aren't limited to using built-in JavaScript objects, you are free to perform any manipulation you like. If more complex processing, including importing 3rd party JavaScript modules is required you should use modules and environments instead.

== Further information

* Availability: All Offerings
* https://docs.oracle.com/en/database/oracle/oracle-database/23/mlejs/calling-mle-js-functions.html#GUID-B0BBB967-2C4E-43D0-8D38-F4962AD23FE2[Documentation]
90 changes: 90 additions & 0 deletions features/javascript-call-specifications.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
= JavaScript Call Specifications
:database-version: 23.2.0
:database-category: javascript

[[feature_summary]]

Writing JavaScript link:javascript-modules.html[modules] and link:javascript-environments.html[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 link:https://docs.oracle.com/en/database/oracle/oracle-database/23/lnpls/call-specification.html#GUID-C5F117AE-E9A2-499B-BA6A-35D072575BAD[call specification]. A JavaScript call specification consists of the following:

* the link:javascript-modules.html[module] name
* an (optional) reference to a link:javascript-environments.html[environment]
* the (simplified) JavaScript function's signature as per the link:javascript-modules.html[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

. The creation of a JavaScript module (`hello_module`) in the current user's schema featuring a single function named `hello()`
. The addition of a call specification `f_hello()` exposing the JavaScript function to SQL and PL/SQL
. A sample invocation of the previously defined function

[source,sql]
[subs="verbatim"]
----
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
[source,sql]
[subs="verbatim"]
----
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

* Availability: All Offerings
* link:https://docs.oracle.com/en/database/oracle/oracle-database/23/mlejs/calling-mle-js-functions.html#GUID-55400971-3660-47D7-B60C-D2F76EE0FD42[Documentation]
* https://blogs.oracle.com/developers/post/using-javascript-community-modules-in-oracle-database-23c-free-developer-release[Example]
* https://blogs.oracle.com/developers/post/introduction-javascript-oracle-database-23c-free-developer-release[Introductory Blog Post]
137 changes: 137 additions & 0 deletions features/javascript-environments.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
= JavaScript Environments
:database-version: 23.2.0
:database-category: javascript

[[feature_summary]]

JavaScript Environments, just like JavaScript modules, are schema objects persisted in the database. They perform a vital function in applications involving multiple JavaScript modules. Unlike `node.js` or `deno` projects JavaScript modules aren't persisted on the file system, they are stored in the database. Developers cannot simply import modules based on their location in the file system, they need to use environments instead.

The following example demonstrates the use of environments.


[source,javascript]
[subs="verbatim"]
----
create or replace MLE module module_one
language javascript as

// this function is exported and will be called by
// module_2's greeting() function
export function hello(who) {

return 'hello ' + who;
}
/

create or replace MLE module module_two
language javascript as

// before module_1's hello() function can be imported a
// so-called import name must be defined by means of creating
// a JavaScript environment. The module name does not have to
// match the import name
import { hello } from 'module1'

export function greeting() {

const who = 'JavaScript';
return hello(who);
}
/

// the mapping between import name and module name is defined
// in an environment
create or replace mle env example_env
imports (
'module1' module module_one
);

// with the module in place it is possible to invoke module_2's
// greeting function. Refer to the section about call specifications
// for more details about invoking JavaScript code in SQL and PL/SQL
create or replace function f_greeting
return varchar2 as
mle module module_two
env example_env
signature 'greeting';
/

select
f_greeting;

----

.Result
[source]
[subs="verbatim"]
----
SQL> create or replace MLE module module_one
2 language javascript as
3
4 // this function is exported and will be called by
5 // module_2's greeting() function
6 export function hello(who) {
7
8 return 'hello ' + who;
9 }
10 /

MLE module created.

SQL> create or replace MLE module module_two
2 language javascript as
3
4 // before module_1's hello() function can be imported a
5 // so-called import name must be defined by means of creating
6 // a JavaScript environment. The module name does not have to
7 // match the import name
8 import { hello } from 'module1'
9
10 export function greeting() {
11
12 const who = 'JavaScript';
13 return hello(who);
14 }
15 /

MLE module created.

SQL> -- the mapping between import name and module name is defined
SQL> -- in an environment
SQL> create or replace mle env example_env
2 imports (
3 'module1' module module_one
4 );

MLE env created.

SQL> -- with the module in place it is possible to invoke module_2's
SQL> -- greeting function. Refer to the section about call specifications
SQL> -- for more details about invoking JavaScript code in SQL and PL/SQL
SQL> create or replace function f_greeting
2 return varchar2 as
3 mle module module_two
4 env example_env
5 signature 'greeting';
6 /

Function created.

SQL> -- call the function
SQL> select
2 f_greeting;

F_GREETING
-------------------------------------------------------------------------------
hello JavaScript
----

== Benefits

JavaScript Environments play a crucial role during the development of JavaScript stored procedures. They are most useful providing means to map an import name as used in a JavaScript module to the actual module itself. Furthermore they are essential entities for the definition of link:javascript-call-specifications.html[call specifications. ]

== Further information

* Availability: All Offerings
* https://docs.oracle.com/en/database/oracle/oracle-database/23/mlejs/mle-js-modules-and-environments.html#GUID-EB682328-BA26-4422-9304-62D412D28B2F[Documentation]
* https://blogs.oracle.com/developers/post/using-javascript-community-modules-in-oracle-database-23c-free-developer-release[Blog post]
Loading