Skip to content

Instantly share code, notes, and snippets.

@Luiyit
Last active November 27, 2023 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luiyit/adc3a892b0360fd9f25b06cdbbfd7d10 to your computer and use it in GitHub Desktop.
Save Luiyit/adc3a892b0360fd9f25b06cdbbfd7d10 to your computer and use it in GitHub Desktop.
Configurate MongoDB with Prisma client

Mongo DB + Prisma

MongoDB is a NoSQL database and Prisma is an ORM for NodeJS. Here are the steps to create a MongoDB database and set up Prisma to use it.

Install MongoDB locally (If you need replica set use docker)

This option works, but you will need to use Docker because prisma needs something called Replica Set.So, the easy way to have it is using docker or creating the database on MongoDB Atlas.

I recommend just to use MongoDB inside a docker container and then connect to it from Prisma. But here are the steps to install it locally.

MongoDB Locally

  1. Create a MongoDB account (optional) Website

  2. Install MondoDB Server Server

  3. Install MondoDB Shell Shell

  4. Add the bin path to the system (inside path environment) path: C:\Program Files\MongoDB\Server\7.0\bin

  5. Create this empty folders. We need it to be able to start the mongoDB server C:\data\db

  6. Test the installation

  mongod --version
  mongos --version
  1. Open a bash and run mongod

  2. Open another console and run mongosh

  3. By default MongoDB create a server with none authentication. connection url: mongodb://localhost:27017/DATABASE_NAME?

  4. To create a user you can follow this Instructions. But the steps are simple. On mongosh run this command

use admin

db.createUser({ user: "mongo-admin", pwd: "mongo-password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })

Here a couple of options about the role

db.createUser({ ..., roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ] })
db.createUser({ ..., roles: [ { role: "root", db: "admin" } ] } })
db.createUser({ ..., roles: [ {  roles: [ { role: "readWrite", db: "my-database" }, { role: "read", db:"another-database" } ] } ] } )

MongoDB through Docker [RECOMMENDED]

  1. Create a folder called mongodb-rs inside your project folder.

  2. Inside the folder create a Dockerfile with this content

FROM mongo:4

# 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.getUser('$MONGO_INITDB_ROOT_USERNAME') || 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;
  1. Create a docker-compose.yml file with this content
version: "3.1"
services:
  mongo:
    build: ./mongodb-rs
    restart: always
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
      - MONGO_INITDB_DATABASE=demo
      - MONGO_REPLICA_HOST=localhost
      - MONGO_REPLICA_PORT=27017
    ports:
      - 27017:27017
  1. Run those command
  docker-compose build
  docker-compose up -d
  1. Create connection url: mongodb://localhost:27017/<MONGO_INITDB_DATABASE>?authSource=admin&directConnection=true

  2. You can check this article if something was wrong Ref

  3. Important note: Because you are using MongoDB inside a docker container, you don't need a local server running. So, you should stop the local server and just use the docker container. If you don't do this, you will have server conflict. So, make sure you stopped all the local servers.

  mongod --shutdown

Prisma

To use prisma you can follow this Instructions. But the steps are simple.

  1. Install Prisma
  2. run npx prisma init
  3. Set your connection url on the .env file
  4. Create a basic collection definition inside schema.prisma file.
model User {
  id       String     @id @default(auto()) @map("_id") @db.ObjectId
  email    String     @unique
  name     String?
}
  1. run npx run generate This generate a custom client based on your schema.prisma file.

Links

MongoDB prisma + Mongo express + mongo + prisma

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment