feat: migrate backend storage to postgres
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
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';
|
||||
|
||||
@@ -25,31 +25,48 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
const now = new Date().toISOString();
|
||||
|
||||
const result = db
|
||||
.prepare<unknown, [string, string, string, string, number, string, string]>(
|
||||
`
|
||||
INSERT INTO telegram_notification_configs (name, bot_token, chat_id, notification_types, is_enabled, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
)
|
||||
.run(
|
||||
const { rows } = await query<{
|
||||
id: number;
|
||||
name: string;
|
||||
bot_token: string;
|
||||
chat_id: string;
|
||||
notification_types: string;
|
||||
is_enabled: boolean;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}>(
|
||||
`INSERT INTO telegram_notification_configs (
|
||||
name,
|
||||
bot_token,
|
||||
chat_id,
|
||||
notification_types,
|
||||
is_enabled,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, name, bot_token, chat_id, notification_types, is_enabled, created_at, updated_at`,
|
||||
[
|
||||
body.name,
|
||||
body.botToken,
|
||||
body.chatId,
|
||||
JSON.stringify(notificationTypes),
|
||||
body.isEnabled !== false ? 1 : 0,
|
||||
body.isEnabled !== false,
|
||||
now,
|
||||
now,
|
||||
);
|
||||
],
|
||||
);
|
||||
|
||||
const row = rows[0];
|
||||
|
||||
return useResponseSuccess({
|
||||
id: result.lastInsertRowid,
|
||||
name: body.name,
|
||||
botToken: body.botToken,
|
||||
chatId: body.chatId,
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
botToken: row.bot_token,
|
||||
chatId: row.chat_id,
|
||||
notificationTypes,
|
||||
isEnabled: body.isEnabled !== false,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
isEnabled: row.is_enabled,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user