feat: add topic alias resolution APIs

This commit is contained in:
2026-05-11 11:53:56 +09:00
parent ba03ecb85b
commit 428f5021e8
9 changed files with 252 additions and 31 deletions

View File

@@ -1,7 +1,7 @@
import type { Kysely, Transaction } from 'kysely';
import type { IdentityDatabaseSchema } from '../types/database';
import type { TopicRecord } from '../types/domain';
import type { TopicAliasRecord, TopicRecord } from '../types/domain';
export type DatabaseExecutor = Kysely<IdentityDatabaseSchema> | Transaction<IdentityDatabaseSchema>;
@@ -20,14 +20,48 @@ export async function findTopicRowByNormalizedName(
.executeTakeFirst();
}
export async function findTopicRowByNormalizedAlias(
executor: DatabaseExecutor,
normalizedAlias: string,
): Promise<TopicRecord | undefined> {
return executor
.selectFrom('topic_aliases')
.innerJoin('topics', 'topics.id', 'topic_aliases.topic_id')
.selectAll('topics')
.where('topic_aliases.normalized_alias', '=', normalizedAlias)
.executeTakeFirst();
}
export async function findTopicRowByNameOrAlias(
executor: DatabaseExecutor,
normalizedName: string,
): Promise<TopicRecord | undefined> {
const directMatch = await findTopicRowByNormalizedName(executor, normalizedName);
if (directMatch) {
return directMatch;
}
return findTopicRowByNormalizedAlias(executor, normalizedName);
}
export async function listTopicAliasRowsForTopicId(
executor: DatabaseExecutor,
topicId: string,
): Promise<TopicAliasRecord[]> {
return executor
.selectFrom('topic_aliases')
.selectAll()
.where('topic_id', '=', topicId)
.orderBy('is_primary', 'desc')
.orderBy('normalized_alias', 'asc')
.execute();
}
export async function listTopicRows(
executor: DatabaseExecutor,
limit?: number,
): Promise<TopicRecord[]> {
let query = executor
.selectFrom('topics')
.selectAll()
.orderBy('normalized_name', 'asc');
let query = executor.selectFrom('topics').selectAll().orderBy('normalized_name', 'asc');
if (limit !== undefined) {
query = query.limit(limit);