Feature: Integrate Discover component with improved capabilities - Added category filters and personalization features - Implemented support for multiple languages and search engines - Added preferences saving - Fixed Chinese language search issue

This commit is contained in:
haddadrm
2025-04-01 16:19:05 +04:00
parent 1e87175b1b
commit 51381827d0
9 changed files with 2075 additions and 101 deletions

View File

@@ -3,9 +3,42 @@ import Database from 'better-sqlite3';
import * as schema from './schema';
import path from 'path';
// Create SQLite connection
const sqlite = new Database(path.join(process.cwd(), 'data/db.sqlite'));
const db = drizzle(sqlite, {
schema: schema,
});
// Initialize database schema
(function initializeDatabase() {
console.log('[DB] Checking database schema...');
try {
// Check if userPreferences table exists
const tableExists = sqlite.prepare(`
SELECT name FROM sqlite_master
WHERE type='table' AND name=?;
`).all('userPreferences').length > 0;
if (!tableExists) {
console.log('[DB] Creating userPreferences table...');
sqlite.prepare(`
CREATE TABLE userPreferences (
id INTEGER PRIMARY KEY,
userId TEXT NOT NULL UNIQUE,
categories TEXT DEFAULT '[]' NOT NULL,
languages TEXT DEFAULT '[]' NOT NULL,
createdAt TEXT NOT NULL,
updatedAt TEXT NOT NULL
);
`).run();
console.log('[DB] userPreferences table created successfully.');
} else {
console.log('[DB] userPreferences table already exists.');
}
} catch (error) {
console.error('[DB] Error during database initialization:', error);
}
})();
export default db;

61
src/lib/db/migrate.ts Normal file
View File

@@ -0,0 +1,61 @@
import db from './index';
import { userPreferences } from './schema';
import { sql } from 'drizzle-orm';
/**
* Run database migrations to ensure schema is up to date.
* This is designed to run once at application startup.
*/
export async function runMigrations() {
console.log('[DB Migration] Checking database schema...');
try {
// Check if userPreferences table exists
const tableExists = await checkIfTableExists('userPreferences');
if (!tableExists) {
console.log('[DB Migration] Creating userPreferences table...');
await createUserPreferencesTable();
console.log('[DB Migration] userPreferences table created successfully.');
} else {
console.log('[DB Migration] userPreferences table already exists.');
}
console.log('[DB Migration] Database schema is up to date.');
} catch (error) {
console.error('[DB Migration] Error during migration:', error);
// Don't throw the error - we want the application to continue even if migration fails
}
}
/**
* Check if a table exists in the database
*/
async function checkIfTableExists(tableName: string): Promise<boolean> {
const result = db.$client.prepare(`
SELECT name FROM sqlite_master
WHERE type='table' AND name=?;
`).all(tableName);
return result.length > 0;
}
/**
* Create the userPreferences table using the schema definition
*/
async function createUserPreferencesTable() {
// Create the table using a raw SQL query based on our schema
db.$client.prepare(`
CREATE TABLE userPreferences (
id INTEGER PRIMARY KEY,
userId TEXT NOT NULL UNIQUE,
categories TEXT DEFAULT '[]' NOT NULL,
languages TEXT DEFAULT '[]' NOT NULL,
createdAt TEXT NOT NULL,
updatedAt TEXT NOT NULL
);
`).run();
}
// Run migrations automatically when this module is imported
runMigrations();

View File

@@ -26,3 +26,17 @@ export const chats = sqliteTable('chats', {
.$type<File[]>()
.default(sql`'[]'`),
});
// Add user preferences table for Discover features
export const userPreferences = sqliteTable('userPreferences', {
id: integer('id').primaryKey(),
userId: text('userId').notNull().unique(),
categories: text('categories', { mode: 'json' })
.$type<string[]>()
.default(sql`'[]'`), // Categories will be set at the application level
languages: text('languages', { mode: 'json' })
.$type<string[]>()
.default(sql`'[]'`), // Languages will be set at the application level
createdAt: text('createdAt').notNull(),
updatedAt: text('updatedAt').notNull(),
});