Giter VIP home page Giter VIP logo

next-prisma's Introduction

NextJS with Prisma

This sample project shows how to use NextJS with Prisma.

Prisma is an open-source ORM and database toolkit that makes working with databases a breeze. You can learn more at prisma.io.

How to Run the Project

Clone the repo and install dependencies.

# with npm
npm i

# with yarn
yarn

After all dependencies have been installed, run the app.

# with npm
npm run dev

# with yarn
yarn dev

The app will be served at localhost:3000.

This sample project demonstrates how to access data with Prisma in two distinct ways: in the getServerSideProps function and from an API route.

Access from getServerSideProps

The getServerSideProps function is used to prepare a NextJS page before it renders. It's useful for getting some initial data to be rendered.

This function runs on the server which means Prisma can be used here to query for data.

The project demonstrates how to make an initial query for data with Prisma in this function.

// pages/index.ts

export async function getServerSideProps() {
  const contacts = await prisma.contact.findMany();
  return {
    props: {
      initialContacts: contacts
    }
  };
}

Access from an API Route

Next API routes use serverless functions to execute code in a backend environment. This is another place where Prisma can be used to access data.

This project demonstrates how to save data with Prisma from an API route.

// pages/api/contacts.ts

import type { NextApiRequest, NextApiResponse } from 'next';

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export default async (req: NextApiRequest, res: NextApiResponse) => {
  if (req.method !== 'POST') {
    return res.status(405).json({ message: 'Method not allowed' });
  }

  try {
    const contact = JSON.parse(req.body);
    const savedContact = await prisma.contact.create({ data: contact });
    res.status(200).json(savedContact);
  } catch (err) {
    res.status(400).json({ message: 'Something went wrong' });
  }
};

License

MIT

next-prisma's People

Contributors

chenkie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.