You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

74 lines
2.0 KiB

import { dbGlobal } from "drizzle-pkg/lib/db";
import { users } from "drizzle-pkg/lib/schema/auth";
import { and, count, desc, eq, like, or, asc } from "drizzle-orm";
export default defineWrappedResponseHandler(async (event) => {
const query = getQuery(event);
const page = query.page ? Number(query.page) : 1;
const pageSize = query.pageSize ? Number(query.pageSize) : 10;
const search = query.search as string | undefined;
const status = query.status as string | undefined;
const role = query.role as string | undefined;
const sortBy = query.sortBy as string | undefined;
const sortOrder = query.sortOrder as string | undefined;
const offset = (page - 1) * pageSize;
const searchPattern = search ? `%${search}%` : undefined;
// Build conditions array
const conditions: any[] = [];
if (searchPattern) {
conditions.push(
or(
like(users.username, searchPattern),
like(users.email, searchPattern),
like(users.nickname, searchPattern),
)
);
}
if (status) {
conditions.push(eq(users.status, status));
}
if (role) {
conditions.push(eq(users.role, role));
}
const whereClause = conditions.length > 0 ? and(...conditions) : undefined;
// Build orderBy
const orderColumn = sortBy === "username" ? users.username : users.createdAt;
const orderDirection = sortOrder === "asc" ? asc(orderColumn) : desc(orderColumn);
const [totalResult] = await dbGlobal
.select({ total: count() })
.from(users)
.where(whereClause);
const list = await dbGlobal
.select({
id: users.id,
username: users.username,
email: users.email,
nickname: users.nickname,
avatar: users.avatar,
role: users.role,
status: users.status,
createdAt: users.createdAt,
})
.from(users)
.where(whereClause)
.orderBy(orderDirection)
.limit(pageSize)
.offset(offset);
return R.success({
list,
total: totalResult?.total ?? 0,
page,
pageSize,
totalPages: Math.ceil((totalResult?.total ?? 0) / pageSize),
});
});