js_lang stored function or procedure¶
This feature is a tech preview. Before using this feature in production, we recommend that you test restoring production from physical backups in your environment, and also use the alternative backup method for redundancy.
Once the component’s installed, you can write stored functions and procedures in JavaScript. The syntax looks like this:
CREATE
[DEFINER = user]
FUNCTION [IF NOT EXISTS] sp_name ([func_parameter[,...]])
RETURNS type
LANGUAGE JS [other-func-characteristic ...] AS routine_body
CREATE
[DEFINER = user]
PROCEDURE [IF NOT EXISTS] sp_name ([proc_parameter[,...]])
LANGUAGE JS [other-proc-characteristic ...] AS routine_body
routine_body:
text_string_literal | dollar_quoted_string
Use the LANGUAGE JS clause when creating a routine.
End each statement with a semicolon. Without a statement terminator, the client sends the statements as one statement and the server returns ERROR 1064.
When binary logging is enabled, CREATE FUNCTION fails with ERROR 1418 unless the function declares DETERMINISTIC, NO SQL, or READS SQL DATA. You can set log_bin_trust_function_creators instead.
CREATE FUNCTION f1(n INT) RETURNS INT LANGUAGE JS DETERMINISTIC AS $$
return n*42;
$$;
CREATE PROCEDURE p1(a INT, b INT, OUT r INT) LANGUAGE JS AS $$
r = a * b;
$$;
SELECT f1(2);
CALL p1(6, 7, @r);
SELECT @r;
Expected result
+-------+
| f1(2) |
+-------+
| 84 |
+-------+
+------+
| @r |
+------+
| 42 |
+------+
You can modify or delete stored programs in JS by using the standard ALTER PROCEDURE/FUNCTION and DROP PROCEDURE/FUNCTION statements. These statements do not require the CREATE_JS_ROUTINE privilege.