The landscape of backend development has changed dramatically, with modern frameworks focusing on speed and simplicity. Elysia is one such framework for Bun.js, offering a clean and efficient way to build APIs. However, as projects grow, maintaining structure, modularity, and scalability becomes challenging. This is where Better-Elysia steps in.

Let me introduce you to Better-Elysia, a library I created to make Elysia even more powerful and organized. It retains the simplicity of Elysia while adding advanced features that make building APIs more intuitive and scalable for larger applications.

Why Better-Elysia?

Elysia is great for small, quick projects. But when your application grows in complexity, it requires more structure and tools for maintainability. That’s where Better-Elysia shines. It provides:

  • Modularity: Break your app into manageable pieces with modules.
  • Powerful Decorators: Simplify route definitions and dependency injections with intuitive syntax.
  • Automated Documentation: Effortlessly generate API documentation.
  • Enhanced Scalability: Build large-scale, production-ready applications.

Think of it as giving Elysia superpowers taking an already streamlined framework and making it an ideal choice for larger applications.

 Key Features of Better-Elysia

1. Modules for Better Organization

Better-Elysia enables you to structure your application using modules. Modules allow you to encapsulate related controllers, making your application easier to scale and maintain.

Here’s an example of creating an AppModule:

@Module({ 
    controllers: [UserController], 
}) 
export class AppModule {}

This approach encourages modular design, allowing you to break your application into cohesive units.

2. Controllers for Clean Routing

Routing in Better-Elysia is simple and elegant. With decorators like @Controller and @Get, you can define routes that are both readable and maintainable. No more cluttered route definitions everything is neatly organized within controllers.

Example:

@Controller(’/api/users’) 
export class UserController { 
    @Get() 
    getAllUsers() { 
    return this.userService.findAll(); 
} 
    @Post() 
    createUser(@Body() userData) { 
    return this.userService.create(userData); 
  } 
}

With this approach, routing logic stays clean and focused, making it easier to debug and extend.

3. Simplified Dependency Management

Better-Elysia introduces a robust and intuitive system for managing services and dependencies. With the @Service decorator, you can create reusable services that are injected as singletons into your controllers. This eliminates the need for manual dependency management and promotes modularity.

Example:

@Service() 
      export class UserService { 
        findAll() { 
         return this.userRepository.find(); 
  } 

create(userData) 
   { return this.userRepository.save(userData); 
 } 
}

When you annotate a class with @Service, it becomes available to any controller that requires it. Better-Elysia automatically handles the dependency injection, making your codebase easier to manage and test.

Getting Started with Better-Elysia

Here’s a step-by-step guide to setting up your project with Better-Elysia.

1. Installation

Start by creating a new Bun.js application. Run the following command in an empty project directory:

```bun init```

Then, install the necessary dependencies, including Better-Elysia:

```bun add better-elysia```

2. Setting Up Your Application

Create your main application file, typically index.ts, and initialize Better-Elysia with the ElysiaFactory.

import { ElysiaFactory, Module, Controller, Get } from ’better-elysia’;
@Module({
  controllers: [UserController]
})
export class AppModule {}

@Controller(’/api/users’)
class UserController {
  @Get()
  getAllUsers() {
    return { message: ’Welcome to Better-Elysia!’ };
  }
}

async function bootstrap() {
  const app = await ElysiaFactory.create(AppModule, {
    cors: true,
    swagger: true,
  });

  app.listen(3000);
  console.log(’Application is running on http://localhost:3000’);
}
bootstrap();

3. Adding Swagger for API Documentation

Swagger integration is straightforward with Better-Elysia. By enabling it in the ElysiaFactory options, you can automatically generate API documentation for your endpoints.

const app = await ElysiaFactory.create(AppModule, {

  swagger: {
    title: ’My Better-Elysia API’,
    version: ’1.0.0’,
  },
});

Visit `http://localhost:3000/swagger` to explore your API documentation.

4. Using Middleware and CORS

Better-Elysia supports middleware, such as CORS, out of the box. You can enable it globally during app creation:

const app = await ElysiaFactory.create(AppModule, {

  cors: {
    origin: ’*’,
    methods: [’GET’, ’POST’, ’PUT’, ’DELETE’],
  },
});

Conclusion

Better-Elysia enhances the already powerful Elysia framework by adding structure, scalability, and advanced features like decorators and automated documentation. Whether you’re building a small API or a large-scale application, Better-Elysia provides the tools to keep your code clean, organized, and maintainable.

If you’re working on a Bun.js project, give Better-Elysia a try! You’ll find that it makes API development faster, easier, and far more enjoyable.

Our Trusted
Partner.

Unlock Valuable Cloud and Technology Credits

Imagine reducing your operational costs by up to $100,000 annually without compromising on the technology you rely on. Through our partnerships with leading cloud and technology providers like AWS (Amazon Web Services), Google Cloud Platform (GCP), Microsoft Azure, and Nvidia Inception, we can help you secure up to $25,000 in credits over two years (subject to approval).

These credits can cover essential server fees and offer additional perks, such as:

  • Google Workspace accounts
  • Microsoft accounts
  • Stripe processing fee waivers up to $25,000
  • And many other valuable benefits

Why Choose Our Partnership?

By leveraging these credits, you can significantly optimize your operational expenses. Whether you're a startup or a growing business, the savings from these partnerships ranging from $5,000 to $100,000 annually can make a huge difference in scaling your business efficiently.

The approval process requires company registration and meeting specific requirements, but we provide full support to guide you through every step. Start saving on your cloud infrastructure today and unlock the full potential of your business.

exclusive-partnersexclusive-partners

Let's TALK

Let's TALK and bring your ideas to life! Our experienced team is dedicated to helping your business grow and thrive. Reach out today for personalized support or request your free quote to kickstart your journey to success.

DIGITAL PRODUCTUI/UX DESIGNDIGITAL STUDIOBRANDING DESIGNUI/UX DESIGNEMAIL MARKETINGBRANDING DESIGNUI/UX DESIGNEMAIL MARKETING
DIGITAL PRODUCTUI/UX DESIGNDIGITAL STUDIOBRANDING DESIGNUI/UX DESIGNEMAIL MARKETINGBRANDING DESIGNUI/UX DESIGNEMAIL MARKETING