# Tables and Views

## Entity Base

All the base TypeScript classes in an ORM file that represent a table or view (entity) extend the base class `EntityBase`.  This base class is located in the `wire-orm-base.ts` file.

```javascript
import { EntityBase } from "./wire-orm-base.ts";

class Categories extends EntityBase {
 
  public CategoryID: number;
  public CategoryName: string;
  public Description: string;

...

}
```

All the fields in an entity are defined on the ORM entity class which allows query results to be referenced using strong types.  [See DataTable Interface](#datatable-interface) for details.

## Fields

All the fields in an entity are created by name in an `enum` structure.   The naming convention for the fields `enum` is *\[entity]Field*.

```javascript
enum CategoriesField {
  CategoryID = "CategoryID",
  CategoryName = "CategoryName",
  Description = "Description"
}
```

## Constructor

The constructor for an entity class take three parameters.

| Parameter | Description                                                                                                           |
| --------- | --------------------------------------------------------------------------------------------------------------------- |
| source    | The `wire.data.DataSource` object used to make calls to the query service                                             |
| entity    | The name of the entity in the SQL database                                                                            |
| keys?     | An optional array of primary key fields if they exist and the entity is a table.  These are used in write operations. |

```typescript
class Categories extends EntityBase {
   ...
   constructor() {
      super(Northwind, "Categories", [CategoriesField.CategoryID]);
   }

```

## DataTable Interface

The results of base queries are returned as [wire.data.DataTable](https://docs.wirebootstrap.com/wirebootstrap/working-with-datatables/datatables) objects.  In order for the `Rows` property on a data table to contains typed objects for a ORM class, an interface is used to override the property on the data table for each class.  The `execAsync` method calls into its modified base method returning an instance of the typed object.

```typescript
interface ICatagoriesDataTable extends Omit<IWireDataTable, "Rows"> {
  Rows: Array<Orders>;
}

class Categories extends EntityBase {
...
   async execAsync(): Promise<ICatagoriesDataTable> {
      return super.execAsync$();
   }
}
```


---

# 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.wirebootstrap.com/orm/tables-and-views.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.
