ai-bot/app/_lib/data/search.ts

72 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-01-23 17:14:36 +08:00
"use server";
import { getCollection } from "../mongodb";
export type SearchTypeItem = {
name: string;
2025-02-07 19:02:22 +08:00
key?: string;
2025-01-23 17:14:36 +08:00
includes: string[];
2025-01-27 02:58:43 +08:00
_id: string;
2025-01-23 17:14:36 +08:00
priority: number;
}
export type SearchWayItemType = {
label: string;
value: string;
fullName: string;
2025-01-27 02:58:43 +08:00
_id: string;
2025-01-23 17:14:36 +08:00
}
export async function getSearchTypeList({ page = 1, pageSize = 9999 }: {
page?: number;
pageSize?: number;
}) {
const collection = await getCollection('search-type');
const startIndex = (page - 1) * pageSize;
const pipeline = [
{ $sort: { priority: 1 } },
{ $skip: startIndex },
{ $limit: pageSize },
{
$addFields: {
_id: { $toString: "$_id" }
}
}
];
const cursor = collection.aggregate<SearchTypeItem>(pipeline);
const list = await cursor.toArray();
// 计算总数量
const total = (await collection.find<SearchTypeItem>({}).toArray()).length
return {
total,
list
}
}
export async function getSearchWayList({ page = 1, pageSize = 9999 }: {
page?: number;
pageSize?: number;
}) {
const collection = await getCollection('search');
const startIndex = (page - 1) * pageSize;
const pipeline = [
{ $sort: { priority: 1 } },
{ $skip: startIndex },
{ $limit: pageSize },
{
$addFields: {
_id: { $toString: "$_id" }
}
}
];
const cursor = collection.aggregate<SearchWayItemType>(pipeline);
const list = await cursor.toArray();
// 计算总数量
2025-02-07 19:02:22 +08:00
const total = await collection.countDocuments()
2025-01-23 17:14:36 +08:00
return {
total,
list
}
}