feat: 添加 flowme-channel 功能,包括获取、创建、更新和删除接口;更新 flowme 列表接口以支持时间范围和类型过滤

This commit is contained in:
2026-03-12 00:55:42 +08:00
parent 61a809ecd7
commit 99141a926e
4 changed files with 41 additions and 17 deletions

View File

@@ -45,7 +45,7 @@ export const flowme = pgTable("flowme", {
data: jsonb().default({}), data: jsonb().default({}),
channelId: uuid().references(() => flowmeChannels.id, { onDelete: 'set null' }), channelId: uuid().references(() => flowmeChannels.id, { onDelete: 'set null' }),
type: text('type').default(''), type: text('type').default(''), // muse,
source: text('source').default(''), source: text('source').default(''),
importance: integer('importance').default(0), // 重要性等级 importance: integer('importance').default(0), // 重要性等级
isArchived: boolean('isArchived').default(false), // 是否归档 isArchived: boolean('isArchived').default(false), // 是否归档

View File

@@ -2,4 +2,4 @@ import './list.ts'
// flowme channel 相关路由 // flowme channel 相关路由
import './flowme-channel/list.ts' import '../flowme-channel/list.ts'

View File

@@ -1,4 +1,4 @@
import { desc, eq, count, or, like, and } from 'drizzle-orm'; import { desc, eq, or, like, and, gte, lte } from 'drizzle-orm';
import { schema, app, db } from '@/app.ts' import { schema, app, db } from '@/app.ts'
import z from 'zod'; import z from 'zod';
@@ -11,16 +11,21 @@ app.route({
metadata: { metadata: {
args: { args: {
page: z.number().describe('页码, 默认为 1').optional(), page: z.number().describe('页码, 默认为 1').optional(),
pageSize: z.number().describe('每页数量, 默认为 20').optional(), pageSize: z.number().describe('每页数量, 默认为 100').optional(),
search: z.string().describe('搜索关键词').optional(), search: z.string().describe('搜索关键词').optional(),
channelId: z.string().describe('频道ID').optional(), channelId: z.string().describe('频道ID').optional(),
type: z.string().describe('类型').optional(),
sort: z.enum(['ASC', 'DESC']).describe('排序方式ASC 或 DESC默认为 DESC').optional(), sort: z.enum(['ASC', 'DESC']).describe('排序方式ASC 或 DESC默认为 DESC').optional(),
timeRange: z.object({
from: z.string().describe('开始时间ISO 格式').optional(),
to: z.string().describe('结束时间ISO 格式').optional(),
}).describe('时间范围过滤').optional(),
} }
} }
}).define(async (ctx) => { }).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser; const tokenUser = ctx.state.tokenUser;
const uid = tokenUser.id; const uid = tokenUser.id;
const { page = 1, pageSize = 20, search, channelId, sort = 'DESC' } = ctx.query || {}; const { page = 1, pageSize = 100, search, channelId, type, sort = 'DESC', timeRange } = ctx.query || {};
const offset = (page - 1) * pageSize; const offset = (page - 1) * pageSize;
const orderByField = sort === 'ASC' ? schema.flowme.updatedAt : desc(schema.flowme.updatedAt); const orderByField = sort === 'ASC' ? schema.flowme.updatedAt : desc(schema.flowme.updatedAt);
@@ -41,18 +46,37 @@ app.route({
eq(schema.flowme.channelId, channelId) eq(schema.flowme.channelId, channelId)
); );
} }
if (type) {
whereCondition = and(
whereCondition,
eq(schema.flowme.type, type)
);
}
if (timeRange) {
const { from, to } = timeRange;
if (from) {
whereCondition = and(
whereCondition,
gte(schema.flowme.updatedAt, new Date(from))
);
}
if (to) {
whereCondition = and(
whereCondition,
lte(schema.flowme.updatedAt, new Date(to))
);
}
}
const [list, totalCount] = await Promise.all([ const rows = await db.select()
db.select() .from(schema.flowme)
.from(schema.flowme) .where(whereCondition)
.where(whereCondition) .limit(pageSize + 1)
.limit(pageSize) .offset(offset)
.offset(offset) .orderBy(orderByField);
.orderBy(orderByField),
db.select({ count: count() }) const hasMore = rows.length > pageSize;
.from(schema.flowme) const list = hasMore ? rows.slice(0, pageSize) : rows;
.where(whereCondition)
]);
ctx.body = { ctx.body = {
list, list,
@@ -60,7 +84,7 @@ app.route({
page, page,
current: page, current: page,
pageSize, pageSize,
total: totalCount[0]?.count || 0, hasMore,
}, },
}; };
return ctx; return ctx;