# Models

Models in FhyTS are responsible for representing and managing application data.

## Basic Structure

All models in FhyTS are derived from the Model base class provided by the fhyts engine:

```javascript
import { Model } from 'fhyts';
```

## Interface Contracts

It is recommended to define TypeScript interfaces to maintain data type consistency:

```javascript
export interface User {
  id: number;
  name: string;
  email: string;
}
```

## Model Implementation

Models are used to manage data collections, whether from memory, files, databases, or other external sources:

```javascript
export class UserModel extends Model {
  private users: User[] = [
    { id: 1, name: 'Nova User', email: 'nova@example.com' },
    { id: 2, name: 'Jane Doe', email: 'jane@example.com' },
  ];

  async findAll(): Promise<User[]> {
    return this.users;
  }

  async findById(id: number): Promise<User | null> {
    return this.users.find(u => u.id === id) || null;
  }
}
```

## API Model (Inherited)

| Method          | Description                                                    |
| --------------- | ------------------------------------------------------------ |
| `constructor()` | Model initialization. Can be used to inject dependencies. |
| `boot()`        | Optional lifecycle method for initial data load or binding. |

## Best Practices

* Use TypeScript interfaces for all data entities.
* Place your model files in the app/models directory.
* Avoid placing complex business logic in models—use Services for that.
* Use asynchronous functions to anticipate future database integration.

## Example File Locations

```bash
models/
    └── UserModel.ts
```

## Advanced Integration

Models can be used directly in controllers or services, as part of a dependency injection container, or invoked explicitly:

```javascript
import { UserModel } from './models/UserModel';

const userModel = new UserModel();
const users = await userModel.findAll();
```

## Note

* Models in FhyTS do not assume a database by default.
* For database integrations such as MySQL, MongoDB, or others, models can be extended with external adapters or the ORM of your choice.

## Next

Once you understand how to use Models, you can move on to the [Services](#services) section to manage business logic and integration between application components.