Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Mongoose, Sequelize, and TypeORM comparisons * API comparisons landing page
- Loading branch information
1 parent
a1a7354
commit bbd5345
Showing
6 changed files
with
1,096 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
286 changes: 286 additions & 0 deletions
286
content/02-understand-prisma/06-api-comparisons/01-prisma-and-mongoose.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]}}) | ||
``` |
Oops, something went wrong.