- Add storage module with SQLite persistence via better-sqlite3 - Add deduplication module for feed item dedup - Add infrastructure directory for deployment config - Add .env.example for environment variables - Update dependencies: kysely, better-sqlite3, pg
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
/**
|
|
* Database connection factory.
|
|
* Creates Kysely instances for SQLite or PostgreSQL based on configuration.
|
|
*/
|
|
|
|
import { Kysely, SqliteDialect, PostgresDialect } from 'kysely';
|
|
import SqliteDatabase from 'better-sqlite3';
|
|
import { Pool } from 'pg';
|
|
import type { Database } from './database.js';
|
|
import type { DatabaseConfig } from './config.js';
|
|
|
|
export function createDatabase(config: DatabaseConfig): Kysely<Database> {
|
|
if (config.type === 'sqlite') {
|
|
if (!config.sqlite) {
|
|
throw new Error('SQLite configuration required');
|
|
}
|
|
|
|
const sqliteDb = new SqliteDatabase(config.sqlite.path);
|
|
sqliteDb.pragma('journal_mode = WAL');
|
|
|
|
return new Kysely<Database>({
|
|
dialect: new SqliteDialect({
|
|
database: sqliteDb,
|
|
}),
|
|
});
|
|
}
|
|
|
|
if (config.type === 'postgres') {
|
|
if (!config.postgres) {
|
|
throw new Error('PostgreSQL configuration required');
|
|
}
|
|
|
|
// Check if using connection string or individual params
|
|
if ('connectionString' in config.postgres) {
|
|
return new Kysely<Database>({
|
|
dialect: new PostgresDialect({
|
|
pool: new Pool({
|
|
connectionString: config.postgres.connectionString,
|
|
}),
|
|
}),
|
|
});
|
|
} else {
|
|
return new Kysely<Database>({
|
|
dialect: new PostgresDialect({
|
|
pool: new Pool({
|
|
host: config.postgres.host,
|
|
port: config.postgres.port,
|
|
database: config.postgres.database,
|
|
user: config.postgres.user,
|
|
password: config.postgres.password,
|
|
}),
|
|
}),
|
|
});
|
|
}
|
|
}
|
|
|
|
throw new Error(`Unsupported database type: ${config.type}`);
|
|
}
|