introduce TRPC and service layer
This commit is contained in:
43
server/trpc/context.ts
Normal file
43
server/trpc/context.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import type { inferAsyncReturnType } from '@trpc/server'
|
||||
import { H3Event } from 'h3';
|
||||
import { serverSupabaseClient } from '#supabase/server';
|
||||
import SupabaseClient from '@supabase/supabase-js/dist/module/SupabaseClient';
|
||||
import { User } from '@supabase/supabase-js';
|
||||
import UserService from '~~/lib/services/user.service';
|
||||
|
||||
let prisma: PrismaClient | undefined
|
||||
let supabase: SupabaseClient | undefined
|
||||
let user: User | null = null;
|
||||
let dbUser: any | undefined
|
||||
|
||||
export async function createContext(event: H3Event){
|
||||
if (!supabase) {
|
||||
supabase = serverSupabaseClient(event)
|
||||
}
|
||||
if (!user) {
|
||||
({data: { user }} = await supabase.auth.getUser());
|
||||
}
|
||||
if (!prisma) {
|
||||
prisma = new PrismaClient()
|
||||
}
|
||||
if (!dbUser && user) {
|
||||
const userService = new UserService(prisma);
|
||||
dbUser = await userService.getUserBySupabaseId(user.id);
|
||||
|
||||
if (!dbUser && user) {
|
||||
dbUser = await userService.createUser( user.id, user.user_metadata.full_name );
|
||||
console.log(`\n Created user \n ${JSON.stringify(dbUser)}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO - This seems excessive, trim context when I have figured out what I actually need
|
||||
return {
|
||||
supabase,
|
||||
user,
|
||||
prisma,
|
||||
dbUser,
|
||||
}
|
||||
};
|
||||
|
||||
export type Context = inferAsyncReturnType<typeof createContext>
|
||||
21
server/trpc/trpc.ts
Normal file
21
server/trpc/trpc.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* This is your entry point to setup the root configuration for tRPC on the server.
|
||||
* - `initTRPC` should only be used once per app.
|
||||
* - We export only the functionality that we use so we can enforce which base procedures should be used
|
||||
*
|
||||
* Learn how to create protected base procedures and other things below:
|
||||
* @see https://trpc.io/docs/v10/router
|
||||
* @see https://trpc.io/docs/v10/procedures
|
||||
*/
|
||||
import { initTRPC } from '@trpc/server'
|
||||
import { Context } from './context';
|
||||
|
||||
const t = initTRPC.context<Context>().create()
|
||||
|
||||
/**
|
||||
* Unprotected procedure
|
||||
**/
|
||||
export const publicProcedure = t.procedure;
|
||||
|
||||
export const router = t.router;
|
||||
export const middleware = t.middleware;
|
||||
Reference in New Issue
Block a user