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

79 lines
2.0 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;
key: string;
includes: string[];
priority: number;
}
export type SearchWayItemType = {
label: string;
value: string;
fullName: string;
key: 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.find<SearchWayItemType>({}).toArray()).length
return {
total,
list
}
}
export async function addSearchType(searchTypeItem: SearchTypeItem) {
const collection = await getCollection('search-type')
collection.insertOne(searchTypeItem)
}
export async function addSearchWay(searchItem: SearchWayItemType) {
const collection = await getCollection('search')
collection.insertOne(searchItem)
}