functiontransactionsrollbacksurrealdb

Transactions inside functions in SurrealDB


Is there a way to commit data inside a function in SurrealDB? It throws a syntax error if I place BEGIN TRANSACTION within the { and } blocks:

There was a problem with the database: Parse error: Expected closing delimiter '}'
    |
193 | BEGIN TRANSACTION;
    |       ^^^^^^^^^^^ 
    |
192 | DEFINE FUNCTION fn::log($message: string) {
    |                                           ^ Expected this delimiter to close

I've also noticed that, if used within an event handler, the changes my function makes will be rolled-back if an exception is raised outside of my function call. I'm assuming this means that functions and event handlers disallow the use of BEGIN and COMMIT, but I am not sure why.

EDIT: This is in SurrealDB v2.0.0-alpha.8


Solution

  • This is currently not supported, as we currently only support transaction statements as top-level statements, not as part of function bodies, blocks, or subqueries.

    Feel free to weigh in on your Feature Request regarding the same: https://github.com/surrealdb/surrealdb/issues/3404#issuecomment-2167723725