MLE: Running JavaScript Inside Oracle Database

Oracle 23ai ships MLE (Multi-Language Engine) as a fully supported feature. MLE allows you to write stored procedures, functions, and triggers in JavaScript (using the GraalVM runtime) and run them inside the Oracle Database process.

Creating a JavaScript function:

CREATE OR REPLACE MLE MODULE formatting_utils
LANGUAGE JAVASCRIPT AS
$$
export function formatCurrency(amount, currency = 'USD') {
    return new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: currency
    }).format(amount);
}

export function slugify(text) {
    return text.toLowerCase()
               .replace(/[^a-z0-9]+/g, '-')
               .replace(/^-|-$/g, '');
}
$$;

Calling JavaScript from SQL:

CREATE OR REPLACE FUNCTION format_currency(p_amount NUMBER, p_currency VARCHAR2 DEFAULT 'USD')
RETURN VARCHAR2
AS MLE MODULE formatting_utils SIGNATURE 'formatCurrency(number, string)';
/

SELECT product_name,
       format_currency(price) AS formatted_price
FROM   products;

Accessing the database from JavaScript:

-- JavaScript can issue SQL within the same session
CREATE OR REPLACE MLE MODULE order_processor
LANGUAGE JAVASCRIPT AS
$$
export function processOrder(orderId) {
    const result = session.execute(
        'SELECT status FROM orders WHERE order_id = :id',
        { id: orderId }
    );
    if (result.rows[0].STATUS === 'PENDING') {
        session.execute(
            'UPDATE orders SET status = :s WHERE order_id = :id',
            { s: 'PROCESSING', id: orderId }
        );
    }
}
$$;

Why this matters: Teams with JavaScript expertise can now contribute to database-layer logic without learning PL/SQL. Complex string manipulation, JSON processing, and business rules that are awkward in PL/SQL are natural in JavaScript.

The runtime is GraalVM — a secure, sandboxed JavaScript engine. It does not have access to the filesystem or network outside of Oracle’s SQL interface.

Discover more from grepOra

Subscribe now to keep reading and get access to the full archive.

Continue reading