feat: migrate backend storage to postgres
Some checks failed
Deploy to Production / Build and Test (push) Successful in 10m51s
Deploy to Production / Deploy to Server (push) Failing after 6m41s

This commit is contained in:
你的用户名
2025-11-06 22:01:50 +08:00
parent 3646405a47
commit b68511b2e2
28 changed files with 2641 additions and 1801 deletions

View File

@@ -1,17 +1,19 @@
import db from '~/utils/sqlite';
import { query } from '~/utils/db';
import { useResponseError, useResponseSuccess } from '~/utils/response';
export default defineEventHandler((event) => {
const id = event.context.params?.id;
if (!id) {
export default defineEventHandler(async (event) => {
const idParam = event.context.params?.id;
const id = Number(idParam);
if (!idParam || Number.isNaN(id)) {
return useResponseError('缺少ID参数', -1);
}
const result = db
.prepare('DELETE FROM telegram_notification_configs WHERE id = ?')
.run(id);
const result = await query(
'DELETE FROM telegram_notification_configs WHERE id = $1',
[id],
);
if (result.changes === 0) {
if (result.rowCount === 0) {
return useResponseError('配置不存在或删除失败', -1);
}

View File

@@ -1,28 +1,34 @@
import { readBody } from 'h3';
import db from '~/utils/sqlite';
import { query } from '~/utils/db';
import { useResponseError, useResponseSuccess } from '~/utils/response';
import { testTelegramConfig } from '~/utils/telegram-bot';
export default defineEventHandler(async (event) => {
const id = event.context.params?.id;
if (!id) {
const idParam = event.context.params?.id;
const id = Number(idParam);
if (!idParam || Number.isNaN(id)) {
return useResponseError('缺少ID参数', -1);
}
const body = await readBody(event);
// 如果更新了botToken或chatId需要测试配置
if (body.botToken || body.chatId) {
const existing = db
.prepare<{ bot_token: string; chat_id: string }>('SELECT bot_token, chat_id FROM telegram_notification_configs WHERE id = ?')
.get(id);
if (body.botToken !== undefined || body.chatId !== undefined) {
const { rows } = await query<{
bot_token: string;
chat_id: string;
}>(
'SELECT bot_token, chat_id FROM telegram_notification_configs WHERE id = $1',
[id],
);
const existing = rows[0];
if (!existing) {
return useResponseError('配置不存在', -1);
}
const tokenToTest = body.botToken || existing.bot_token;
const chatIdToTest = body.chatId || existing.chat_id;
const tokenToTest = body.botToken ?? existing.bot_token;
const chatIdToTest = body.chatId ?? existing.chat_id;
const testResult = await testTelegramConfig(tokenToTest, chatIdToTest);
if (!testResult.success) {
@@ -34,51 +40,65 @@ export default defineEventHandler(async (event) => {
}
const updates: string[] = [];
const values: (string | number)[] = [];
const values: any[] = [];
if (body.name !== undefined) {
updates.push('name = ?');
values.push(body.name);
updates.push(`name = $${values.length}`);
}
if (body.botToken !== undefined) {
updates.push('bot_token = ?');
values.push(body.botToken);
updates.push(`bot_token = $${values.length}`);
}
if (body.chatId !== undefined) {
updates.push('chat_id = ?');
values.push(body.chatId);
updates.push(`chat_id = $${values.length}`);
}
if (body.notificationTypes !== undefined) {
updates.push('notification_types = ?');
values.push(JSON.stringify(body.notificationTypes));
updates.push(`notification_types = $${values.length}`);
}
if (body.isEnabled !== undefined) {
updates.push('is_enabled = ?');
values.push(body.isEnabled ? 1 : 0);
values.push(body.isEnabled !== false);
updates.push(`is_enabled = $${values.length}`);
}
if (updates.length === 0) {
return useResponseError('没有可更新的字段', -1);
}
updates.push('updated_at = ?');
values.push(new Date().toISOString());
updates.push(`updated_at = $${values.length}`);
values.push(id);
const idPosition = values.length;
db.prepare(`UPDATE telegram_notification_configs SET ${updates.join(', ')} WHERE id = ?`).run(
...values,
const updateResult = await query(
`UPDATE telegram_notification_configs
SET ${updates.join(', ')}
WHERE id = $${idPosition}`,
values,
);
const updated = db
.prepare<{ id: number; name: string; bot_token: string; chat_id: string; notification_types: string; is_enabled: number; created_at: string; updated_at: string }>(
'SELECT * FROM telegram_notification_configs WHERE id = ?',
)
.get(id);
if (updateResult.rowCount === 0) {
return useResponseError('配置不存在', -1);
}
const { rows: updatedRows } = await query<{
id: number;
name: string;
bot_token: string;
chat_id: string;
notification_types: string;
is_enabled: boolean;
created_at: string;
updated_at: string;
}>('SELECT * FROM telegram_notification_configs WHERE id = $1', [id]);
const updated = updatedRows[0];
if (!updated) {
return useResponseError('更新失败', -1);
}
@@ -89,7 +109,7 @@ export default defineEventHandler(async (event) => {
botToken: updated.bot_token,
chatId: updated.chat_id,
notificationTypes: JSON.parse(updated.notification_types) as string[],
isEnabled: updated.is_enabled === 1,
isEnabled: updated.is_enabled,
createdAt: updated.created_at,
updatedAt: updated.updated_at,
});