diff --git a/bun.lockb b/bun.lockb index 72b1d6b..9b4f15b 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 823fa66..603beca 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/prisma/seed.ts b/prisma/seed.ts index 058c1eb..f64841c 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -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[] = [ { diff --git a/src/app.ts b/src/app.ts index 6899dac..a56c551 100644 --- a/src/app.ts +++ b/src/app.ts @@ -16,7 +16,10 @@ const app: FastifyPluginAsync = async (fastify, opts): Promise => { 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, }) } diff --git a/src/domains/example/handler/example.handler.ts b/src/domains/example/handler/example.handler.ts new file mode 100644 index 0000000..40b03b0 --- /dev/null +++ b/src/domains/example/handler/example.handler.ts @@ -0,0 +1 @@ +export const exampleHandler = () => 'this is an example' diff --git a/test/routes/example.test.ts b/src/domains/example/router/example.router.test.ts similarity index 80% rename from test/routes/example.test.ts rename to src/domains/example/router/example.router.test.ts index 3e502c8..200f5df 100644 --- a/test/routes/example.test.ts +++ b/src/domains/example/router/example.router.test.ts @@ -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({ diff --git a/src/routes/example/index.route.ts b/src/domains/example/router/example.router.ts similarity index 78% rename from src/routes/example/index.route.ts rename to src/domains/example/router/example.router.ts index eb9b76e..d0c3a93 100644 --- a/src/routes/example/index.route.ts +++ b/src/domains/example/router/example.router.ts @@ -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 => { fastify .withTypeProvider() .get( - '/', + '/example', { schema: { response: { 200: z.string() } } }, - async (_request, _reply) => { - return 'this is an example' - }, + exampleHandler, ) } diff --git a/src/domains/users/db/findAllUsers.db.ts b/src/domains/users/db/findAllUsers.db.ts deleted file mode 100644 index 340b7e3..0000000 --- a/src/domains/users/db/findAllUsers.db.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { PrismaClient } from '@prisma/client' - -export const findAllUsers = (prisma: PrismaClient) => prisma.user.findMany() diff --git a/src/domains/users/db/createUser.db.ts b/src/domains/users/db/users.db.ts similarity index 61% rename from src/domains/users/db/createUser.db.ts rename to src/domains/users/db/users.db.ts index e3a0c63..9c88f5d 100644 --- a/src/domains/users/db/createUser.db.ts +++ b/src/domains/users/db/users.db.ts @@ -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({ @@ -8,3 +8,5 @@ export const createUser = (prisma: PrismaClient, user: CreateUserSchema) => name: user.name, }, }) + +export const findAllUsers = (prisma: PrismaClient) => prisma.user.findMany() diff --git a/src/domains/users/handler/users.handler.ts b/src/domains/users/handler/users.handler.ts new file mode 100644 index 0000000..672fe97 --- /dev/null +++ b/src/domains/users/handler/users.handler.ts @@ -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) +} diff --git a/src/routes/users/users.route.ts b/src/domains/users/router/users.router.ts similarity index 53% rename from src/routes/users/users.route.ts rename to src/domains/users/router/users.router.ts index 1c79a95..bb83ac1 100644 --- a/src/routes/users/users.route.ts +++ b/src/domains/users/router/users.router.ts @@ -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() - .get('/', { schema: { response: { 200: findAllUsersSchema } } }, () => { - return findAllUsers(fastify.prisma) - }) + .get( + '/users', + { schema: { response: { 200: findAllUsersSchema } } }, + findAllUsersHandler, + ) fastify.withTypeProvider().post( - '/', + '/users', { schema: { body: createUserSchema, @@ -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, ) } diff --git a/src/domains/users/schemas/user.schema.ts b/src/domains/users/schemas/users.schema.ts similarity index 100% rename from src/domains/users/schemas/user.schema.ts rename to src/domains/users/schemas/users.schema.ts diff --git a/src/libs/__mocks__/prisma.ts b/src/libs/__mocks__/prisma.ts new file mode 100644 index 0000000..037d7bc --- /dev/null +++ b/src/libs/__mocks__/prisma.ts @@ -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() +export default prisma diff --git a/src/libs/prisma.ts b/src/libs/prisma.ts new file mode 100644 index 0000000..cad25fe --- /dev/null +++ b/src/libs/prisma.ts @@ -0,0 +1,4 @@ +import { PrismaClient } from '@prisma/client' + +const prisma = new PrismaClient() +export default prisma diff --git a/src/plugins/prisma.plugin.ts b/src/plugins/prisma.plugin.ts deleted file mode 100644 index 087d0c7..0000000 --- a/src/plugins/prisma.plugin.ts +++ /dev/null @@ -1,21 +0,0 @@ -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(async (fastify, _opts) => { - const prisma = new PrismaClient() - - await prisma.$connect() - - fastify.decorate('prisma', prisma) - - fastify.addHook('onClose', async fastify => { - await fastify.prisma.$disconnect() - }) -}) diff --git a/src/routes/root.route.ts b/src/routes/root.route.ts deleted file mode 100644 index 3ce3b3b..0000000 --- a/src/routes/root.route.ts +++ /dev/null @@ -1,9 +0,0 @@ -import type { FastifyPluginAsync } from 'fastify' - -const root: FastifyPluginAsync = async (fastify, _opts): Promise => { - fastify.get('/', async (_request, _reply) => { - return { root: true } - }) -} - -export default root diff --git a/test/routes/root.test.ts b/test/routes/root.test.ts deleted file mode 100644 index 9147cfc..0000000 --- a/test/routes/root.test.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { it, expect } from 'vitest' - -import fastify from '../helper' - -it('default root route', async () => { - const res = await fastify.inject({ - url: '/', - }) - expect(JSON.parse(res.payload)).toHaveProperty('root', true) -})