# Calculation

## Overview

A **Calculation** object provides a dynamic, calculated value in a [Table](/reference/servoycore/object-model/database-server/table.md) in a [Database Server](/reference/servoycore/object-model/database-server.md). A calculation is a JavaScript function, which is always computed on-demand, whenever it is displayed or referenced in code. It can be added to the UI the same as any other Data Provider. A calculation is `read-only`.

## File Structure

The code for a Calculation is stored in a script file in a [Solution](/reference/servoy-developer/project-file-structure/workspace-folder/solution-folder.md) directory -> "datasources" directory -> "table-name" directory. The file has the same name as the table "\_calculation", with a `.js` extension. For example, the `products` table would have the calculation file `products_calculation.js`.\
Nesting file path: "solution/datasources/server-name/table-name/table-name\_calculations.js"

## Properties Summary

The following properties can be set for a Calculation object.

| Property                        | Summary                                                                                          |
| ------------------------------- | ------------------------------------------------------------------------------------------------ |
| [Name](#name)                   | The name of the calculation. It is also the same name as the function that returns the value.    |
| [Returned Type](#returned-type) | The data type of the object that will be returned from the calculation                           |
| [Code](#code)                   | This is the JavaScipt function that defines the calculation.                                     |
| [Stored](#stored)               | A calculation is said to be stored when a physical column with the same name exists on the table |

## Properties Details

### Name

The name of the calculation. It is also the same name as the function that returns the value.

### Returned Type

The data type of the object that will be returned from the calculation. All data types are generalized to one of five [standard types](/reference/servoycore/dev-api/database-manager/jscolumn.md#constants-summary):

* [DATETIME](/reference/servoycore/dev-api/database-manager/jscolumn.md#datetime)
* [INTEGER](/reference/servoycore/dev-api/database-manager/jscolumn.md#integer)
* [MEDIA](/reference/servoycore/dev-api/database-manager/jscolumn.md#media)
* [NUMBER](/reference/servoycore/dev-api/database-manager/jscolumn.md#number)
* [TEXT](/reference/servoycore/dev-api/database-manager/jscolumn.md#text)

### Code

This is the JavaScript function that defines the calculation. It takes no arguments and should return a value. Other data providers from this table, related data sources, and scope variables can be used.

**Example**

```javascript
/**
 * Gets the subtotal of the order line
 * @properties={type:8,typeid:36,uuid:"214515EE-2D6C-4C0B-AF3D-5C4C0E165952"}
 */
function subtotal()
{
	return quantity * unitprice;
}
```

### Stored

A calculation is said to be stored when a physical column with the same name exists on the table. The real column can be updated opportunistically when a calculation runs.

{% hint style="warning" %}
Stored calculations can be unpredictable, there is no guaranteed way to know exactly when they will run to update the underlying column in the table, especially if they are complex or depend on other calculations or methods.

A better approach is to use [Table Events](#user-content-fn-1)[^1] to catch changes to the table and make updates to other columns if necessary.
{% endhint %}

[^1]: [Table Events](/guides/develop/application-design/data-modeling/databases/tables/events-triggers.md) are part of the event model that Servoy provides at the data layer, giving developers the opportunity to implement validation and execute business logic just before and after data changes are committed to the database.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/reference/servoycore/object-model/database-server/table/calculation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
