minimonolith 0.15.1 → 0.15.3
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.
- package/README.md +4 -23
- package/package.json +6 -1
- package/src/api/createAPI.js +2 -0
- package/src/model/loadModels.js +7 -3
- package/src/service/registerService.js +3 -3
package/README.md
CHANGED
|
@@ -6,25 +6,6 @@
|
|
|
6
6
|
|
|
7
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
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
9
|
## Example Project
|
|
29
10
|
|
|
30
11
|
Here's an example project using `minimonolith`:
|
|
@@ -37,11 +18,11 @@ Here's an example project using `minimonolith`:
|
|
|
37
18
|
├── server.js // For local development
|
|
38
19
|
├── index.js // Root of the code in a deployed AWS Lambda
|
|
39
20
|
└── todo
|
|
40
|
-
├── index.js //
|
|
41
|
-
├── model.js //
|
|
21
|
+
├── index.js // Service 'todo' exported method handlers are declared here
|
|
22
|
+
├── model.js // Optional: Sequelize model for service 'todo' is declared here
|
|
42
23
|
└── get
|
|
43
|
-
├── index.js //
|
|
44
|
-
└── valid.js //
|
|
24
|
+
├── index.js // Service 'todo' method 'get' handler
|
|
25
|
+
└── valid.js // Optional: Method 'get' handler validation, if body not empty
|
|
45
26
|
```
|
|
46
27
|
|
|
47
28
|
### server.js
|
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "minimonolith",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.15.
|
|
4
|
+
"version": "0.15.3",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"start:development": "nodemon --inspect=0.0.0.0 ./server.js",
|
|
9
9
|
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest --coverage"
|
|
10
10
|
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"lambda-api",
|
|
13
|
+
"zod",
|
|
14
|
+
"sequelize"
|
|
15
|
+
],
|
|
11
16
|
"dependencies": {
|
|
12
17
|
"lambda-api": "^1.0.1",
|
|
13
18
|
"sequelize": "^6.30.0",
|
package/src/api/createAPI.js
CHANGED
|
@@ -36,6 +36,8 @@ const createAPI = () => {
|
|
|
36
36
|
API.handler = async () => {
|
|
37
37
|
if (API.ORM) API.MODELS = await loadAndSyncModels(API);
|
|
38
38
|
else console.log({ ROUTE_CODE, INFO: 'NO_DATABASE_REGISTERED' });
|
|
39
|
+
|
|
40
|
+
console.log({ ROUTE_CODE, INFO: 'LISTENING' });
|
|
39
41
|
return apiHandler(API);
|
|
40
42
|
}
|
|
41
43
|
|
package/src/model/loadModels.js
CHANGED
|
@@ -10,16 +10,20 @@ const registerModel = async (SERVICE_NAME, SERVICE_URL) => {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
const loadAndSyncModels = async API => {
|
|
13
|
-
console.log({ ROUTE_CODE, LOADING_MODELS: 'LOADING_MODELS' });
|
|
14
13
|
const MODELS = Object.keys(modelSchemas).reduce((loadedModels, serviceName) => {
|
|
14
|
+
console.log({ ROUTE_CODE, LOADING_MODEL: serviceName });
|
|
15
15
|
loadedModels[serviceName] = modelSchemas[serviceName](API.ORM, DataTypes);
|
|
16
16
|
return loadedModels;
|
|
17
17
|
}, {});
|
|
18
18
|
|
|
19
|
-
Object.entries(MODELS).forEach(([serviceName, model]) => {
|
|
19
|
+
Object.entries(MODELS).forEach(([serviceName, model]) => {
|
|
20
|
+
console.log({ ROUTE_CODE, ASSOCIATING_MODEL: serviceName });
|
|
21
|
+
model.associate(MODELS);
|
|
22
|
+
});
|
|
20
23
|
|
|
21
|
-
console.log({ ROUTE_CODE, SYNCING_ORM: '
|
|
24
|
+
console.log({ ROUTE_CODE, SYNCING_ORM: 'WAIT_SYNCING_ORM' });
|
|
22
25
|
await API.ORM.sync({ alter: process.env.MM_API_LOCAL_ENV ? true : false });
|
|
26
|
+
console.log({ ROUTE_CODE, SYNCING_ORM: 'DONE_SYNCING_ORM' });
|
|
23
27
|
|
|
24
28
|
return MODELS;
|
|
25
29
|
};
|
|
@@ -43,10 +43,10 @@ const registerService = async (API, SERVICE_NAME, SRC_FOLDER, MODULE_FOLDER) =>
|
|
|
43
43
|
|
|
44
44
|
const methodType = getMethodType(METHOD_NAME);
|
|
45
45
|
API[methodType](METHOD_ROUTE, async (event, res) => {
|
|
46
|
-
const { body, params,
|
|
46
|
+
const { body, params, query } = event;
|
|
47
47
|
const claims = event.requestContext.authorizer?.jwt?.claims;
|
|
48
|
-
const parsedBody = body || Object.keys(params).length > 0 ||
|
|
49
|
-
{ ...body, ...params, ...
|
|
48
|
+
const parsedBody = body || Object.keys(params).length > 0 || query ?
|
|
49
|
+
{ ...body, ...params, ...query } : undefined;
|
|
50
50
|
await exposedMethodHandler(parsedBody, res, claims, API,
|
|
51
51
|
SERVICE_METHODS[METHOD_CODE], METHOD_ROUTE_CODE);
|
|
52
52
|
});
|