Add storage, dedup modules and infrastructure configuration
- 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
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Database configuration types and loader.
|
||||
* Supports SQLite (local) and PostgreSQL (production) via environment variables.
|
||||
*/
|
||||
|
||||
export interface SqliteConfig {
|
||||
path: string;
|
||||
}
|
||||
|
||||
export interface PostgresConfig {
|
||||
host: string;
|
||||
port: number;
|
||||
database: string;
|
||||
user: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface PostgresUrlConfig {
|
||||
connectionString: string;
|
||||
}
|
||||
|
||||
export type DatabaseType = 'sqlite' | 'postgres';
|
||||
|
||||
export interface DatabaseConfig {
|
||||
type: DatabaseType;
|
||||
sqlite?: SqliteConfig;
|
||||
postgres?: PostgresConfig | PostgresUrlConfig;
|
||||
}
|
||||
|
||||
function getEnvVar(name: string, defaultValue?: string): string {
|
||||
const value = process.env[name];
|
||||
if (value === undefined) {
|
||||
if (defaultValue !== undefined) {
|
||||
return defaultValue;
|
||||
}
|
||||
throw new Error(`Missing required environment variable: ${name}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function loadConfig(): DatabaseConfig {
|
||||
const type = getEnvVar('PULSE_DATABASE_TYPE', 'sqlite') as DatabaseType;
|
||||
|
||||
if (type === 'sqlite') {
|
||||
return {
|
||||
type: 'sqlite',
|
||||
sqlite: {
|
||||
path: getEnvVar('PULSE_SQLITE_PATH', './data/pulse.db'),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'postgres') {
|
||||
const connectionString = process.env.PULSE_DATABASE_URL;
|
||||
|
||||
if (connectionString) {
|
||||
return {
|
||||
type: 'postgres',
|
||||
postgres: { connectionString },
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'postgres',
|
||||
postgres: {
|
||||
host: getEnvVar('PULSE_POSTGRES_HOST'),
|
||||
port: parseInt(getEnvVar('PULSE_POSTGRES_PORT', '5432'), 10),
|
||||
database: getEnvVar('PULSE_POSTGRES_DATABASE'),
|
||||
user: getEnvVar('PULSE_POSTGRES_USER'),
|
||||
password: getEnvVar('PULSE_POSTGRES_PASSWORD'),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`Unsupported database type: ${type}. Use 'sqlite' or 'postgres'.`);
|
||||
}
|
||||
|
||||
export function validateConfig(config: DatabaseConfig): void {
|
||||
if (config.type === 'sqlite') {
|
||||
if (!config.sqlite) {
|
||||
throw new Error('SQLite configuration missing');
|
||||
}
|
||||
if (!config.sqlite.path) {
|
||||
throw new Error('SQLite path is required');
|
||||
}
|
||||
} else if (config.type === 'postgres') {
|
||||
if (!config.postgres) {
|
||||
throw new Error('PostgreSQL configuration missing');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 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}`);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Kysely database type definitions.
|
||||
* Defines the table schemas for type-safe queries.
|
||||
*/
|
||||
|
||||
export interface FeedItemTable {
|
||||
id: string;
|
||||
source: string;
|
||||
title: string;
|
||||
url: string;
|
||||
published_at: string; // ISO 8601 format
|
||||
content: string | null;
|
||||
summary: string | null;
|
||||
created_at: string; // ISO 8601 format
|
||||
}
|
||||
|
||||
export interface SeenIdTable {
|
||||
id: string;
|
||||
seen_at: string; // ISO 8601 format
|
||||
}
|
||||
|
||||
// Database interface used by Kysely
|
||||
export interface Database {
|
||||
feed_items: FeedItemTable;
|
||||
seen_ids: SeenIdTable;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Database infrastructure exports.
|
||||
* Provides configured database instance and schema management.
|
||||
*/
|
||||
|
||||
import { createDatabase } from './connection.js';
|
||||
import { loadConfig, validateConfig, type DatabaseConfig } from './config.js';
|
||||
import { migrate, reset } from './schema.js';
|
||||
import type { Database } from './database.js';
|
||||
import type { Kysely } from 'kysely';
|
||||
|
||||
// Export types
|
||||
export type { Database, FeedItemTable, SeenIdTable } from './database.js';
|
||||
export type { DatabaseConfig, DatabaseType, SqliteConfig, PostgresConfig, PostgresUrlConfig } from './config.js';
|
||||
|
||||
// Export functions for creating database instances
|
||||
export { createDatabase, loadConfig, validateConfig, migrate, reset };
|
||||
|
||||
/**
|
||||
* Get a configured database instance.
|
||||
* Uses environment variables for configuration.
|
||||
*/
|
||||
export function getDatabase(): Kysely<Database> {
|
||||
const config = loadConfig();
|
||||
validateConfig(config);
|
||||
return createDatabase(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a database instance with explicit config (for testing).
|
||||
*/
|
||||
export function getDatabaseWithConfig(config: DatabaseConfig): Kysely<Database> {
|
||||
validateConfig(config);
|
||||
return createDatabase(config);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Database schema migrations.
|
||||
* Creates tables idempotently.
|
||||
*/
|
||||
|
||||
import type { Kysely } from 'kysely';
|
||||
import type { Database } from './database.js';
|
||||
|
||||
export async function migrate(db: Kysely<Database>): Promise<void> {
|
||||
// Create feed_items table
|
||||
await db.schema
|
||||
.createTable('feed_items')
|
||||
.ifNotExists()
|
||||
.addColumn('id', 'varchar(64)', (col) => col.primaryKey())
|
||||
.addColumn('source', 'varchar(2048)', (col) => col.notNull())
|
||||
.addColumn('title', 'varchar(512)', (col) => col.notNull())
|
||||
.addColumn('url', 'varchar(2048)', (col) => col.notNull())
|
||||
.addColumn('published_at', 'varchar(32)', (col) => col.notNull())
|
||||
.addColumn('content', 'text')
|
||||
.addColumn('summary', 'text')
|
||||
.addColumn('created_at', 'varchar(32)', (col) => col.notNull().defaultTo('CURRENT_TIMESTAMP'))
|
||||
.execute();
|
||||
|
||||
// Create indexes for feed_items
|
||||
await db.schema
|
||||
.createIndex('idx_feed_items_source')
|
||||
.ifNotExists()
|
||||
.on('feed_items')
|
||||
.column('source')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createIndex('idx_feed_items_published')
|
||||
.ifNotExists()
|
||||
.on('feed_items')
|
||||
.column('published_at')
|
||||
.execute();
|
||||
|
||||
// Create seen_ids table
|
||||
await db.schema
|
||||
.createTable('seen_ids')
|
||||
.ifNotExists()
|
||||
.addColumn('id', 'varchar(64)', (col) => col.primaryKey())
|
||||
.addColumn('seen_at', 'varchar(32)', (col) => col.notNull().defaultTo('CURRENT_TIMESTAMP'))
|
||||
.execute();
|
||||
}
|
||||
|
||||
export async function reset(db: Kysely<Database>): Promise<void> {
|
||||
// Drop tables (for testing)
|
||||
await db.schema.dropTable('seen_ids').ifExists().execute();
|
||||
await db.schema.dropIndex('idx_feed_items_published').ifExists().execute();
|
||||
await db.schema.dropIndex('idx_feed_items_source').ifExists().execute();
|
||||
await db.schema.dropTable('feed_items').ifExists().execute();
|
||||
}
|
||||
Reference in New Issue
Block a user