Files
code-center/src/routes/mark/services/mark.ts
abearxiong 266b7b33de Refactor config management to use Drizzle ORM
- Replaced Sequelize with Drizzle ORM in config-related routes and models.
- Updated database queries to use Drizzle's syntax for selecting, inserting, updating, and deleting configurations.
- Removed the ConfigModel class and replaced it with direct database interactions.
- Introduced nanoid for generating unique IDs for new configurations.
- Added new routes for managing marks, including CRUD operations and versioning.
- Implemented transaction handling for critical operations in the MarkModel.
- Enhanced error handling and validation in routes.
2026-02-05 16:31:11 +08:00

86 lines
2.6 KiB
TypeScript

import { eq, desc, asc, and, like, or, count } from 'drizzle-orm';
import { app, db, schema } from '../../../app.ts';
export class MarkServices {
static getList = async (opts: {
/** 查询用户的 */
uid?: string;
query?: {
page?: number;
pageSize?: number;
search?: string;
markType?: string;
sort?: string;
};
/**
* 查询类型
* simple: 简单查询 默认
*/
queryType?: string;
}) => {
const { uid, query = {} } = opts;
const { page = 1, pageSize = 999, search, sort = 'DESC' } = query;
const conditions = [];
if (uid) {
conditions.push(eq(schema.microMark.uid, uid));
}
if (search) {
conditions.push(
or(
like(schema.microMark.title, `%${search}%`),
like(schema.microMark.summary, `%${search}%`)
)
);
}
if (opts.query?.markType) {
conditions.push(eq(schema.microMark.markType, opts.query.markType));
}
const whereClause = conditions.length > 0 ? and(...conditions) : undefined;
const queryType = opts.queryType || 'simple';
let selectFields: any = {};
if (queryType === 'simple') {
// Exclude data, config, cover, description
selectFields = {
id: schema.microMark.id,
title: schema.microMark.title,
tags: schema.microMark.tags,
uname: schema.microMark.uname,
uid: schema.microMark.uid,
createdAt: schema.microMark.createdAt,
updatedAt: schema.microMark.updatedAt,
thumbnail: schema.microMark.thumbnail,
link: schema.microMark.link,
summary: schema.microMark.summary,
markType: schema.microMark.markType,
puid: schema.microMark.puid,
deletedAt: schema.microMark.deletedAt,
version: schema.microMark.version,
fileList: schema.microMark.fileList,
key: schema.microMark.key,
};
}
const orderByField = sort === 'ASC' ? asc(schema.microMark.updatedAt) : desc(schema.microMark.updatedAt);
const [rows, totalResult] = await Promise.all([
queryType === 'simple'
? db.select(selectFields).from(schema.microMark).where(whereClause).orderBy(orderByField).limit(pageSize).offset((page - 1) * pageSize)
: db.select().from(schema.microMark).where(whereClause).orderBy(orderByField).limit(pageSize).offset((page - 1) * pageSize),
db.select({ count: count() }).from(schema.microMark).where(whereClause)
]);
return {
pagination: {
current: page,
pageSize,
total: totalResult[0]?.count || 0,
},
list: rows,
};
};
}