Querying MongoDB with Prisma and Railway
Published:
Learn how to deploy and host a MongoDB database with Railway and query data by connecting to the database with Prisma Client.
Outline
- Introduction
- Create Prisma Project
- Provision a MongoDB Database with Railway
- Connect Railway Database to Prisma Project
- Create a Script to Query the Database
All of this project’s code can be found in the First Look monorepo on my GitHub.
Introduction
In this guide we will deploy a MongoDB database with Railway, add seed data to the database, connect to the database through a connection string, and create a Node script to query that seed data with Prisma Client.
Create Prisma Project
Create a blank new project and initialize a package.json
.
Install the prisma
dependencies.
Set type
to module
in package.json
.
Initialize Prisma Schema
prisma init
scaffolds a basic Prisma project.
The only other necessary file is index.js
. This is used for running test commands against our database with the Prisma Client.
Our project now has the following structure:
Prisma Schema
Define a Post
model by adding the following to the schema.prisma
file.
previewFeatures
must be set to mongoDb
in the client
generator to enable MongoDB support.
Provision a MongoDB Database with Railway
There are two ways to setup a MongoDB database with Railway, through the dashboard or through the CLI.
Railway Dashboard
To use the dashboard, click dev.new and choose “Provision MongoDB.”
After the database is setup click “MongoDB” on the left to see the default test database that is autogenerated.
Choose “Connect” to find your connection string.
Railway CLI
If you want to use the Railway CLI instead of the dashboard, first you need to install it. To verify that you successfully installed the CLI, run the following command to check the CLI version.
Run railway login
to authenticate your Railway account. If you do not have a Railway account you will be prompted to create one.
railway init
initializes a project and asks if you want to start with an empty project or a starter template. Select “Empty Project” and give your project a name.
Run railway add
and select MongoDB to add the MongoDB plugin to your Railway project.
Connect Railway Database to Prisma Project
Return to the Railway dashboard to find your connection string.
Set Environment Variable
Inside your .env
file include DATABASE_URL
and set the variable to the connection string provided by Railway. Specify the database name and authentication source at the end of the connection string by adding /test?authSource=admin
after the port number.
Sync the schema with the database by using prisma db push
.
Seed Database
You can also connect to the database directly with the mongosh
command.
The database can be seeded from the Railway dashboard or through mongosh
with the following seed command.
Check the data tab in the Railway dashboard to see the data.
Generate Prisma Client
Generate the Prisma Client with the prisma generate
command.
Create a Script to Query the Database
Add the following script to index.js
to test that we can read data from our database. This function runs the findMany
query on our post
collection and returns all posts in the collection.
Run the Script
Run node index.js
to execute the main
function.
If you followed along correctly you should get the following output: