# JSDataSourceNode

## Overview

The `JSDataSourceNode` class facilitates interaction with data source nodes within a Servoy solution model. It enables the creation, retrieval, and management of calculations and foundset methods associated with a data source. \* This class is tied to an `IApplication` instance and a specified data source, allowing dynamic manipulation of related objects.

\## Core Functionality

The class provides methods for retrieving and managing calculations and foundset methods. Calculations can be created using JavaScript code, \* with the ability to specify types and validate names. Similarly, foundset methods can be dynamically defined and linked to the data source. Both calculations and methods support retrieval, enumeration, and removal, enhancing the flexibility of database scripting.

\## Additional Features

The `JSDataSourceNode` ensures persistence management by integrating with the Servoy framework’s `FlattenedSolution` and `TableNode` objects. It supports cloning of script providers and handles runtime exceptions gracefully, ensuring stability. It also provides utility methods like `hashCode`, `equals`, and `toString` for seamless object management in Java environments.

## Methods Summarized

| Type                                                                                              | Name                                                    | Summary                                                                                                                                                              |
| ------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [JSCalculation](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jscalculation) | [getCalculation(name)](#getcalculation-name)            | Get an existing calculation for the datasource node.                                                                                                                 |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)                        | [getCalculations()](#getcalculations)                   | Gets all the calculations for the datasource node.                                                                                                                   |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                      | [getDataSource()](#getdatasource)                       | Get the data source for this node.                                                                                                                                   |
| [JSMethod](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jsmethod)           | [getMethod(name)](#getmethod-name)                      | Get an existing foundset method for the datasource node.                                                                                                             |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)                        | [getMethods()](#getmethods)                             | Gets all the foundset methods for the datasource node.                                                                                                               |
| [JSCalculation](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jscalculation) | [newCalculation(code)](#newcalculation-code)            | Creates a new calculation for the given code, the type will be the column where it could be build on (if name is a column name), else it will default to JSVariable. |
| [JSCalculation](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jscalculation) | [newCalculation(code, type)](#newcalculation-code-type) | Creates a new calculation for the given code and the type, if it builds on a column (name is a column name) then type will be ignored.                               |
| [JSMethod](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jsmethod)           | [newMethod(code)](#newmethod-code)                      | Creates a new foundset method with the specified code.                                                                                                               |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                    | [removeCalculation(name)](#removecalculation-name)      | Removes the calculation specified by name.                                                                                                                           |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                    | [removeMethod(name)](#removemethod-name)                | Removes the foundset method specified by name.                                                                                                                       |

## Methods Detailed

### getCalculation(name)

Get an existing calculation for the datasource node.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **name** The name of the calculation

**Returns:** [JSCalculation](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jscalculation) a JSCalculation object for the specified calculation name, or null if no such calculation exists.

**Sample**

```js
var calc = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation() { return 123; }", JSVariable.INTEGER);
var calc2 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation2() { return '20'; }");
var calc3 = solutionModel.getDataSourceNode("db:/example_data/employees").newCalculation("function myCalculation3() { return 'Hello World!'; }",	JSVariable.TEXT);

var c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation");
application.output("Name: " + c.getName() + ", Stored: " + c.isStored());

var allCalcs = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculations();
for (var i = 0; i < allCalcs.length; i++) {
	application.output(allCalcs[i]);
}
```

### getCalculations()

Gets all the calculations for the datasource node.

**Returns:** [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) an array of all JSCalculation objects for the data source node.

**Sample**

```js
var calc = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation() { return 123; }", JSVariable.INTEGER);
var calc2 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation2() { return '20'; }");
var calc3 = solutionModel.getDataSourceNode("db:/example_data/employees").newCalculation("function myCalculation3() { return 'Hello World!'; }",	JSVariable.TEXT);

var c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation");
application.output("Name: " + c.getName() + ", Stored: " + c.isStored());

var allCalcs = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculations();
for (var i = 0; i < allCalcs.length; i++) {
	application.output(allCalcs[i]);
}
```

### getDataSource()

Get the data source for this node.

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) the dataSource

**Sample**

```js
var nodeDataSource = solutionModel.getDataSourceNode("db:/example_data/customers").getDataSource();
```

### getMethod(name)

Get an existing foundset method for the datasource node.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **name** The name of the method

**Returns:** [JSMethod](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jsmethod) the JSMethod object for the specified method name, or null if no such method exists.

**Sample**

```js
var method = solutionModel.getDataSourceNode("db:/example_data/orders").newMethod("function doubleSize() { return 2*getSize(); }");

application.output('Doubled orders for this customer: '+customers_to_orders.doubleSize())
```

### getMethods()

Gets all the foundset methods for the datasource node.

**Returns:** [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) an array of all JSMethod objects for the data source node.

**Sample**

```js
var method = solutionModel.getDataSourceNode("db:/example_data/orders").newMethod("function doubleSize() { return 2*getSize(); }");

application.output('Doubled orders for this customer: '+customers_to_orders.doubleSize())
```

### newCalculation(code)

Creates a new calculation for the given code, the type will be the column where it could be build on (if name is a column name), else it will default to JSVariable.TEXT;

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **code** The code of the calculation, this must be a full function declaration.

**Returns:** [JSCalculation](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jscalculation) the created JSCalculation object for the specified code with default type TEXT.

**Sample**

```js
var calc = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation() { return 123; }", JSVariable.INTEGER);
var calc2 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation2() { return '20'; }");
var calc3 = solutionModel.getDataSourceNode("db:/example_data/employees").newCalculation("function myCalculation3() { return 'Hello World!'; }",	JSVariable.TEXT);

var c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation");
application.output("Name: " + c.getName() + ", Stored: " + c.isStored());

var allCalcs = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculations();
for (var i = 0; i < allCalcs.length; i++) {
	application.output(allCalcs[i]);
}
```

### newCalculation(code, type)

Creates a new calculation for the given code and the type, if it builds on a column (name is a column name) then type will be ignored.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **code** The code of the calculation, this must be a full function declaration.
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **type** The type of the calculation, one of the JSVariable types.

**Returns:** [JSCalculation](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jscalculation) the created JSCalculation object for the specified code and type.

**Sample**

```js
var calc = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation() { return 123; }", JSVariable.INTEGER);
var calc2 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation2() { return '20'; }");
var calc3 = solutionModel.getDataSourceNode("db:/example_data/employees").newCalculation("function myCalculation3() { return 'Hello World!'; }",	JSVariable.TEXT);

var c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation");
application.output("Name: " + c.getName() + ", Stored: " + c.isStored());

var allCalcs = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculations();
for (var i = 0; i < allCalcs.length; i++) {
	application.output(allCalcs[i]);
}
```

### newMethod(code)

Creates a new foundset method with the specified code.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **code** the specified code for the foundset method

**Returns:** [JSMethod](https://docs.servoy.com/reference/servoycore/dev-api/solutionmodel/jsmethod) a JSMethod object

**Sample**

```js
var method = solutionModel.getDataSourceNode("db:/example_data/orders").newMethod("function doubleSize() { return 2*getSize(); }");

application.output('Doubled orders for this customer: '+customers_to_orders.doubleSize())
```

### removeCalculation(name)

Removes the calculation specified by name.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **name** the name of the calculation to be removed

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) true if the removal was successful, false otherwise

**Sample**

```js
var calc1 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation1() { return 123; }", JSVariable.INTEGER);
var calc2 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation2() { return '20'; }");

var c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation1");
application.output("Name: " + c.getName() + ", Stored: " + c.isStored());

solutionModel.getDataSourceNode("db:/example_data/customers").removeCalculation("myCalculation1");
c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation1");
if (c != null) {
	application.output("myCalculation could not be removed.");
}

var allCalcs = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculations();
for (var i = 0; i < allCalcs.length; i++) {
	application.output(allCalcs[i]);
}
```

### removeMethod(name)

Removes the foundset method specified by name.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **name** the name of the method to be removed

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) true if the removal was successful, false otherwise

**Sample**

```js
var method1 = solutionModel.getDataSourceNode("db:/example_data/customers").newMethod("function myFoundsetMethod1() { return 123; }");
var method2 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myFoundsetMethod2() { return '20'; }");

var m = solutionModel.getDataSourceNode("db:/example_data/customers").getMethod("myFoundsetMethod1");
application.output("Name: " + m.getName());

solutionModel.getDataSourceNode("db:/example_data/customers").removeMethod("myFoundsetMethod1");
m = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myFoundsetMethod1");
if (m != null) { application.output("myFoundsetMethod1 could not be removed."); }

var allMethods = solutionModel.getDataSourceNode("db:/example_data/customers").getMethod();
for (var i = 0; i < allMethods; i++)
{
	application.output(allMethods[i]);
}
```

***
