prisma init

πŸ‘¨β€πŸ’Ό Let's initialize our application with a database and prisma schema. We don't have an existing database to pull from, so we'll create a new one using the Prisma CLI.
🐨 In the playground directory, run the following command:
npx prisma init --url file:./data.db
By using the --url flag, we're telling Prisma to create a SQLite database called data.db in the prisma directory.
Once you've done that, you should have two new files:
  1. - Environment variable for the DATABASE_URL
  2. - Prisma schema
The .env file is auto-loaded by Prisma to find your database connection string. The prisma/schema.prisma file is where you'll define your database schema and is already loaded with a little bit of information:
// 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 = "sqlite"
  url      = env("DATABASE_URL")
}
This just has a few bits of configuration in it for now. If you'd like to dive into what these different bits mean, check out the Prisma Schema docs.
🐨 Add user to the schema. We have the following fields:
  • id should be a string, serve as the unique identifier for each record in the User model, and its default value should be generated by the cuid function ensuring uniqueness and collision resistance (@default(cuid())).
  • email needs to be a string and should be unique to prevent users from creating multiple accounts with the same email address.
  • username should be a string and needs to be unique to ensure every user has a distinct username.
  • name is a string that represents the name of the user. It is an optional field as indicated by the question mark (?), so it can be null.
  • createdAt is a DateTime field that defaults to the current timestamp (@default(now())) when a new user record is created.
  • updatedAt is a DateTime field that updates to the current timestamp whenever there is any update to a specific user record. This is handled automatically by Prisma with the @updatedAt attribute.
Check the Prisma Schema docs for examples of the schema syntax.
πŸ’° Note, if you get stuck on the syntax, remember you can check the Diff Tab to check your work against the solution.
Great, now that we've got that, let's push that schema to our database:
npx prisma db push
You should get output that says something like:
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": SQLite database "data.db" at "file:./data.db"

SQLite database data.db created at file:./data.db

πŸš€  Your database is now in sync with your Prisma schema. Done in 9ms

βœ” Generated Prisma Client (5.0.0 | library) to ./node_modules/@prisma/client in 36ms
With that, let's open up Prisma Studio to see what we've got:
npx prisma studio
You should have the User model with the fields we defined in the schema. You should also be able to create new users and see them in the database. Go ahead and create a new user. You can use the following values:
  • email: kody@kcd.dev
  • username: kody
  • name: Kody
Save that and you should have a new user in the database! Great job! You've initialized your SQLite database with Prisma.