feat(db): migration and database setup

This commit is contained in:
2025-11-10 16:57:27 +02:00
parent 594b08a2d1
commit fbca02bec6
31 changed files with 2854 additions and 1928 deletions
+65
View File
@@ -0,0 +1,65 @@
import { eq } from "drizzle-orm";
import { v7 as uuidV7 } from "uuid";
import { Database } from "@/client";
import { NotFoundError } from "@/errors";
import { Credibility, source } from "@/schema";
export type CreateSourceParams = {
name: string;
url: string;
displayName?: string;
description?: string;
credibility: Credibility;
updatedAt?: Date;
};
export async function createSource(db: Database, params: CreateSourceParams) {
const [result] = await db
.insert(source)
.values({ id: uuidV7(), ...params })
.returning();
return result;
}
export type DeleteSourceParams = {
id: string;
};
export async function deleteSource(db: Database, params: DeleteSourceParams) {
const [result] = await db.delete(source).where(eq(source.id, params.id)).returning();
return result;
}
export async function getSourceByName(db: Database, name: string) {
return db.query.source.findFirst({
where: eq(source.name, name),
});
}
export async function getSourceIdByName(db: Database, name: string): Promise<string> {
const result = await db.query.source.findFirst({
columns: {
id: true,
},
where: eq(source.name, name),
});
if (!result) {
throw new NotFoundError(`Source with name "${name}" not found`);
}
return result.id;
}
export type GetSourceByIdParams = {
id: string;
};
export async function getSourceById(db: Database, params: GetSourceByIdParams) {
return db.query.source.findFirst({
where: eq(source.id, params.id),
});
}