56
loading...
This website collects cookies to deliver better user experience
Prisma Migrate
and a nice GUI that allows you to visualize and update data in your connected database called Prisma Studio
. Our focus in this article and the rest of the series will be on the Prisma Client
and its rich feature-set.If you'd like to learn more about the different types of ORMs and where Prisma fits in and differs from those, please give this page a read.
This tutorial will assume a basic knowledge of JavaScript and its development ecosystem, TypeScript, and Database Terminology. If you want to brush up on these, check out these pages on npm, TypeScript, and SQLite
npm
inside of it, which we will be using to install various packages. (For all you yarn-lovers 🐈, feel free to use that instead)mkdir <my-project>
cd <my-project>
npm init -y
npm i -d prisma typescript ts-node @types/node
tsconfig.json
file, a simple TypeScript file, and a script to our package.json
that we can run to start our development server.// tsconfig.json
// This is just a basic setup, feel free to tweak as needed
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}
// main.ts
console.log("I'm Running!")
package.json
, add the following to the "scripts"
section:"dev": "ts-node main",
npm run dev
init
command and tell it we're going to use SQLite as our datasource. For a full list of options available to the init
command, check out these docs.prisma init --datasource-provider sqlite
prisma
as well as a .env
file in your project's root. The contents of that folder should just be a file named schema.prisma
, which is the file where we will define how the Prisma Client
should get generated and model our data..env
file let's make sure the DATASOURCE_URL
env variable specifies a file location that makes sense (I'm outputting it directly into the prisma
folder):DATABASE_URL="file:dev.db"
.env
variables using the env()
function in a .schema
file. You can see its usage by opening up prisma.schema
and checking out the url
attribute of the datasource
block..schema
file that do different things and have tons of different options. We'll just set up a simple User
model for the purposes of this tutorial.model User {
id Int @id @default(autoincrement())
firstName String
lastName String
email String
}
In a future article we'll dive deeper into the contents of this file and how to model out your data
Prisma Client
after pushing the schema)prisma db push
Prisma Studio
. Run this command to open up the studioprisma studio
User
model you should see a nice table view of your database table with options to search and add rows to the table. Pretty sweet! This tool definitely comes in handy working with your data. Prisma Client
is generated by default into your node_modules
folder under @prisma/client
. To start, go ahead and modify your main.ts
. Import and instantiate the Prisma client.import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {}
main()
.catch( e => { throw e })
.finally( async () => await prisma.$disconnect() )
PrismaClient
class that was generated by Prisma based off of the model definitions you gave it. Right now our main()
function doesn't do anything, this is where we will add some code to interact with our data. db push
. Go ahead and run the following command to generate the Prisma Client and try again.prisma generate
main()
function, lets try to print out all of the users
in our database.async function main() {
const users = await prisma.user.findMany();
console.log(JSON.stringify(users));
}
Prisma Studio
and add a record to your User table.Prisma Client
docs.