refactor: update import paths for database modules and remove legacy db.js and schema.js files

This commit is contained in:
Haileyesus
2026-04-24 15:11:25 +03:00
parent 15171e1428
commit 4bd07c3ece
18 changed files with 102 additions and 750 deletions

View File

@@ -1,3 +1,4 @@
export { initializeDatabase } from '@/modules/database/init-db.js';
export { apiKeysDb } from '@/modules/database/repositories/api-keys.js';
export { appConfigDb } from '@/modules/database/repositories/app-config.js';
export { credentialsDb } from '@/modules/database/repositories/credentials.js';
@@ -6,6 +7,6 @@ export { notificationPreferencesDb } from '@/modules/database/repositories/notif
export { projectsDb } from '@/modules/database/repositories/projects.db.js';
export { pushSubscriptionsDb } from '@/modules/database/repositories/push-subscriptions.js';
export { scanStateDb } from '@/modules/database/repositories/scan-state.db.js';
export { sessionsDb } from '@/modules/database/repositories/sessions.db.js';
export { sessionsDb, applyCustomSessionNames } from '@/modules/database/repositories/sessions.db.js';
export { userDb } from '@/modules/database/repositories/users.js';
export { vapidKeysDb } from '@/modules/database/repositories/vapid-keys.js';

View File

@@ -23,6 +23,11 @@ type SessionMetadataLookupRow = Pick<
'session_id' | 'provider' | 'project_path' | 'jsonl_path' | 'custom_name' | 'created_at' | 'updated_at'
>;
type LegacySessionSummary = {
id: string;
summary?: string;
};
function normalizeTimestamp(value?: string): string | null {
if (!value) return null;
@@ -185,8 +190,81 @@ export const sessionsDb = {
return new Map(rows.map((row) => [row.session_id, row.custom_name]));
},
/**
* Legacy-compatibility method kept for parity with `server/database/db.js`.
* TODO: Remove after all legacy imports are migrated to the new repository API.
*/
setName(sessionId: string, provider: string, customName: string): void {
const db = getConnection();
db.prepare(
`INSERT INTO sessions (session_id, provider, custom_name)
VALUES (?, ?, ?)
ON CONFLICT(session_id, provider) DO UPDATE SET
custom_name = excluded.custom_name,
updated_at = CURRENT_TIMESTAMP`
).run(sessionId, provider, customName);
},
/**
* Legacy-compatibility method kept for parity with `server/database/db.js`.
* TODO: Remove after all legacy imports are migrated to the new repository API.
*/
getName(sessionId: string, provider: string): string | null {
return sessionsDb.getSessionName(sessionId, provider);
},
/**
* Legacy-compatibility method kept for parity with `server/database/db.js`.
* TODO: Remove after all legacy imports are migrated to the new repository API.
*/
getNames(sessionIds: string[], provider: string): Map<string, string> {
return sessionsDb.getSessionNames(sessionIds, provider);
},
/**
* Legacy-compatibility method kept for parity with `server/database/db.js`.
* TODO: Remove after all legacy imports are migrated to the new repository API.
*/
deleteName(sessionId: string, provider: string): boolean {
const db = getConnection();
return (
db
.prepare(
`DELETE FROM sessions
WHERE session_id = ? AND provider = ?`
)
.run(sessionId, provider).changes > 0
);
},
deleteSession(sessionId: string): void {
const db = getConnection();
db.prepare('DELETE FROM sessions WHERE session_id = ?').run(sessionId);
},
};
/**
* Legacy-compatibility helper kept for parity with `server/database/db.js`.
* TODO: Remove after all legacy imports are migrated to the new repository API.
*/
export function applyCustomSessionNames(
sessions: LegacySessionSummary[] | null | undefined,
provider: string
): void {
if (!sessions?.length) return;
try {
const sessionIds = sessions.map((session) => session.id);
const customNames = sessionsDb.getNames(sessionIds, provider);
for (const session of sessions) {
const customName = customNames.get(session.id);
if (customName) {
session.summary = customName;
}
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`[DB] Failed to apply custom session names for ${provider}:`, message);
}
}

View File

@@ -138,12 +138,13 @@ ${PUSH_SUBSCRIPTIONS_TABLE_SCHEMA_SQL}
CREATE INDEX IF NOT EXISTS idx_push_subscriptions_user_id ON push_subscriptions(user_id);
${PROJECTS_TABLE_SCHEMA_SQL}
CREATE INDEX IF NOT EXISTS idx_projects_is_starred ON projects(isStarred);
CREATE INDEX IF NOT EXISTS idx_projects_is_archived ON projects(isArchived);
-- NOTE: These indexes are created in migrations after legacy table-shape repairs.
-- Creating them here can fail on upgraded installs where projects lacks those columns.
${SESSIONS_TABLE_SCHEMA_SQL}
CREATE INDEX IF NOT EXISTS idx_session_ids_lookup ON sessions(session_id);
CREATE INDEX IF NOT EXISTS idx_sessions_project_path ON sessions(project_path);
-- NOTE: This index is created in migrations after sessions is rebuilt to include project_path.
-- Creating it here can fail on upgraded installs where the legacy sessions table has no project_path.
${LAST_SCANNED_AT_SQL}