First Commit Test

This commit is contained in:
2023-04-11 15:08:56 +02:00
commit 4079fade0a
24 changed files with 11503 additions and 0 deletions

View File

@ -0,0 +1,13 @@
/**
* Fetch all `examples` from the database. Run `npx prisma db push` at least once for this to work.
*
* If you are using `tRPC` you can access the prisma-client by adding it to the context:
* ```ts
* export async function createContext(event: H3Event) {
* return { prisma: event.context.prisma }
* }
*
* export type Context = inferAsyncReturnType<typeof createContext>
* ```
*/
export default defineEventHandler(event => event.context.prisma.example.findMany())

View File

@ -0,0 +1,9 @@
import { createNuxtApiHandler } from 'trpc-nuxt'
import { appRouter } from '~/server/trpc/routers'
import { createContext } from '~/server/trpc/context'
// export API handler
export default createNuxtApiHandler({
router: appRouter,
createContext
})

View File

@ -0,0 +1,16 @@
import { PrismaClient } from '@prisma/client'
let prisma: PrismaClient
declare module 'h3' {
interface H3EventContext {
prisma: PrismaClient
}
}
export default eventHandler((event) => {
if (!prisma) {
prisma = new PrismaClient()
}
event.context.prisma = prisma
})

18
server/trpc/context.ts Normal file
View File

@ -0,0 +1,18 @@
import { inferAsyncReturnType } from '@trpc/server'
import type { H3Event } from 'h3'
/**
* Creates context for an incoming request
* @link https://trpc.io/docs/context
*/
export function createContext (_event: H3Event) {
/**
* Add any trpc-request context here. E.g., you could add `prisma` like this (if you've added it via sidebase):
* ```ts
* return { prisma: _event.context.prisma }
* ```
*/
return {}
}
export type Context = inferAsyncReturnType<typeof createContext>

View File

@ -0,0 +1,20 @@
import { z } from 'zod'
import { publicProcedure, router } from '../trpc'
export const appRouter = router({
hello: publicProcedure
.input(
z.object({
text: z.string().nullish()
})
)
.query(({ input }) => {
return {
greeting: `hello ${input?.text ?? 'world'}`,
time: new Date()
}
})
})
// export type definition of API
export type AppRouter = typeof appRouter

23
server/trpc/trpc.ts Normal file
View File

@ -0,0 +1,23 @@
/**
* 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 superjson from 'superjson'
import { Context } from '~/server/trpc/context'
const t = initTRPC.context<Context>().create({
transformer: superjson
})
/**
* Unprotected procedure
**/
export const publicProcedure = t.procedure
export const router = t.router
export const middleware = t.middleware