all
This commit is contained in:
1 parent
90125aede9
commit
2d6c29a1f3
404 files changed
+126983
-85
No files matched your search
@@ -0,0 +1,48 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return -- `model` is the deliberate untyped boundary to Prisma's ORM; the public contract stays fully typed via T/CreateInput/UpdateInput/WhereKey above. */
|
||||
import { Inject } from "@nestjs/common";
|
||||
import { DB } from "../database/database.tokens.js";
|
||||
import type { Database } from "../prisma/db.js";
|
||||
import type { IRepository } from "./repository.interface.js";
|
||||
|
||||
/**
|
||||
* Generic CRUD repository over a `db.orm.public.<Model>` collection.
|
||||
* Subclasses only declare which columns to select and which model to target.
|
||||
*/
|
||||
export abstract class BaseRepository<
|
||||
T,
|
||||
CreateInput,
|
||||
UpdateInput,
|
||||
WhereKey extends object,
|
||||
> implements IRepository<T, CreateInput, UpdateInput, WhereKey> {
|
||||
constructor(@Inject(DB) protected readonly db: Database) {}
|
||||
|
||||
/** The `db.orm.public.<Model>` collection this repository targets. */
|
||||
protected abstract readonly model: any;
|
||||
|
||||
/** Columns fetched by `findAll()`. */
|
||||
protected abstract readonly columns: readonly string[];
|
||||
|
||||
findAll(): Promise<T[]> {
|
||||
return this.model
|
||||
.select(...this.columns)
|
||||
.all()
|
||||
.toArray();
|
||||
}
|
||||
|
||||
findById(where: WhereKey): Promise<T | null> {
|
||||
return this.model.where(where).first();
|
||||
}
|
||||
|
||||
create(data: CreateInput): Promise<T> {
|
||||
return this.model.create(data);
|
||||
}
|
||||
|
||||
async update(where: WhereKey, data: UpdateInput): Promise<T> {
|
||||
await this.model.where(where).update(data);
|
||||
return this.findById(where) as Promise<T>;
|
||||
}
|
||||
|
||||
async delete(where: WhereKey): Promise<void> {
|
||||
await this.model.where(where).delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
import type { IRepository } from "./repository.interface.js";
|
||||
import type { IService } from "./service.interface.js";
|
||||
|
||||
/**
|
||||
* Generic business layer: delegates to an `IRepository`, translating a
|
||||
* missing row into a `NotFoundException`. Subclasses may override
|
||||
* `notFoundMessage` for a more specific error message.
|
||||
*/
|
||||
export abstract class BaseService<
|
||||
T,
|
||||
CreateInput,
|
||||
UpdateInput,
|
||||
WhereKey,
|
||||
> implements IService<T, CreateInput, UpdateInput, WhereKey> {
|
||||
constructor(
|
||||
protected readonly repository: IRepository<
|
||||
T,
|
||||
CreateInput,
|
||||
UpdateInput,
|
||||
WhereKey
|
||||
>,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<T[]> {
|
||||
return this.repository.findAll();
|
||||
}
|
||||
|
||||
async findById(where: WhereKey): Promise<T> {
|
||||
const entity = await this.repository.findById(where);
|
||||
if (!entity) {
|
||||
throw new NotFoundException(this.notFoundMessage(where));
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
create(data: CreateInput): Promise<T> {
|
||||
return this.repository.create(data);
|
||||
}
|
||||
|
||||
async update(where: WhereKey, data: UpdateInput): Promise<T> {
|
||||
await this.findById(where);
|
||||
return this.repository.update(where, data);
|
||||
}
|
||||
|
||||
async delete(where: WhereKey): Promise<void> {
|
||||
await this.findById(where);
|
||||
await this.repository.delete(where);
|
||||
}
|
||||
|
||||
protected notFoundMessage(where: WhereKey): string {
|
||||
return `Resource not found: ${JSON.stringify(where)}`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import {
|
||||
randomBytes,
|
||||
scrypt as scryptCallback,
|
||||
timingSafeEqual,
|
||||
} from "node:crypto";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
const scrypt = promisify(scryptCallback);
|
||||
const KEY_LENGTH = 64;
|
||||
|
||||
export async function hashPassword(password: string): Promise<string> {
|
||||
const salt = randomBytes(16).toString("hex");
|
||||
const derivedKey = (await scrypt(password, salt, KEY_LENGTH)) as Buffer;
|
||||
return `${salt}:${derivedKey.toString("hex")}`;
|
||||
}
|
||||
|
||||
export async function verifyPassword(
|
||||
password: string,
|
||||
storedHash: string,
|
||||
): Promise<boolean> {
|
||||
const [salt, key] = storedHash.split(":");
|
||||
if (!salt || !key) {
|
||||
return false;
|
||||
}
|
||||
const keyBuffer = Buffer.from(key, "hex");
|
||||
const derivedKey = (await scrypt(password, salt, KEY_LENGTH)) as Buffer;
|
||||
if (derivedKey.length !== keyBuffer.length) {
|
||||
return false;
|
||||
}
|
||||
return timingSafeEqual(derivedKey, keyBuffer);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export interface IRepository<T, CreateInput, UpdateInput, WhereKey> {
|
||||
findAll(): Promise<T[]>;
|
||||
findById(where: WhereKey): Promise<T | null>;
|
||||
create(data: CreateInput): Promise<T>;
|
||||
update(where: WhereKey, data: UpdateInput): Promise<T>;
|
||||
delete(where: WhereKey): Promise<void>;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export interface IService<T, CreateInput, UpdateInput, WhereKey> {
|
||||
findAll(): Promise<T[]>;
|
||||
findById(where: WhereKey): Promise<T>;
|
||||
create(data: CreateInput): Promise<T>;
|
||||
update(where: WhereKey, data: UpdateInput): Promise<T>;
|
||||
delete(where: WhereKey): Promise<void>;
|
||||
}
|
||||
Reference in new issue
Block a user