Module Olusturma Sirasi
- Drizzle schema (src/db/schema.ts'e ekle)
- Migration (npx drizzle-kit generate)
- Zod schema (src/modules/[name]/[name].schema.ts)
- Service (src/modules/[name]/[name].service.ts)
- Controller (src/modules/[name]/[name].controller.ts)
- Routes (src/modules/[name]/[name].routes.ts)
- Test (src/modules/[name]/tests/)
Route + Controller
// routes.ts
export async function postRoutes(fastify: FastifyInstance) {
fastify.post('/', { schema: createPostSchema }, createPostHandler);
fastify.get('/:id', { schema: getPostSchema }, getPostHandler);
}
// controller.ts
export async function createPostHandler(request: FastifyRequest<{Body: CreatePostInput}>, reply: FastifyReply) {
const post = await postService.create(request.body);
return reply.status(201).send({ data: post });
}
Service
export class PostService {
async create(input: CreatePostInput): Promise<Post> {
const [post] = await db.insert(posts).values(input).returning();
return post;
}
}
Zod Schema
export const createPostSchema = {
body: z.object({ title: z.string().min(1).max(255), body: z.string() }),
response: { 201: z.object({ data: postSchema }) },
};
export type CreatePostInput = z.infer<typeof createPostSchema.body>;