import { CoreDatabaseContract } from 'niro-health';
import type { User } from './entities';
export abstract class UserDatabaseContract extends CoreDatabaseContract<User> {
abstract findByEmail(email: string): Promise<User | null>;
}
Let's create an in-memory bank by implementing our contract.
db/memory.ts
import type { INestApplication } from '@nestjs/common';
import type { ISimilarityFilterService, SimilarityFilterType as Type } from 'niro-health';
import type { User } from './entities';
import { UserDatabaseContract } from './contracts';
import * as _ from 'lodash';
export class UserMemoryDB extends UserDatacbaseContract {
private readonly _similarityFilterService: ISimilarityFilterService;
constructor(
protected readonly app: INestApplication,
private users: User[] = [],
) {
super(app);
this._similarityFilterService = this.app.get<ISimilarityFilterService>(
'ISimilarityFilterService',
);
}
async create(data: User): Promise<User> {
this.users.push(data);
return data;
}
async findAll(limit?: number, offset?: number): Promise<User[]> {
return this.users.slice(offset || 0, limit || this.users.length);
}
async findOne(id: number | string): Promise<User | null> {
return this.users.find((user) => user.id === id);
}
async findBy(filter: Partial<User>, similarity?: Type): Promise<User[]> {
return this.users.filter((key) =>
this._similarityFilterService.execute<User>(
filter,
key as User,
similarity || 'full',
),
) as User[];
}
async findByEmail(email: string): Promise<User | null> {
const hash = this.hashText(email);
return this.users.find((user) => user.hash.email === hash);
}
async update(id: number | string, newData: User): Promise<User | null> {
this.users = this.users.map((user) =>
user.id === id ? { ...user, ..._.omitBy(newData, _.isNil) } : user,
) as User[];
return await this.findOne(id);
}
async delete(id: number | string): Promise<boolean> {
this.users = this.users.filter((user) => user.id !== id);
return true;
}
}
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.