# 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](/reference/servoycore/dev-api/js-lib/array.md)                     | [getColumnNames()](#getcolumnnames)                                 | Get the column names of a datasource.                                                                                                                                                 |
| [String](/reference/servoycore/dev-api/js-lib/string.md)                   | [getDataSource()](#getdatasource)                                   | Get the datasource string.                                                                                                                                                            |
| [JSFoundSet](/reference/servoycore/dev-api/database-manager/jsfoundset.md) | [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](/reference/servoycore/dev-api/database-manager/jsfoundset.md) | [getViewFoundSet(query)](#getviewfoundset-query)                    | Creates a view foundset with the provided query and automatically registers it.                                                                                                       |
| [JSFoundSet](/reference/servoycore/dev-api/database-manager/jsfoundset.md) | [getViewFoundSet(query, register)](#getviewfoundset-query-register) | Creates a view foundset with the provided query and the option to register it.                                                                                                        |
| [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md)                 | [unregister()](#unregister)                                         | Unregisters a view foundset associated to this view datasource.                                                                                                                       |

## Methods Detailed

### getColumnNames()

Get the column names of a datasource.

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

### getDataSource()

Get the datasource string.

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) 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](/reference/servoycore/dev-api/database-manager/jsfoundset.md) 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](/reference/servoycore/dev-api/database-manager/qbselect.md) **query** a QBSelect query object

**Returns:** [JSFoundSet](/reference/servoycore/dev-api/database-manager/jsfoundset.md) 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](/reference/servoycore/dev-api/database-manager/qbselect.md) **query** a QBSelect query object
* [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) **register** ;

**Returns:** [JSFoundSet](/reference/servoycore/dev-api/database-manager/jsfoundset.md) 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](/reference/servoycore/dev-api/js-lib/boolean.md) true if the view foundset was unregistered, false otherwise

**Sample**

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

***


---

# 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/dev-api/jsviewdatasource.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.
