# Core Entity

{% hint style="info" %}
This is an abstract class and is used as a base for other modules.
{% endhint %}

### Imports

{% tabs %}
{% tab title="Contract" %}

```typescript
import { CoreEntityContract } from 'niro-health';
```

{% endtab %}

{% tab title="Interface" %}

```typescript
import type { ICoreEntityContract } from 'niro-health';
```

{% endtab %}
{% endtabs %}

### Method of Use

To implement this class, you will need an create entity.

{% code title="entities/index.ts" %}

```typescript
import { CoreEntityContract } from 'niro-health';

export class User extends CoreEntityContract {
  username: string;
  email: string;
  password: string;

  constructor(data: Partial<User>) {
    super(data);
    this.username = data?.username;
    this.email = data?.email;
    this.password = data?.password;
  }
}
```

{% endcode %}

### Entities with relationships

We know that in the development of clean architecture, we should not have coupling between entities, even if one depends on the other. Here is an example using two entities, users and files, and how we can create a type that creates a union between them so that this type can be used by ORMs.

First, let's create the entities.

{% code title="users/entities/index.ts" %}

```typescript
import { CoreEntityContract } from 'niro-health';

export class User extends CoreEntityContract {
  username: string;
  email: string;
  password: string;
  hash: {
    email: string;
  };

  constructor(data: Partial<User>) {
    super(data);
    this.username = data?.username;
    this.email = data?.email;
    this.password = data?.password;
    this.hash = data?.hash;
  }
}
```

{% endcode %}

{% code title="files/entities/index.ts" %}

```typescript
import { CoreEntityContract } from 'niro-health';

export class File extends CoreEntityContract {
  authorId: string;
  name: string;
  mimetype: string;

  constructor(data: Partial<User>) {
    super(data);
    this.authorId = data?.authorId;
    this.name = data?.name;
    this.mimetype = data?.mimetype;
  }
}
```

{% endcode %}

Now we will create the type to join the entities.

{% code title="users/types/entityWithRelation.ts" %}

```typescript
import { User } from './users/entities';
import { File } from './files/entities';

export type EntityWithRelation = User & {
  files: File[];
};
```

{% endcode %}

{% code title="files/types/entityWithRelation.ts" %}

```typescript
import { File } from './files/entities';
import { User } from './users/entities';

export type EntityWithRelation = File & {
  author: User;
};
```

{% endcode %}

Let's choose [`Prisma`](https://www.prisma.io/) as the **`ORM`** to implement our database contract.

{% code title="prisma/schema.prisma" %}

```scheme
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "linux-musl", "debian-openssl-1.1.x", "linux-musl-openssl-3.0.x"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        String    @id @default(uuid())
  username  String    @unique
  email     String    @unique
  password  String
  hash      Json
  status    Int       @default(1) // 1: normal, 0: disabled
  expired   Boolean   @default(false)
  deleted   Boolean   @default(false)
  createdAt DateTime  @default(now())
  updatedAt DateTime  @default(now())
  expiredAt DateTime?
  deletedAt DateTime?

  files File[]

  @@map("users")
}

model File {
  id             String    @id @default(uuid())
  authorId       String
  name           String
  mimetype       String
  status         Int       @default(1) // 1: normal, 0: disabled
  expired        Boolean   @default(false)
  deleted        Boolean   @default(false)
  createdAt      DateTime  @default(now())
  updatedAt      DateTime  @default(now())
  expiredAt      DateTime?
  deletedAt      DateTime?

  author User @relation(fields: [authorId], references: [id])

  @@map("files")
}
```

{% endcode %}

Our implementation is complete.

### Properties

<table><thead><tr><th width="272.3333333333333">Property</th><th width="108" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>id</code></td><td align="center"><code>public</code></td><td><code>Unique identifier for the entity.</code></td></tr><tr><td><code>status</code></td><td align="center"><code>public</code></td><td><code>Status of the entity. 1 = enable, 0 = disable.</code></td></tr><tr><td><code>expired</code></td><td align="center"><code>public</code></td><td><code>Expired status of the entity. true = expired, false = not expired.</code></td></tr><tr><td><code>deleted</code></td><td align="center"><code>public</code></td><td><code>Deleted status of the entity. true = deleted, false = not deleted.</code></td></tr><tr><td><code>createdAt</code></td><td align="center"><code>public</code></td><td><code>Date when the entity was created.</code></td></tr><tr><td><code>updatedAt</code></td><td align="center"><code>public</code></td><td><code>Date when the entity was updated.</code></td></tr><tr><td><code>expiredAt</code></td><td align="center"><code>public</code></td><td><code>Date when the entity was expired.</code></td></tr><tr><td><code>deletedAt</code></td><td align="center"><code>public</code></td><td><code>Date when the entity was deleted.</code></td></tr></tbody></table>

### Methods

<table><thead><tr><th width="266.3333333333333">Method</th><th width="112" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><mark style="color:blue;"><strong>get</strong></mark> <code>now</code></td><td align="center"><code>private</code></td><td><code>Get current date.</code></td></tr><tr><td><code>enable</code></td><td align="center"><code>public</code></td><td><code>Set status to enable, 1 = enable.</code></td></tr><tr><td><code>disable</code></td><td align="center"><code>public</code></td><td><code>Set status to disable, 0 = disable.</code></td></tr><tr><td><code>expire</code></td><td align="center"><code>public</code></td><td><code>Set expired to true and set expiredAt to current date.</code></td></tr><tr><td><code>delete</code></td><td align="center"><code>public</code></td><td><code>Set deleted to true and set deletedAt to current date.</code></td></tr><tr><td><code>unExpire</code></td><td align="center"><code>public</code></td><td><code>Set expired to false and set expiredAt to null.</code></td></tr><tr><td><code>unDelete</code></td><td align="center"><code>public</code></td><td><code>Set deleted to false and set deletedAt to null.</code></td></tr><tr><td><code>isEnable</code></td><td align="center"><code>public</code></td><td><code>Check if status is enable, 1 = enable.</code></td></tr><tr><td><code>isDisable</code></td><td align="center"><code>public</code></td><td><code>Check if status is disable, 0 = disable.</code></td></tr><tr><td><code>isExpired</code></td><td align="center"><code>public</code></td><td><code>Check if expired is true. true = expired.</code></td></tr><tr><td><code>isDeleted</code></td><td align="center"><code>public</code></td><td><code>Check if deleted is true. true = deleted.</code></td></tr><tr><td><code>isNotExpired</code></td><td align="center"><code>public</code></td><td><code>Check if expired is false. false = not expired.</code></td></tr><tr><td><code>isNotDeleted</code></td><td align="center"><code>public</code></td><td><code>Check if deleted is false. false = not deleted.</code></td></tr><tr><td><code>getCreatedAt</code></td><td align="center"><code>public</code></td><td><code>Get createdAt date.</code></td></tr><tr><td><code>getUpdatedAt</code></td><td align="center"><code>public</code></td><td><code>Get updatedAt date.</code></td></tr><tr><td><code>getExpiredAt</code></td><td align="center"><code>public</code></td><td><code>Get expiredAt date.</code></td></tr><tr><td><code>getDeletedAt</code></td><td align="center"><code>public</code></td><td><code>Get deletedAt date.</code></td></tr><tr><td><code>setCreatedAt</code></td><td align="center"><code>public</code></td><td><code>Set createdAt date.</code></td></tr><tr><td><code>setUpdatedAt</code></td><td align="center"><code>public</code></td><td><code>Set updatedAt date.</code></td></tr><tr><td><code>setExpiredAt</code></td><td align="center"><code>public</code></td><td><code>Set expiredAt date.</code></td></tr><tr><td><code>setDeletedAt</code></td><td align="center"><code>public</code></td><td><code>Set deletedAt date.</code></td></tr><tr><td><code>get</code></td><td align="center"><code>public</code></td><td><code>Get entity properties.</code></td></tr></tbody></table>
