javascript | js | nodejs

what is T3 stack with NextJS, Prisma and tRPC

what is T3 stack with NextJS, Prisma and tRPC

index

The “T3 stack” is not a widely recognized term, but based on your additional information about “trpc,” “Prisma,” and “Next.js,” it seems that you are referring to a specific technology stack that includes these tools.

  1. Next.js: Next.js is a popular React framework for building server-side rendered (SSR) and static websites. It provides features like automatic code splitting, server-side rendering, and static site generation. Next.js simplifies the development of complex React applications by offering a structured approach and various features out of the box.

  2. Prisma: Prisma is an open-source database toolkit that simplifies database access and management. It provides an Object-Relational Mapping (ORM) layer and a query builder to interact with databases. Prisma supports multiple databases, including PostgreSQL, MySQL, and SQLite, and helps streamline database operations.

  3. trpc: trpc (short for “tiny rpc”) is a framework-agnostic, type-safe RPC (Remote Procedure Call) library for TypeScript. It allows you to define RPC methods on the server and use them on the client side seamlessly. trpc simplifies the process of building APIs and handling data communication between the frontend and backend.

In this context, the “T3 stack” refers to the combination of Next.js, Prisma, and trpc in a web development stack. Next.js provides the frontend framework, Prisma handles the database layer, and trpc facilitates the communication between the frontend and backend.

This stack offers benefits such as efficient server-side rendering, simplified database access and management, and a type-safe RPC approach for API communication. It can be a powerful combination for building modern web applications with a solid frontend framework, a robust database layer, and effective data communication.

T3 stack with tRPC, Next JS and Prisma

  1. Set up a new Next.js project:

    • Create a new directory for your project and navigate into it.
    • Initialize a new Next.js project by running the following command:
      npx create-next-app .
  2. Install necessary dependencies:

    • Install Prisma and trpc by running the following command:
      npm install prisma trpc
    • Install any additional dependencies you may need, such as a database driver for Prisma (e.g., npm install @prisma/client for Prisma with PostgreSQL).
  3. Set up Prisma:

    • Configure Prisma by creating a prisma directory in your project root.
    • Create a schema.prisma file inside the prisma directory and define your database schema using Prisma’s schema language.
    • Run the following command to generate the Prisma client:
      npx prisma generate
  4. Create trpc server and endpoints:

    • Create a new file, for example, trpcServer.ts, and import necessary dependencies:

      import { createNextApiHandler } from '@trpc/server/adapters/next';
      import { router } from './trpc';
    • Create a trpc router file, for example, trpc.ts, and define your trpc router and API endpoints:

      import { createRouter } from 'trpc';
      import { prisma } from '@prisma/client';
      
      const router = createRouter()
        .query('users', {
          async resolve() {
            return prisma.user.findMany();
          },
        })
        .mutation('createUser', {
          input: z.object({
            name: z.string(),
            email: z.string().email(),
          }),
          async resolve({ input }) {
            return prisma.user.create({
              data: input,
            });
          },
        });
      
      export { router };
    • In trpcServer.ts, create a Next.js API route handler and assign the trpc router:

      export default createNextApiHandler({
        router,
        transformer: {
          onBeforeOutput: (output) => {
            // Modify output if needed
            return output;
          },
        },
      });
  5. Define Next.js API route:

    • Create a new directory pages/api if it doesn’t exist.
    • Inside pages/api, create a new file, for example, trpc.ts, and import the trpc server:
      import trpcServer from '../../trpcServer';
      
      export default trpcServer;
  6. Build the application:

    • Run the following command to build the Next.js application:
      npm run build
  7. Start the application:

    • Run the following command to start the Next.js application:
      npm run start

Now, you have a Next.js application set up with Prisma and trpc integrated. You can create additional trpc endpoints, modify the Prisma queries, and build your application’s frontend using Next.js components.

To access the trpc endpoints, you can make requests to the corresponding API routes created under pages/api. For example, GET /api/trpc will trigger the trpc server and execute the defined API endpoints.

Remember to adapt and expand the example to fit your specific needs and application requirements.

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub


Is this page helpful?

Related SnippetsView All

Related ArticlesView All

Related VideosView All

Stack Overflow Clone - APIs Integration Redux Toolkit [Closure] - App Demo #05

Become Ninja Developer - API security Best Practices with Node JS Packages #15

Nest JS Microservices using HTTP Gateway and Redis Services (DEMO) #nestjs #microservices #16