minimonolith
v0.27.0
Published
[](https://codecov.io/gh/DeepHackDev/minimonolith-lib)
Readme
minimonolith
minimonolith is a lightweight library designed to help you build serverless APIs using AWS Lambda, with a focus on simplicity and ease of use. The library provides a straightforward structure to organize your API's modules, methods, validation, and models while handling common tasks like database connection and request validation.
In addition to its simplicity, minimonolith enables seamless inter-module communication within your API. This allows modules to call one another's functionality without directly importing them, fostering a modular design. For example, you can call the get method of the todo module from the todoList module using MODULES.todo.get({ id }). By registering modules within the API, you can easily call their methods from other modules, which not only promotes a clean architecture but also paves the way for future support of automated end-to-end testing.
Example Project
Here's an example project using minimonolith:
.
├── package.json
├── .gitignore
├── .env
├── server.js // For local development
├── index.js // Root of the code in a AWS Lambda
└── todo
├── index.js // Module 'todo' exported methods
├── model.js // Optional: Sequelize model for module
└── get
└─── handler.js // Module 'todo' method 'get' handlerserver.js
This file is used for local development. It runs a local server using minimonolith's getServer function:
// server.js
import { getServerFactory } from 'minimonolith';
import dotenv from 'dotenv';
dotenv.config({ path: './.env' })
const { lambdaHandler } = await import('./index.js');
const serverFactory = await getServerFactory();
const server = serverFactory(lambdaHandler);
server.listen(8080);index.js
This file serves as the root of the code in a deployed AWS Lambda:
// index.js
import { getNewAPI } from 'minimonolith';
import todo from './todo/index.js';
const API = await getNewAPI({ ERROR_LEVEL: 0 });
await API.postModule(todo);
await API.postDatabaseModule({
DB_DIALECT: process.env.DB_DIALECT,
DB_HOST: process.env.DB_HOST,
DB_PORT: process.env.DB_PORT ?
parseInt(process.env.DB_PORT, 10) : undefined,
DB_DB: process.env.DB_DB,
DB_USER: process.env.DB_USER,
DB_PASS: process.env.DB_PASS,
DB_STORAGE: process.env.DB_STORAGE,
});
export const lambdaHandler = await API.getSyncedHandler();ERROR_LEVEL can be:
- 0: DEBUG logs
- 1: INFO logs and below (default)
- 2: ERROR logs and below
todo/index.js
Here, we declare the method handlers for the todo module:
// todo/index.js
import get from './get/handler.js';
import getOne from './getOne/handler.js';
import post from './post/handler.js';
import patch from './patch/handler.js';
import deleteTodo from './delete/handler.js';
import model from './model.js';
export default {
name: 'todo',
endpoints: {
'get': get,
'post': post,
'patch:id': patch,·
'delete:id': deleteTodo,
},
model,
};Modules need to export name, endpoints and model attributes.
todo/model.js
In this file, we define a Sequelize model for the todo module:
// todo/model.js
export default moduleName => (orm, types) => {
const schema = orm.define(moduleName, {
name: {
type: types.STRING,
allowNull: false
},
});
schema.associate = MODELS => {
/*
schema.belongsTo(MODELS.todoList, {
foreignKey: 'todoListId',
as: 'todoList'
});
*/
};
return schema;
};For now every module can have at most one model, which will have the same name as the module.
todo/get/index.js
This file contains the get:id method handler for the todo module. It retrieves a todo item by its ID:
// todo/get/index.js
export default async ({ body, MODELS }) => {
const id = body.id;
const todo = await MODELS.todo.findOne({ where: { id } });
const todoJSON = todo.toJSON();
return { body: todoJSON };
}Endpoint receive MODELS, MODULES, body and claims, and
can return body, cookies, headers and a custom status.
Database Authentication
To set up authentication for the database you need to pass necessary variables to postDatabaseModule as at index.js above. Assuming using same env variable names as at index.js above, the .env file should be like the following.
For MySQL:
DEV_ENV=TRUE
PROD_ENV=FALSE
DB_DIALECT=mysql
DB_HOST=<your_database_endpoint>
DB_PORT=<your_database_port>
DB_DB=<your_database_name>
DB_USER=<your_database_username>
DB_PASS=<your_database_password>For SQLite in memory:
DB_DIALECT=sqlite
DB_DB=<your_database_name>
DB_STORAGE=:memory: # Need to also pass to API.postDatabaseModule()Response Codes
Success
- POST -> 201
- DELETE -> 204
- Everything else -> 200
- Custom -> status value in handler.js return statement
Runtime Error
- ANY -> 500
