entity.repository.ts•5.74 kB
/**
* Entity repository implementation
*/
import {
Entity,
EntityCreateInput,
EntityOrderByWithRelationInput
} from '../models/prisma.types';
import { prisma } from '../utils/db';
import {
ISearchableRepository,
SearchOptions,
SearchResult
} from './base.repository';
import { logger } from '../utils/logger';
/**
* Repository for entity data access
*/
export class EntityRepository implements ISearchableRepository<Entity, number> {
/**
* Find entity by ID
* @param id Entity ID
* @returns Entity or null if not found
*/
async findById(id: number): Promise<Entity | null> {
try {
return await prisma.entity.findUnique({
where: { id }
});
} catch (error) {
logger.error('Error finding entity by ID:', error);
throw error;
}
}
/**
* Find all entities
* @returns Array of entities
*/
async findAll(): Promise<Entity[]> {
try {
return await prisma.entity.findMany();
} catch (error) {
logger.error('Error finding all entities:', error);
throw error;
}
}
/**
* Create a new entity
* @param data Entity data
* @returns Created entity
*/
async create(data: Omit<Entity, 'id' | 'createdAt' | 'updatedAt'>): Promise<Entity> {
try {
return await prisma.entity.create({
data: data as EntityCreateInput
});
} catch (error) {
logger.error('Error creating entity:', error);
throw error;
}
}
/**
* Update an entity
* @param id Entity ID
* @param data Updated entity data
* @returns Updated entity
*/
async update(id: number, data: Partial<Entity>): Promise<Entity> {
try {
return await prisma.entity.update({
where: { id },
data
});
} catch (error) {
logger.error('Error updating entity:', error);
throw error;
}
}
/**
* Delete an entity
* @param id Entity ID
* @returns True if deleted, false otherwise
*/
async delete(id: number): Promise<boolean> {
try {
await prisma.entity.delete({
where: { id }
});
return true;
} catch (error) {
logger.error('Error deleting entity:', error);
return false;
}
}
/**
* Search entities with filtering, sorting, and pagination
* @param options Search options
* @returns Search results with pagination metadata
*/
async search(options: SearchOptions): Promise<SearchResult<Entity>> {
const { skip = 0, take = 10, orderBy = { articleCount: 'desc' }, where = {} } = options;
const page = Math.floor(skip / take) + 1;
try {
// Get total count for pagination
const total = await prisma.entity.count({ where });
// Get results with pagination
const items = await prisma.entity.findMany({
skip,
take,
where,
orderBy: orderBy as EntityOrderByWithRelationInput
});
const totalPages = Math.ceil(total / take);
return {
items,
total,
page,
limit: take,
totalPages,
hasNext: page < totalPages,
hasPrevious: page > 1
};
} catch (error) {
logger.error('Error searching entities:', error);
throw error;
}
}
/**
* Find entity by name
* @param name Entity name
* @returns Entity or null if not found
*/
async findByName(name: string): Promise<Entity | null> {
try {
return await prisma.entity.findUnique({
where: { name }
});
} catch (error) {
logger.error('Error finding entity by name:', error);
throw error;
}
}
/**
* Find or create an entity by name and type
* @param name Entity name
* @param type Entity type
* @returns Entity
*/
async findOrCreate(name: string, type: string): Promise<Entity> {
try {
const existingEntity = await this.findByName(name);
if (existingEntity) {
return existingEntity;
}
return await prisma.entity.create({
data: {
name,
type,
articleCount: 1
}
});
} catch (error) {
logger.error('Error finding or creating entity:', error);
throw error;
}
}
/**
* Get entity with associated articles
* @param id Entity ID
* @returns Entity with articles or null if not found
*/
async getEntityWithArticles(id: number): Promise<any | null> {
try {
return await prisma.entity.findUnique({
where: { id },
include: {
articles: {
include: {
article: true
}
}
}
});
} catch (error) {
logger.error('Error getting entity with articles:', error);
throw error;
}
}
/**
* Get entities by type
* @param type Entity type (PERSON, ORGANIZATION, LOCATION)
* @param limit Maximum number of results
* @returns Array of entities
*/
async getEntitiesByType(type: string, limit = 50): Promise<Entity[]> {
try {
return await prisma.entity.findMany({
where: { type },
take: limit,
orderBy: { articleCount: 'desc' }
});
} catch (error) {
logger.error('Error getting entities by type:', error);
throw error;
}
}
/**
* Increment article count for an entity
* @param id Entity ID
* @returns Updated entity
*/
async incrementArticleCount(id: number): Promise<Entity> {
try {
return await prisma.entity.update({
where: { id },
data: {
articleCount: {
increment: 1
}
}
});
} catch (error) {
logger.error('Error incrementing entity article count:', error);
throw error;
}
}
}