Using Prisma with MongoDB + Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
using Prisma with a dockerized mongodb should be easy right unfortunately you'd be wrong but in this video I'm gonna make it easy for you [Music] now in order to get this done we're going to run into kind of three main issues or three main nuances and how to get this whole setup running and I'm going to take you through each of those and then at the end of the video I'll tell you a little bit about why I prefer this setup to the recommended setup by Prisma the first thing we're going to do is do our standard Prisma setup we're going to do say pnpm install Prisma and npm installed Dev dependencies Prisma client now I'm using um this is in the context of a mono repo I'm using pnpm there's going to be a couple things that we'll do a little bit differently because of that but really this should all be very similar to a standard setup so with Prisma installed we will make sure to Prisma init and this will set up our standard Prisma files it'll also set up an EnV file that we'll take a look at so let's go take a look at our DOT EnV file so by default Prisma sets us up for postgres which is nice but in our schema file we want to make sure we're using mongodb so we'll put in mongodb as the provider and then because I'm in a monorepo one other thing that I have to do is I have to set an output directory because I want prisma's generated client to be only in this workspace so we're going to say node module slash dot Prisma slash client to make sure that's a little bit more localized another thing I like to do is validate each step as I implement it and so what we're going to do next is we're going to say npx Prisma generate which will attempt to generate the client because we don't have any models in here yet though Prisma is going to complain at us a little bit and say uh you don't have any models you can define a model like this so let's just follow their instructions here we're going to add a user model which is going to be in compliance with a mongodb setup here so we did that after we changed the provider we ran the generate gives us this little code and if we just run that generate command again it should say everything is good yep and one little Nuance here again because I'm in this mono repo setup the import looks a little bit different here you can just ignore that on my machine use whatever's showing up on your machine all right and here's our first little Nuance here Prisma uses transactions and in mongodb those are only supported when you're using a replica set so what we're going to have to do is set up a mongodb replica set with a single running instance of a single Docker container everything has like a node of one right thankfully Prisma doesn't leave us alone in this and so we can go out whoops we can go out to prisma's GitHub and they have a Docker file under their GitHub both mongodb replica in here they've given us a basic setup of a Docker image that spins up a replica set one so we're going to copy that we're just going to reuse it in our environment same exact way they do so to do this we're going to make a directory called mongodb RS or replica set and then in there we will create a Docker file and we'll go ahead and just open that up in vs code and paste in the docker file contents from the Prisma GitHub and let's just take a minute to quickly go through this so we kind of understand what's happening so we start from the base image we're going to override the entry point and specify a custom mongod command for startup we're specifying a few environment variables here like the replica Port like down below we've got the username password getting used okay so basically what this is doing is we're going to overwrite it and we're going to capture the PID then we're going to make a couple of custom calls so down here it says admin so we're running a few commands as admin the commands are uh repo command which is a replica set command and user command set up a user so that's replica dot initi replica set dot initiate um tells to run as a replica set we set up a default user to be the root user and then we basically reattach that PID to the root running process of the container so that everything can shut down cleanly so now let's go ahead and build that container set it up I'm going to use Docker compose for that of course so we're going to touch dockercompose.yaml and in there we paste in just a basic Docker compose setup here I'll make sure we want to build that image I named it mongodb not RS um and we just make sure to set up all these environment variables the username password initial database uh localhost Port so let's run Docker compose up compose up Dash D and we'll say Docker compose uh logs follow see the output and we can see that it worked everything replica set online great again for the sake of validation let's make sure that we can see our uh database in there and that like the auth credentials work so we can say Docker compose exec so in the current running container we're going to execute commands and it's going to enter into the shell so we're going to say user is uh password is Big example it was cool so we can see here that the command prompt inside of is saying rs0 replica set zero primary if I say show DBS I do see the standard databases admin config and local and you'll notice that our default mongodatabase isn't in there yet one little Nuance here is that will not create a database until a collection for that database has been created so let's leave this terminal open and we'll go ahead and create another one and we're going to make sure that we're in the right directory so I have the Prisma schema set up and I want to be able to push this collection into my database but this is kind of the second Nuance that I always run into when I'm running with is the URL strings so if we go look at our EnV file for Prisma it's still set up for postgres so let's set this up for the connection to our database so it's going to be mongodb uh username is password is example connection Port is 27017 the database is these are some query string parameters for postgres so we'll get rid of those this is what I always want to write this is wrong yes we want to connect to the database but the um users that is going to authenticate with are in the admin database so in order to do that in order to make sure that we're connecting and validating against the admin database we have to say auth source is admin can tell you how many times I spent I spin my wheels for getting that little query string parameter so with that in place we should be able to say npx Charisma oh boy fingers are in the wrong place Prisma DB push and it should successfully connect and push in our user schema which should result in a user collection looks like that worked but let's go back to our um shell and validate that we have the new database awesome we do we have and if I use I can say DB user dot count to show that that collection does in fact exist but it's empty of course all right let's hop out of here clear that now normally I could go start seeding my data doing some of that but I want to jump to the third Nuance with this setup like I said Prisma got us started with the docker file but they're missing something if I go and just shut down my Docker compose set up here if I say Docker compose stop there it goes and I run it again I just try to bring it back up again and this time we'll do it without detaching because I just want to see the logs right away what we'll see is an error has occurred okay let's just kill it it says error couldn't add user user admin already exists so the reason this is happening is if we go back to our Docker file we see that whenever this container spins up it's always trying to create the user but the user already exists if you started it once stopped it and are restarting it again so in order to improve the development flow here all we have to do is add a little command in front that says DB get user checks if the user is already there if it comes back as a null then it will go create the user otherwise it just skips the step now to use this we have to make sure that we say Docker compose build oops whoops how do you spell build how do you spell build this will make sure that we pick up our changes from the docker file then if I Docker compose up again pose logs Dash F2 follow we don't see that error occurring anymore and if we scroll up we do see this success message replica set online well there you go you guys are all set up with Prisma and Docker now when going through the Prisma docs they're going to recommend that you use Atlas which is the cloud offering of mongodb but there are three main reasons why I don't really prefer this setup the first reason is that I don't like to have to register an account to anybody in order to play with their technology and that's what you have to do with mongodb Atlas number two I really prefer to have my stuff locally so I can work offline I feel like I can connect my tools better and have more control over data seating and stuff like that when I'm doing things locally and the main reason reason number three is probably that once I set this all up in Docker compose and I've got my app in there my database maybe a redis I can put all that into my CI system I can push it on every commit I can have it running through end-to-end tests everything is ephemeral all the data seating is controlled there's no extra environment set up that I have to do no extra Cloud costs anything like that so I really prefer to do everything in a controlled Docker environments like this well thank you so much for joining me today I hope you guys learned something and I'll see you in the next one [Music]
Info
Channel: jemini-io
Views: 1,817
Rating: undefined out of 5
Keywords:
Id: mj5MxsEiHe8
Channel Id: undefined
Length: 10min 58sec (658 seconds)
Published: Fri Feb 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.