BraveVN
BraveVN

Reputation: 430

Is Prisma ORM stack good for new webapp (Nextjs + Nodejs)

I know the title is silly but I'm in real confusion now. Let's me explain the details:

I'm going to build a new webapp using NextJS (basically React) and I want my backend to use GraphQL. When searching for some additional technologies, I found Prisma but don't understand it correctly even reading the whole docs & website.

  1. In the traditional app, we have FrontendBackendDatabase, they're 3 important parts of a webapp
  2. I can use Nextjs in FE, Nodejs to build GraphQL APIs in BE, using MongoDB or PostgresQL as DB
  3. Now we have Prisma. When searching for some expamles, I have something growing in my mind:
    3.1 So Prisma is like a version of LinQ in Nodejs app?
    3.2 NextJS (FE) can use Prisma to query data from DBWhere is the BE part? Is Prisma considered BE?
    3.3 Talk about scalability, if I want to build a new mobile app that sync with the webapp, I will not have the Backend to reuse and have to implement the whole stuff from the scratch: something like React Native + Prisma, correct?
    3.4 I also found some other examples showing that we can use Prisma in Nodejs app (BE) to build APIs. Why do we need Prisma when Nodejs + MongoDB is enough?

I'm so confused about them so I hope the questions are clear.

Upvotes: 3

Views: 7213

Answers (3)

m.js
m.js

Reputation: 1

I do not believe Mongoose is an ORM, it's an ODM. MongoDB is intrinsically non-relational, it's a Document Database. I suppose including Mongoose isn't that criminal in the list of ORM's, but really I think the differentiation is still important.

Upvotes: -2

airsoftFreak
airsoftFreak

Reputation: 1608

ORM is an object-relational mapper. Meaning you don't have to write certain Database queries from scratch

Example raw mongodb query

db.mycol.findOne({title: "MongoDB Overview"})
{
    "_id" : ObjectId("5dd6542170fb13eec3963bf0"),
    "title" : "MongoDB Overview",
    "description" : "MongoDB is no SQL database",
    "by" : "tutorials point",
    "url" : "http://www.tutorialspoint.com",
    "tags" : [
        "mongodb",
        "database",
        "NoSQL"
    ],
    "likes" : 100
}

using an ORM like Mongoosejs or Prisma

Mongoosejs

let character = await Character.findOne({ name: 'Frodo' })

Prisma

const allPosts: Post[] = await prisma.post.findMany()

ORM makes your life easier. If you like to write raw queries, go ahead :)

List of ORMs that you can choose from

  1. https://typeorm.io/#/
  2. https://mongoosejs.com/
  3. https://www.prisma.io/
  4. https://sequelize.org/

For me, I would personally go with Prisma or Mongoose

  1. You can call Prisma from Next.js but it's better to call it from your node.js API. If it's a small project yeah go ahead and use Next.js and Prisma
  2. You can use react-native and node.js as your backend. For ORM of course you can choose any of the ORMs above
  3. When it comes to scalability, I don't think you should think about this first, which is more towards your Infrastructure like AWS. I recommend you to use Vercel for Next.js and Heroku for your Node.js api
  4. You can use RDS from AWS to spin up your PostgreSQL or MySQL
  5. You can spin up a mongodb database from mongodb atlas

Upvotes: 6

Nurul Sundarani
Nurul Sundarani

Reputation: 7618

Prisma is a Database Toolkit so that's definitely a part of the Backend.

The Database Toolkit involves Prisma ORM (Basically you won't have to write RAW SQL Queries in your GraphQL API to fetch data from your database, Prisma will do it for you). You will write your Prisma Queries in your GraphQL queries to fetch data and mutations to create data.

Next.js blurs the lines between client and server. You can use Prisma in the front end with Next.js in getStaticProps(), getServerSideProps() and API Routes but in your case, this won't be scalable as the mobile app cannot use these methods. Instead, a stand-alone server will be preferable and your GraphQL APIs can be consumed by both the Next.js Web App as well as React Native mobile app.

By using Prisma along with Node.js and MongoDB would make building app faster as Prisma exposes CRUD and other aggregation operations using PrismaClient based on your models.

This official example of using Prisma with GraphQL and Next.js should be helpful.

Upvotes: 2

Related Questions