add auth and user handling
This commit is contained in:
parent
db057ce342
commit
1fce467571
19 changed files with 356 additions and 32 deletions
41
src/repositories/AbstractRepository.ts
Normal file
41
src/repositories/AbstractRepository.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import { Repository, DeepPartial } from "typeorm";
|
||||
import { AppDataSource } from "../data-source";
|
||||
|
||||
export abstract class AbstractRepository<T> {
|
||||
protected repo: Repository<T>;
|
||||
|
||||
constructor(entity: { new (): T }) {
|
||||
this.repo = AppDataSource.getRepository(entity);
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<T | null> {
|
||||
return this.repo.findOne({ where: { id } as any });
|
||||
}
|
||||
|
||||
async findAll(): Promise<T[]> {
|
||||
return this.repo.find();
|
||||
}
|
||||
|
||||
async create(data: DeepPartial<T>): Promise<T> {
|
||||
const entity = this.repo.create(data);
|
||||
return this.repo.save(entity);
|
||||
}
|
||||
|
||||
/* async update(id: string, partialData: DeepPartial<T>): Promise<T> {
|
||||
await this.repo.update(id as any, partialData);
|
||||
const updated = await this.findById(id);
|
||||
if (!updated) {
|
||||
throw new Error("Entity not found after update");
|
||||
}
|
||||
return updated;
|
||||
} */
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.repo.delete(id as any);
|
||||
}
|
||||
|
||||
async save(entity: T): Promise<T> {
|
||||
return this.repo.save(entity);
|
||||
}
|
||||
|
||||
}
|
||||
12
src/repositories/UserRepository.ts
Normal file
12
src/repositories/UserRepository.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { AbstractRepository } from "./AbstractRepository";
|
||||
import { UserEntity } from "../entities/UserEntity";
|
||||
|
||||
export class UserRepository extends AbstractRepository<UserEntity> {
|
||||
constructor() {
|
||||
super(UserEntity);
|
||||
}
|
||||
|
||||
async findByUserName(userName: string): Promise<UserEntity | null> {
|
||||
return this.repo.findOne({ where: { userName } });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue