Skip to content

Commit

Permalink
Comparison tables (#338)
Browse files Browse the repository at this point in the history
* Mongoose, Sequelize, and TypeORM comparisons
* API comparisons landing page
  • Loading branch information
mhwelander committed May 27, 2020
1 parent a1a7354 commit bbd5345
Show file tree
Hide file tree
Showing 6 changed files with 1,096 additions and 2 deletions.
Expand Up @@ -10,7 +10,7 @@ To answer the question briefly: _No, Prisma is not an ORM_.

The main goal of Prisma is to make working with databases easier for application developers. In that sense, it shares the same goals with ORMs, but takes a fundamentally different approach.

ORMs are libraries that map tables in your database to classes in your programming language. Prisma on the other hand is an auto-generated query builder that exposes queries which are _tailored_ to your models. All Prisma Client queries return plain old JavaScript objects.
ORMs are libraries that map tables in your database to classes in your programming language. Prisma, on the other hand, is a database toolkit. The toolkit includes Prisma Client, which is an auto-generated query builder that exposes queries which are _tailored_ to your models. All Prisma Client queries return plain old JavaScript objects.

To understand how Prisma and ORMs differ conceptually, here's a brief overview of how their building blocks relate to databases:

Expand Down
@@ -0,0 +1,286 @@
---
title: 'Comparing Prisma and Mongoose'
metaTitle: 'Comparing Prisma and Mongoose'
metaDescription: 'Compare the Prisma and Mongoose APIs.'
---

## Overview

This page compares the Prisma and [Mongoose](https://mongoosejs.com/docs/guide.html) APIs.


## Fetching single objects

**Prisma**

```ts
const user = await prisma.user.findOne({
where: {
id: 1,
},
});
```
**Mongoose**

```ts
const result = await User.findById("5eb9354624286a04e42401d8");
```

## Fetching selected scalars of single objects

**Prisma**

```ts
const user = await prisma.user.findOne({
where: {
id: 1,
},
select: {
name: true,
},
});
```

**Mongoose**

```ts
const user = await User.findById("5eb9354624286a04e42401d8").select([
"name",
"email",
]);
```

## Fetching relations

**Prisma**
<CodeBlock languages={["Using include", "Fluent API"]}>

```ts
const posts = await prisma.user.findOne({
where: {
id: 2,
},
include: {
post: true,
},
});
```

```ts
const posts = await prisma.user
.findOne({
where: {
id: 2,
},
})
.post();
```

</CodeBlock>

**Mongoose**

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

## Filtering for concrete values

**Prisma**

```ts
const posts = await prisma.post.findMany({
where: {
title: {
contains: "Hello World",
},
},
});
```


**Mongoose**

```ts
const user = await Post.find({
title: "Hello World"
})
```

## Other filter criteria

**Prisma**

Prisma generates many [additional filters](../../reference/tools-and-interfaces/prisma-client/filtering) that are commonly used in modern application development.

**Mongoose**

Mongoose exposes the [MongoDB query selectors](https://docs.mongodb.com/manual/reference/operator/query/#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, the following query returns users with one or more posts with "Hello" in the title:

```ts
const posts = await prisma.user.findMany({
where: {
Post: {
some: {
title: {
contains: "Hello",
},
},
},
}
});
```

**Mongoose**

Mongoose doesn't offer a dedicated API for relation filters. You can get similar functionality by adding an additional step to filter the results returned by the query.

## Pagination

**Prisma**

Cursor-style pagination:

```ts
const page = prisma.post.findMany({
before: {
id: 242,
},
last: 20,
});
```

Offset pagination:

```ts
const cc = prisma.post.findMany({
skip: 200,
first: 20,
});
```

**Mongoose**

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

## Creating objects

**Prisma**

```ts
const user = await prisma.user.create({
data: {
email: "alice@prisma.io",
},
});
```
**Mongoose**

<CodeBlock languages={["Using `create`", "Using `save`"]}>

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

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

</CodeBlock>

## Updating objects

**Prisma**

```ts
const user = await prisma.user.update({
data: {
name: "Alicia",
},
where: {
id: 2
}
});
```
**Mongoose**

<CodeBlock languages={["Using `findOneAndUpdate`", "Using `save`"]}>

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

```ts
user.name = "James"
user.email =" james@prisma.com"
await user.save()
```

</CodeBlock>

## Deleting objects

**Prisma**

```ts
const user = prisma.user.delete({
where: {
id: 10,
},
});
```
**Mongoose**

```ts
await User.deleteOne({ _id: id })
```

## Batch deletes

**Prisma**

```ts
const users = await prisma.user.deleteMany({
where: {
id: {
in: [1, 2, 6, 6, 22, 21, 25],
},
},
});
```

**Mongoose**

```ts
await User.deleteMany({ userUID: uid, id: { $in: [10, 2, 3, 5]}})
```

0 comments on commit bbd5345

Please sign in to comment.