mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-09-20 08:11:33 +00:00
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:
@@ -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
61
src/lib/db/migrate.ts
Normal 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();
|
@@ -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(),
|
||||
});
|
||||
|
Reference in New Issue
Block a user