-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4791667
commit c77bbba
Showing
16 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
PORT=3000 | ||
DATABASE_URL="file:./prisma/data.db" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,3 +66,5 @@ dist | |
|
||
# env | ||
.env | ||
|
||
*.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- CreateTable | ||
CREATE TABLE "User" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"email" TEXT NOT NULL, | ||
"name" TEXT | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Post" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" DATETIME NOT NULL, | ||
"title" TEXT NOT NULL, | ||
"content" TEXT, | ||
"authorId" TEXT, | ||
CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "sqlite" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "sqlite" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model User { | ||
id String @id @default(cuid()) | ||
email String @unique | ||
name String? | ||
posts Post[] | ||
} | ||
|
||
model Post { | ||
id String @id @default(cuid()) | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
title String | ||
content String? | ||
author User? @relation(fields: [authorId], references: [id]) | ||
authorId String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { Prisma } from '@prisma/client' | ||
import { PrismaClient } from '@prisma/client' | ||
import { cleanupDb } from '../test/utils/db.util' | ||
|
||
const prisma = new PrismaClient() | ||
|
||
const users: Prisma.UserCreateInput[] = [ | ||
{ | ||
name: 'Pierre', | ||
email: 'pierre@example.com', | ||
posts: { | ||
create: [ | ||
{ | ||
title: 'First Post', | ||
content: 'This is an example post generated from `prisma/seed.ts`', | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
name: 'John', | ||
email: 'john@example.io', | ||
posts: { | ||
create: [ | ||
{ | ||
title: 'Second Post', | ||
content: | ||
'This is an other example post generated from `prisma/seed.ts`', | ||
}, | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
async function seed() { | ||
console.log('🌱 Seeding...') | ||
console.time(`🌱 Database has been seeded`) | ||
|
||
console.time('🧹 Cleaned up the database...') | ||
await cleanupDb(prisma) | ||
console.timeEnd('🧹 Cleaned up the database...') | ||
|
||
console.time(`👤 Created ${users.length} users...`) | ||
for (const user of users) { | ||
await prisma.user.create({ | ||
data: user, | ||
}) | ||
} | ||
console.timeEnd(`👤 Created ${users.length} users...`) | ||
|
||
console.timeEnd(`🌱 Database has been seeded`) | ||
} | ||
|
||
seed() | ||
.catch(e => { | ||
console.error(e) | ||
process.exit(1) | ||
}) | ||
.finally(async () => { | ||
await prisma.$disconnect() | ||
}) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
import type { FastifyPluginAsync } from 'fastify' | ||
import fp from 'fastify-plugin' | ||
|
||
declare module 'fastify' { | ||
interface FastifyInstance { | ||
prisma: PrismaClient | ||
} | ||
} | ||
|
||
export default fp<FastifyPluginAsync>(async (fastify, _opts) => { | ||
const prisma = new PrismaClient() | ||
|
||
await prisma.$connect() | ||
|
||
fastify.decorate('prisma', prisma) | ||
|
||
fastify.addHook('onClose', async fastify => { | ||
await fastify.prisma.$disconnect() | ||
}) | ||
}) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { FastifyPluginAsync } from 'fastify' | ||
|
||
const users: FastifyPluginAsync = async (fastify, _opts) => { | ||
fastify.get('/', async (_request, _reply) => { | ||
return fastify.prisma.user.findMany() | ||
}) | ||
} | ||
|
||
export default users |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { PrismaClient } from '@prisma/client' | ||
|
||
export async function cleanupDb(prisma: PrismaClient) { | ||
const tables = await prisma.$queryRaw< | ||
{ name: string }[] | ||
>`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_prisma_migrations';` | ||
|
||
await prisma.$transaction([ | ||
// Disable FK constraints to avoid relation conflicts during deletion | ||
prisma.$executeRawUnsafe(`PRAGMA foreign_keys = OFF`), | ||
// Delete all rows from each table, preserving table structures | ||
...tables.map(({ name }) => | ||
prisma.$executeRawUnsafe(`DELETE from "${name}"`), | ||
), | ||
prisma.$executeRawUnsafe(`PRAGMA foreign_keys = ON`), | ||
]) | ||
} |