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

72 lines
1.7 KiB
TypeScript

"use server";
import { getCollection } from "../mongodb";
export type SearchTypeItem = {
name: string;
key?: string;
includes: string[];
_id: string;
priority: number;
}
export type SearchWayItemType = {
label: string;
value: string;
fullName: string;
_id: string;
}
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();
// 计算总数量
const total = await collection.countDocuments()
return {
total,
list
}
}