MongoDB in Remix.js

Table of contents

No heading

No headings in the article.

Follow these instructions in your basic Remix.js project to connect your existing MongoDB to your app.

In your terminal, install these packages as a dev dependency. This command is for Remix.js project set with Typescript settings. So typescript will be in package.json. If you have done a Remix.js setup without Typescript add typescript while installing this and run npx tsc --init to add tsconfig file.

npm i -D prisma ts-node @types/node

After installing these packages, initialize the Prisma with this command:

npx prisma init --datasource-provider mongodb

This will create .env and prisma/schema.prisma file. In VSCode extensions install Prisma extension to help you in autocomplete and formating the Prisma file.

If you have done these steps previously and the default provider is sqlite, then fret not. All you have to do is open the file prisma/schema.prisma .

Now open your MongoDB cloud platform and signin. In the database section, look for Connect option. There are some instructions to connect the mongodb with Node. Something like below. Replace the password field. Open .env file and add this as the DATABASE_URL .

mongodb+srv://username:<password>@cluster0.z2wefqm.mongodb.net/?retryWrites=true&w=majority

In the above line, to create a new database, add your database name between .net and ? like . . .mongodb.net/your-database-name?. . . . A new database will be automatically created once synchronised.