πŸ“Core Database

The contract that provides methods for database.

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

Imports

import { CoreDatabaseContract } 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;
  }
}

Next, you can implement the contract.

Let's create an in-memory bank by implementing our contract.

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.

Now we will create the type to join the entities.

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

Our implementation is complete.

Properties

Property
Scope
Description

_randomService

protected

Instance of Random service.

_stringExService

protected

Instance of StringEx service.

_cryptoService

protected

Instance of Crypto service.

Methods

Method
Scope
Description

generateUUID

public

Generate UUID.

hashText

public

Generate hash by text.

compareHashText

public

Compare hash by text with hashed text.

hashPassword

public

Generate hash for password.

compareHashPassword

public

Compare hash for password with hashed password.

encrypt

public

Encrypt data string to base64.

decrypt

public

Decrypt encrypted base64 to string.

create

abstract

Create new model.

findAll

abstract

Find all models.

findOne

abstract

Find one model by id.

findBy

abstract

Find models by filter.

update

abstract

Update model by id.

delete

abstract

Delete model by id.

Last updated