minimonolith 0.3.0 → 0.4.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 CHANGED
@@ -1,9 +1,6 @@
1
1
  import { z } from 'zod';
2
2
 
3
- import { serviceHandler } from './src/serviceHandler/index.js';
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,7 +1,7 @@
1
1
  {
2
2
  "name": "minimonolith",
3
3
  "type": "module",
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -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
 
@@ -5,8 +5,11 @@ export const registerModel = (serviceName, modelSchema) => {
5
5
  }
6
6
 
7
7
  export const loadModels = (orm, types) => {
8
- return Object.keys(modelSchemas).reduce((loadedModels, serviceName) => {
8
+ const MODELS = Object.keys(modelSchemas).reduce((loadedModels, serviceName) => {
9
9
  loadedModels[serviceName] = modelSchemas[serviceName](orm, types);
10
10
  return loadedModels;
11
11
  }, {});
12
+
13
+ Object.entries(MODELS).forEach(([serviceName, model]) => { model.associate(MODELS); });
14
+ return MODELS;
12
15
  }
@@ -7,7 +7,7 @@ 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='', MODULE_FOLDER='node_modules/') => {
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}/`;
@@ -0,0 +1,8 @@
1
+ import { z } from 'zod';
2
+
3
+ const VALIDATOR = (MODELS, ROUTE_CODE) =>
4
+ z.object({
5
+ id: z.number(),
6
+ }).strict();
7
+
8
+ export { VALIDATOR };
@@ -0,0 +1,6 @@
1
+ import { z } from 'zod';
2
+
3
+ const VALIDATOR = (MODELS, ROUTE_CODE) =>
4
+ z.undefined();
5
+
6
+ export { VALIDATOR };
@@ -0,0 +1,5 @@
1
+ import { modelSchema } from './model.js';
2
+
3
+ const methods = ['get', 'post', 'delete'];
4
+
5
+ export { modelSchema, methods };
@@ -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
+ };
@@ -0,0 +1,8 @@
1
+ import { z } from 'zod';
2
+
3
+ const VALIDATOR = (MODELS, ROUTE_CODE) =>
4
+ z.object({
5
+ name: z.string(),
6
+ }).strict();
7
+
8
+ export { VALIDATOR };