Computable attribute (without caching)

Description

Computable attributes (formula) - are used to instantly generate a string expression as a result according to a preset algorithm when accessing a class object through the API. For example, when you open the object.

At the meta class level, the computable attributes store the algorithm for generating a string expression in the formula property.

For example you can access all unique values from the nameattribute, which values are stored in the ownOrg collection. All the unique values, divided by “,” are united in one string expression.

"formula": {
  "merge": [
    "$ownOrg",
    "name",
    null,
    1,
    ", "
  ]
}

For example, If you have a collection with non-unique values (for example, “ownOrg1”, “ownOrg2” and again “ownOrg1”) and you need to obtain only the unique values of the collection “ownOrg1 and ownOrg2”, then the above described formula for the computable attribute using the merge operation will be useful.

Depending on the function, you can refer to the necessary attribute to get the value through the attributes of the “Reference” and “Collection” types.

When saving changes and closing the form of an object, the result is not saved in the attribute if caching is not configured.

If the meta class has a few computable attributes, then the order of the calculation is set in the orderNumber property. You can use the results of calculations for a given order - orderNumber in the subsequent computable attributes.

You can specify computable attributes in the semantics of a class or an attribute.

If you set Null in the formula property, the attribute will not be computable and caching should not be applied to such an attribute.

Formula record model

Each formula begins with a description of the object and the function in the JSON format .

"formula": {
  "function1": [
    {
      "function2": [
        "operand3"
      ]
    },
    "operand1",
    "operand2"
  ]
}

In the object, be sure to specify: ref: a suitable function <supported-functions> with the required number of operands to get the result in function1.

The object contains the full description of the algorithm, that controls the calculations except the functions stored in the depended computable attributes.

An array in a function stores the order of the operands that are passed to the function.

The functions operands could be:

  • String values, storing the constant

    "formula": {
      "function1": [
        "string"
      ]
    }
    
  • String values that store: doc: variables </3_development/metadata_structure/meta_variables>.

    "formula": {
      "function1": [
        "$attr1"
      ]
    }
    
  • Numerical values

    "formula": {
      "function1": [
        3.14
      ]
    }
    
  • Empty values

    "formula": {
      "function1": [
        null
      ]
    }
    
  • Objects

    "formula": {
      "function1": [
        {
          "function2": [
            "operand1"
          ]
        }
      ]
    }
    

Example of applying the formula:

{
      "orderNumber": 5,
      "name": "addressString",
      "caption": "",
      "type": 0,
      "size": null,
      "decimals": 0,
      "allowedFileTypes": null,
      "maxFileCount": 0,
      "nullable": true,
      "readonly": false,
      "indexed": true,
      "unique": false,
      "autoassigned": false,
      "hint": null,
      "defaultValue": null,
      "refClass": "",
      "itemsClass": "",
      "backRef": "",
      "backColl": "",
      "binding": "",
      "semantic": null,
      "selConditions": [],
      "selSorting": [],
      "selectionProvider": null,
      "indexSearch": false,
      "eagerLoading": false,
      "formula": {
        "concat": [
          {
            "if": [
              "$zipCode",
              {
                "concat": [
                  "$zipCode"
                ]
              },
              ""
            ]
          },
          " ",
          {
            "if": [
              "$subjectFederation",
              "$subjectFederation",
              ""
            ]
          },
          {
            "if": [
              "$federationBorough",
              {
                "concat": [
                  ", ",
                  "$federationBorough"
                ]
              },
              ""
            ]
          },
          {
            "if": [
              {
                "and": [
                  {
                    "ne": [
                      "$subjectFederation",
                      "Санкт-Петербург г"
                    ]
                  },
                  {
                    "ne": [
                      "$subjectFederation",
                      "Москва г"
                    ]
                  }
                ]
              },
              {
                "concat": [
                  ", ",
                  "$town"
                ]
              },
              ""
            ]
          },
          {
            "if": [
              "$street",
              {
                "concat": [
                  ", ",
                  "$street"
                ]
              },
              ""
            ]
          },
          {
            "if": [
              "$houseNumber",
              {
                "concat": [
                  ", Дом ",
                  "$houseNumber"
                ]
              },
              ""
            ]
          },
          {
            "if": [
              "$flatNumber",
              {
                "concat": [
                  ", Квартира (офис) ",
                  "$flatNumber"
                ]
              },
              ""
            ]
          }
        ]
      }
 }

Result: the output of the address with spaces and commas between the values of the attributes.

Supported functions

eq - equal

ne - not equal

lt - less

gt - greater

lte - less or equal

gte - greater or equal

and - and

or - or

not - not

add - arithmetic addition

sub - arithmetical subtraction

mul - arithmetical multiplication

div - arithmetical division

nempty - not empty

empty - empty

pad - additional symbols to the desired string length

next - :ref:`retrieves the new value of the sequence <autoassigned-attribute> `

merge - concatenation of attributes in the collection

size - available attribute values - string and collection. For strings - returns the length, for collections - the number of elements

element - derive an arbitrary element from an array, indexation is from 0 ([array of values], [element index: 0 is the first element, last is the last element])

dateAdd - an addition to the data (in the notation momentjs - [Date], [add interval (number)], [unit (line [d, m, y, h, min, s, ms)]

dateDiff - difference between dates (in the notation momentjs - [unitism], [Date1], [Date2])

now - current date and time

concat - string concatenation

substring - получение подстроки ([Строка], [ с какого символа], [сколько символов])

obj - forming an object: odd arguments - property names, even arguments - values

aggregation:

max, min, avg, sum, count

All aggregation operations take the following arguments:

either

[$Имя атрибута коллекции], [Имя агрегируемого атрибута], [функция фильтрации элементов коллекции]

either

[Имя класса], [Имя агрегируемого атрибута], [Объект фильтра сформированный функцией obj соответствующий нотации фильтров mongodb]

1 - indicates the uniqueness of the object, so it allows to count only unique objects for aggregation operations

\n - line break

Example:

"formula": {
        "merge": [
          "$atr1",
          "atr2.name",
          null,
          1,
          "\n"
        ]
      },

Auto-assignment and getting the value of an attribute in a calculated expression

  1. Set the autoassigned: true, so that calculated expression is not executed when opening the create form. The expressions will be calculated before saving. This is relevant when using the next operation in calculations, since it is not always necessary to derive the next value each time opening the creation form.
  2. The default values are calculated before the object is written to the DB (so there is nothing yet in simple auto-assigned attributes at the stage of their calculation).
  3. The next($id) operation (if the $id has a value) will always return 1, since for each object a separate sequence will be created, from which only the first value will be selected.