feat: 添加 flowme-life 功能,包括创建、更新、删除和列表接口,导入 life JSON 数据到数据库
This commit is contained in:
@@ -483,49 +483,3 @@ export const queryViews = pgTable("query_views", {
|
||||
index('query_title_idx').using('btree', table.title.asc().nullsLast()),
|
||||
]);
|
||||
|
||||
export const flowme = pgTable("flowme", {
|
||||
id: uuid().primaryKey().notNull().defaultRandom(),
|
||||
uid: uuid(),
|
||||
|
||||
title: text('title').default(''),
|
||||
tags: jsonb().default([]),
|
||||
summary: text('summary').default(''),
|
||||
description: text('description').default(''),
|
||||
link: text('link').default(''),
|
||||
data: jsonb().default({}),
|
||||
|
||||
channelId: uuid().references(() => flowmeChannels.id, { onDelete: 'set null' }),
|
||||
type: text('type').default(''),
|
||||
source: text('source').default(''),
|
||||
importance: integer('importance').default(0), // 重要性等级
|
||||
isArchived: boolean('isArchived').default(false), // 是否归档
|
||||
|
||||
createdAt: timestamp('createdAt').notNull().defaultNow(),
|
||||
updatedAt: timestamp('updatedAt').notNull().defaultNow().$onUpdate(() => new Date()),
|
||||
|
||||
}, (table) => [
|
||||
index('flowme_uid_idx').using('btree', table.uid.asc().nullsLast()),
|
||||
index('flowme_title_idx').using('btree', table.title.asc().nullsLast()),
|
||||
index('flowme_channel_id_idx').using('btree', table.channelId.asc().nullsLast()),
|
||||
]);
|
||||
|
||||
|
||||
export const flowmeChannels = pgTable("flowme_channels", {
|
||||
id: uuid().primaryKey().notNull().defaultRandom(),
|
||||
uid: uuid(),
|
||||
title: text('title').default(''),
|
||||
tags: jsonb().default([]),
|
||||
summary: text('summary').default(''),
|
||||
description: text('description').default(''),
|
||||
link: text('link').default(''),
|
||||
data: jsonb().default({}),
|
||||
|
||||
key: text('key').default(''),
|
||||
color: text('color').default('#007bff'),
|
||||
createdAt: timestamp('createdAt').notNull().defaultNow(),
|
||||
updatedAt: timestamp('updatedAt').notNull().defaultNow().$onUpdate(() => new Date()),
|
||||
}, (table) => [
|
||||
index('flowme_channels_uid_idx').using('btree', table.uid.asc().nullsLast()),
|
||||
index('flowme_channels_key_idx').using('btree', table.key.asc().nullsLast()),
|
||||
index('flowme_channels_title_idx').using('btree', table.title.asc().nullsLast()),
|
||||
]);
|
||||
@@ -2,4 +2,6 @@ import { pgTable, uuid, jsonb, timestamp, text } from "drizzle-orm/pg-core";
|
||||
import { InferSelectModel, InferInsertModel, desc } from "drizzle-orm";
|
||||
export * from './drizzle/schema.ts';
|
||||
|
||||
export * from './schemas/n-code-schema.ts'
|
||||
export * from './schemas/n-code-schema.ts'
|
||||
|
||||
export * from './schemas/life-schema.ts'
|
||||
75
src/db/schemas/life-schema.ts
Normal file
75
src/db/schemas/life-schema.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { pgTable, serial, text, jsonb, varchar, timestamp, unique, uuid, doublePrecision, json, integer, boolean, index, uniqueIndex, pgEnum } from "drizzle-orm/pg-core"
|
||||
import { sql, sum } from "drizzle-orm"
|
||||
|
||||
export const life = pgTable("flowme_life", {
|
||||
id: uuid().primaryKey().notNull().defaultRandom(),
|
||||
uid: uuid(),
|
||||
|
||||
title: text('title').default(''),
|
||||
tags: jsonb().default([]),
|
||||
summary: text('summary').default(''),
|
||||
description: text('description').default(''),
|
||||
link: text('link').default(''),
|
||||
data: jsonb().default({}),
|
||||
|
||||
effectiveAt: text('effectiveAt').default(''),
|
||||
type: text('type').default(''),
|
||||
prompt: text('prompt').default(''),
|
||||
taskType: text('taskType').default(''),
|
||||
taskResult: jsonb('taskResult').default({}),
|
||||
|
||||
createdAt: timestamp('createdAt').notNull().defaultNow(),
|
||||
updatedAt: timestamp('updatedAt').notNull().defaultNow().$onUpdate(() => new Date()),
|
||||
}, (table) => [
|
||||
index('life_uid_idx').using('btree', table.uid.asc().nullsLast()),
|
||||
index('life_title_idx').using('btree', table.title.asc().nullsLast()),
|
||||
index('life_effective_at_idx').using('btree', table.effectiveAt.asc().nullsLast()),
|
||||
index('life_summary_idx').using('btree', table.summary.asc().nullsLast()),
|
||||
]);
|
||||
|
||||
export const flowme = pgTable("flowme", {
|
||||
id: uuid().primaryKey().notNull().defaultRandom(),
|
||||
uid: uuid(),
|
||||
|
||||
title: text('title').default(''),
|
||||
tags: jsonb().default([]),
|
||||
summary: text('summary').default(''),
|
||||
description: text('description').default(''),
|
||||
link: text('link').default(''),
|
||||
data: jsonb().default({}),
|
||||
|
||||
channelId: uuid().references(() => flowmeChannels.id, { onDelete: 'set null' }),
|
||||
type: text('type').default(''),
|
||||
source: text('source').default(''),
|
||||
importance: integer('importance').default(0), // 重要性等级
|
||||
isArchived: boolean('isArchived').default(false), // 是否归档
|
||||
|
||||
createdAt: timestamp('createdAt').notNull().defaultNow(),
|
||||
updatedAt: timestamp('updatedAt').notNull().defaultNow().$onUpdate(() => new Date()),
|
||||
|
||||
}, (table) => [
|
||||
index('flowme_uid_idx').using('btree', table.uid.asc().nullsLast()),
|
||||
index('flowme_title_idx').using('btree', table.title.asc().nullsLast()),
|
||||
index('flowme_channel_id_idx').using('btree', table.channelId.asc().nullsLast()),
|
||||
]);
|
||||
|
||||
|
||||
export const flowmeChannels = pgTable("flowme_channels", {
|
||||
id: uuid().primaryKey().notNull().defaultRandom(),
|
||||
uid: uuid(),
|
||||
title: text('title').default(''),
|
||||
tags: jsonb().default([]),
|
||||
summary: text('summary').default(''),
|
||||
description: text('description').default(''),
|
||||
link: text('link').default(''),
|
||||
data: jsonb().default({}),
|
||||
|
||||
key: text('key').default(''),
|
||||
color: text('color').default('#007bff'),
|
||||
createdAt: timestamp('createdAt').notNull().defaultNow(),
|
||||
updatedAt: timestamp('updatedAt').notNull().defaultNow().$onUpdate(() => new Date()),
|
||||
}, (table) => [
|
||||
index('flowme_channels_uid_idx').using('btree', table.uid.asc().nullsLast()),
|
||||
index('flowme_channels_key_idx').using('btree', table.key.asc().nullsLast()),
|
||||
index('flowme_channels_title_idx').using('btree', table.title.asc().nullsLast()),
|
||||
]);
|
||||
1
src/routes/flowme-life/index.ts
Normal file
1
src/routes/flowme-life/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts'
|
||||
202
src/routes/flowme-life/list.ts
Normal file
202
src/routes/flowme-life/list.ts
Normal file
@@ -0,0 +1,202 @@
|
||||
import { desc, eq, count, or, like, and } from 'drizzle-orm';
|
||||
import { schema, app, db } from '@/app.ts'
|
||||
import z from 'zod';
|
||||
app.route({
|
||||
path: 'flowme-life',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
description: '获取 flowme-life 列表',
|
||||
metadata: {
|
||||
args: {
|
||||
page: z.number().describe('页码, 默认为 1').optional(),
|
||||
pageSize: z.number().describe('每页数量, 默认为 20').optional(),
|
||||
search: z.string().describe('搜索关键词').optional(),
|
||||
sort: z.enum(['ASC', 'DESC']).describe('排序方式,ASC 或 DESC,默认为 DESC').optional(),
|
||||
}
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const uid = tokenUser.id;
|
||||
const { page = 1, pageSize = 20, search, sort = 'DESC' } = ctx.query || {};
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
const orderByField = sort === 'ASC' ? schema.life.updatedAt : desc(schema.life.updatedAt);
|
||||
|
||||
let whereCondition = eq(schema.life.uid, uid);
|
||||
if (search) {
|
||||
whereCondition = and(
|
||||
eq(schema.life.uid, uid),
|
||||
or(
|
||||
like(schema.life.title, `%${search}%`),
|
||||
like(schema.life.summary, `%${search}%`)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const [list, totalCount] = await Promise.all([
|
||||
db.select()
|
||||
.from(schema.life)
|
||||
.where(whereCondition)
|
||||
.limit(pageSize)
|
||||
.offset(offset)
|
||||
.orderBy(orderByField),
|
||||
db.select({ count: count() })
|
||||
.from(schema.life)
|
||||
.where(whereCondition)
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
list,
|
||||
pagination: {
|
||||
page,
|
||||
current: page,
|
||||
pageSize,
|
||||
total: totalCount[0]?.count || 0,
|
||||
},
|
||||
};
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
app.route({
|
||||
path: 'flowme-life',
|
||||
key: 'create',
|
||||
middleware: ['auth'],
|
||||
description: '创建一个 flowme-life',
|
||||
metadata: {
|
||||
args: {
|
||||
data: z.object({
|
||||
title: z.string().describe('标题').optional(),
|
||||
summary: z.string().describe('摘要').optional(),
|
||||
description: z.string().describe('描述').optional(),
|
||||
tags: z.array(z.string()).describe('标签').optional(),
|
||||
link: z.string().describe('链接').optional(),
|
||||
data: z.record(z.string(), z.any()).describe('数据').optional(),
|
||||
effectiveAt: z.string().describe('生效日期').optional(),
|
||||
type: z.string().describe('类型').optional(),
|
||||
prompt: z.string().describe('提示词').optional(),
|
||||
taskType: z.string().describe('任务类型').optional(),
|
||||
taskResult: z.record(z.string(), z.any()).describe('任务结果').optional(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const { uid, updatedAt, createdAt, ...rest } = ctx.query.data || {};
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const lifeItem = await db.insert(schema.life).values({
|
||||
title: rest.title || '',
|
||||
summary: rest.summary || '',
|
||||
description: rest.description || '',
|
||||
tags: rest.tags || [],
|
||||
link: rest.link || '',
|
||||
data: rest.data || {},
|
||||
effectiveAt: rest.effectiveAt || '',
|
||||
type: rest.type || '',
|
||||
prompt: rest.prompt || '',
|
||||
taskType: rest.taskType || '',
|
||||
taskResult: rest.taskResult || {},
|
||||
uid: tokenUser.id,
|
||||
}).returning();
|
||||
ctx.body = lifeItem;
|
||||
}).addTo(app);
|
||||
|
||||
app.route({
|
||||
path: 'flowme-life',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
description: '更新一个 flowme-life',
|
||||
metadata: {
|
||||
args: {
|
||||
data: z.object({
|
||||
id: z.string().describe('ID'),
|
||||
title: z.string().describe('标题').optional(),
|
||||
summary: z.string().describe('摘要').optional(),
|
||||
description: z.string().describe('描述').optional(),
|
||||
tags: z.array(z.string()).describe('标签').optional(),
|
||||
link: z.string().describe('链接').optional(),
|
||||
data: z.record(z.string(), z.any()).describe('数据').optional(),
|
||||
effectiveAt: z.string().describe('生效日期').optional(),
|
||||
type: z.string().describe('类型').optional(),
|
||||
prompt: z.string().describe('提示词').optional(),
|
||||
taskType: z.string().describe('任务类型').optional(),
|
||||
taskResult: z.record(z.string(), z.any()).describe('任务结果').optional(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const { id, uid, updatedAt, createdAt, ...rest } = ctx.query.data || {};
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id 参数缺失');
|
||||
}
|
||||
const existing = await db.select().from(schema.life).where(eq(schema.life.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的 flowme-life');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限更新该 flowme-life');
|
||||
}
|
||||
const lifeItem = await db.update(schema.life).set({
|
||||
title: rest.title,
|
||||
summary: rest.summary,
|
||||
description: rest.description,
|
||||
tags: rest.tags,
|
||||
link: rest.link,
|
||||
data: rest.data,
|
||||
effectiveAt: rest.effectiveAt,
|
||||
type: rest.type,
|
||||
prompt: rest.prompt,
|
||||
taskType: rest.taskType,
|
||||
taskResult: rest.taskResult,
|
||||
}).where(eq(schema.life.id, id)).returning();
|
||||
ctx.body = lifeItem;
|
||||
}).addTo(app);
|
||||
|
||||
app.route({
|
||||
path: 'flowme-life',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除 flowme-life',
|
||||
metadata: {
|
||||
args: {
|
||||
data: z.object({
|
||||
id: z.string().describe('ID'),
|
||||
})
|
||||
}
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id } = ctx.query.data || {};
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id 参数缺失');
|
||||
}
|
||||
const existing = await db.select().from(schema.life).where(eq(schema.life.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的 flowme-life');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限删除该 flowme-life');
|
||||
}
|
||||
await db.delete(schema.life).where(eq(schema.life.id, id));
|
||||
ctx.body = { success: true };
|
||||
}).addTo(app);
|
||||
|
||||
app.route({
|
||||
path: 'flowme-life',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
description: '获取单个 flowme-life, 参数: data.id 必填',
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id } = ctx.query.data || {};
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id 参数缺失');
|
||||
}
|
||||
const existing = await db.select().from(schema.life).where(eq(schema.life.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的 flowme-life');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限查看该 flowme-life');
|
||||
}
|
||||
ctx.body = existing[0];
|
||||
}).addTo(app);
|
||||
@@ -22,4 +22,6 @@ import './query-views/index.ts';
|
||||
|
||||
import './flowme/index.ts'
|
||||
|
||||
import './n5-link/index.ts'
|
||||
import './n5-link/index.ts'
|
||||
|
||||
import './flowme-life/index.ts'
|
||||
Reference in New Issue
Block a user