# JSTableObject

## Overview

The `JSTableObject` is an enhanced version of `JSTable`, designed to represent a table that can be modified programmatically before being finalized in the database. This object supports dynamic schema modifications, such as adding or deleting columns. The changes made to a `JSTableObject` do not immediately impact the database and must be explicitly synchronized using methods like `JSServer.synchronizeWithDB`.

Developers can create new columns in the table by specifying attributes such as the column name, data type, length, and whether the column allows null values. Primary key configurations can also be set programmatically. This flexibility is particularly useful when working with dynamically generated tables or when adjustments to a schema are needed during runtime.

## Methods Summarized

| Type                                                                                                           | Name                                                                                                                         | Summary                                                                                           |
| -------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [JSColumnObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jscolumnobject) | [createNewColumn(columnName, type, length)](#createnewcolumn-columnname-type-length)                                         | Creates a new column in this table.                                                               |
| [JSColumnObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jscolumnobject) | [createNewColumn(columnName, type, length, allowNull)](#createnewcolumn-columnname-type-length-allownull)                    | Creates a new column in this table.                                                               |
| [JSColumnObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jscolumnobject) | [createNewColumn(columnName, type, length, allowNull, pkColumn)](#createnewcolumn-columnname-type-length-allownull-pkcolumn) | Creates a new column in this table.                                                               |
| void                                                                                                           | [deleteColumn(columnName)](#deletecolumn-columnname)                                                                         | Deletes the column with the specified name from this table.                                       |
| [JSColumn](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jscolumn)                     | [getColumn(name)](#getcolumn-name)                                                                                           | Returns a JSColumn for the named column (or column dataproviderID).                               |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)                                     | [getColumnNames()](#getcolumnnames)                                                                                          | Returns an array containing the names of all table columns.                                       |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                                   | [getDataSource()](#getdatasource)                                                                                            | Returns the table data source uri.                                                                |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                                   | [getQuotedSQLName()](#getquotedsqlname)                                                                                      | Returns a quoted version of the table name, if necessary, as defined by the actual database used. |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)                                     | [getRowIdentifierColumnNames()](#getrowidentifiercolumnnames)                                                                | Returns an array containing the names of the identifier (PK) column(s).                           |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                                   | [getSQLName()](#getsqlname)                                                                                                  | Returns the table name as defined in the database.                                                |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                                   | [getServerName()](#getservername)                                                                                            | Returns the Servoy server name.                                                                   |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                                 | [isMetadataTable()](#ismetadatatable)                                                                                        | Returns whether table was flagged as metadata table.                                              |

## Methods Detailed

### createNewColumn(columnName, type, length)

Creates a new column in this table. The name, type and length of the new column must be specified. For specifying the\
type of the column, use the JSColumn constants. The column is not actually created in the database until this\
table is synchronized with the database using the JSServer.synchronizeWithDB method.

The method returns a JSColumn instance that corresponds to the newly created column. If any error occurs and the column cannot be created, then the method\
returns null.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **columnName** ;
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **type** ;
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **length** ;

**Returns:** [JSColumnObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jscolumnobject) A JSColumnObject instance representing the newly created column, or null if the column could not be created.

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (server)
{
	var table = server.createNewTable("users");
	if (table)
	{
		var pk = table.createNewColumn("id", JSColumn.MEDIA, 16); // can also use (JSColumn.TEXT, 36) for UUIDs
		pk.rowIdentifierType = JSColumn.PK_COLUMN;
		pk.setFlag(JSColumn.UUID_COLUMN, true)
		pk.sequenceType = JSColumn.UUID_GENERATOR
		var c = table.createNewColumn("name", JSColumn.TEXT, 100);
		c.allowNull = false
		table.createNewColumn("age", JSColumn.INTEGER, 0);
		table.createNewColumn("last_login", JSColumn.DATETIME, 0);
		var result = server.synchronizeWithDB(table);
		if (result) application.output("Table successfully created.");
		else application.output("Table not created.");
	}
}
```

### createNewColumn(columnName, type, length, allowNull)

Creates a new column in this table. The name, type and length of the new column must be specified. For specifying the\
type of the column, use the JSColumn constants. The column is not actually created in the database until this\
table is synchronized with the database using the JSServer.synchronizeWithDB method.

The method returns a JSColumn instance that corresponds to the newly created column. If any error occurs and the column cannot be created, then the method\
returns null.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **columnName** ;
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **type** ;
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **length** ;
* [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) **allowNull** ;

**Returns:** [JSColumnObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jscolumnobject) A JSColumnObject instance representing the newly created column, or null if the column could not be created.

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (server)
{
	var table = server.createNewTable("users");
	if (table)
	{
		var pk = table.createNewColumn("id", JSColumn.MEDIA, 16); // can also use (JSColumn.TEXT, 36) for UUIDs
		pk.rowIdentifierType = JSColumn.PK_COLUMN;
		pk.setFlag(JSColumn.UUID_COLUMN, true)
		pk.sequenceType = JSColumn.UUID_GENERATOR
		var c = table.createNewColumn("name", JSColumn.TEXT, 100);
		c.allowNull = false
		table.createNewColumn("age", JSColumn.INTEGER, 0);
		table.createNewColumn("last_login", JSColumn.DATETIME, 0);
		var result = server.synchronizeWithDB(table);
		if (result) application.output("Table successfully created.");
		else application.output("Table not created.");
	}
}
```

### createNewColumn(columnName, type, length, allowNull, pkColumn)

Creates a new column in this table. The name, type and length of the new column must be specified. For specifying the\
type of the column, use the JSColumn constants. The column is not actually created in the database until this\
table is synchronized with the database using the JSServer.synchronizeWithDB method.

The method returns a JSColumn instance that corresponds to the newly created column. If any error occurs and the column cannot be created, then the method\
returns null.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **columnName** ;
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **type** ;
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **length** ;
* [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) **allowNull** ;
* [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) **pkColumn** ;

**Returns:** [JSColumnObject](https://docs.servoy.com/reference/servoyextensions/server-plugins/maintenance/jscolumnobject) A JSColumnObject instance representing the newly created column, or null if the column could not be created.

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (server)
{
	var table = server.createNewTable("users");
	if (table)
	{
		var pk = table.createNewColumn("id", JSColumn.MEDIA, 16); // can also use (JSColumn.TEXT, 36) for UUIDs
		pk.rowIdentifierType = JSColumn.PK_COLUMN;
		pk.setFlag(JSColumn.UUID_COLUMN, true)
		pk.sequenceType = JSColumn.UUID_GENERATOR
		var c = table.createNewColumn("name", JSColumn.TEXT, 100);
		c.allowNull = false
		table.createNewColumn("age", JSColumn.INTEGER, 0);
		table.createNewColumn("last_login", JSColumn.DATETIME, 0);
		var result = server.synchronizeWithDB(table);
		if (result) application.output("Table successfully created.");
		else application.output("Table not created.");
	}
}
```

### deleteColumn(columnName)

Deletes the column with the specified name from this table. The column is not actually deleted from the database until this table is synchronized with the database using the JSServer.synchronizeWithDB method.

**Parameters**

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

**Returns:** void

**Sample**

```js
var server = plugins.maintenance.getServer("example_data");
if (server) {
	var table = server.getTable("users");
	if (table) {
		table.deleteColumn("last_login");
		server.synchronizeWithDB(table);
	}
}
```

### getColumn(name)

Returns a JSColumn for the named column (or column dataproviderID).

**Parameters**

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

**Returns:** [JSColumn](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jscolumn) JSColumn column.

**Sample**

```js
var jsTable = databaseManager.getTable('udm', 'campaigns')
var jsColumn = jsTable.getColumn('campaign_name')
```

### getColumnNames()

Returns an array containing the names of all table columns.\
If the table is in mem, then the internal rowid column name is not returned.

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

**Sample**

```js
var jsTable = databaseManager.getTable('udm', 'campaigns')
var columnNames = jsTable.getColumnNames()
```

### getDataSource()

Returns the table data source uri.

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

**Sample**

```js
var jsTable = databaseManager.getTable('udm', 'campaigns')
var dataSource = jsTable.getDataSource()
```

### getQuotedSQLName()

Returns a quoted version of the table name, if necessary, as defined by the actual database used.

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) String table name, quoted if needed.

**Sample**

```js
//use with the raw SQL plugin:
//if the table name contains characters that are illegal in sql, the table name will be quoted
var jsTable = databaseManager.getTable('udm', 'campaigns')
var quotedTableName = jsTable.getQuotedSQLName()
plugins.rawSQL.executeSQL('udm',  quotedTableName,  'select * from ' + quotedTableName + ' where is_active = ?', [1])
```

### getRowIdentifierColumnNames()

Returns an array containing the names of the identifier (PK) column(s).\
&#x20;Please note that if the table is in mem, then the internal rowid column name is also returned.

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

**Sample**

```js
var jsTable = databaseManager.getTable('udm', 'campaigns')
var identifierColumnNames = jsTable.getRowIdentifierColumnNames()
```

### getSQLName()

Returns the table name as defined in the database.

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

**Sample**

```js
var jsTable = databaseManager.getTable('udm', 'campaigns')
var tableNameForQuery = jsTable.getSQLName()
```

### getServerName()

Returns the Servoy server name.

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

**Sample**

```js
var jsTable = databaseManager.getTable('udm', 'campaigns')
var serverName = jsTable.getServerName()
```

### isMetadataTable()

Returns whether table was flagged as metadata table.

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

**Sample**

```js
var jsTable = databaseManager.getTable('udm', 'campaigns')
var isMetaDataTable = jsTable.isMetadataTable()
```

***
