Nestjs Mongodb Prisma Tutorial
This tutorials explains about Integration prisma Mongodb with NestJS with tutorials examples
Create a NestJS application
@nestjs/cli
is a CLI command line tool to create a boilerplate code.
First, Install using below command
npm i -g @nestjs/cli
Here is an Output:
E:\work\nestjs>npm i -g @nestjs/cli
changed 263 packages in 48s
44 packages are looking for funding
run `npm fund` for details
After installed CLI, Create a NestJS application
nest new nest-prisma-mongodb-example
nest new
create an application with following things
- Application protototype files and folder
- package.json with nestjs dependencies
- Install dependencies
E:\work\nestjs>nest new nest-prisma-mongodb-example
⚡ We will scaffold your app in a few seconds..
? Which package manager would you ❤️ to use? npm
CREATE nest-prisma-mongodb-example/.eslintrc.js (663 bytes)
CREATE nest-prisma-mongodb-example/.prettierrc (51 bytes)
CREATE nest-prisma-mongodb-example/nest-cli.json (171 bytes)
CREATE nest-prisma-mongodb-example/package.json (1968 bytes)
CREATE nest-prisma-mongodb-example/README.md (3340 bytes)
CREATE nest-prisma-mongodb-example/tsconfig.build.json (97 bytes)
CREATE nest-prisma-mongodb-example/tsconfig.json (546 bytes)
CREATE nest-prisma-mongodb-example/src/app.controller.spec.ts (617 bytes)
CREATE nest-prisma-mongodb-example/src/app.controller.ts (274 bytes)
CREATE nest-prisma-mongodb-example/src/app.module.ts (249 bytes)
CREATE nest-prisma-mongodb-example/src/app.service.ts (142 bytes)
CREATE nest-prisma-mongodb-example/src/main.ts (208 bytes)
CREATE nest-prisma-mongodb-example/test/app.e2e-spec.ts (630 bytes)
CREATE nest-prisma-mongodb-example/test/jest-e2e.json (183 bytes)
✔ Installation in progress... ☕
🚀 Successfully created project nest-prisma-mongodb-example
👉 Get started with the following commands:
$ cd nest-prisma-mongodb-example
$ npm run start
Thanks for installing Nest 🙏
Please consider donating to our open collective
to help us maintain this package.
🍷 Donate: https://opencollective.com/nest
Change to project directory
E:\work\nestjs>cd nest-prisma-mongodb-example\
E:\work\nestjs\nest-prisma-mongodb-example>
Next, Install prisma CLI🔗 into your application
npm install prisma --save-dev
It adds prisma
as devDependencies in package.json, install to app node_modules folder.
{
"devDependencies": {
"prisma": "^5.2.0"
}
}
Next, Initialize prisma related configuration using below command
npx prisma init
prisma init
does setup prism for the application, does following things
- Creates a
prisma
folder in application - This folder contains
schema.prisma
, schema file mapping for your database table, also database connection data source provider configuration.
Default provider is postgress and url configuration mapped wiht env
file key
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Output:
E:\work\nestjs\nest-prisma-mongodb-example>npx prisma init
✔ Your Prisma schema was created at prisma/schema.prisma
You can now open it in your favorite editor.
warn You already have a .gitignore file. Don't forget to add `.env` in it to not commit any private information.
Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started
2. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql, sqlite, sqlserver, mongodb or cockroachdb.
3. Run prisma db pull to turn your database schema into a Prisma schema.
4. Run prisma generate to generate the Prisma Client. You can then start querying your database.
More information in our documentation:
https://pris.ly/d/getting-started
- Also, Creates
.env
file which contains environment specific // This is your Prisma schema file, // learn more about it in the docs: https://pris.ly/d/prisma-schema🔗
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Next, Change provider from postgresql to mongodb
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}