Functions
Syntax
In Pragma, you can use functions written in many languages for your data validation/transformation and user authorization. Functions can be imported using an import
statement such as:
Where ./js-functions.js
and ./py-functions.py
are files at the same location as your project's Pragmafile
. Note that Pragma will also look for files in your project's directory even if their paths don't start with ./
.
An import
consists of three components:
- The location of the function code (file or directory)
- The name of the import (the identifier after
as
) - A configuration block for the import, containing keys and values
An import's name can be used to reference all the functions defined within the specified code path. For example:
These references to functions can be passed to directives, such as @onRead
, or authorization rules.
You can find full examples of Pragma projects using functions here.
Pragma supports importing JavaScript and Python functions, with support for many languages (including Go, Swift, Rust, Ruby, PHP, Java, and .NET) coming soon.
JavaScript Functions
You can import a single JavaScript file containing the function definitions that you want to reference by the import's name. Alternatively, you can import an NPM project, which is a directory containing:
- A valid
package.json
file that must contain amain
entry (typicallyindex.js
) - The file specified in the
main
entry of thepackage.json
file, exporting any functions you would like to reference in yourPragmafile
- Any NPM dependencies you're using, all inside the
node_modules
directory (you need to runnpm install
. Pragma doesn't automatically install dependencies inpackage.json
)
JavaScript functions exported by the NPM project, or defined in a single .js
file can be regular functions, or they can be async
functions (functions that return a JavaScript Promise
).