Skip to content

Functions

Krzysztof Słysz edited this page Dec 7, 2021 · 11 revisions

Functions

There is possibility to use in a rule the database native functions.
About PostgreSQL native functions, you can read (here).

For example, the NOW() function, returns the current time as timestamp. So we can create a simple rule using this function. The JSON structure forces us to write the function name as a string. So, to distinguish the name of the function from the regular text, add the #func: prefix before the function name. Databukcet server read such a string as a function.

{
  "rules": [
    ["createdAt", "<", "#func:NOW()"]		
  ]
} 

The above example is correct, but it does not make much sense, because it is always true. How can we modify this example, to get all data created at least one day ago? To create this rule, we can use another function named "INTERVAL":

{
  "rules": [
    ["createdAt", "<", "#func:NOW() - INTERVAL '1 DAY'"]		
  ]
} 

We can also verify the correctness of such a function structures through SQL query:

select NOW() - INTERVAL '1 DAY';

If the SQL query works, the rule also should work.

Clone this wiki locally