question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting Started Documentation with MongoDB on Prisma should talk about Replica Set requirement (Epic 3)

See original GitHub issue

~Currently our getting started guide assumes you know how to get a MongoDB connection URL from somewhere:~

  • Update: we’ll do the above in the Prisma Data Guide because getting a connection URL is not only relevant to Prisma Users.

https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/mongodb/connect-your-database-typescript-mongodb

This assumes that the MongoDB url is configured as a replica set. The default local MongoDB does not work like this and you’ll end up with an error like this:

Transactions are not supported by this deployment

There’s a bunch of good suggestions in this issue on how to configure your local MongoDB to support the replicaset.

Extra: consider also updating Getting Started Guide example schema to use embedded types.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
Jolg42commented, Mar 17, 2022

I think we should document the current MongoDB docker compose setup Pierre created, it might be the easiest one command setup (if you have docker compose).

https://github.com/prisma/prisma/blob/main/docker/docker-compose.yml#L91-L99 So

version: '3.7'

services:
  mongodb:
    build: ./mongodb-replica
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: prisma
      MONGO_REPLICA_HOST: localhost
      MONGO_REPLICA_PORT: 27017
    ports:
      - '27017:27017'

Note I changed the port from current setup to use the default port (so we’ll need to do a quick check if that still works after, it should, but let’s verify)

https://github.com/prisma/prisma/blob/main/docker/mongodb_replica/Dockerfile

FROM mongo:5

# we take over the default & start mongo in replica set mode in a background task
ENTRYPOINT mongod --port $MONGO_REPLICA_PORT --replSet rs0 --bind_ip 0.0.0.0 & MONGOD_PID=$!; \
# we prepare the replica set with a single node and prepare the root user config
INIT_REPL_CMD="rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: '$MONGO_REPLICA_HOST:$MONGO_REPLICA_PORT' }] })"; \
INIT_USER_CMD="db.createUser({ user: '$MONGO_INITDB_ROOT_USERNAME', pwd: '$MONGO_INITDB_ROOT_PASSWORD', roles: [ 'root' ] })"; \
# we wait for the replica set to be ready and then submit the commands just above
until (mongo admin --port $MONGO_REPLICA_PORT --eval "$INIT_REPL_CMD && $INIT_USER_CMD"); do sleep 1; done; \
# we are done but we keep the container by waiting on signals from the mongo task
echo "REPLICA SET ONLINE"; wait $MONGOD_PID;

Note that this setup is only recommended for development.

4reactions
paulrostorpcommented, Feb 16, 2022

@Jolg42 I tweaked the dockerfile to get it to work on my machine

  1. switch to use bind_ip_all
  2. (optional) I also disabled authentication, so that databases can be created on the fly. I find this useful when running several services locally, each using their own database.
FROM mongo:5

# we take over the default & start mongo in replica set mode in a background task
ENTRYPOINT mongod --port $MONGO_REPLICA_PORT --replSet rs0 --bind_ip_all & MONGOD_PID=$!; \
# we prepare the replica set with a single node and prepare the root user config
INIT_REPL_CMD="rs.initiate()"; \
# we wait for the replica set to be ready and then submit the commands just above
until (mongo admin --port $MONGO_REPLICA_PORT --eval "$INIT_REPL_CMD"); do sleep 1; done; \
# we are done but we keep the container by waiting on signals from the mongo task
echo "REPLICA SET ONLINE"; wait $MONGOD_PID;

An additional requirement when using replicaSet and no auth is to use the directConnection flag

DATABASE_URL="mongodb://localhost:27017/my-service?directConnection=true"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Prisma with MongoDB
When using transactions, MongoDB requires replication of your data set to be enabled. To do this, you will need to configure a replica...
Read more >
Using MongoDB with Prisma - YouTube
MongoDB is currently supported as a preview feature in Prisma. In this video, Ryan Chenkie walks through how to use MongoDB in a...
Read more >
MongoDB vs Realm | What are the differences? - StackShare
In Udemy there is a free course about it to get started. ... as it will support/guide you in creating a structured data...
Read more >
Untitled
Crf450 parts for sale, To quoc goi ten minh dan truong karaoke, Backward induction 3 players, Jacques cartier facts timeline, All mobile themes...
Read more >
Prisma Mongodb can't create a user model - Stack Overflow
I'm Running Mongodb 4+ version I solved it as below As the error describes you need to create a replica. Either you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found