minimonolith 0.12.0 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +130 -0
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,3 +1,133 @@
1
1
  # minimonolith
2
2
 
3
3
  [![codecov](https://codecov.io/gh/DeepHackDev/minimonolith-lib/branch/master/graph/badge.svg?token=ORFNKKJRSE)](https://codecov.io/gh/DeepHackDev/minimonolith-lib)
4
+
5
+ `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 services, methods, validation, and models while handling common tasks like database connection and request validation.
6
+
7
+ In addition to its simplicity, `minimonolith` enables seamless inter-service communication within your API. This allows services 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 service from the todoList service using SERVICES.todo.get({ id }). By registering services within the API, you can easily call their methods from other services, which not only promotes a clean architecture but also paves the way for future support of automated end-to-end testing.
8
+
9
+ ## Project Structure
10
+
11
+ A typical project using `minimonolith` will have the following structure:
12
+
13
+ ```go
14
+ .
15
+ ├── package.json
16
+ ├── .gitignore
17
+ ├── .env
18
+ ├── server.js // For local development
19
+ ├── index.js // Root of the code in a deployed AWS Lambda
20
+ └── service1
21
+ ├── index.js // Service1 method handlers are declared here
22
+ ├── model.js // Optional: Sequelize model for Service1 is declared here
23
+ └── method1
24
+ ├── index.js // Service1 Method1 handler is declared here
25
+ └── valid.js // Optional: Method1 handler body validation, if not empty
26
+ ```
27
+
28
+ ## Example Project
29
+
30
+ Here's an example project using `minimonolith`:
31
+
32
+ ```go
33
+ .
34
+ ├── package.json
35
+ ├── .gitignore
36
+ ├── .env
37
+ ├── server.js // For local development
38
+ ├── index.js // Root of the code in a deployed AWS Lambda
39
+ └── todo
40
+ ├── index.js // Todo method handlers are declared here
41
+ ├── model.js // Todo Sequelize model is declared here
42
+ └── get
43
+ ├── index.js // /todo/get/ handler
44
+ └── valid.js // /todo/get/index.js body validation
45
+ ```
46
+
47
+ ### server.js
48
+
49
+ This file is used for local development. It runs a local server using `minimonolith`'s `runLambdaServer` function:
50
+
51
+ ```js
52
+ // server.js
53
+ import { runLambdaServer, loadEnvFile } from 'minimonolith';
54
+ loadEnvFile(); // .env file loaded by default
55
+
56
+ const lambdaRootFile = await import('./index.js');
57
+ runLambdaServer(lambdaRootFile.lambdaHandler, 8080);
58
+ ```
59
+
60
+ ### index.js
61
+
62
+ This file serves as the root of the code in a deployed AWS Lambda:
63
+
64
+ ```js
65
+ // index.js
66
+ 'use strict';
67
+
68
+ import { createAPI } from 'minimonolith';
69
+
70
+ const API = createAPI();
71
+
72
+ await API.registerHealthService(); // Adds health check service to route /
73
+ await API.registerService('todo'); // Registers all /todo routes with respective method handlers
74
+ await API.registerDatabaseService(); // Sets up the database connection
75
+
76
+ export const lambdaHandler = await API.handler(); // Synchronizes Sequelize models with the database
77
+ ```
78
+
79
+ ### todo/index.js
80
+
81
+ Here, we declare the method handlers for the `todo` service:
82
+
83
+ ```js
84
+ // todo/index.js
85
+ export const methods = ['getAll', 'get:id', 'post', 'patch:id', 'delete:id'];
86
+ ```
87
+
88
+ ### todo/model.js
89
+
90
+ In this file, we define a Sequelize model for the `todo` service:
91
+
92
+ ```js
93
+ // todo/model.js
94
+ export const modelSchema = serviceName => (orm, types) => {
95
+ const schema = orm.define(serviceName, {
96
+ name: {
97
+ type: types.STRING,
98
+ allowNull: false
99
+ },
100
+ });
101
+
102
+ schema.associate = MODELS => {}; // e.g. MODELS.todo.belongsTo(MODELS.todoList, {...});
103
+
104
+ return schema;
105
+ };
106
+ ```
107
+
108
+ ### todo/get/index.js
109
+
110
+ This file contains the `get:id` method handler for the `todo` service. It retrieves a todo item by its ID:
111
+
112
+ ```js
113
+ // todo/get/index.js
114
+ export default async ({ body, MODELS }) => {
115
+ const id = parseInt(body.id);
116
+ return await MODELS.todo.findOne({ where: { id } });
117
+ }
118
+ ```
119
+
120
+ ### todo/get/valid.js
121
+
122
+ This file validates the `get:id` method's input, ensuring that the provided `id` is a string and exists in the `todo` model:
123
+
124
+ ```js
125
+ // todo/get/valid.js
126
+ import { z, DB_VALIDATION } from 'minimonolith';
127
+
128
+ export default (MODELS, ROUTE_CODE) => ({
129
+ id: z.string().superRefine(
130
+ DB_VALIDATION.exists(MODELS.todo, 'id', { parseInt: true })
131
+ ),
132
+ })
133
+ ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "minimonolith",
3
3
  "type": "module",
4
- "version": "0.12.0",
4
+ "version": "0.12.2",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -10,7 +10,6 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "lambda-api": "^1.0.1",
13
- "mysql2": "^3.2.0",
14
13
  "sequelize": "^6.30.0",
15
14
  "zod": "^3.21.4"
16
15
  },
@@ -19,6 +18,7 @@
19
18
  "@aws-sdk/s3-request-presigner": "^3.304.0",
20
19
  "dotenv": "^16.0.3",
21
20
  "jest": "^29.5.0",
21
+ "mysql2": "^3.2.0",
22
22
  "sqlite3": "^5.1.6"
23
23
  }
24
24
  }