> For the complete documentation index, see [llms.txt](https://docs.schemajs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.schemajs.com/reference-guides/schemajs-global/table.md).

# Table

In **SchemaJS**, the `Table` global represents the structure of a database table, defining its columns and the queries that can be performed on it. It provides an interface for easily constructing, manipulating, and querying database tables, making it highly flexible for various use cases.

## Constructor

```typescript
new Table(tblName: string);
```

## addColumn

Adds a [column](/reference-guides/schemajs-global/column.md) to the table. Columns define the structure of each entry in the table, such as the data type and constraints.

```typescript
table.addColumn(col: Column);
```

## addQuery

Defines a custom query for the table. This query can be executed later, allowing dynamic and specific data retrieval operations.

```typescript
table.addQuery("findById", async (req) => {
    let query = new QueryBuilder().where("id", "=", req.id);
    return await query(query);
});
```

## on

The `.on()` method in the `Table` global allows you to register hooks (event listeners) that execute custom logic when specific actions, such as inserts, occur on the table. It provides a way to extend or modify the behavior of table operations, like inserting new rows, by running custom code before or after the operation completes.

```typescript
table.on("insert", (rows: any[]) => {
        console.log("Insertion detected", rows);
});
```
