48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { Database } from 'bun:sqlite';
|
|
import type { Stream, StreamSummary } from './types.ts';
|
|
|
|
const dbName = './db/strimserve.db';
|
|
|
|
// Create a new database object and initialize it with the schema
|
|
const db = new Database(dbName);
|
|
|
|
export function getStreams(): StreamSummary[] {
|
|
const indexData = db
|
|
.prepare(
|
|
'SELECT id, stream_date, filename, title, tags, length_seconds ' +
|
|
'FROM Stream ORDER BY id DESC'
|
|
)
|
|
.all() as Record<string, unknown>[];
|
|
return indexData.map((stream) => ({
|
|
id: stream.id as string,
|
|
stream_date: Date.parse(stream.stream_date as string),
|
|
filename: stream.filename as string,
|
|
title: stream.title as string | null,
|
|
tags: stream.tags ? JSON.parse(stream.tags as string) : {},
|
|
length_seconds: stream.length_seconds as number
|
|
}));
|
|
}
|
|
|
|
export function getStreamInfo(streamId: string): Stream | null {
|
|
const streamData = db
|
|
.prepare(
|
|
'SELECT id, stream_date, filename, ' +
|
|
'format, title, description, tags, ' +
|
|
'length_seconds, tracks FROM Stream ' +
|
|
'WHERE id = ?'
|
|
)
|
|
.get(streamId) as Record<string, unknown> | null;
|
|
if (!streamData) return null;
|
|
return {
|
|
id: streamData.id as string,
|
|
stream_date: Date.parse(streamData.stream_date as string),
|
|
filename: streamData.filename as string,
|
|
format: streamData.format as string,
|
|
title: streamData.title as string | null,
|
|
description: streamData.description as string | null,
|
|
tags: streamData.tags ? JSON.parse(streamData.tags as string) : {},
|
|
length_seconds: streamData.length_seconds as number,
|
|
tracks: JSON.parse(streamData.tracks as string)
|
|
};
|
|
}
|