Skip to content

Commit

Permalink
chore: move db functions to db.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-shuman committed Sep 6, 2024
1 parent 8251a36 commit 1c40989
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 41 deletions.
32 changes: 32 additions & 0 deletions src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import BetterSqlite3 from 'better-sqlite3';
import { mkdirSync } from 'node:fs';
import type { FeedItem } from './feed';

export function recreateDb(): BetterSqlite3.Database {
mkdirSync('./data', { recursive: true });

const db = BetterSqlite3('data/local.sqlite');

return db.exec(
`
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT NOT NULL,
title TEXT,
description TEXT,
content TEXT,
author TEXT,
date TEXT,
favicon TEXT
)
`
);
}

export function getArticles(): FeedItem[] {
const db = recreateDb();

const articles = db.prepare('SELECT * FROM articles').all() as FeedItem[];

return articles;
}
31 changes: 0 additions & 31 deletions src/lib/feed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { env } from '$env/dynamic/private';
import { type ArticleData } from '@extractus/article-extractor';
import BetterSqlite3 from 'better-sqlite3';
import { Feed } from 'feed';
import { mkdirSync } from 'node:fs';

export interface FeedItem {
id: number;
Expand All @@ -17,27 +15,6 @@ export interface FeedItem {

const baseUrl = `${env.SECURE ? 'https' : 'http'}://${env.HOST}:${env.PORT}`;

export function recreateDb(): BetterSqlite3.Database {
mkdirSync('./data', { recursive: true });

const db = BetterSqlite3('data/local.sqlite');

return db.exec(
`
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT NOT NULL,
title TEXT,
description TEXT,
content TEXT,
author TEXT,
date TEXT,
favicon TEXT
)
`
);
}

export function generateFeed(items: FeedItem[]): Feed {
const feed = new Feed({
title: env.FEED_TITLE ?? 'readl8r',
Expand Down Expand Up @@ -76,14 +53,6 @@ export function generateFeed(items: FeedItem[]): Feed {
return feed;
}

export function getArticles(): FeedItem[] {
const db = recreateDb();

const articles = db.prepare('SELECT * FROM articles').all() as unknown as FeedItem[];

return articles;
}

const adjectives: string[] = [
// goofy
'Witty',
Expand Down
3 changes: 2 additions & 1 deletion src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { env } from '$env/dynamic/private';
import { generateFeedDescription, generateFeedTitle, getArticles } from '$lib/feed';
import { getArticles } from '$lib/db';
import { generateFeedDescription, generateFeedTitle } from '$lib/feed';
import type { LayoutServerLoad } from './$types';

export const load: LayoutServerLoad = async () => ({
Expand Down
2 changes: 1 addition & 1 deletion src/routes/articles/+server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getArticles } from '$lib/feed';
import { getArticles } from '$lib/db';
import { type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async () =>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/articles/add/+server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAuthorized } from '$lib/auth';
import { recreateDb } from '$lib/feed';
import { recreateDb } from '$lib/db';
import { extract } from '@extractus/article-extractor';
import { type RequestHandler } from '@sveltejs/kit';

Expand Down
2 changes: 1 addition & 1 deletion src/routes/articles/clear/+server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAuthorized } from '$lib/auth';
import { recreateDb } from '$lib/feed';
import { recreateDb } from '$lib/db';
import { type RequestHandler } from '@sveltejs/kit';

export const POST: RequestHandler = async ({ request }) => {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/atom/+server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateFeed, getArticles } from '$lib/feed';
import { getArticles } from '$lib/db';
import { generateFeed } from '$lib/feed';
import { type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/feed.xml/+server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateFeed, getArticles } from '$lib/feed';
import { getArticles } from '$lib/db';
import { generateFeed } from '$lib/feed';
import { type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/feed/+server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateFeed, getArticles } from '$lib/feed';
import { getArticles } from '$lib/db';
import { generateFeed } from '$lib/feed';
import { type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/json/+server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateFeed, getArticles } from '$lib/feed';
import { getArticles } from '$lib/db';
import { generateFeed } from '$lib/feed';
import { type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/rss.xml/+server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateFeed, getArticles } from '$lib/feed';
import { getArticles } from '$lib/db';
import { generateFeed } from '$lib/feed';
import { type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/rss/+server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateFeed, getArticles } from '$lib/feed';
import { getArticles } from '$lib/db';
import { generateFeed } from '$lib/feed';
import { type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async () => {
Expand Down

0 comments on commit 1c40989

Please sign in to comment.