Prisma is a Typescript based ORM for Javascript Frameworks such as React, Vuejs and Angular. It allows developers easily configure database and hand data using model classes.
Mongo DB
MongoDB is a NoSQL database for web, primarily. It is widely used in cloud based web and Mobile application.
How to configure MongoDB on Prima
In Prisma configuration file we can use preview version on MongoDB driver. The connection string can be stored in .env file (at the route of your project).
Local database
Form local database we can use the following connection string
//.env
DATABASE_URL = mongodb://localhost:27017/blogdata?retryWrites=true&w=majority
Mongo Atlas database
For cloud based Mongo database we have to rely on the following connection.
//.env
DATABASE_URL = mongodb+srv://admin:RRHEfMsK8zHWsGTI@cluster0.cqnll.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
schema.prisma
Finally the Prisma Schema file would look like the following,
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongodb"]
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String
}
Migrating DB
If you are familiar with Prisma and Schema, we used to run migrate command to update the database with model.
There is an exception for Mongo, you don’t need to Migrate, instead run npx prisma generate command after any change made to schema.
To update the model we use npx prisma db push command.
- Prisma MongoDB connection
- How to clear all element in mongoose object array in Nodejs
- How to export multiple components in Reactjs
- How to add element to mongoose object array in Nodejs
- How to interact with input in Reactjs
- How to populate multiple MongoDB objects on the same path in express-node
- How to populate selected fields in a mongo document path in expressjs
- How to populate and display nested mongo objectin express-node route
- How to render mongo object in Reactjs
- How to render mongo document in Reactjs