obsolete `conversations` db layer

master
Avraham Sakal 3 weeks ago
parent 51b14c8b2b
commit 3941b73b03

@ -1,26 +1,43 @@
import type { CommittedMessage } from "../../types";
import { router, publicProcedure, createCallerFactory } from "./server";
export const conversations = router({
fetchAll: publicProcedure.query(async ({ ctx: { db, jwt } }) => {
console.log("jwt", jwt);
return await db.conversations.findAll({ userId: jwt?.id as string });
fetchAll: publicProcedure.query(async ({ ctx: { dbClient, jwt } }) => {
const userId = jwt?.id as string | null;
if (!userId) return [];
const rows = await dbClient
.selectFrom("conversations")
.where("userId", "=", userId)
.selectAll()
.execute();
return rows;
}),
fetchOne: publicProcedure
.input((x) => x as { id: string })
.query(async ({ input: { id }, ctx: { db } }) => {
return await db.conversations.findById(id);
.query(async ({ input: { id }, ctx: { dbClient } }) => {
const row = await dbClient
.selectFrom("conversations")
.selectAll()
.where("id", "=", id)
.execute();
return row[0];
}),
start: publicProcedure.mutation(async ({ ctx: { db, jwt } }) => {
start: publicProcedure.mutation(async ({ ctx: { dbClient, jwt } }) => {
const row = {
title: "New Conversation",
userId: jwt?.id as string,
};
return await db.conversations.create(row);
const insertedRows = await dbClient
.insertInto("conversations")
.values(row)
.returningAll()
.execute();
return insertedRows[0];
}),
deleteOne: publicProcedure
.input((x) => x as { id: string })
.mutation(async ({ input: { id }, ctx: { db } }) => {
await db.conversations.delete(id);
.mutation(async ({ input: { id }, ctx: { dbClient } }) => {
await dbClient.deleteFrom("conversations").where("id", "=", id).execute();
return { ok: true };
}),
updateTitle: publicProcedure
@ -31,14 +48,23 @@ export const conversations = router({
title: string;
}
)
.mutation(async ({ input: { id, title }, ctx: { db } }) => {
await db.conversations.update(id, { title });
.mutation(async ({ input: { id, title }, ctx: { dbClient } }) => {
await dbClient
.updateTable("conversations")
.set({ title })
.where("id", "=", id)
.execute();
return { ok: true };
}),
fetchMessages: publicProcedure
.input((x) => x as { conversationId: string })
.query(async ({ input: { conversationId }, ctx: { db } }) => {
return await db.conversations.fetchMessages(conversationId);
.query(async ({ input: { conversationId }, ctx: { dbClient } }) => {
const rows = await dbClient
.selectFrom("messages")
.selectAll()
.where("conversationId", "=", conversationId)
.execute();
return rows as Array<CommittedMessage>;
}),
});

@ -1,8 +1,8 @@
import type { TSchema } from "@sinclair/typebox";
import { TypeCompiler } from "@sinclair/typebox/compiler";
import { initTRPC, TRPCError } from "@trpc/server";
import type { getDb } from "../../database/postgres";
import type { getOpenrouter } from "@/server/provider.js";
import type { getDb, getDbClient } from "../../database/postgres";
import type { getOpenrouter } from "@server/provider.js";
import type { JWT } from "@auth/core/jwt";
/**
@ -13,6 +13,7 @@ const t = initTRPC
.context<
object & {
db: ReturnType<typeof getDb>;
dbClient: ReturnType<typeof getDbClient>;
openrouter: ReturnType<typeof getOpenrouter>;
jwt?: JWT | null;
}

Loading…
Cancel
Save