Skip to content

Commit 30362d9

Browse files
Please add the JavaScript category (#13)
* add javascript category page * add Javascript to the list of categories * add javascript category * local antora invocation to avoid a global install * User proper case for JS, don't specify current release status Signed-off-by: Gerald Venzl <gerald.venzl@oracle.com> --------- Signed-off-by: Gerald Venzl <gerald.venzl@oracle.com> Co-authored-by: Gerald Venzl <gerald.venzl@oracle.com>
1 parent c4aecd9 commit 30362d9

File tree

7 files changed

+459
-1
lines changed

7 files changed

+459
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
= Categories
22

3+
* xref:javascript/index.adoc[]
34
* xref:plsql/index.adoc[]
45
* xref:sql/index.adoc[]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
= JavaScript
2+
3+
Oracle Database supports a rich set of languages for writing user-defined functions and stored procedures, including PL/SQL, Java, and C.
4+
With Oracle Database Multilingual Engine (MLE), developers have the additional option to run JavaScript code starting with Oracle Database 23c.

features/inline-javascript.adoc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
= Inline JavaScript Procedures
2+
:database-version: 23.2.0
3+
:database-category: javascript
4+
5+
[[feature_summary]]
6+
7+
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.
8+
9+
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.
10+
11+
[source,sql]
12+
[subs="verbatim"]
13+
----
14+
create or replace function epoch_to_Date (
15+
P_EPOCH number
16+
) return date
17+
as mle language javascript
18+
q'~
19+
let d = new Date(0);
20+
d.setUTCSeconds(P_EPOCH);
21+
22+
return d;
23+
~';
24+
/
25+
26+
select
27+
to_char(
28+
epoch_to_date(1684758614),
29+
'yyyy-mm-dd hh24:mi:ss'
30+
) the_date;
31+
32+
----
33+
34+
.Result
35+
[source,sql]
36+
[subs="verbatim"]
37+
----
38+
SQL> create or replace function epoch_to_date (
39+
2 P_EPOCH number
40+
3 ) return date
41+
4 as mle language javascript
42+
5 q'~
43+
6 let d = new Date(0);
44+
7 d.setUTCSeconds(P_EPOCH);
45+
8
46+
9 return d;
47+
10 ~';
48+
11 /
49+
50+
Function created.
51+
52+
SQL>
53+
SQL> select
54+
2 to_char(
55+
3 epoch_to_date(1684758614),
56+
4 'yyyy-mm-dd hh24:mi:ss'
57+
5 ) the_date;
58+
59+
THE_DATE
60+
-------------------
61+
2023-05-22 12:30:14
62+
----
63+
64+
== Benefits
65+
66+
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.
67+
68+
== Further information
69+
70+
* Availability: All Offerings
71+
* https://docs.oracle.com/en/database/oracle/oracle-database/23/mlejs/calling-mle-js-functions.html#GUID-B0BBB967-2C4E-43D0-8D38-F4962AD23FE2[Documentation]
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
= JavaScript Call Specifications
2+
:database-version: 23.2.0
3+
:database-category: javascript
4+
5+
[[feature_summary]]
6+
7+
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:
8+
9+
* the link:javascript-modules.html[module] name
10+
* an (optional) reference to a link:javascript-environments.html[environment]
11+
* the (simplified) JavaScript function's signature as per the link:javascript-modules.html[module] code
12+
13+
All client code, regardless whether it's written in Java, Python, or even with `node-oracledb`, can access JavaScript stored procedures in the database.
14+
15+
The following example demonstrates
16+
17+
. The creation of a JavaScript module (`hello_module`) in the current user's schema featuring a single function named `hello()`
18+
. The addition of a call specification `f_hello()` exposing the JavaScript function to SQL and PL/SQL
19+
. A sample invocation of the previously defined function
20+
21+
[source,sql]
22+
[subs="verbatim"]
23+
----
24+
create or replace mle module hello_module
25+
language javascript as
26+
27+
// JavaScript code to follow from here
28+
/**
29+
* return a friendly greeting
30+
* @param {string} who - who should be greeted?
31+
* @returns {string}
32+
*/
33+
export function hello(who) {
34+
return 'hello ' + who;
35+
}
36+
/
37+
38+
create or replace function f_hello(
39+
p_who varchar2)
40+
return varchar2
41+
as mle module hello_module
42+
signature 'hello';
43+
/
44+
45+
select
46+
f_hello('JavaScript');
47+
----
48+
49+
.Result
50+
[source,sql]
51+
[subs="verbatim"]
52+
----
53+
SQL> create or replace mle module hello_module
54+
2 language javascript as
55+
3
56+
4 export function hello(who) {
57+
5
58+
6 return 'hello ' + who;
59+
7 }
60+
8 /
61+
62+
MLE module created.
63+
64+
SQL> create or replace function f_hello(
65+
2 p_who varchar2)
66+
3 return varchar2
67+
4 as mle module hello_module
68+
5 signature 'hello';
69+
6 /
70+
71+
Function created.
72+
73+
SQL> select
74+
2 hello('JavaScript');
75+
76+
HELLO('JAVASCRIPT')
77+
-------------------------------------------------------------------------------
78+
hello JavaScript
79+
----
80+
81+
== Benefits
82+
83+
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.
84+
85+
== Further information
86+
87+
* Availability: All Offerings
88+
* link:https://docs.oracle.com/en/database/oracle/oracle-database/23/mlejs/calling-mle-js-functions.html#GUID-55400971-3660-47D7-B60C-D2F76EE0FD42[Documentation]
89+
* https://blogs.oracle.com/developers/post/using-javascript-community-modules-in-oracle-database-23c-free-developer-release[Example]
90+
* https://blogs.oracle.com/developers/post/introduction-javascript-oracle-database-23c-free-developer-release[Introductory Blog Post]
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
= JavaScript Environments
2+
:database-version: 23.2.0
3+
:database-category: javascript
4+
5+
[[feature_summary]]
6+
7+
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.
8+
9+
The following example demonstrates the use of environments.
10+
11+
12+
[source,javascript]
13+
[subs="verbatim"]
14+
----
15+
create or replace MLE module module_one
16+
language javascript as
17+
18+
// this function is exported and will be called by
19+
// module_2's greeting() function
20+
export function hello(who) {
21+
22+
return 'hello ' + who;
23+
}
24+
/
25+
26+
create or replace MLE module module_two
27+
language javascript as
28+
29+
// before module_1's hello() function can be imported a
30+
// so-called import name must be defined by means of creating
31+
// a JavaScript environment. The module name does not have to
32+
// match the import name
33+
import { hello } from 'module1'
34+
35+
export function greeting() {
36+
37+
const who = 'JavaScript';
38+
return hello(who);
39+
}
40+
/
41+
42+
// the mapping between import name and module name is defined
43+
// in an environment
44+
create or replace mle env example_env
45+
imports (
46+
'module1' module module_one
47+
);
48+
49+
// with the module in place it is possible to invoke module_2's
50+
// greeting function. Refer to the section about call specifications
51+
// for more details about invoking JavaScript code in SQL and PL/SQL
52+
create or replace function f_greeting
53+
return varchar2 as
54+
mle module module_two
55+
env example_env
56+
signature 'greeting';
57+
/
58+
59+
select
60+
f_greeting;
61+
62+
----
63+
64+
.Result
65+
[source]
66+
[subs="verbatim"]
67+
----
68+
SQL> create or replace MLE module module_one
69+
2 language javascript as
70+
3
71+
4 // this function is exported and will be called by
72+
5 // module_2's greeting() function
73+
6 export function hello(who) {
74+
7
75+
8 return 'hello ' + who;
76+
9 }
77+
10 /
78+
79+
MLE module created.
80+
81+
SQL> create or replace MLE module module_two
82+
2 language javascript as
83+
3
84+
4 // before module_1's hello() function can be imported a
85+
5 // so-called import name must be defined by means of creating
86+
6 // a JavaScript environment. The module name does not have to
87+
7 // match the import name
88+
8 import { hello } from 'module1'
89+
9
90+
10 export function greeting() {
91+
11
92+
12 const who = 'JavaScript';
93+
13 return hello(who);
94+
14 }
95+
15 /
96+
97+
MLE module created.
98+
99+
SQL> -- the mapping between import name and module name is defined
100+
SQL> -- in an environment
101+
SQL> create or replace mle env example_env
102+
2 imports (
103+
3 'module1' module module_one
104+
4 );
105+
106+
MLE env created.
107+
108+
SQL> -- with the module in place it is possible to invoke module_2's
109+
SQL> -- greeting function. Refer to the section about call specifications
110+
SQL> -- for more details about invoking JavaScript code in SQL and PL/SQL
111+
SQL> create or replace function f_greeting
112+
2 return varchar2 as
113+
3 mle module module_two
114+
4 env example_env
115+
5 signature 'greeting';
116+
6 /
117+
118+
Function created.
119+
120+
SQL> -- call the function
121+
SQL> select
122+
2 f_greeting;
123+
124+
F_GREETING
125+
-------------------------------------------------------------------------------
126+
hello JavaScript
127+
----
128+
129+
== Benefits
130+
131+
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. ]
132+
133+
== Further information
134+
135+
* Availability: All Offerings
136+
* https://docs.oracle.com/en/database/oracle/oracle-database/23/mlejs/mle-js-modules-and-environments.html#GUID-EB682328-BA26-4422-9304-62D412D28B2F[Documentation]
137+
* https://blogs.oracle.com/developers/post/using-javascript-community-modules-in-oracle-database-23c-free-developer-release[Blog post]

0 commit comments

Comments
 (0)