Skip to content

Commit

Permalink
feat(prisma-seeds): ✨ configure seeds for prisma and add a singleton …
Browse files Browse the repository at this point in the history
…prisma object

configure script to seed data for prisma in the prisma/seed and create a prisma singleton in lib,
and add an example for usage in the main page

closes #27
  • Loading branch information
AhmadHRai committed Dec 6, 2023
1 parent e0ff192 commit 37ce22b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,14 @@
"stylelint-config-styled-components": "^0.1.1",
"tailwindcss": "^3.3.5",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5",
"zod": "^3.22.4",
"zustand": "^4.4.6"
},
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
"keywords": [
"react",
"nextjs",
Expand Down
8 changes: 4 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ datasource db {
}

model User {
id Int @id @default(autoincrement())
email String
password String
name String?
id Int @id @default(autoincrement())
email String @unique
password String
name String?
}
23 changes: 23 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
const user = await prisma.user.upsert({
where: { email: 'test@test.com' },
update: {},
create: {
email: 'test@test.com',
name: 'Test User',
password: `$2y$12$GBfcgD6XwaMferSOdYGiduw3Awuo95QAPhxFE0oNJ.Ds8qj3pzEZy`
}
})
console.log({ user })
}
main()
.then(() => prisma.$disconnect())
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
2 changes: 1 addition & 1 deletion src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NextAuth from 'next-auth'

import { authOptions } from '@/app/lib/auth'
import { authOptions } from '@/lib/auth'

const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
10 changes: 9 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import Image from 'next/image'
import Link from 'next/link'

export default function Home() {
// import { prisma } from '@/lib/prisma'

export default /* async */ function Home() {
/* const user = await prisma.user.findFirst({
where: {
email: 'test@test.com'
}
}) */
return (
<main className='flex min-h-screen flex-col items-center justify-between p-24'>
{/* <span>Hello {user?.name}</span> */}
<div className='z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex'>
<p className='fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30'>
Get started by editing&nbsp;
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Create a singleton for prisma client
import { PrismaClient } from '@prisma/client'

// PrismaClient is attached to the `global` object in development to prevent
// exhausting your database connection limit.
//
// Learn more:
// https://pris.ly/d/help/next-js-best-practices

const globalForPrisma = global as unknown as { prisma: PrismaClient }

export const prisma =
globalForPrisma.prisma ||
new PrismaClient({
log: ['query']
})

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

0 comments on commit 37ce22b

Please sign in to comment.