# JSViewDatasource

## Overview

A `JSViewDataSource` provides methods for managing and manipulating view-based data sources in Servoy. It enables the retrieval of column names, creation and management of view foundsets, and dynamic querying capabilities. Methods include obtaining column names through `getColumnNames()`, fetching the data source string with `getDataSource()`, and accessing the `ViewFoundSet` with `getFoundSet()`.

Additionally, `getViewFoundSet(query)` creates and optionally registers view foundsets from a specified query object. The system retains registered view foundsets in memory until explicitly disposed of using `dispose()`. The `unregister()` method facilitates removing view foundsets associated with the datasource, optimizing memory management.

For more details, refer to [ViewDataSource Documentation](https://docs.servoy.com/reference/servoycore/dev-api/datasources/viewdatasource).

## Methods Summarized

| Type                                                                                           | Name                                                                | Summary                                                                                                                                                                               |
| ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)                     | [getColumnNames()](#getcolumnnames)                                 | Get the column names of a datasource.                                                                                                                                                 |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                   | [getDataSource()](#getdatasource)                                   | Get the datasource string.                                                                                                                                                            |
| [JSFoundSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset) | [getFoundSet()](#getfoundset)                                       | Returns the ViewFoundSet that was previously created and registered by a call t o #getViewFoundSet(QBSelect) or #getViewFoundSet(QBSelect,boolean) with the register boolean to true. |
| [JSFoundSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset) | [getViewFoundSet(query)](#getviewfoundset-query)                    | Creates a view foundset with the provided query and automatically registers it.                                                                                                       |
| [JSFoundSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset) | [getViewFoundSet(query, register)](#getviewfoundset-query-register) | Creates a view foundset with the provided query and the option to register it.                                                                                                        |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                 | [unregister()](#unregister)                                         | Unregisters a view foundset associated to this view datasource.                                                                                                                       |

## Methods Detailed

### getColumnNames()

Get the column names of a datasource.

**Returns:** [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) String\[] column names

### getDataSource()

Get the datasource string.

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

**Sample**

```js
datasources.view.orders.getDataSource() // returns 'view:orders'
```

### getFoundSet()

Returns the ViewFoundSet that was previously created and registered by a call t o #getViewFoundSet(QBSelect) or #getViewFoundSet(QBSelect,boolean) with the register boolean to true.\
It will return null when it can't find a ViewFoundSet for this datasource.

**Returns:** [JSFoundSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset) A new ViewFoundSet for the datasource.

**Sample**

```js
var fs = datasources.view.x.orders.getFoundSet()
var record = fs.getRecord(1)
// changes to records can only be done for ViewFoundSets that also do have the pk selected for the table of the column you change.
record.emp_name = 'John'
fs.save(record);
```

### getViewFoundSet(query)

Creates a view foundset with the provided query and automatically registers it.\
Registered ViewFoundSets will be kept in memory by the system until ViewFoundSet.dispose() is called.

**Parameters**

* [QBSelect](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/qbselect) **query** a QBSelect query object

**Returns:** [JSFoundSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset) the registered view foundset, or null if the datasource columns do not match with the query columns

**Sample**

```js
var query = datasources.db.example_data.orders.createSelect();
query.result.add(query.columns.shipname);
query.result.add(query.columns.orderid);
query.where.add(query.columns.orderid.le(1280));

var viewfs = datasources.view.a.getViewFoundSet(query);
```

### getViewFoundSet(query, register)

Creates a view foundset with the provided query and the option to register it.\
A registered ViewFoundSets will be kept in memory by the system until ViewFoundSet.dispose() is called.

**Parameters**

* [QBSelect](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/qbselect) **query** a QBSelect query object
* [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) **register** ;

**Returns:** [JSFoundSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset) the registered view foundset, or null if the datasource columns do not match with the query columns

**Sample**

```js
var query = datasources.db.example_data.orders.createSelect();
query.result.add(query.columns.shipname);
query.result.add(query.columns.orderid);
query.where.add(query.columns.orderid.le(1280));

var viewfs = datasources.view.a.getViewFoundSet(query, false);
```

### unregister()

Unregisters a view foundset associated to this view datasource.

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

**Sample**

```js
datasources.view.a.unregister();
```

***
