Open MarttiR opened 3 years ago
A significantly better workaround is to not use the CREATE FUNCTION
statement, but instead construct an INSERT
statement. This is also how Function Management in Studio inserts functions.
Make sure to escape backslashes, pipes and double quotes from the code.
const statement = `INSERT INTO OFunction CONTENT {
"name": "someFunction",
"code": "${someFunctionCodeString.replace(/\\|"/g, (m) => '\\'+m)}",
"parameters": ["param1","param2"],
"idempotent": true,
"language": "JAVASCRIPT"
};`;
As an idea for future development: the above is done so functions can be kept in their own .js
and .sql
files, from which jest-docblock parses @idempotent
and @param
pragmas; the filename and extension are used for function name and language.
Having everything about the function in one file is a nice pattern for organizing custom functions, and it would be great if OrientJS included a function manager that did this out-of-the-box.
OrientJS version: 3.0.11
When using
CREATE FUNCTION
insession.batch()
, having a newline in the function body causes a lexical error in OrientDB.The same
CREATE FUNCTION
statements work from Functions Management in Studio, although that way the functions get saved without any newlines.Working example with everything on one line:
But if the code is split into multiple lines...
... it produces an error:
Stack trace from OrientDB logs:
Workaround
As a workaround, the newlines can be escaped, but this can be a burden for larger scripts:
These examples use JavaScript functions, but the problem is the same when creating functions with
LANGUAGE SQL
.