📝Core Entity

The contract that provides methods for entities.

This is an abstract class and is used as a base for other modules.

Imports

import { CoreEntityContract } from 'niro-health';

Method of Use

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

entities/index.ts
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;
  }
}

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.

users/entities/index.ts
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;
  }
}
files/entities/index.ts
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;
  }
}

Now we will create the type to join the entities.

users/types/entityWithRelation.ts
import { User } from './users/entities';
import { File } from './files/entities';

export type EntityWithRelation = User & {
  files: File[];
};
files/types/entityWithRelation.ts
import { File } from './files/entities';
import { User } from './users/entities';

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

Let's choose Prisma as the ORM to implement our database contract.

prisma/schema.prisma
// 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")
}

Our implementation is complete.

Properties

Property
Scope
Description

id

public

Unique identifier for the entity.

status

public

Status of the entity. 1 = enable, 0 = disable.

expired

public

Expired status of the entity. true = expired, false = not expired.

deleted

public

Deleted status of the entity. true = deleted, false = not deleted.

createdAt

public

Date when the entity was created.

updatedAt

public

Date when the entity was updated.

expiredAt

public

Date when the entity was expired.

deletedAt

public

Date when the entity was deleted.

Methods

Method
Scope
Description

get now

private

Get current date.

enable

public

Set status to enable, 1 = enable.

disable

public

Set status to disable, 0 = disable.

expire

public

Set expired to true and set expiredAt to current date.

delete

public

Set deleted to true and set deletedAt to current date.

unExpire

public

Set expired to false and set expiredAt to null.

unDelete

public

Set deleted to false and set deletedAt to null.

isEnable

public

Check if status is enable, 1 = enable.

isDisable

public

Check if status is disable, 0 = disable.

isExpired

public

Check if expired is true. true = expired.

isDeleted

public

Check if deleted is true. true = deleted.

isNotExpired

public

Check if expired is false. false = not expired.

isNotDeleted

public

Check if deleted is false. false = not deleted.

getCreatedAt

public

Get createdAt date.

getUpdatedAt

public

Get updatedAt date.

getExpiredAt

public

Get expiredAt date.

getDeletedAt

public

Get deletedAt date.

setCreatedAt

public

Set createdAt date.

setUpdatedAt

public

Set updatedAt date.

setExpiredAt

public

Set expiredAt date.

setDeletedAt

public

Set deletedAt date.

get

public

Get entity properties.

Last updated