skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

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


  2. 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].0 generate" for this old version.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search