Install dependencies with
bun installRun dev server
bun run devRun dev server in production mode
bun run dev:prodRun via docker (also runs in production mode)
docker build -t bun-elysia-template-v2 .
docker run -p 8080:8080 bun-elysia-template-v2Testing
bun test- Postgrest and TypeORM
- Authentication and Authorization with JWT
- Swagger API documentation
- Unit test with bun:test
- Dockerfile for production
- Entities: Create new entities in
src/entitiesfolder then import them insrc/data-source.ts - Routes: define as an elysia plugin in
src/plugins.tsneed to chaining methods so can refer type :app.post().get() - Services: Create new services in
src/servicesfolder then export it as a elysia decorate so it can be dependency injected into plugins
//UserService.ts
class UserService{}
export default new Elysia()
.decorate('userService', new UserService())//plugin/auth.ts
const authPlugin = new Elysia()
.group("/auth", (group) =>
group
.use(userService))
- Global try catch so you don't need to wrap every function with try catch: look at
src/middlewares/errorMiddleware.ts - Global response so don't need to wrap anything when return: look at
src/middlewares/responseMiddleware.ts - For routes that need authentication, use the
derive()api of elysia and passed in theisAuthenticatedmiddleware
app
.derive(isAuthenticated())
.get("/me", async ({user}) => {})- All endpoints defined under the
derive(isAuthenticated())will require a valid JWT token to access - All endpoints defined below the
derive(isAdmin())will not require jwt - After
derive()you can use user in request context to get the logged in user - For testing
- Create test as
*.test.tsintestfolder - Run test with
bun test
- Create test as
Happy coding! Created by Lilhuy - the CodingCat 🐱 - 05/22/2024