feat: migrate backend storage to postgres
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { existsSync } from 'node:fs';
|
||||
|
||||
import db from './sqlite';
|
||||
import { query } from './db';
|
||||
|
||||
interface MediaRow {
|
||||
id: number;
|
||||
@@ -47,7 +47,7 @@ export interface MediaMessage {
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
available: boolean;
|
||||
downloadUrl: string | null;
|
||||
downloadUrl: null | string;
|
||||
}
|
||||
|
||||
function mapMediaRow(row: MediaRow): MediaMessage {
|
||||
@@ -78,40 +78,85 @@ function mapMediaRow(row: MediaRow): MediaMessage {
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchMediaMessages(params: {
|
||||
limit?: number;
|
||||
fileTypes?: string[];
|
||||
} = {}) {
|
||||
const clauses: string[] = [];
|
||||
const bindParams: Record<string, unknown> = {};
|
||||
export async function fetchMediaMessages(
|
||||
params: {
|
||||
fileTypes?: string[];
|
||||
limit?: number;
|
||||
} = {},
|
||||
) {
|
||||
const whereClauses: string[] = [];
|
||||
const queryParams: any[] = [];
|
||||
|
||||
if (params.fileTypes && params.fileTypes.length > 0) {
|
||||
clauses.push(
|
||||
`file_type IN (${params.fileTypes.map((_, index) => `@type${index}`).join(', ')})`,
|
||||
);
|
||||
params.fileTypes.forEach((type, index) => {
|
||||
bindParams[`type${index}`] = type;
|
||||
const placeholders = params.fileTypes.map((type) => {
|
||||
queryParams.push(type);
|
||||
return `$${queryParams.length}`;
|
||||
});
|
||||
whereClauses.push(`file_type IN (${placeholders.join(', ')})`);
|
||||
}
|
||||
|
||||
const where = clauses.length > 0 ? `WHERE ${clauses.join(' AND ')}` : '';
|
||||
const where =
|
||||
whereClauses.length > 0 ? `WHERE ${whereClauses.join(' AND ')}` : '';
|
||||
const limitClause =
|
||||
params.limit && params.limit > 0 ? `LIMIT ${Number(params.limit)}` : '';
|
||||
|
||||
const stmt = db.prepare<MediaRow>(
|
||||
`SELECT id, chat_id, message_id, user_id, username, display_name, file_type, file_id, file_unique_id, caption, file_name, file_path, file_size, mime_type, duration, width, height, forwarded_to, created_at, updated_at FROM finance_media_messages ${where} ORDER BY datetime(created_at) DESC, id DESC ${limitClause}`,
|
||||
const { rows } = await query<MediaRow>(
|
||||
`SELECT id,
|
||||
chat_id,
|
||||
message_id,
|
||||
user_id,
|
||||
username,
|
||||
display_name,
|
||||
file_type,
|
||||
file_id,
|
||||
file_unique_id,
|
||||
caption,
|
||||
file_name,
|
||||
file_path,
|
||||
file_size,
|
||||
mime_type,
|
||||
duration,
|
||||
width,
|
||||
height,
|
||||
forwarded_to,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM finance_media_messages
|
||||
${where}
|
||||
ORDER BY created_at DESC, id DESC
|
||||
${limitClause}`,
|
||||
queryParams,
|
||||
);
|
||||
|
||||
return stmt.all(bindParams).map(mapMediaRow);
|
||||
return rows.map((row) => mapMediaRow(row));
|
||||
}
|
||||
|
||||
export function getMediaMessageById(id: number) {
|
||||
const stmt = db.prepare<MediaRow>(
|
||||
`SELECT id, chat_id, message_id, user_id, username, display_name, file_type, file_id, file_unique_id, caption, file_name, file_path, file_size, mime_type, duration, width, height, forwarded_to, created_at, updated_at FROM finance_media_messages WHERE id = ?`,
|
||||
export async function getMediaMessageById(id: number) {
|
||||
const { rows } = await query<MediaRow>(
|
||||
`SELECT id,
|
||||
chat_id,
|
||||
message_id,
|
||||
user_id,
|
||||
username,
|
||||
display_name,
|
||||
file_type,
|
||||
file_id,
|
||||
file_unique_id,
|
||||
caption,
|
||||
file_name,
|
||||
file_path,
|
||||
file_size,
|
||||
mime_type,
|
||||
duration,
|
||||
width,
|
||||
height,
|
||||
forwarded_to,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM finance_media_messages
|
||||
WHERE id = $1`,
|
||||
[id],
|
||||
);
|
||||
|
||||
const row = stmt.get(id);
|
||||
|
||||
const row = rows[0];
|
||||
return row ? mapMediaRow(row) : null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user