Prisma + MongoDB -> Replica set
Asked Answered
A

3

6

Error Invalid "prisma.user.create()". Prisma needs to perform transactions, which requires your MongoDB server to be run as a replica set.

I used Nx (nx.dev) with MongoDB/Express with Prisma.

Akira answered 1/9, 2022 at 13:22 Comment(0)
H
8

Local Instance with docker

An easy to use docker image is available that creates a single instance replica

  • Pull the image with docker pull prismagraphql/mongo-single-replica:5.0.3
  • Run the image with
docker run --name mongo \
      -p 27017:27017 \
      -e MONGO_INITDB_ROOT_USERNAME="monty" \
      -e MONGO_INITDB_ROOT_PASSWORD="pass" \
      -d prismagraphql/mongo-single-replica:5.0.3
  • The connection URL should like this
DATABASE_URL="mongodb://monty:pass@localhost:27017/db_name?authSource=admin&directConnection=true"

You must provide authSource=admin option otherwise authentication will fail

MongoDB Atlas

  • Create a free cluster with all default values
  • Note the username, password and the hostname
  • The URL should look like this
DATABASE_URL="mongodb+srv://username:password@cluster_name.random_string.net/db_name?retryWrites=true&w=majority"

Note that the official documentaion currently follows the previous format used for the local instance but here it does not include the port number and has the +srv suffix, without which I ran into some problems.

If you want use the format used in the documentation then you must provide the option ssl=true and for the hostname you have to use the primary cluster which looks like random_string.mongodb.net:27017, which you can find in the overview tab after clicking in your cluster name

Had answered 18/4, 2023 at 10:1 Comment(0)
A
5

Apparently, after long hours of facing continuous issues in connecting Prisma and MongoDB(local), we have 3 ways of fixing this. You have to use either Docker or MongoDB Atlas.

Docker - Docker
MongoDB Atlas - MongoDB Replica

Ps. Your MongoDB URL env should be looking like this -> mongodb://localhost:27017/<your-db-name>?retryWrites=true&w=majority.

I decided to finally choose the option 3. Jumping from MongoDB to PostgreSQL which don't have any replica issues, but no noSQL :)

Akira answered 1/9, 2022 at 13:22 Comment(2)
I faced this issue too, any ideas why it can't work with just prisma and local MongoDB without docker ro MongoDB Atlas?Paoting
@Paoting They are working on this probably and there is a solution which I didn't try. Github Issue. Prisma docs is suggesting to use either atlas/Docker for the same error PrismaDocs.Akira
D
0

You can use "prisma": "2.26.0" and "@prisma/client": "2.26.0" instead of your current version. They don`t need a replica set. Also you have to use @default(dbgenerated()) instead of @default(auto()) both with "npx [email protected] generate" for this old version.

Degenerate answered 3/3, 2023 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.