MySQL 9.3 Reference Manual Including MySQL NDB Cluster 9.3

15.1.17 CREATE LIBRARY Statement

CREATE LIBRARY [IF NOT EXISTS] [database.]library
    LANGUAGE language
    [COMMENT "comment_text"]
    AS code

This statement creates a JavaScript code library in the named database, if any. If no database is specified, the library is created in the current database. The library name may be any valid SQL identifier. LANGUAGE must be JavaScript (case-insensitive).

COMMENT is optional. The text of the comment must be quoted. This clause was added in MySQL 9.3.0.

code is a string consisting of JavaScript code which is checked for validity at creation time; invalid code causes the statement to be rejected with an error. The code string can be dollar-quoted or single-quoted; it can also be double-quoted as long as ANSI_QUOTES SQL mode is not set.

To execute CREATE LIBRARY, the user must have the CREATE ROUTINE privilege.

To use a library created within a stored program using this statement, the user must have the EXECUTE privilege. This is checked whenever a function or procedure using the library is created.

The first statement creates a JavaScript library named lib1 in the jslib database. The SELECT statement that follows it displays the row in the Information Schema LIBRARIES table corresponding to the library just created.

mysql> CREATE LIBRARY IF NOT EXISTS jslib.lib1 LANGUAGE JAVASCRIPT
    ->     AS $$
    $>       export function f(n) {
    $>         return n
    $>       }
    $>     $$;
Query OK, 0 rows affected (0.02 sec)

mysql> SELECT * FROM information_schema.LIBRARIES
    -> WHERE LIBRARY_SCHEMA='jslib'\G
*************************** 1. row ***************************
   LIBRARY_CATALOG: def
    LIBRARY_SCHEMA: jslib
      LIBRARY_NAME: lib1
LIBRARY_DEFINITION: 
      export function f(n) {
        return n
      }
    
          LANGUAGE: JAVASCRIPT
           CREATED: 2024-12-16 16:36:44
      LAST_ALTERED: 2024-12-16 16:36:44
          SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,
NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
           CREATOR: me@localhost
1 row in set (0.00 sec)

See Section 27.3.8, “Using JavaScript Libraries”, for more information and examples.