User-Defined Functions with JavaScript

  • Capella Operational
  • how-to
    +
    Couchbase Capella lets you extend the SQL++ query language by adding your own functions written in JavaScript.

    On its own, SQL++ includes built-in operations and functions for data manipulation. You can use user-defined functions to create your own extensions to the language.

    With user-defined functions, you can:

    • Create reusable, domain-specific functions for use in your applications.

    • Execute complex logic that may be difficult to do in SQL++.

    • Migrate from Relational Database Management System (RDBMS) stored procedures.

    Couchbase Capella’s user-defined functions are defined with JavaScript, specifically the ECMAScript standard, with some restrictions and extensions. For more information, see JavaScript Functions for Query Reference.

    Using User-Defined Functions in Capella

    You cannot call JavaScript code directly from a SQL++ query. You must create a user-defined function to call JavaScript code in your queries.

    You can create 2 types of user-defined functions:

    Creating a UDF library for your JavaScript functions is optional, but simplifies organization and access control for user-defined functions.

    After you have created your user-defined functions, you can Call a User-Defined Function like any other SQL++ function.

    Inline Functions

    You can create a user-defined function that executes inline SQL++ commands or inline JavaScript. You do not need to create a UDF library before you can create and use inline functions.

    If you create a user-defined function this way, you cannot group related functions or change cluster access restrictions for multiple related functions at once.

    For more information about how to create inline functions, see Create an Inline User-Defined Function.

    Functions From User-Defined Function (UDF) Libraries

    A UDF library is a collection of JavaScript functions. UDF libraries keep your JavaScript functions organized and allow you to set access controls across multiple functions at once.

    After you have created a UDF library, you must create user-defined functions to use the JavaScript functions in that library. The user-defined function creates a link between the JavaScript function in your library and SQL++, letting you call your JavaScript code.

    Next Steps

    For more information about the specifics of JavaScript for user-defined functions in Capella, see: