From 9683925991a106834da9b438fa395a9c8df5d20e Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Fri, 1 Apr 2022 16:10:08 -0500 Subject: [PATCH 01/18] add P1 => P2 MongoDB Upgrade Guide --- .../08-upgrade-from-mongodb-beta.mdx | 231 ++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx new file mode 100644 index 0000000000..483f473426 --- /dev/null +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -0,0 +1,231 @@ +--- +title: 'Upgrade from MongoDB Beta' +metaTitle: 'Upgrade from the Prisma 1 MongoDB Beta to Prisma 2' +metaDescription: 'Learn how to upgrade your MongoDB application running Prisma 1 to Prisma 2.' +--- + +## Introduction + +This guide aims to help you migrate from the MongoDB Beta on Prisma 1 to MongoDB on Prisma 2. To learn more about the differences between Prisma 1 and Prisma 2, you can refer to [this document](https://www.prisma.io/docs/guides/upgrade-guides/upgrade-from-prisma-1/how-to-upgrade#main-differences-between-prisma-1-and-prisma-version-2x-and-later). + +The scope of this guide is to give you the workflow necessary to perform the migration and highlight some of the gotchas you may encounter. + +We unfortunately can't cover all possible scenarios or changes required, but this guide should help you on your journey. Join [our community Slack](https://slack.prisma.io/) or create an issue [on Github](https://github.com/prisma/prisma1/issues/new/choose) with any questions. + +Please perform this migration on your staging environment before trying this in production! + +## Requirements + +- Must be running MongoDB 4.2+ as a replica set (MongoDB Atlas does this for you automatically) +- Node.js 12.6.x or 14.x or 16.x +- TypeScript 4.1.x + +## Installing Prisma 2 + +In your project directory run the following commands: + +```bash +$ npm install prisma +$ npx prisma init --datasource-provider=mongodb +``` + +This should create the following files: + +- `prisma/schema.prisma`: An initial Prisma 2 Schema +- `.env`: Environment file where you'll store your connection string + + + +If you see the following error: + +``` +ERROR File schema.prisma already exists in your project. +Please try again in a project that is not yet using Prisma. +``` + +You have likely a `prisma/` directory in your project already. Rename that directory to something like `_prisma/` and try again + + + +## Find the Connection String to your MongoDB Database + +Next you'll want to find the connection string to your MongoDB database. You should be able to find it in your docker-compose file or on Atlas. It's what you'd pass to MongoDB Compass. The connection string should look something like this: + +```bash +mongodb://:@:27017 +``` + +The database that stores application data in Prisma 1 is called `default_default`, so we'll add that to the end of the connection string and update the `DATABASE_URL` key in the `.env` file + +```bash +DATABASE_URL="mongodb://prisma:prisma@localhost:27017/default_default" +``` + +## Introspect your MongoDB Database + +You're now ready to pull the structure of your database down into your Prisma Schema + +```bash +$ npx prisma db pull +``` + +And you should see your Prisma Schema in `prisma/schema.prisma` populated with your models. + + + +If you see the following error: `Error in connector: SCRAM failure: Authentication failed.`, try adding `?authSource=admin` to the end of your connection string and trying again. + + + +If you run into any other errors with introspection, please refer to this documentation. + +## Touching up your Prisma Schema + +The generated Prisma Client from a freshly introspected MongoDB database doesn't have the best API. You can adjust the names and use `@map` and `@@map` to map to the underlying collection and field names: + +```diff +- model posts { ++ model Post { + id String @id @default(auto()) @map("_id") @db.ObjectId + published Boolean + title String ++ @@map("posts") + } + +- model users { ++ model User { + id String @id @default(auto()) @map("_id") @db.ObjectId + email String @unique(map: "email_U") + name String +- posts String[] @db.ObjectId ++ postIds String[] @db.ObjectId @map("posts") + + @@index([posts], map: "posts_R") ++ @@map("users") + } +``` + +Take caution in doing these renames because you need to make sure the Prisma Schema still maps properly to the underlying database collections and field names. + +Unlike SQL databases, MongoDB doesn't have an explicit understanding of relationships between data. This means that Prisma's introspection is unable to infer those relationships for you. + +However, where Prisma 1 stores foreign keys is different than where Prisma 2 expects foreign keys, so if you want to take advantage of relationships, you'll need to shift where the foreign keys are on your database before adding the relationships. + + + +💡 Download the Prisma 2 VSCode Extension to provide autocomplete and helpful error messages as you transition your Prisma Schema + + + +## Generating a Prisma Client + +With the Prisma Schema populated with the schema of your data, you're now ready to generate a Typescript Client to read and write to your MongoDB database + +```bash +$ prisma generate +``` + +## Testing Reads + +Create a simple `test.ts` script to verify that the Prisma Client can read and write to your application. Note that this guide is using the example in the [Prisma 1 examples repository](https://github.com/prisma/prisma1-examples/tree/master/typescript/docker-mongodb), but the code will change depending on your application. + +```ts +import { PrismaClient } from '@prisma/client' +const prisma = new PrismaClient() + +async function main() { + await prisma.$connect() + const posts = await prisma.post.findMany() + console.log(posts) +} + +main() + .catch(console.error) + .finally(() => prisma.$disconnect()) +``` + +Make sure `ts-node` is installed and run: + +```bash +ts-node test.ts +``` + +You should see a list of your data: + +```bash +[ + { + comments: [], + id: '62435a83fca136000996ba16', + content: 'https://www.prisma.io/day/', + published: true, + title: 'Join us for Prisma Day 2019 in Berlin', + wasCreated: 2022-03-29T19:14:11.172Z, + wasUpdated: 2022-03-29T19:14:11.172Z + }, + { + comments: [ [Object] ], + id: '62435a83fca136000996ba18', + content: 'https://graphqlweekly.com/', + published: true, + title: 'Subscribe to GraphQL Weekly for community news', + wasCreated: 2022-03-29T19:14:11.369Z, + wasUpdated: 2022-03-29T19:14:11.369Z + }, + { + comments: [], + id: '62435a83fca136000996ba1a', + content: 'https://twitter.com/prisma', + published: false, + title: 'Follow Prisma on Twitter', + wasCreated: 2022-03-29T19:14:11.375Z, + wasUpdated: 2022-03-29T19:14:11.375Z + } +] +``` + +## Testing Writes + +You can then alter your `test.ts` to try writes: + +```ts +import { PrismaClient } from '@prisma/client' +const prisma = new PrismaClient() + +async function main() { + await prisma.$connect() + const user = await prisma.user.create({ + data: { + email: 'alice@prisma.io', + name: 'Alice', + }, + }) + console.log(user) +} + +main() + .catch(console.error) + .finally(() => prisma.$disconnect()) +``` + +And you should see a user has been created. + + + +If you see the following error: + +``` +Prisma needs to perform transactions, which requires your MongoDB server to be run as a replica set. [https://pris.ly/d/mongodb-replica-set](https://pris.ly/d/mongodb-replica-set) +``` + +This means that your MongoDB database isn't running as a replica set. Refer to the link above for steps to resolve this issue. + + + +## Upgrading your Application + +Now that you have a working Prisma Client, you can start replacing Prisma 1 queries with Prisma 2 queries. The [Prisma Client Reference](https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#filter-conditions-and-operators) is a helpful resource for learning how to use the Prisma 2 Client. + +## Conclusion + +I hope this brief guide was helpful in getting you started on the right path. Let us know if you have any questions or issues. We really appreciate your support over the years. From d59d0a88b5bf4889ebf177b4bbb81b39c6409827 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:52:44 -0500 Subject: [PATCH 02/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Tana M Berry --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 483f473426..4492ebb1f8 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -63,7 +63,7 @@ DATABASE_URL="mongodb://prisma:prisma@localhost:27017/default_default" ## Introspect your MongoDB Database -You're now ready to pull the structure of your database down into your Prisma Schema +You're now ready to pull the structure of your database down into your Prisma Schema. ```bash $ npx prisma db pull From 4dd5a32d834fdfcffc26bb273c6202c87a140a2f Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:52:50 -0500 Subject: [PATCH 03/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Tana M Berry --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 4492ebb1f8..6cffdfdb83 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -69,7 +69,7 @@ You're now ready to pull the structure of your database down into your Prisma Sc $ npx prisma db pull ``` -And you should see your Prisma Schema in `prisma/schema.prisma` populated with your models. +And you should see your Prisma schema in `prisma/schema.prisma` populated with your models. From 0881da5f99416a09aca8242657f7ff1240163dc9 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:53:28 -0500 Subject: [PATCH 04/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Tana M Berry --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 6cffdfdb83..6a81255101 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -113,7 +113,7 @@ However, where Prisma 1 stores foreign keys is different than where Prisma 2 exp -💡 Download the Prisma 2 VSCode Extension to provide autocomplete and helpful error messages as you transition your Prisma Schema +💡 Download the Prisma 2 VSCode Extension to provide autocomplete and helpful error messages as you transition your Prisma schema. From 1f3288c74e240eca1d65c43df0e4610ab9a05f10 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:53:34 -0500 Subject: [PATCH 05/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Tana M Berry --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 6a81255101..a16ea5e01f 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -208,7 +208,7 @@ main() .finally(() => prisma.$disconnect()) ``` -And you should see a user has been created. +And you should see a user was created. From 5f51b0cef4ddc8adfb760be7a3fea3d3ca82898f Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:53:42 -0500 Subject: [PATCH 06/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Tana M Berry --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index a16ea5e01f..49bd0a571f 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -119,7 +119,7 @@ However, where Prisma 1 stores foreign keys is different than where Prisma 2 exp ## Generating a Prisma Client -With the Prisma Schema populated with the schema of your data, you're now ready to generate a Typescript Client to read and write to your MongoDB database +With the Prisma schema populated with the schema of your data, you're now ready to generate a Typescript Client to read and write to your MongoDB database. ```bash $ prisma generate From d7443c7da72714b4f57d05bc8feaf6b0f04e7899 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:54:06 -0500 Subject: [PATCH 07/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 49bd0a571f..8cc4b7870c 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -6,7 +6,7 @@ metaDescription: 'Learn how to upgrade your MongoDB application running Prisma 1 ## Introduction -This guide aims to help you migrate from the MongoDB Beta on Prisma 1 to MongoDB on Prisma 2. To learn more about the differences between Prisma 1 and Prisma 2, you can refer to [this document](https://www.prisma.io/docs/guides/upgrade-guides/upgrade-from-prisma-1/how-to-upgrade#main-differences-between-prisma-1-and-prisma-version-2x-and-later). +This guide aims to help you migrate from the Prisma 1 MongoDB Beta to MongoDB on Prisma 3.12.0 and later. To learn more about the differences between Prisma 1 and Prisma 2.x and later, you can refer to [this document](https://www.prisma.io/docs/guides/upgrade-guides/upgrade-from-prisma-1/how-to-upgrade#main-differences-between-prisma-1-and-prisma-version-2x-and-later). The scope of this guide is to give you the workflow necessary to perform the migration and highlight some of the gotchas you may encounter. From 205b250d548f147e76e8574f0ce49988508a8bce Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:54:15 -0500 Subject: [PATCH 08/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 8cc4b7870c..36f36e68ab 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -20,7 +20,7 @@ Please perform this migration on your staging environment before trying this in - Node.js 12.6.x or 14.x or 16.x - TypeScript 4.1.x -## Installing Prisma 2 +## Installing Prisma 3.12.0 or later In your project directory run the following commands: From f0ba8b1c16f2fd90b1f8e73adc632f76aad66cec Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:54:27 -0500 Subject: [PATCH 09/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 36f36e68ab..29dca77b50 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -25,7 +25,7 @@ Please perform this migration on your staging environment before trying this in In your project directory run the following commands: ```bash -$ npm install prisma +$ npm install prisma@latest $ npx prisma init --datasource-provider=mongodb ``` From 2ab9d856a684d8c41f0407e7ad9d029ad7e71aae Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:54:34 -0500 Subject: [PATCH 10/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 29dca77b50..fd02e58ac2 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -31,7 +31,7 @@ $ npx prisma init --datasource-provider=mongodb This should create the following files: -- `prisma/schema.prisma`: An initial Prisma 2 Schema +- `prisma/schema.prisma`: An initial Prisma schema - `.env`: Environment file where you'll store your connection string From 191bab375fb8317d0f9529c4c019d6f3be0febb9 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:54:40 -0500 Subject: [PATCH 11/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index fd02e58ac2..a3d6c6f533 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -49,7 +49,7 @@ You have likely a `prisma/` directory in your project already. Rename that direc ## Find the Connection String to your MongoDB Database -Next you'll want to find the connection string to your MongoDB database. You should be able to find it in your docker-compose file or on Atlas. It's what you'd pass to MongoDB Compass. The connection string should look something like this: +Next you'll want to find the connection string to your MongoDB database. You should be able to find it in your `docker-compose.yml` file or on MongoDB Atlas. It's what you'd pass to MongoDB Compass. The connection string should look something like this: ```bash mongodb://:@:27017 From 761be80cada7d81639f49f2abbb9ed3f546d6bf3 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:54:49 -0500 Subject: [PATCH 12/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index a3d6c6f533..d6b495d740 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -57,7 +57,7 @@ mongodb://:@:27017 The database that stores application data in Prisma 1 is called `default_default`, so we'll add that to the end of the connection string and update the `DATABASE_URL` key in the `.env` file -```bash +```bash file=.env DATABASE_URL="mongodb://prisma:prisma@localhost:27017/default_default" ``` From 9f6e916a808e97c10ab28bb951ff7e03a4a51ea8 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:55:05 -0500 Subject: [PATCH 13/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index d6b495d740..672056b500 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -81,7 +81,7 @@ If you run into any other errors with introspection, please refer to this docume ## Touching up your Prisma Schema -The generated Prisma Client from a freshly introspected MongoDB database doesn't have the best API. You can adjust the names and use `@map` and `@@map` to map to the underlying collection and field names: +The generated Prisma Client from a freshly introspected Prisma 1 based MongoDB database doesn't have the best API. You can adjust the names and use `@map` and `@@map` to map to the underlying collection and field names: ```diff - model posts { From 8945487fc9d73774ce9ec2808808a397f60acf5c Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:55:26 -0500 Subject: [PATCH 14/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 672056b500..2d776704eb 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -122,7 +122,7 @@ However, where Prisma 1 stores foreign keys is different than where Prisma 2 exp With the Prisma schema populated with the schema of your data, you're now ready to generate a Typescript Client to read and write to your MongoDB database. ```bash -$ prisma generate +$ npx prisma generate ``` ## Testing Reads From b2c3c53889988e5a4068ee4b5773c144f0f0b8b4 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:55:35 -0500 Subject: [PATCH 15/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 2d776704eb..97ce86d774 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -144,7 +144,7 @@ main() .finally(() => prisma.$disconnect()) ``` -Make sure `ts-node` is installed and run: +Make sure `ts-node` is installed globally and run: ```bash ts-node test.ts From 10fc8c043726e56a49f2749f3b732e9b47322d54 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:55:43 -0500 Subject: [PATCH 16/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index 97ce86d774..cdeda5804b 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -215,7 +215,7 @@ And you should see a user was created. If you see the following error: ``` -Prisma needs to perform transactions, which requires your MongoDB server to be run as a replica set. [https://pris.ly/d/mongodb-replica-set](https://pris.ly/d/mongodb-replica-set) +Prisma needs to perform transactions, which requires your MongoDB server to be run as a replica set. https://pris.ly/d/mongodb-replica-set ``` This means that your MongoDB database isn't running as a replica set. Refer to the link above for steps to resolve this issue. From 1e7a4b8dbbb7527d00e290126738166acb243e07 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 14:56:04 -0500 Subject: [PATCH 17/18] Update content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx Co-authored-by: Jan Piotrowski --- .../800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index cdeda5804b..e4cd72b28b 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -218,7 +218,7 @@ If you see the following error: Prisma needs to perform transactions, which requires your MongoDB server to be run as a replica set. https://pris.ly/d/mongodb-replica-set ``` -This means that your MongoDB database isn't running as a replica set. Refer to the link above for steps to resolve this issue. +This means that your MongoDB database isn't running as a replica set. Refer to [the link above](https://pris.ly/d/mongodb-replica-set) for steps to resolve this issue. From 553adca5b120a3e17730ec083cce46466eb98715 Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Mon, 4 Apr 2022 15:13:55 -0500 Subject: [PATCH 18/18] address feedback and add logo :-) --- .../08-upgrade-from-mongodb-beta.mdx | 29 ++++++++++++------- content/doc-images/mongodb-logo.svg | 9 ++++++ 2 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 content/doc-images/mongodb-logo.svg diff --git a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx index e4cd72b28b..3eeefbdd82 100644 --- a/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx +++ b/content/300-guides/300-upgrade-guides/800-upgrade-from-prisma-1/08-upgrade-from-mongodb-beta.mdx @@ -6,13 +6,24 @@ metaDescription: 'Learn how to upgrade your MongoDB application running Prisma 1 ## Introduction -This guide aims to help you migrate from the Prisma 1 MongoDB Beta to MongoDB on Prisma 3.12.0 and later. To learn more about the differences between Prisma 1 and Prisma 2.x and later, you can refer to [this document](https://www.prisma.io/docs/guides/upgrade-guides/upgrade-from-prisma-1/how-to-upgrade#main-differences-between-prisma-1-and-prisma-version-2x-and-later). + + +This guide aims to help you migrate from the Prisma 1 MongoDB Beta to MongoDB on +Prisma 3.12.0 and later. To learn more about the differences between Prisma 1 and +Prisma 2.x and later, you can refer to [this document](https://www.prisma.io/docs/guides/upgrade-guides/upgrade-from-prisma-1/how-to-upgrade#main-differences-between-prisma-1-and-prisma-version-2x-and-later). The scope of this guide is to give you the workflow necessary to perform the migration and highlight some of the gotchas you may encounter. We unfortunately can't cover all possible scenarios or changes required, but this guide should help you on your journey. Join [our community Slack](https://slack.prisma.io/) or create an issue [on Github](https://github.com/prisma/prisma1/issues/new/choose) with any questions. -Please perform this migration on your staging environment before trying this in production! + + Perform this migration on your staging environment before trying this in + production! + ## Requirements @@ -34,7 +45,7 @@ This should create the following files: - `prisma/schema.prisma`: An initial Prisma schema - `.env`: Environment file where you'll store your connection string - + If you see the following error: @@ -71,17 +82,15 @@ $ npx prisma db pull And you should see your Prisma schema in `prisma/schema.prisma` populated with your models. - + If you see the following error: `Error in connector: SCRAM failure: Authentication failed.`, try adding `?authSource=admin` to the end of your connection string and trying again. -If you run into any other errors with introspection, please refer to this documentation. - ## Touching up your Prisma Schema -The generated Prisma Client from a freshly introspected Prisma 1 based MongoDB database doesn't have the best API. You can adjust the names and use `@map` and `@@map` to map to the underlying collection and field names: +The generated Prisma Client from a freshly introspected Prisma 1 based MongoDB database may not have the best API. You can adjust the model names and fields, just be sure to `@map` and `@@map` the original name to the underlying database collection and field names: ```diff - model posts { @@ -109,11 +118,11 @@ Take caution in doing these renames because you need to make sure the Prisma Sch Unlike SQL databases, MongoDB doesn't have an explicit understanding of relationships between data. This means that Prisma's introspection is unable to infer those relationships for you. -However, where Prisma 1 stores foreign keys is different than where Prisma 2 expects foreign keys, so if you want to take advantage of relationships, you'll need to shift where the foreign keys are on your database before adding the relationships. +We typically recommend adding the relationships by hand with the help of [this documentation](https://prisma.io/docs/guides/database/using-prisma-with-mongodb#how-to-add-in-missing-relations-after-introspection). However, Prisma 1 stores foreign keys is different than where Prisma 2 and later expects foreign keys, so if you want to take advantage of relationships, you'll need to shift where the foreign keys are on your database before adding the relationships. -💡 Download the Prisma 2 VSCode Extension to provide autocomplete and helpful error messages as you transition your Prisma schema. +💡 Download the Prisma VSCode Extension to provide autocomplete and helpful error messages as you transition your Prisma schema. @@ -224,7 +233,7 @@ This means that your MongoDB database isn't running as a replica set. Refer to [ ## Upgrading your Application -Now that you have a working Prisma Client, you can start replacing Prisma 1 queries with Prisma 2 queries. The [Prisma Client Reference](https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#filter-conditions-and-operators) is a helpful resource for learning how to use the Prisma 2 Client. +Now that you have a working Prisma Client, you can start replacing Prisma 1 queries with the latest Prisma queries. The [Prisma Client Reference](https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#filter-conditions-and-operators) is a helpful resource for learning how to use the latest Prisma Client. ## Conclusion diff --git a/content/doc-images/mongodb-logo.svg b/content/doc-images/mongodb-logo.svg new file mode 100644 index 0000000000..f4828669b6 --- /dev/null +++ b/content/doc-images/mongodb-logo.svg @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file