Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Latest commit

History

History
279 lines (206 loc) 路 5.16 KB

prisma-vs-mongoose-ys8c.mdx

File metadata and controls

279 lines (206 loc) 路 5.16 KB

import Code from "components/Markdown/Code" import Icon from 'components/Markdown/Icon'

export const meta = { title: "Prisma & Mongoose", position: 80, description: "Learn how Prisma compares to Mongoose" }

Overview

This page compares Prisma with Mongoose. Here is a high-level overview:

Prisma Mongoose
Auto-generated DB client
Type-safe
Declarative data modelling & migrations
Connection pooling
Supported DB types Document & Relational Document
Supported ORM patterns DataMapper Active Record
Fluent API for relations
Relation filters

In the following, you find a detailled comparison of the Mongoose and Prisma APIs.

Fetching single objects

Prisma

const user = await prisma.user({ id })

Mongoose

const user = await findById(id)

Fetching selected scalars of single objects

Prisma

const userFragment = await prisma.user({ id }).$fragment(`
  fragment NameAndEmail on User { id email }`
`)

Mongoose

const user = await findById(id)
  .select("id email")

Fetching relations

Prisma

<Code hideCopy={true} languages={["Fluent API", "Using fragments", "Native GraphQL"]}>

const postsByUser = await prisma
  .user({ id })
  .posts() 
const userWithPosts = await prisma
  .user({ id })
  .$fragment(`
    fragment UserPosts on User {
      posts { id title content published }
    }
  `)
const userWithPosts = await prisma
  .$graphql(`
    query ($id: ID!) {
      user(id: $id) {
        posts { id title content published }
      }
    }
  `, 
  { id }
  )

While the $fragment and the $graphql APIs each return a user object that includes a posts array, the fluent API returns just the posts array and no data about the user.

See the following GitHub issues to learn more about planned iterations of the Prisma client relations API:

Mongoose

const userWithPosts = await User
  .findById(id)
  .populate("posts")

Filtering for concrete values

Prisma

const users = await prisma.users({
  where: {
    name: "Alice"
  }
})

Mongoose

const user = await User.find({
  name: "Alice"
})

Other filter criteria

Prisma

Prisma generates many additional filters that are commonly used in modern application development:

  • <field>_ends_with & <field>_starts_with
  • <field>_not_ends_with & <field>_not_starts_with
  • <field>_gt & <field>_gte
  • <field>_lt & <field>_lte
  • <field>_contains & <field>_not_contains
  • <field>_in & <field>_not_in

Mongoose

Mongoose exposes the MongoDB query selectors as filter criteria.

Relation filters

Prisma

Prisma lets you filter a list based on a criteria that applies not only to the models of the list being retrieved, but to a relation of that model.

For example, you want to fetch only those users that wrote a post with the title "Hello World":

query {
  user(where: {
    posts_some: {
      title: "Hello World"
    }
  }) {
    id
  }
}

Mongoose

Mongoose doesn't offer a dedicated API for relation filters. You can get similar functionality by sending a raw SQL query to the database.

Pagination

Prisma

const posts = await prisma.posts({
  skip: 5,
  first: 10
})

In addition to skip and first, the Prisma API also offers:

  • last
  • before & after for cursor based pagination
  • Relay-style pagination

Mongoose

const posts = await Post.find({
  skip: 5,
  limit: 10
})

Creating objects

Prisma

const user = await prisma.createUser({
  name: "Alice",
  email: "alice@prisma.io"
})

Mongoose

<Code hideCopy={true} languages={["Using create", "Using save"]}>

const user = await User.create({
  name: "Alice",
  email: "alice@prisma.io"
})
const user = new User({
  name: "Alice",
  email: "alice@prisma.io"
})
await user.save()

Updating objects

Prisma

const updatedUser = await prisma.updateUser({
  where: { id },
  data: {
    name: "James",
    email: "james@prisma.io"
  }
})

Mongoose

<Code hideCopy={true} languages={["Using findOneAndUpdate", "Using save"]}>

const updatedUser = await User.findOneAndUpdate(
  { _id: id },
  {
    $set: {
      name: "James",
      email: "james@prisma.io"
    }
  }
)
user.name = "James"
user.email =" james@prisma.com"
await user.save()

Deleting objects

Prisma

const deletedUser = await prisma.deleteUser({ id })

Mongoose

await User.deleteOne({ _id: id })