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.
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.
-
Create a MongoDB account (optional) Website
-
Install MondoDB Server Server
-
Install MondoDB Shell Shell
-
Add the bin path to the system (inside path environment)
path: C:\Program Files\MongoDB\Server\7.0\bin
-
Create this empty folders. We need it to be able to start the mongoDB server
C:\data\db
-
Test the installation
mongod --version
mongos --version
-
Open a bash and run
mongod
-
Open another console and run
mongosh
-
By default MongoDB create a server with none authentication. connection url:
mongodb://localhost:27017/DATABASE_NAME?
-
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" } ] } ] } )
-
Create a folder called
mongodb-rs
inside your project folder. -
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;
- 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
- Run those command
docker-compose build
docker-compose up -d
-
Create connection url:
mongodb://localhost:27017/<MONGO_INITDB_DATABASE>?authSource=admin&directConnection=true
-
You can check this article if something was wrong Ref
-
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
To use prisma you can follow this Instructions. But the steps are simple.
- Install Prisma
- run
npx prisma init
- Set your connection url on the .env file
- 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?
}
- run
npx run generate
This generate a custom client based on your schema.prisma file.