5

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.

Baalamurgan
  • 211
  • 3
  • 8

3 Answers3

6

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

renzhamin
  • 61
  • 1
  • 4
4

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 :)

Baalamurgan
  • 211
  • 3
  • 8
  • I faced this issue too, any ideas why it can't work with just prisma and local MongoDB without docker ro MongoDB Atlas? – user3226932 Sep 17 '22 at 02:18
  • 2
    @user3226932 They are working on this probably and there is a solution which I didn't try. [Github Issue](https://github.com/prisma/prisma/issues/11860#issuecomment-1044695589). Prisma docs is suggesting to use either atlas/Docker for the same error [PrismaDocs](https://www.prisma.io/docs/concepts/database-connectors/mongodb#error-transactions-are-not-supported-by-this-deployment). – Baalamurgan Sep 18 '22 at 04:22
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 prisma@2.26.0 generate" for this old version.

Ihar
  • 11
  • 4