Skip to content

Commit

Permalink
chore: change architecture (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBerger committed Feb 1, 2024
1 parent e72886c commit fa92c91
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 65 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"ts-node": "^10.4.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.2.2",
"vitest": "^1.2.2"
"vitest": "^1.2.2",
"vitest-mock-extended": "^1.3.1"
}
}
4 changes: 1 addition & 3 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Prisma } from '@prisma/client'
import { PrismaClient } from '@prisma/client'
import { cleanupDb } from '../test/utils/db.util'

const prisma = new PrismaClient()
import prisma from '../src/libs/prisma'

const users: Prisma.UserCreateInput[] = [
{
Expand Down
5 changes: 4 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.setSerializerCompiler(serializerCompiler)

fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
dir: path.join(__dirname, 'domains'),
dirNameRoutePrefix: false,
matchFilter: path => path.endsWith('router.ts'),
ignoreFilter: path => path.endsWith('.test.ts'),
options: opts,
})
}
Expand Down
1 change: 1 addition & 0 deletions src/domains/example/handler/example.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const exampleHandler = () => 'this is an example'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { it, expect } from 'vitest'
import fastify from '../helper'
import fastify from '../../../../test/helper'

it('example is loaded', async () => {
const res = await fastify.inject({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { FastifyPluginAsync } from 'fastify'
import type { ZodTypeProvider } from 'fastify-type-provider-zod'
import { z } from 'zod'
import { exampleHandler } from '../handler/example.handler'

const example: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
fastify
.withTypeProvider<ZodTypeProvider>()
.get(
'/',
'/example',
{ schema: { response: { 200: z.string() } } },
async (_request, _reply) => {
return 'this is an example'
},
exampleHandler,
)
}

Expand Down
3 changes: 0 additions & 3 deletions src/domains/users/db/findAllUsers.db.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PrismaClient } from '@prisma/client'
import type { CreateUserSchema } from '../schemas/user.schema'
import type { CreateUserSchema } from '../schemas/users.schema'

export const createUser = (prisma: PrismaClient, user: CreateUserSchema) =>
prisma.user.create({
Expand All @@ -8,3 +8,5 @@ export const createUser = (prisma: PrismaClient, user: CreateUserSchema) =>
name: user.name,
},
})

export const findAllUsers = (prisma: PrismaClient) => prisma.user.findMany()
14 changes: 14 additions & 0 deletions src/domains/users/handler/users.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createUser, findAllUsers } from '../db/users.db'
import prisma from '../../../libs/prisma'
import type { FastifyReply, FastifyRequest } from 'fastify'
import type { CreateUserSchema } from '../schemas/users.schema'

export const findAllUsersHandler = () => findAllUsers(prisma)

export const createUserHandler = async (
req: FastifyRequest<{ Body: CreateUserSchema }>,
reply: FastifyReply,
) => {
const user = await createUser(prisma, req.body)
reply.code(201).send(user)
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import type { FastifyPluginAsync } from 'fastify'
import type { ZodTypeProvider } from 'fastify-type-provider-zod'
import { findAllUsers } from '../../domains/users/db/findAllUsers.db'
import {
createUserResponseSchema,
createUserSchema,
findAllUsersSchema,
} from '../../domains/users/schemas/user.schema'
import { createUser } from '../../domains/users/db/createUser.db'
} from '../schemas/users.schema'
import {
createUserHandler,
findAllUsersHandler,
} from '../handler/users.handler'

const users: FastifyPluginAsync = async (fastify, _opts) => {
fastify
.withTypeProvider<ZodTypeProvider>()
.get('/', { schema: { response: { 200: findAllUsersSchema } } }, () => {
return findAllUsers(fastify.prisma)
})
.get(
'/users',
{ schema: { response: { 200: findAllUsersSchema } } },
findAllUsersHandler,
)

fastify.withTypeProvider<ZodTypeProvider>().post(
'/',
'/users',
{
schema: {
body: createUserSchema,
Expand All @@ -25,10 +29,7 @@ const users: FastifyPluginAsync = async (fastify, _opts) => {
},
},
},
async (request, reply) => {
const user = await createUser(fastify.prisma, request.body)
reply.code(201).send(user)
},
createUserHandler,
)
}

Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions src/libs/__mocks__/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { PrismaClient } from '@prisma/client'
import { beforeEach } from 'vitest'
import { mockDeep, mockReset } from 'vitest-mock-extended'

beforeEach(() => {
mockReset(prisma)
})

const prisma = mockDeep<PrismaClient>()
export default prisma
4 changes: 4 additions & 0 deletions src/libs/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()
export default prisma
21 changes: 0 additions & 21 deletions src/plugins/prisma.plugin.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/routes/root.route.ts

This file was deleted.

10 changes: 0 additions & 10 deletions test/routes/root.test.ts

This file was deleted.

0 comments on commit fa92c91

Please sign in to comment.