-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prisma and a fake new user route
- Loading branch information
Showing
11 changed files
with
144 additions
and
14 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
10 changes: 10 additions & 0 deletions
10
api/prisma/migrations/20241127080850_add_password_field_to_user/migration.sql
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,10 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `password` to the `User` table without a default value. This is not possible if the table is not empty. | ||
- Made the column `name` on table `User` required. This step will fail if there are existing NULL values in that column. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "password" TEXT NOT NULL, | ||
ALTER COLUMN "name" SET NOT NULL; |
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,25 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify'; | ||
import { getAllUserNames } from '../services/user.service'; | ||
import { createUser } from '../services/user.service'; | ||
|
||
export async function getUsersHandler( | ||
request: FastifyRequest, | ||
reply: FastifyReply, | ||
) { | ||
try { | ||
reply.send(await getAllUserNames()); | ||
} catch (error) { | ||
reply.status(500).send({ error: 'An error occurred' }); | ||
} | ||
} | ||
export async function createUserHandler( | ||
request: FastifyRequest, | ||
reply: FastifyReply, | ||
) { | ||
try { | ||
const user = await createUser(); | ||
reply.status(201).send(user); | ||
} catch (error) { | ||
reply.status(500).send({ error: 'An error occurred' }); | ||
} | ||
} |
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,13 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
// Prevent multiple instances in development due to hot reloading | ||
declare global { | ||
var prisma: PrismaClient | undefined; | ||
} | ||
|
||
// Create singleton instance | ||
export const prisma = global.prisma || new PrismaClient(); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
global.prisma = prisma; | ||
} |
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,15 @@ | ||
import { FastifyInstance } from 'fastify'; | ||
import { createUserHandler, getUsersHandler } from '../handlers/user.handler'; | ||
|
||
export default async function userRoutes(fastify: FastifyInstance) { | ||
fastify.route({ | ||
method: 'GET', | ||
url: '/user', | ||
handler: getUsersHandler, | ||
}); | ||
fastify.route({ | ||
method: 'POST', | ||
url: '/user', | ||
handler: createUserHandler, | ||
}); | ||
} |
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 { prisma } from '../lib/prisma'; | ||
import { faker } from '@faker-js/faker'; | ||
|
||
export async function getAllUserNames() { | ||
const users = await prisma.user.findMany({ | ||
select: { | ||
name: true, | ||
}, | ||
}); | ||
return users.map((user) => user.name); | ||
} | ||
export async function createUser() { | ||
const user = await prisma.user.create({ | ||
data: { | ||
name: faker.person.fullName(), | ||
email: faker.internet.email(), | ||
password: faker.internet.password(), | ||
}, | ||
}); | ||
return user; | ||
} |
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