minimonolith 0.3.0 → 0.5.0
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/index.js +2 -5
- package/package.json +1 -1
- package/src/apiHandler/createAPI.js +12 -0
- package/src/databaseHandler/modelsHandler.js +7 -3
- package/src/serviceHandler/serviceHandler.js +3 -3
- package/todoList/delete/valid.js +8 -0
- package/todoList/get/valid.js +6 -0
- package/todoList/index.js +2 -0
- package/todoList/model.js +17 -0
- package/todoList/post/valid.js +8 -0
package/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import { healthHandler } from './src/healthHandler/index.js';
|
|
5
|
-
import { createAPI, apiHandler } from './src/apiHandler/index.js';
|
|
3
|
+
import { createAPI } from './src/apiHandler/index.js';
|
|
6
4
|
import { runLambdaServer, loadEnvVariables } from './src/lambdaServer/index.js';
|
|
7
5
|
|
|
8
|
-
export { runLambdaServer, loadEnvVariables,
|
|
9
|
-
createAPI, apiHandler, serviceHandler, healthHandler, z };
|
|
6
|
+
export { runLambdaServer, loadEnvVariables, createAPI, z };
|
package/package.json
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import lambdaAPICreateAPI from 'lambda-api';
|
|
2
2
|
|
|
3
|
+
import { healthHandler } from '../healthHandler/index.js';
|
|
4
|
+
import { serviceHandler } from '../serviceHandler/index.js';
|
|
5
|
+
import { apiHandler } from './apiHandler.js';
|
|
6
|
+
|
|
3
7
|
const addCORS = API => {
|
|
4
8
|
API.use((req, res, next) => { res.cors(); next(); });
|
|
5
9
|
API.use((err, req, res, next) => { res.cors(); next(); });
|
|
@@ -18,6 +22,14 @@ const createAPI = () => {
|
|
|
18
22
|
|
|
19
23
|
addCORS(API);
|
|
20
24
|
|
|
25
|
+
API.registerService = (SERVICE_NAME, SRC_FOLDER='', MODULE_FOLDER='node_modules/') =>
|
|
26
|
+
serviceHandler(API, SERVICE_NAME, SRC_FOLDER, MODULE_FOLDER);
|
|
27
|
+
|
|
28
|
+
API.registerHealthService = () =>
|
|
29
|
+
healthHandler(API);
|
|
30
|
+
|
|
31
|
+
API.handler = () => apiHandler(API);
|
|
32
|
+
|
|
21
33
|
return API;
|
|
22
34
|
}
|
|
23
35
|
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
const modelSchemas = {};
|
|
2
2
|
|
|
3
|
-
export const registerModel = (
|
|
4
|
-
|
|
3
|
+
export const registerModel = async (SERVICE_NAME, SERVICE_URL) => {
|
|
4
|
+
const SERVICE_MODEL_MODULE = await import(`${SERVICE_URL}model.js`);
|
|
5
|
+
modelSchemas[SERVICE_NAME] = SERVICE_MODEL_MODULE.modelSchema(SERVICE_NAME);
|
|
5
6
|
}
|
|
6
7
|
|
|
7
8
|
export const loadModels = (orm, types) => {
|
|
8
|
-
|
|
9
|
+
const MODELS = Object.keys(modelSchemas).reduce((loadedModels, serviceName) => {
|
|
9
10
|
loadedModels[serviceName] = modelSchemas[serviceName](orm, types);
|
|
10
11
|
return loadedModels;
|
|
11
12
|
}, {});
|
|
13
|
+
|
|
14
|
+
Object.entries(MODELS).forEach(([serviceName, model]) => { model.associate(MODELS); });
|
|
15
|
+
return MODELS;
|
|
12
16
|
}
|
|
@@ -7,15 +7,15 @@ import { getMethodType } from './getMethodType.js';
|
|
|
7
7
|
import { getMethodRouteCode } from './getMethodRouteCode.js';
|
|
8
8
|
import { registerMethods } from './registerMethods.js';
|
|
9
9
|
|
|
10
|
-
const serviceHandler = async (API, SERVICE_NAME, SRC_FOLDER
|
|
10
|
+
const serviceHandler = async (API, SERVICE_NAME, SRC_FOLDER, MODULE_FOLDER) => {
|
|
11
11
|
try {
|
|
12
12
|
const PROJECT_ROOT_PATH = getProjectRoot(import.meta.url, MODULE_FOLDER)+'/';
|
|
13
13
|
const PROJECT_RELATIVE_SERVICE_PATH = './' + SRC_FOLDER + `${SERVICE_NAME}/`;
|
|
14
|
-
//const SERVICE_URL = new URL(RELATIVE_SERVICE_PATH, url.pathToFileURL(process.cwd()+'/'));
|
|
15
14
|
const SERVICE_URL = new URL(PROJECT_RELATIVE_SERVICE_PATH, PROJECT_ROOT_PATH);
|
|
16
15
|
const SERVICE_MODULE = await import(`${SERVICE_URL}index.js`);
|
|
17
16
|
|
|
18
|
-
if (SERVICE_MODULE.modelSchema) registerModel(SERVICE_NAME, SERVICE_MODULE.modelSchema);
|
|
17
|
+
//if (SERVICE_MODULE.modelSchema) registerModel(SERVICE_NAME, SERVICE_MODULE.modelSchema);
|
|
18
|
+
if (SERVICE_MODULE.model) registerModel(SERVICE_NAME, SERVICE_URL);
|
|
19
19
|
const SERVICE_METHODS = await registerMethods(SERVICE_MODULE.methods)(SERVICE_URL);
|
|
20
20
|
|
|
21
21
|
Object.keys(SERVICE_METHODS).forEach(METHOD_NAME => {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const modelSchema = serviceName => (sequelize, type) => {
|
|
2
|
+
const schema = sequelize.define(serviceName, {
|
|
3
|
+
name: {
|
|
4
|
+
type: type.STRING,
|
|
5
|
+
allowNull: false
|
|
6
|
+
},
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
schema.associate = MODELS => {
|
|
10
|
+
schema.hasMany(MODELS.todo, {
|
|
11
|
+
foreignKey: 'todoListId',
|
|
12
|
+
as: 'todoElements',
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
return schema;
|
|
17
|
+
};
|