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:
Edo Limburg
2026-05-05 21:59:50 +02:00
parent 2ea1f5cd95
commit 40ccbbad1a
14 changed files with 1943 additions and 0 deletions
+400
View File
@@ -0,0 +1,400 @@
import { describe, it, expect, beforeEach, afterAll } from 'vitest';
import BetterSqlite3 from 'better-sqlite3';
import { Kysely, SqliteDialect } from 'kysely';
import { DatabaseDedup } from './dedup.js';
import { migrate, reset } from '../../infrastructure/db/schema.js';
import type { Database } from '../../infrastructure/db/database.js';
import type { FeedItem } from '../../interfaces/feed.types.js';
describe('DatabaseDedup', () => {
let sqliteDb: BetterSqlite3.Database;
let db: Kysely<Database>;
let dedup: DatabaseDedup;
beforeEach(async () => {
// Create in-memory database for each test
sqliteDb = new BetterSqlite3(':memory:');
sqliteDb.pragma('journal_mode = WAL');
db = new Kysely<Database>({
dialect: new SqliteDialect({
database: sqliteDb,
}),
});
// Reset and migrate
await reset(db);
await migrate(db);
dedup = new DatabaseDedup(db);
});
afterAll(async () => {
await db.destroy();
});
describe('filter', () => {
it('returns all items when nothing is marked seen', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article 1',
url: 'https://example.com/1',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
{
id: 'item2',
source: 'https://example.com/feed.xml',
title: 'Article 2',
url: 'https://example.com/2',
publishedAt: new Date('2024-09-06T10:00:00Z'),
},
];
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(2);
expect(filtered[0].id).toBe('item1');
expect(filtered[1].id).toBe('item2');
});
it('excludes items that have been marked seen', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article 1',
url: 'https://example.com/1',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
{
id: 'item2',
source: 'https://example.com/feed.xml',
title: 'Article 2',
url: 'https://example.com/2',
publishedAt: new Date('2024-09-06T10:00:00Z'),
},
];
// Mark first item as seen
await dedup.markSeen([items[0]]);
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(1);
expect(filtered[0].id).toBe('item2');
});
it('returns empty array when all items are seen', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article 1',
url: 'https://example.com/1',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
await dedup.markSeen(items);
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(0);
});
it('returns empty array for empty input', async () => {
const filtered = await dedup.filter([]);
expect(filtered).toHaveLength(0);
});
it('handles partial matches correctly', async () => {
const items: FeedItem[] = [
{
id: 'seen-item',
source: 'https://example.com/feed.xml',
title: 'Seen Article',
url: 'https://example.com/seen',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
{
id: 'new-item',
source: 'https://example.com/feed.xml',
title: 'New Article',
url: 'https://example.com/new',
publishedAt: new Date('2024-09-06T10:00:00Z'),
},
{
id: 'another-seen',
source: 'https://example.com/feed.xml',
title: 'Another Seen',
url: 'https://example.com/another',
publishedAt: new Date('2024-09-06T11:00:00Z'),
},
];
await dedup.markSeen([items[0], items[2]]);
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(1);
expect(filtered[0].id).toBe('new-item');
});
});
describe('markSeen', () => {
it('marks items as seen', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article 1',
url: 'https://example.com/1',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
await dedup.markSeen(items);
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(0);
});
it('marks multiple items at once', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article 1',
url: 'https://example.com/1',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
{
id: 'item2',
source: 'https://example.com/feed.xml',
title: 'Article 2',
url: 'https://example.com/2',
publishedAt: new Date('2024-09-06T10:00:00Z'),
},
];
await dedup.markSeen(items);
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(0);
});
it('handles empty array gracefully', async () => {
await dedup.markSeen([]);
// Should not throw
});
it('is idempotent - marking same item twice does not error', async () => {
const item: FeedItem = {
id: 'duplicate-id',
source: 'https://example.com/feed.xml',
title: 'Article',
url: 'https://example.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
};
await dedup.markSeen([item]);
await dedup.markSeen([item]); // Should not throw
const filtered = await dedup.filter([item]);
expect(filtered).toHaveLength(0);
});
it('marks items incrementally', async () => {
const item1: FeedItem = {
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article 1',
url: 'https://example.com/1',
publishedAt: new Date('2024-09-06T09:00:00Z'),
};
const item2: FeedItem = {
id: 'item2',
source: 'https://example.com/feed.xml',
title: 'Article 2',
url: 'https://example.com/2',
publishedAt: new Date('2024-09-06T10:00:00Z'),
};
// Mark first item
await dedup.markSeen([item1]);
let filtered = await dedup.filter([item1, item2]);
expect(filtered).toHaveLength(1);
expect(filtered[0].id).toBe('item2');
// Mark second item
await dedup.markSeen([item2]);
filtered = await dedup.filter([item1, item2]);
expect(filtered).toHaveLength(0);
});
});
describe('integration scenarios', () => {
it('end-to-end: filter then mark workflow', async () => {
const items: FeedItem[] = [
{
id: 'new1',
source: 'https://example.com/feed.xml',
title: 'New Article 1',
url: 'https://example.com/new1',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
{
id: 'new2',
source: 'https://example.com/feed.xml',
title: 'New Article 2',
url: 'https://example.com/new2',
publishedAt: new Date('2024-09-06T10:00:00Z'),
},
];
// Simulate feed fetch
const newItems = await dedup.filter(items);
expect(newItems).toHaveLength(2);
// Mark as seen after display
await dedup.markSeen(newItems);
// Next fetch should return empty
const nextFetch = await dedup.filter(items);
expect(nextFetch).toHaveLength(0);
});
it('handles items with same IDs from different sources', async () => {
// This shouldn't happen with proper ID generation, but test it anyway
const items: FeedItem[] = [
{
id: 'same-hash-id',
source: 'https://source1.com/feed.xml',
title: 'Article from Source 1',
url: 'https://source1.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
{
id: 'same-hash-id',
source: 'https://source2.com/feed.xml',
title: 'Article from Source 2',
url: 'https://source2.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
// Mark first as seen
await dedup.markSeen([items[0]]);
// Both should be filtered since they have same ID
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(0);
});
it('preserves item data integrity', async () => {
const item: FeedItem = {
id: 'complete-item',
source: 'https://example.com/feed.xml',
title: 'Complete Article',
url: 'https://example.com/complete',
publishedAt: new Date('2024-09-06T09:30:00Z'),
summary: 'A summary',
content: 'Full content',
};
// Mark as seen
await dedup.markSeen([item]);
// Filter should exclude the item
const filtered = await dedup.filter([item]);
expect(filtered).toHaveLength(0);
});
it('handles large batches efficiently', async () => {
// Create 100 items
const items: FeedItem[] = Array.from({ length: 100 }, (_, i) => ({
id: `batch-item-${i}`,
source: 'https://example.com/feed.xml',
title: `Article ${i}`,
url: `https://example.com/${i}`,
publishedAt: new Date(`2024-09-${String(i + 1).padStart(2, '0')}T09:00:00Z`),
}));
// All should be returned initially
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(100);
// Mark half as seen
const seenItems = items.slice(0, 50);
await dedup.markSeen(seenItems);
// Only unseen items should be returned
const filteredAgain = await dedup.filter(items);
expect(filteredAgain).toHaveLength(50);
expect(filteredAgain[0].id).toBe('batch-item-50');
});
});
describe('edge cases', () => {
it('handles items with special characters in IDs', async () => {
const items: FeedItem[] = [
{
id: 'item-with-::special::chars',
source: 'https://example.com/feed.xml',
title: 'Special ID Article',
url: 'https://example.com/special',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
await dedup.markSeen(items);
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(0);
});
it('handles very long IDs', async () => {
const longId = 'a'.repeat(64); // Max length per schema
const items: FeedItem[] = [
{
id: longId,
source: 'https://example.com/feed.xml',
title: 'Long ID Article',
url: 'https://example.com/long',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
await dedup.markSeen(items);
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(0);
});
it('handles duplicate IDs in single filter call', async () => {
const item: FeedItem = {
id: 'duplicate-in-input',
source: 'https://example.com/feed.xml',
title: 'Article',
url: 'https://example.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
};
// Same item twice in input
const items = [item, item];
// Filter returns both since neither has been marked seen
// (filter only removes items from seen_ids table, not deduplicates input)
const filtered = await dedup.filter(items);
expect(filtered).toHaveLength(2);
// Mark one as seen
await dedup.markSeen([item]);
// Now both are filtered since they share the same ID
const filteredAgain = await dedup.filter(items);
expect(filteredAgain).toHaveLength(0);
});
});
});
+55
View File
@@ -0,0 +1,55 @@
/**
* Deduplication module implementation.
* Tracks seen item IDs to filter duplicates from feeds.
*/
import type { Kysely } from 'kysely';
import type { FeedItem } from '../../interfaces/feed.types.js';
import type { IDedup } from '../../interfaces/dedup.interface.js';
import type { Database, SeenIdTable } from '../../infrastructure/db/database.js';
export class DatabaseDedup implements IDedup {
private readonly db: Kysely<Database>;
constructor(db: Kysely<Database>) {
this.db = db;
}
async filter(items: FeedItem[]): Promise<FeedItem[]> {
if (items.length === 0) {
return [];
}
const ids = items.map((item) => item.id);
// Query which IDs are already in the seen table
const seenRows = await this.db
.selectFrom('seen_ids')
.select('id')
.where('id', 'in', ids)
.execute();
const seenIds = new Set(seenRows.map((row) => row.id));
// Return only items NOT in seen table
return items.filter((item) => !seenIds.has(item.id));
}
async markSeen(items: FeedItem[]): Promise<void> {
if (items.length === 0) {
return;
}
const rows: SeenIdTable[] = items.map((item) => ({
id: item.id,
seen_at: new Date().toISOString(),
}));
// Insert or ignore (idempotent)
await this.db
.insertInto('seen_ids')
.values(rows)
.onConflict((oc) => oc.column('id').doNothing())
.execute();
}
}
+1
View File
@@ -0,0 +1 @@
export { DatabaseDedup } from './dedup.js';
+1
View File
@@ -0,0 +1 @@
export { SqlStorage } from './storage.js';
+430
View File
@@ -0,0 +1,430 @@
import { describe, it, expect, beforeEach, afterAll } from 'vitest';
import BetterSqlite3 from 'better-sqlite3';
import { Kysely, SqliteDialect } from 'kysely';
import { SqlStorage } from './storage.js';
import { migrate, reset } from '../../infrastructure/db/schema.js';
import type { Database } from '../../infrastructure/db/database.js';
import type { FeedItem } from '../../interfaces/feed.types.js';
describe('SqlStorage', () => {
let sqliteDb: BetterSqlite3.Database;
let db: Kysely<Database>;
let storage: SqlStorage;
beforeEach(async () => {
// Create in-memory database for each test
sqliteDb = new BetterSqlite3(':memory:');
sqliteDb.pragma('journal_mode = WAL');
db = new Kysely<Database>({
dialect: new SqliteDialect({
database: sqliteDb,
}),
});
// Reset and migrate
await reset(db);
await migrate(db);
storage = new SqlStorage(db);
});
afterAll(async () => {
await db.destroy();
});
describe('save', () => {
it('saves a single item', async () => {
const item: FeedItem = {
id: 'abc123',
source: 'https://example.com/feed.xml',
title: 'Test Article',
url: 'https://example.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
summary: 'A summary',
content: 'Full content',
};
await storage.save([item]);
const recent = await storage.getRecent(1);
expect(recent).toHaveLength(1);
expect(recent[0].id).toBe('abc123');
expect(recent[0].title).toBe('Test Article');
});
it('saves multiple items in batch', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article 1',
url: 'https://example.com/1',
publishedAt: new Date('2024-09-06T10:00:00Z'),
},
{
id: 'item2',
source: 'https://example.com/feed.xml',
title: 'Article 2',
url: 'https://example.com/2',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
await storage.save(items);
const recent = await storage.getRecent(10);
expect(recent).toHaveLength(2);
});
it('handles empty array gracefully', async () => {
await storage.save([]);
const recent = await storage.getRecent(10);
expect(recent).toHaveLength(0);
});
it('updates existing item on duplicate id (upsert)', async () => {
const item: FeedItem = {
id: 'same-id',
source: 'https://example.com/feed.xml',
title: 'Original Title',
url: 'https://example.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
summary: 'Original summary',
};
await storage.save([item]);
// Update with new title and content
const updatedItem: FeedItem = {
...item,
title: 'Updated Title',
content: 'Updated content',
summary: 'Updated summary',
};
await storage.save([updatedItem]);
const recent = await storage.getRecent(1);
expect(recent).toHaveLength(1);
expect(recent[0].title).toBe('Updated Title');
expect(recent[0].content).toBe('Updated content');
});
it('preserves other fields during upsert', async () => {
const item: FeedItem = {
id: 'preserved-id',
source: 'https://example.com/feed.xml',
title: 'Title',
url: 'https://example.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
summary: 'Summary',
content: 'Content',
};
await storage.save([item]);
// Update only title
const updatedItem: FeedItem = {
...item,
title: 'New Title',
};
await storage.save([updatedItem]);
const recent = await storage.getRecent(1);
expect(recent[0].url).toBe('https://example.com/article'); // unchanged
expect(recent[0].publishedAt).toEqual(new Date('2024-09-06T09:00:00Z')); // unchanged
});
});
describe('getRecent', () => {
it('returns items ordered by published date desc', async () => {
const items: FeedItem[] = [
{
id: 'old',
source: 'https://example.com/feed.xml',
title: 'Old Article',
url: 'https://example.com/old',
publishedAt: new Date('2024-09-05T09:00:00Z'),
},
{
id: 'new',
source: 'https://example.com/feed.xml',
title: 'New Article',
url: 'https://example.com/new',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
await storage.save(items);
const recent = await storage.getRecent(10);
expect(recent[0].title).toBe('New Article');
expect(recent[1].title).toBe('Old Article');
});
it('respects limit parameter', async () => {
const items: FeedItem[] = Array.from({ length: 5 }, (_, i) => ({
id: `item${i}`,
source: 'https://example.com/feed.xml',
title: `Article ${i}`,
url: `https://example.com/${i}`,
publishedAt: new Date(`2024-09-0${i + 1}T09:00:00Z`),
}));
await storage.save(items);
const recent = await storage.getRecent(2);
expect(recent).toHaveLength(2);
});
it('returns empty array when no items', async () => {
const recent = await storage.getRecent(10);
expect(recent).toHaveLength(0);
});
});
describe('getBySource', () => {
it('returns items filtered by source', async () => {
const items: FeedItem[] = [
{
id: 'source1-item',
source: 'https://source1.com/feed.xml',
title: 'Source 1 Article',
url: 'https://source1.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
{
id: 'source2-item',
source: 'https://source2.com/feed.xml',
title: 'Source 2 Article',
url: 'https://source2.com/article',
publishedAt: new Date('2024-09-06T10:00:00Z'),
},
];
await storage.save(items);
const source1Items = await storage.getBySource('https://source1.com/feed.xml', 10);
expect(source1Items).toHaveLength(1);
expect(source1Items[0].title).toBe('Source 1 Article');
});
it('respects limit parameter', async () => {
const items: FeedItem[] = Array.from({ length: 5 }, (_, i) => ({
id: `item${i}`,
source: 'https://example.com/feed.xml',
title: `Article ${i}`,
url: `https://example.com/${i}`,
publishedAt: new Date(`2024-09-0${i + 1}T09:00:00Z`),
}));
await storage.save(items);
const sourceItems = await storage.getBySource('https://example.com/feed.xml', 2);
expect(sourceItems).toHaveLength(2);
});
it('returns empty array when no matching source', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article',
url: 'https://example.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
await storage.save(items);
const result = await storage.getBySource('https://other.com/feed.xml', 10);
expect(result).toHaveLength(0);
});
});
describe('search', () => {
it('finds items by title', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'JavaScript Tutorial',
url: 'https://example.com/js',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
{
id: 'item2',
source: 'https://example.com/feed.xml',
title: 'Python Guide',
url: 'https://example.com/py',
publishedAt: new Date('2024-09-06T10:00:00Z'),
},
];
await storage.save(items);
const results = await storage.search('JavaScript');
expect(results).toHaveLength(1);
expect(results[0].title).toBe('JavaScript Tutorial');
});
it('finds items by summary', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article One',
url: 'https://example.com/1',
publishedAt: new Date('2024-09-06T09:00:00Z'),
summary: 'Learn about async programming',
},
{
id: 'item2',
source: 'https://example.com/feed.xml',
title: 'Article Two',
url: 'https://example.com/2',
publishedAt: new Date('2024-09-06T10:00:00Z'),
summary: 'Introduction to types',
},
];
await storage.save(items);
const results = await storage.search('async');
expect(results).toHaveLength(1);
expect(results[0].title).toBe('Article One');
});
it('finds items by content', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article One',
url: 'https://example.com/1',
publishedAt: new Date('2024-09-06T09:00:00Z'),
content: 'Detailed explanation of closures',
},
{
id: 'item2',
source: 'https://example.com/feed.xml',
title: 'Article Two',
url: 'https://example.com/2',
publishedAt: new Date('2024-09-06T10:00:00Z'),
content: 'Overview of interfaces',
},
];
await storage.save(items);
const results = await storage.search('closures');
expect(results).toHaveLength(1);
expect(results[0].title).toBe('Article One');
});
it('returns multiple matching items', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'React Tutorial',
url: 'https://example.com/react',
publishedAt: new Date('2024-09-05T09:00:00Z'),
},
{
id: 'item2',
source: 'https://example.com/feed.xml',
title: 'Vue Guide',
url: 'https://example.com/vue',
publishedAt: new Date('2024-09-06T10:00:00Z'),
summary: 'A framework tutorial',
},
];
await storage.save(items);
const results = await storage.search('Tutorial');
expect(results).toHaveLength(2);
});
it('returns empty array when no matches', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'Article',
url: 'https://example.com/article',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
await storage.save(items);
const results = await storage.search('nonexistent');
expect(results).toHaveLength(0);
});
it('performs case-sensitive search (as per SQL LIKE)', async () => {
const items: FeedItem[] = [
{
id: 'item1',
source: 'https://example.com/feed.xml',
title: 'JavaScript',
url: 'https://example.com/js',
publishedAt: new Date('2024-09-06T09:00:00Z'),
},
];
await storage.save(items);
// SQLite LIKE is case-insensitive by default for ASCII
const results = await storage.search('javascript');
expect(results).toHaveLength(1);
});
});
describe('FeedItem properties preservation', () => {
it('preserves all FeedItem fields', async () => {
const item: FeedItem = {
id: 'full-item',
source: 'https://example.com/feed.xml',
title: 'Complete Article',
url: 'https://example.com/complete',
publishedAt: new Date('2024-09-06T09:30:00Z'),
summary: 'A brief summary',
content: 'The full article content here',
};
await storage.save([item]);
const recent = await storage.getRecent(1);
const retrieved = recent[0];
expect(retrieved.id).toBe(item.id);
expect(retrieved.source).toBe(item.source);
expect(retrieved.title).toBe(item.title);
expect(retrieved.url).toBe(item.url);
expect(retrieved.publishedAt).toEqual(item.publishedAt);
expect(retrieved.summary).toBe(item.summary);
expect(retrieved.content).toBe(item.content);
});
it('handles items without optional fields', async () => {
const item: FeedItem = {
id: 'minimal-item',
source: 'https://example.com/feed.xml',
title: 'Minimal Article',
url: 'https://example.com/minimal',
publishedAt: new Date('2024-09-06T09:00:00Z'),
};
await storage.save([item]);
const recent = await storage.getRecent(1);
expect(recent[0].summary).toBeUndefined();
expect(recent[0].content).toBeUndefined();
});
});
});
+101
View File
@@ -0,0 +1,101 @@
/**
* Storage module implementation.
* Persists FeedItems to database using Kysely.
*/
import type { Kysely } from 'kysely';
import type { FeedItem } from '../../interfaces/feed.types.js';
import type { IStorage } from '../../interfaces/storage.interface.js';
import type { Database, FeedItemTable } from '../../infrastructure/db/database.js';
export class SqlStorage implements IStorage {
private readonly db: Kysely<Database>;
constructor(db: Kysely<Database>) {
this.db = db;
}
async save(items: FeedItem[]): Promise<void> {
if (items.length === 0) {
return;
}
const rows: FeedItemTable[] = items.map((item) => ({
id: item.id,
source: item.source,
title: item.title,
url: item.url,
published_at: item.publishedAt.toISOString(),
content: item.content ?? null,
summary: item.summary ?? null,
created_at: new Date().toISOString(),
}));
// Upsert: insert or update on conflict
await this.db
.insertInto('feed_items')
.values(rows)
.onConflict((oc) =>
oc.column('id').doUpdateSet({
title: (eb) => eb.ref('excluded.title'),
content: (eb) => eb.ref('excluded.content'),
summary: (eb) => eb.ref('excluded.summary'),
})
)
.execute();
}
async getRecent(limit: number): Promise<FeedItem[]> {
const rows = await this.db
.selectFrom('feed_items')
.selectAll()
.orderBy('published_at', 'desc')
.limit(limit)
.execute();
return rows.map(this.rowToFeedItem);
}
async getBySource(source: string, limit: number): Promise<FeedItem[]> {
const rows = await this.db
.selectFrom('feed_items')
.selectAll()
.where('source', '=', source)
.orderBy('published_at', 'desc')
.limit(limit)
.execute();
return rows.map(this.rowToFeedItem);
}
async search(query: string): Promise<FeedItem[]> {
const searchTerm = `%${query}%`;
const rows = await this.db
.selectFrom('feed_items')
.selectAll()
.where((eb) =>
eb.or([
eb('title', 'like', searchTerm),
eb('summary', 'like', searchTerm),
eb('content', 'like', searchTerm),
])
)
.orderBy('published_at', 'desc')
.execute();
return rows.map(this.rowToFeedItem);
}
private rowToFeedItem(row: FeedItemTable): FeedItem {
return {
id: row.id,
source: row.source,
title: row.title,
url: row.url,
publishedAt: new Date(row.published_at),
content: row.content ?? undefined,
summary: row.summary ?? undefined,
};
}
}