introduce TRPC and service layer

This commit is contained in:
Michael Dausmann
2023-02-05 11:55:49 +11:00
parent 791192a1ae
commit bba070d985
13 changed files with 307 additions and 58 deletions

View File

@@ -1,25 +0,0 @@
import { PrismaClient } from '@prisma/client';
import { serverSupabaseClient } from '#supabase/server';
const prisma = new PrismaClient();
export default defineEventHandler(async (event) => {
const client = serverSupabaseClient(event)
const user = await client.auth.getUser();
const dbUser = await prisma.user.findFirstOrThrow({
where: {
supabase_uid: user.data.id // TODO - this shit is messy.. typing
},
include: {
membership: true, // Return all fields
},
});
const data = await prisma.note.findMany({
where:{
account_id: dbUser.membership?.account_id
}
});
return data;
})

37
server/api/trpc/[trpc].ts Normal file
View File

@@ -0,0 +1,37 @@
/**
* This is the API-handler of your app that contains all your API routes.
* On a bigger app, you will probably want to split this file up into multiple files.
*/
import { createNuxtApiHandler } from 'trpc-nuxt'
import { z } from 'zod'
import { publicProcedure, router } from '~/server/trpc/trpc'
import { createContext } from '~~/server/trpc/context';
import NotesService from '~~/lib/services/notes.service';
export const appRouter = router({
notes: publicProcedure
.input(
z.object({
text: z.string().nullish(),
}),
)
.query(async ({ ctx, input }) => {
const notesService = new NotesService(ctx.prisma);
const notes = await notesService.getNotesForAccountId(ctx.dbUser.membership?.account_id);
return {
notes,
}
}),
})
// export only the type definition of the API
// None of the actual implementation is exposed to the client
export type AppRouter = typeof appRouter;
// export API handler
export default createNuxtApiHandler({
router: appRouter,
createContext: createContext,
onError({ error}) { console.error(error)}, // TODO - logging and reporting
})