Prisma
Prisma is an ORM for TypeScript, that allows you to define your database schema and models in a
schema.prisma
Prisma Client
Located at
src/server/db.ts
Schema
You will find the Prisma schema file at
/prisma/schema.prisma
With NextAuth.js
When you select NextAuth.js in combination with Prisma, the schema file is generated and set up for you with the recommended values for the
User
Session
Account
VerificationToken
Default Database
The default database is an SQLite database, which is great for development and quickly spinning up a proof-of-concept but is not recommended for production. You can change the database to use by changing the
provider
datasource
postgresql
mysql
Seeding your Database
Seeding your databaseβ is a great way to quickly populate your database with test data to help you get started. In order to setup seeding, you will need to create a
seed.ts
/prisma
seed
package.json
ts-node
package.json
{
"scripts": {
"db-seed": "NODE_ENV=development prisma db seed"
},
"prisma": {
"seed": "tsx prisma/seed.ts"
}
}
prisma/seed.ts
import { db } from "../src/server/db";
async function main() {
const id = "cl9ebqhxk00003b600tymydho";
await db.example.upsert({
where: {
id,
},
create: {
id,
},
update: {},
});
}
main()
.then(async () => {
await db.$disconnect();
})
.catch(async (e) => {
console.error(e);
await db.$disconnect();
process.exit(1);
});
Then, just run
pnpm db-seed
npm
yarn
Useful Resources
Resource | Link |
---|---|
Prisma Docs | https://www.prisma.io/docs/β |
Prisma GitHub | https://github.com/prisma/prismaβ |
Prisma Migrate Playground | https://playground.prisma.io/guidesβ |
NextAuth.JS Prisma Adapter | https://next-auth.js.org/adapters/prismaβ |
PlanetScale Connection Guide | https://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project/relational-databases/connect-your-database-typescript-planetscaleβ |