fix: correct import ordering and update tests for drizzle-orm migration
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { drizzle as drizzlePostgres } from "drizzle-orm/node-postgres";
|
||||
import { drizzle as drizzleSqlite } from "drizzle-orm/better-sqlite3";
|
||||
import Database from "better-sqlite3";
|
||||
import { drizzle as drizzleSqlite } from "drizzle-orm/better-sqlite3";
|
||||
import { drizzle as drizzlePostgres } from "drizzle-orm/node-postgres";
|
||||
import { Pool } from "pg";
|
||||
import { config } from "../config";
|
||||
import { createChildLogger } from "../logger";
|
||||
@@ -8,7 +8,10 @@ import * as schema from "./schema";
|
||||
|
||||
const logger = createChildLogger("drizzle");
|
||||
|
||||
let db: ReturnType<typeof drizzlePostgres> | ReturnType<typeof drizzleSqlite> | null = null;
|
||||
let db:
|
||||
| ReturnType<typeof drizzlePostgres>
|
||||
| ReturnType<typeof drizzleSqlite>
|
||||
| null = null;
|
||||
|
||||
/**
|
||||
* Initialize the database connection based on DATABASE_TYPE config
|
||||
@@ -51,7 +54,7 @@ export async function initializeDatabase() {
|
||||
export function getDatabase() {
|
||||
if (db === null) {
|
||||
throw new Error(
|
||||
"Database not initialized. Call initializeDatabase() first."
|
||||
"Database not initialized. Call initializeDatabase() first.",
|
||||
);
|
||||
}
|
||||
return db;
|
||||
|
||||
+11
-17
@@ -1,18 +1,18 @@
|
||||
import {
|
||||
bigint as pgBigint,
|
||||
foreignKey as pgForeignKey,
|
||||
index as pgIndex,
|
||||
integer as pgInteger,
|
||||
real as pgReal,
|
||||
pgTable,
|
||||
text as pgText,
|
||||
integer as pgInteger,
|
||||
bigint as pgBigint,
|
||||
real as pgReal,
|
||||
index as pgIndex,
|
||||
foreignKey as pgForeignKey,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import {
|
||||
sqliteTable,
|
||||
text as sqliteText,
|
||||
index as sqliteIndex,
|
||||
integer as sqliteInteger,
|
||||
real as sqliteReal,
|
||||
index as sqliteIndex,
|
||||
sqliteTable,
|
||||
text as sqliteText,
|
||||
} from "drizzle-orm/sqlite-core";
|
||||
import { config } from "../config";
|
||||
|
||||
@@ -257,14 +257,10 @@ export const sqliteUIStateTable = sqliteTable("ui_state", {
|
||||
// ========================================
|
||||
|
||||
export const muxerJobsTable =
|
||||
config.DATABASE_TYPE === "postgres"
|
||||
? pgMuxerJobsTable
|
||||
: sqliteMuxerJobsTable;
|
||||
config.DATABASE_TYPE === "postgres" ? pgMuxerJobsTable : sqliteMuxerJobsTable;
|
||||
|
||||
export const messagesTable =
|
||||
config.DATABASE_TYPE === "postgres"
|
||||
? pgMessagesTable
|
||||
: sqliteMessagesTable;
|
||||
config.DATABASE_TYPE === "postgres" ? pgMessagesTable : sqliteMessagesTable;
|
||||
|
||||
export const attachmentsTable =
|
||||
config.DATABASE_TYPE === "postgres"
|
||||
@@ -272,9 +268,7 @@ export const attachmentsTable =
|
||||
: sqliteAttachmentsTable;
|
||||
|
||||
export const uiStateTable =
|
||||
config.DATABASE_TYPE === "postgres"
|
||||
? pgUIStateTable
|
||||
: sqliteUIStateTable;
|
||||
config.DATABASE_TYPE === "postgres" ? pgUIStateTable : sqliteUIStateTable;
|
||||
|
||||
// Export table types for use in queries
|
||||
export type MuxerJob = typeof muxerJobsTable.$inferSelect;
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import "@snazzah/davey";
|
||||
import "dotenv/config";
|
||||
import { Client } from "discord.js-selfbot-v13";
|
||||
import { config } from "./config";
|
||||
import { initializeDatabase, closeDatabase } from "./database/drizzle";
|
||||
import { closeDatabase, initializeDatabase } from "./database/drizzle";
|
||||
import { createChildLogger } from "./logger";
|
||||
import { startPendingAIAnalysisWorker } from "./moderation/aiAnalyzer";
|
||||
import { syncBacklogMessages } from "./moderation/backlogSync";
|
||||
|
||||
@@ -245,9 +245,7 @@ Satu JSON object per pesan dalam array.`,
|
||||
return { results, raw: response };
|
||||
}
|
||||
|
||||
async function analyzeAndStoreBatch(
|
||||
messages: MessageRecord[],
|
||||
): Promise<void> {
|
||||
async function analyzeAndStoreBatch(messages: MessageRecord[]): Promise<void> {
|
||||
if (messages.length === 0) return;
|
||||
|
||||
const analyzableMessages = messages.filter(
|
||||
@@ -359,9 +357,7 @@ async function drainQueue(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
export function queueMessageAnalysis(
|
||||
messageId: string,
|
||||
): void {
|
||||
export function queueMessageAnalysis(messageId: string): void {
|
||||
if (!config.AI_ANALYSIS_ENABLED) return;
|
||||
logger.debug({ messageId }, "Queueing AI analysis");
|
||||
queuedMessageIds.add(messageId);
|
||||
|
||||
@@ -40,9 +40,7 @@ async function syncChannelMessages(
|
||||
return synced;
|
||||
}
|
||||
|
||||
export async function syncBacklogMessages(
|
||||
client: Client,
|
||||
): Promise<void> {
|
||||
export async function syncBacklogMessages(client: Client): Promise<void> {
|
||||
if (!config.MONITOR_GUILD_ID) {
|
||||
logger.warn("MONITOR_GUILD_ID not configured, skipping backlog sync");
|
||||
return;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { Client, Message } from "discord.js-selfbot-v13";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { config } from "../config";
|
||||
import { createChildLogger } from "../logger";
|
||||
import { getDatabase } from "../database/drizzle";
|
||||
import { messagesTable } from "../database/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { createChildLogger } from "../logger";
|
||||
import { queueMessageAnalysis } from "./aiAnalyzer";
|
||||
import {
|
||||
getDisplayContent,
|
||||
@@ -94,9 +94,7 @@ export async function captureMessage(
|
||||
);
|
||||
}
|
||||
|
||||
export function registerMessageCapture(
|
||||
client: Client,
|
||||
): void {
|
||||
export function registerMessageCapture(client: Client): void {
|
||||
client.on("messageCreate", async (message) => {
|
||||
if (!message.guildId || message.guildId !== config.MONITOR_GUILD_ID) return;
|
||||
if (message.author?.bot) return;
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import { and, asc, desc, eq, isNull, or } from "drizzle-orm";
|
||||
import { getDatabase } from "../database/drizzle";
|
||||
import { messagesTable, attachmentsTable } from "../database/schema";
|
||||
import { eq, or, desc, asc, and, isNull } from "drizzle-orm";
|
||||
import { attachmentsTable, messagesTable } from "../database/schema";
|
||||
import { createChildLogger } from "../logger";
|
||||
import type { AttachmentRecord, MessageRecord } from "./types";
|
||||
|
||||
const logger = createChildLogger("message-store");
|
||||
|
||||
export async function insertMessage(
|
||||
message: MessageRecord,
|
||||
): Promise<void> {
|
||||
export async function insertMessage(message: MessageRecord): Promise<void> {
|
||||
try {
|
||||
const db = getDatabase() as any;
|
||||
await db.insert(messagesTable).values(message).onConflictDoNothing();
|
||||
|
||||
+13
-8
@@ -1,6 +1,9 @@
|
||||
import { getDatabase as getDrizzleDatabase, initializeDatabase } from "./database/drizzle";
|
||||
import { and, asc, eq, lt, sql } from "drizzle-orm";
|
||||
import {
|
||||
getDatabase as getDrizzleDatabase,
|
||||
initializeDatabase,
|
||||
} from "./database/drizzle";
|
||||
import { muxerJobsTable, uiStateTable } from "./database/schema";
|
||||
import { eq, asc, lt, and, sql } from "drizzle-orm";
|
||||
import { createChildLogger } from "./logger";
|
||||
|
||||
const logger = createChildLogger("muxer-queue");
|
||||
@@ -224,9 +227,10 @@ export async function cleanupCompletedJobs(
|
||||
),
|
||||
);
|
||||
|
||||
const deletedCount = typeof result === "object" && "rowsAffected" in result
|
||||
? result.rowsAffected
|
||||
: 0;
|
||||
const deletedCount =
|
||||
typeof result === "object" && "rowsAffected" in result
|
||||
? result.rowsAffected
|
||||
: 0;
|
||||
|
||||
logger.info({ deletedCount }, "Cleaned up completed jobs");
|
||||
|
||||
@@ -258,9 +262,10 @@ export async function getJobStats(): Promise<{
|
||||
};
|
||||
|
||||
for (const row of rows) {
|
||||
const count = typeof row.count === "object" && "count" in row.count
|
||||
? (row.count as any).count
|
||||
: Number(row.count);
|
||||
const count =
|
||||
typeof row.count === "object" && "count" in row.count
|
||||
? (row.count as any).count
|
||||
: Number(row.count);
|
||||
if (row.status === "pending") stats.pending = count;
|
||||
else if (row.status === "processing") stats.processing = count;
|
||||
else if (row.status === "completed") stats.completed = count;
|
||||
|
||||
+6
-2
@@ -5,6 +5,7 @@ import http from "http";
|
||||
import path from "path";
|
||||
import * as prism from "prism-media";
|
||||
import { WebSocketServer } from "ws";
|
||||
import { getDatabase } from "./database/drizzle";
|
||||
import { AppError } from "./errors";
|
||||
import { createChildLogger, logger } from "./logger";
|
||||
import { getMetrics, uptimeGauge } from "./metrics";
|
||||
@@ -18,7 +19,6 @@ import {
|
||||
getPersistedValue,
|
||||
setPersistedValue,
|
||||
} from "./muxer-queue";
|
||||
import { getDatabase } from "./database/drizzle";
|
||||
import { discordPlayer } from "./player";
|
||||
import type { VoiceController } from "./voiceController";
|
||||
|
||||
@@ -296,7 +296,11 @@ export async function startWebserver(
|
||||
count: attachments.length,
|
||||
});
|
||||
} else {
|
||||
const messages = await getMessagesByChannel(channel, limitNum, offsetNum);
|
||||
const messages = await getMessagesByChannel(
|
||||
channel,
|
||||
limitNum,
|
||||
offsetNum,
|
||||
);
|
||||
res.json({
|
||||
type: "text",
|
||||
data: messages,
|
||||
|
||||
Reference in New Issue
Block a user