Start from scratch with MongoDB (15 min) | typescript-mongodb
Learn how to create a new Node.js or TypeScript project from scratch by connecting Prisma to your MongoDB database and generating a Prisma Client for database access. The following tutorial introduces you to the Prisma CLI and Prisma Client .
See System requirements for exact version requirements.
Make sure you have your database connection URL at hand. If you don’t have a database server running and just want to explore Prisma, check out the Quickstart .
The MongoDB database connector uses transactions to support nested writes. Transactions require a replica set deployment. The easiest way to deploy a replica set is with Atlas . It’s free to get started.
Access to a MongoDB 4.2+ server with a replica set deployment. We recommend using MongoDB Atlas .
Node.js installed on your machine
In order to successfully complete this guide, you need:
Create project setup
As a first step, create a project directory and navigate into it:
$
mkdir hello-prisma
$
cd hello-prisma
Next, initialize a TypeScript project and add the Prisma CLI as a development dependency to it:
$
npm init -y
$
npm install prisma typescript ts-node @types/node --save-dev
This creates a
package.json
with an initial setup for your TypeScript app.Next, initialize TypeScript:
$
npx tsc --init
Next, initialize a Node.js project and add the Prisma CLI as a development dependency to it:
$
npm init -y
$
npm install prisma --save-dev
This creates a
package.json
with an initial setup for a Node.js app.You can now invoke the Prisma CLI by prefixing it with
npx
:
$
npx prisma
Next, set up your Prisma project by creating your Prisma schema file with the following command:
$
npx prisma init
This command does two things:
- creates a new directory called
prisma
that contains a file calledschema.prisma
, which contains the Prisma schema with your database connection variable and schema models - creates the
.env
file in the root directory of the project, which is used for defining environment variables (such as your database connection)