# JSServer

## Overview

The `JSServer` class represents a database server object and is commonly accessed through the Servoy maintenance plugin. It provides methods to interact with a database server, allowing developers to create, modify, and remove tables dynamically. Through `JSServer`, developers can synchronize table definitions with the database, reload the database model to reflect changes made externally, and retrieve or validate the current state of the database server.

## Functionality

Developers can create new tables with specified columns, retrieve existing tables, or drop them entirely. Tables can be synchronized with the database, enabling changes to column definitions or metadata. Additionally, the class supports reloading the server's data model to capture external modifications, such as those made through raw SQL operations. Utility methods allow retrieval of all table names on the server and verification of the server's validity for use.

## Methods Summarized

| Type                                                                                                         | Name                                                   | Summary                                                                                         |
| ------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------ | ----------------------------------------------------------------------------------------------- |
| [JSTableObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jstableobject) | [createNewTable(tableName)](#createnewtable-tablename) | Creates in this server a new table with the specified name.                                     |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                               | [dropTable(tableName)](#droptable-tablename)           | Drops the table with the specified name from this server.                                       |
| [JSTableObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jstableobject) | [getTable(tableName)](#gettable-tablename)             | Returns a JSTable instance corresponding to the table with the specified name from this server. |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)                                   | [getTableNames()](#gettablenames)                      | Returns an array with the names of all tables in this server.                                   |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                               | [isValid()](#isvalid)                                  | Get valid state for the server.                                                                 |
| void                                                                                                         | [reloadDataModel()](#reloaddatamodel)                  | Reloads the datamodel from the database, if changed externally or via rawSQL plugin.            |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                               | [synchronizeWithDB(table)](#synchronizewithdb-table)   | Synchronizes a JSTable instance with the database.                                              |

## Methods Detailed

### createNewTable(tableName)

Creates in this server a new table with the specified name.

**Parameters**

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

**Returns:** [JSTableObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jstableobject) JSTableObject created table.

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (server)
{
	var table = server.createNewTable("new_table");
	if (table) {
		var pk = table.createNewColumn("new_table_id", JSColumn.INTEGER, 0);
		pk.rowIdentifierType = JSColumn.PK_COLUMN;
		if (server.synchronizeWithDB(table))
			application.output("New table created in the database.");
		else
			application.output("New table not created in database.");
	}
	else application.output("New table not created at all.");
}
```

### dropTable(tableName)

Drops the table with the specified name from this server.

**Parameters**

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

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) boolean success.

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (server) {
	var result = server.dropTable("new_table");
	if (result)
		application.output("Table dropped.");
	else
		application.output("Table not dropped.");
}
```

### getTable(tableName)

Returns a JSTable instance corresponding to the table with the specified name from this server.

**Parameters**

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

**Returns:** [JSTableObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jstableobject) JSTableObject table.

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (server) {
	var table = server.getTable("employees");
	if (table) {
		var colNames = table.getColumnNames()
		application.output("Table has " + colNames.length + " columns.");
		for (var i=0; i<colNames.length; i++)
			application.output("Column " + i + ": " + colNames[i]);
	}
}
```

### getTableNames()

Returns an array with the names of all tables in this server.

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

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (server) {
	var tableNames = server.getTableNames();
	application.output("There are " + tableNames.length + " tables.");
	for (var i=0; i<tableNames.length; i++)
		application.output("Table " + i + ": " + tableNames[i]);
}
else {
	plugins.dialogs.showInfoDialog("Attention","Server 'example_data' cannot be found.","OK");
}
```

### isValid()

Get valid state for the server.

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) boolean valid state.

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (!server.isValid()) {
	application.output("Server not valid!");
}
```

### reloadDataModel()

Reloads the datamodel from the database, if changed externally or via rawSQL plugin.

This call is not needed after a call to synchronizeWithDB().

**Returns:** void

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
var result = plugins.rawSQL.executeSQL("example_data", null, 'CREATE TABLE raw_table (raw_table_id INTEGER)');
if (result) {
	application.output("Table created through rawSQL plugin.");
	if (server) {
		server.reloadDataModel();
		// All existing JSTableObject/JSColumn object references are invalid now! Use getTable to get new ones.
		var table = server.getTable("raw_table");
		if (table) {
			var colNames = table.getColumnNames()
			application.output("Table has " + colNames.length + " columns.");
			for (var i=0; i<colNames.length; i++)
				application.output("Column " + i + ": " + colNames[i]);
		}
	}
}
else {
	application.output("Raw table creation failed: " + plugins.rawSQL.getException());
}
```

### synchronizeWithDB(table)

Synchronizes a JSTable instance with the database. If columns were added to or removed from the JSTable instance, all these changes will now be persisted to the database.

**Parameters**

* [JSTableObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jstableobject) **table** A JSTableObject instance that should be synchronized.

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) boolean success.

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (server)
{
	var table = server.createNewTable("new_table");
	if (table) {
		var pk = table.createNewColumn("new_table_id", JSColumn.INTEGER, 0);
		pk.rowIdentifierType = JSColumn.PK_COLUMN;
		if (server.synchronizeWithDB(table))
			application.output("New table created in the database.");
		else
			application.output("New table not created in database.");
	}
	else application.output("New table not created at all.");
}
```

***
