Search code examples
surrealdb

How to call functions using the HTTP API and pass them JSON objects as parameters


Rather than developing a transformation pipeline in the application layer, I'd like to post standard JSON to SurrealDB and define functions acting like old-school stored procedures to store update, read and generally speaking interact with the data. This would abstract the SurrealDB-specific concerns from the application layer.

However, I cannot find in the documentation how one may use the HTTP API to call a user-defined function and pass it a Json object. Can someone help by providing a sample of what such an HTTP request should look like?


Solution

  • Here is the answer from the SurrealDB Team:

    Hey, you'd most likely want to use the /sql endpoint Then in there use the function you defined beforehand and have the json in the header and use that as a parameter in the SurrealQL

    something like curl -X POST -u "root:root" -H "NS: mynamespace" -H "DB: mydatabase" -H "Accept: application/json" -d "SELECT fn::my_function($my_param) FROM my_table" http://localhost:8000/sql

    could alternatively use the return syntax RETURN fn::my_function($my_param);

    And apparently, named JSON parameters can be passed to the function by encoding them as URL parameters:

    You can set the variable via url query parameters at this point in time