Skip to main content

Basica

The Foundational Library of Modern Applications

Core Features

Application lifecycle management

Handle application startup and shutdown automatically

Configuration validation

Add a Typebox schema and validate your configuration before the app starts

Plugin system

Seamless integration with your favorite library

Healthchecks

Check on a glance if your service is up and running

Structured logging

Fast, structured logging provided by Pino

Graceful shutdown

Stop serving clients and perform cleanups before exiting

Dependency injection

Declaratively define dependencies: no magic, no boilerplate

Highly configurable

Sensible and excessively customizable defaults

Observability ready

Powered by OpenTelemetry

Why?

Building software, especially microservices, often requires to choose between a framework or a DIY approach, but both approaches have their flaws:
  • Frameworks lift work from the developer, at the cost of flexibility, hidden magic and performance.
  • Mixing and matching libraries, while as flexible as you can make it, is very hard to maintain. Especially if you have to manage multiple services.
Basica comes to the rescue! Basica is a foundational library to boostrap performant applications, which provides opinonated, but configurable defaults at the cost of minimal overhead. This results in a cohesive experience across projects, focusing development effort on what really matters, products.

Quickstart

npm install @basica/core @basica/config @basica/fastify
index.ts
import { IocContainer } from "@basica/core/ioc";
import { loggerFactory } from "@basica/core/logger";
import { AppBuilder } from "@basica/core";
import { configure, envProvider } from "@basica/config";

import { lifecyclePlugin } from "@basica/fastify";

import { Type } from "@sinclair/typebox";

// Validate configuration
const config = configure(envProvider(), Type.Object({
logger: loggerConfigSchema
}));

// Dependency injection
const container = new IocContainer()
.addSingleton("logger", () => loggerFactory(config.logger))
.addSingleton("svc", (s) => ({
hello: () => {
s.logger.info("svc called!");
return "hello world";
},
healthcheck: () => ({ status: "healthy" }),
}));

const app = new AppBuilder(container)
// Lifecycle management
.configureLifecycle((b, c) => b
// Healthchecks
.addHealthcheck("svc", (c) => c.svc)
// Plugins
.with(lifecyclePlugin, (b) => b
.addFastifyEntrypoint("http", (f) => f
.mapHealthchecks({ path: "/health" })
.configureApp((app) => {
app
.useOpenapi()
.fastify.get("/", () => c.svc.hello());
}
)
)
)
).build();

app.run();

Acknowledgments

Special thanks to the following open source projects, from which Basica takes inspiration: