Starting August 2nd 2023 this repository will be archived.
The easiest way to create your own API.
- Autoloading for routes & plugins
- JWT Authentication with both access & refresh tokens
- Prisma for easy db management / creation
Unneeded plugins can be removed by deleting the corresponding file from the plugins folder and removing the dependency in the package.json.
- accessToken payload accessible via
server.jwtPayload
1. clone the github repo
git clone https://github.com/sek-consulting/fastify-api-skeleton.git your/directory/
2. change the provider of the datasource in the schema.prisma
if needed and set the DATABASE_URL
in your .env file
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
3. use db.push
to synchronize your schema with the database
Let's say you want to create some kind of content management system with posts. Here is how you would do that.
// data model
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
@@map("posts")
}
// add reference to user
model User {
id Int @id @default(autoincrement())
name String
email String
password String
refreshTokens RefreshToken[]
posts Post[] // <-- here
@@map("users")
}
For more information click here.
create the needed file in the services folder (services/post.ts):
import { prisma } from "../prisma"
const getPostById = async (id: number) => {
return await prisma.post.findUnique({ where: { id: id } })
}
export { getPostById }
create the needed file(s) in your routes folder (routes/posts/index.ts):
import { FastifyPluginAsync, FastifyRequest } from "fastify"
import { getPostById } from "../../services/post"
const posts: FastifyPluginAsync = async (server) => {
server.addHook("onRequest", server.authenticate) // this makes all routes below only visible if you're logged-in
server.get("/:id", async (request: FastifyRequest<{ Params: { id: number } }>, reply) => {
const id = request.params.id
const post = await getPostById(id)
if (post) {
return reply.status(200).send({ post })
}
return reply.notFound()
})
}
export default posts
Congratulations! You're done.
- more to come
Any new issues please report in GitHub site