minimonolith 0.26.7 → 0.26.8

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 CHANGED
@@ -55,9 +55,9 @@ import { getNewAPI } from 'minimonolith';
55
55
 
56
56
  import todo from './todo/index.js';
57
57
 
58
- const API = await getNewAPI();
58
+ const API = await getNewAPI({ ERROR_LEVEL: 0 });
59
59
 
60
- await API.postModule(todo.name, todo.module, todo.model);
60
+ await API.postModule(todo);
61
61
  await API.postDatabaseModule({
62
62
  DB_DIALECT: process.env.DB_DIALECT,
63
63
  DB_HOST: process.env.DB_HOST,
@@ -72,6 +72,11 @@ await API.postDatabaseModule({
72
72
  export const lambdaHandler = await API.getSyncedHandler();
73
73
  ```
74
74
 
75
+ `ERROR_LEVEL` can be:
76
+ - 0: DEBUG logs
77
+ - 1: INFO logs and below (default)
78
+ - 2: ERROR logs and below
79
+
75
80
  ### todo/index.js
76
81
 
77
82
  Here, we declare the method handlers for the `todo` module:
@@ -88,7 +93,7 @@ import model from './model.js';
88
93
 
89
94
  export default {
90
95
  name: 'todo',
91
- methods: {
96
+ endpoints: {
92
97
  'get': get,
93
98
  'post': post,
94
99
  'patch:id': patch,·
@@ -98,6 +103,8 @@ export default {
98
103
  };
99
104
  ```
100
105
 
106
+ Modules need to export `name`, `endpoints` and `model` attributes.
107
+
101
108
  ### todo/model.js
102
109
 
103
110
  In this file, we define a Sequelize model for the `todo` module:
@@ -126,6 +133,9 @@ export default moduleName => (orm, types) => {
126
133
  };
127
134
  ```
128
135
 
136
+ For now every module can have at most one model, which will have the
137
+ same name as the module.
138
+
129
139
  ### todo/get/index.js
130
140
 
131
141
  This file contains the `get:id` method handler for the `todo` module. It retrieves a todo item by its ID:
@@ -143,6 +153,9 @@ export default async ({ body, MODELS }) => {
143
153
  }
144
154
  ```
145
155
 
156
+ Endpoint receive `MODELS`, `MODULES`, `body` and `claims`, and
157
+ can return `body`, `cookies`, `headers` and a custom `status`.
158
+
146
159
  ## Database Authentication
147
160
 
148
161
  To set up authentication for the database you need to pass necessary variables to postDatabaseModule as at index.js above.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "minimonolith",
3
3
  "type": "module",
4
- "version": "0.26.7",
4
+ "version": "0.26.8",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -1,11 +1,17 @@
1
1
  import getAPI from '../get/index.js';
2
+ import LOG_MODULE from '../../log/index.js';
3
+ const GREEN = '\x1b[32m', CYAN = '\x1b[36m',
4
+ YELLOW = '\x1b[33m', END = '\x1b[0m';
2
5
 
3
- export default ({ ERROR_LEVEL=0 } = {}) => {
6
+ export default ({ ERROR_LEVEL = 1 } = {}) => {
4
7
  getAPI().MODULES = {};
5
8
  getAPI().SCHEMAS = {};
6
9
  getAPI().MODELS = {};
7
10
  getAPI().ORM = undefined;
8
11
  getAPI().ERROR_LEVEL = ERROR_LEVEL;
9
12
  getAPI().postCORSMiddleware();
13
+ LOG_MODULE.postINFO(`API components:`,
14
+ `(${GREEN}modules${END}, ${CYAN}models${END},`,
15
+ `${YELLOW}endpoints${END})`);
10
16
  return getAPI();
11
17
  };
@@ -3,19 +3,19 @@ import LOG_MODULE from '../../log/index.js';
3
3
 
4
4
  import getAPI from '../get/index.js';
5
5
 
6
- const META_MODULE='API', META_METHOD='GET_SYNCED_HANDLER', META_ROUTE=META_MODULE+'_'+META_METHOD;
6
+ const META_MODULE='API', META_ENDPOINT='GET_SYNCED_HANDLER',
7
+ META_ROUTE=META_MODULE+'_'+META_ENDPOINT;
7
8
 
8
9
  export default async () => {
9
10
  getAPI().postErrorMiddleware();
10
11
  if (getAPI().ORM) await MODEL_MODULE.getSynced();
11
12
 
12
- LOG_MODULE.post({ META_ROUTE, INFO: 'LISTENING' });
13
+ LOG_MODULE.postINFO(`API listening...`);
13
14
 
14
15
  return async (event, context) => {
15
- LOG_MODULE.post({
16
- ROUTE_CODE: 'LAMBDA_EVENT',
17
- PATH: event.requestContext?.http?.path,
18
- METHOD: event.requestContext?.http?.method,
16
+ LOG_MODULE.postINFO('ENDPOINT request:', {
17
+ path: event.requestContext?.http?.path,
18
+ method: event.requestContext?.http?.method,
19
19
  });
20
20
 
21
21
  return await getAPI().run(event, context);
@@ -2,28 +2,31 @@ import fs from 'fs';
2
2
 
3
3
  import MODEL_MODULE from '../../model/index.js';
4
4
  import LOG_MODULE from '../../log/index.js';
5
- import METHOD_MODULE from '../../method/index.js';
5
+ import ENDPOINT_MODULE from '../../endpoint/index.js';
6
6
  import ERROR_MODULE from '../../error/index.js';
7
7
 
8
8
  import getAPI from '../get/index.js';
9
9
 
10
- const META_MODULE_NAME = 'API', META_METHOD_NAME = 'POST_MODULE';
11
- const GREEN = '\x1b[32m', END = '\x1b[0m'
10
+ const META_MODULE_NAME = 'API', META_ENDPOINT_NAME = 'POST_MODULE';
11
+ const GREEN = '\x1b[32m', CYAN = '\x1b[36m',
12
+ YELLOW = '\x1b[33m', END = '\x1b[0m';
12
13
 
13
- export default async ({ name, methods, model }) => {
14
+ export default async ({ name, endpoints, model }) => {
14
15
 
15
- LOG_MODULE.post(`${META_MODULE_NAME} ${META_METHOD_NAME} ${GREEN}${name}${END}`);
16
+ LOG_MODULE.postINFO(` ${GREEN}${name}${END}`);
16
17
 
17
18
  getAPI().MODULES[name] = {};
18
19
  try {
19
20
 
20
21
  await MODEL_MODULE.post(name, model);
21
22
 
22
- for (const METHOD_KEY in methods) {
23
- await METHOD_MODULE.post(name, METHOD_KEY, methods[METHOD_KEY]);
23
+ for (const ENDPOINT_KEY in endpoints) {
24
+ await ENDPOINT_MODULE.post(name, ENDPOINT_KEY,
25
+ endpoints[ENDPOINT_KEY]);
24
26
  }
25
27
 
26
- } catch (META_METHOD_ERROR) {
27
- ERROR_MODULE.postCompiletime({ META_MODULE_NAME, META_METHOD_NAME, META_METHOD_ERROR });
28
+ } catch (META_ENDPOINT_ERROR) {
29
+ ERROR_MODULE.postCompiletime({
30
+ META_MODULE_NAME, META_ENDPOINT_NAME, META_ENDPOINT_ERROR });
28
31
  }
29
32
  };
@@ -7,15 +7,21 @@ import ERROR_MODULE from '../../error/index.js';
7
7
 
8
8
  import postConnection from '../postConnection/index.js'
9
9
 
10
- const META_MODULE='DATABASE', META_METHOD='POST', META_ROUTE=META_MODULE+'_'+META_METHOD;
10
+ const META_MODULE='DATABASE', META_METHOD='POST',
11
+ META_ROUTE=META_MODULE+'_'+META_METHOD;
11
12
 
12
- export default async ({ DB_DIALECT='sqlite', DB_HOST=undefined, DB_PORT=undefined, DB_DB=undefined,
13
- DB_USER=undefined, DB_PASS=undefined, DB_STORAGE=':memory:'} = {}) => {
13
+ export default async ({ DB_DIALECT='sqlite', DB_HOST=undefined,
14
+ DB_PORT=undefined, DB_DB=undefined,
15
+ DB_USER=undefined, DB_PASS=undefined, DB_STORAGE=':memory:'} = {}) =>
16
+ {
14
17
 
15
18
  try {
16
- if (DB_DIALECT!=='sqlite' && (!DB_HOST || !DB_PORT || !DB_DB || !DB_USER || !DB_PASS)) {
19
+ if (DB_DIALECT!=='sqlite' && (!DB_HOST || !DB_PORT ||
20
+ !DB_DB || !DB_USER || !DB_PASS)) {
21
+
17
22
  throw new Error(
18
- 'One of these DB parameters not given: [DB_HOST, DB_PORT, DB_DB, DB_USER, DB_PASS]');
23
+ 'One of these DB parameters not given: ' +
24
+ '[DB_HOST, DB_PORT, DB_DB, DB_USER, DB_PASS]');
19
25
  }
20
26
 
21
27
  const SEQUELIZE_OPTIONS = { dialect: DB_DIALECT, host: DB_HOST,
@@ -23,13 +29,15 @@ export default async ({ DB_DIALECT='sqlite', DB_HOST=undefined, DB_PORT=undefine
23
29
 
24
30
  const DB_VARS = Object.fromEntries(
25
31
  Object.entries({
26
- DB_DIALECT, DB_HOST, DB_PORT, DB_DB, DB_USER, DB_PASS, DB_STORAGE })
32
+ DB_DIALECT, DB_HOST, DB_PORT, DB_DB,
33
+ DB_USER, DB_PASS, DB_STORAGE })
27
34
  .filter(([key, val]) => val != null)
28
35
  );
29
36
 
30
- LOG_MODULE.postINFO({ META_ROUTE, DB_VARS });
37
+ LOG_MODULE.postDEBUG(`DB vars:`, DB_VARS);
31
38
 
32
- API_MODULE.get().ORM = new Sequelize(DB_DB, DB_USER, DB_PASS, SEQUELIZE_OPTIONS);
39
+ API_MODULE.get().ORM = new Sequelize(DB_DB, DB_USER,
40
+ DB_PASS, SEQUELIZE_OPTIONS);
33
41
  await postConnection(API_MODULE.get().ORM);
34
42
 
35
43
  } catch (META_METHOD_ERROR) {
@@ -1,25 +1,39 @@
1
1
  import LOG_MODULE from '../../log/index.js';
2
2
 
3
3
  const META_MODULE_NAME='DATABASE', META_METHOD_NAME='POST_CONNECTION';
4
+ const CYAN = '\x1b[36m', YELLOW = '\x1b[33m', END = '\x1b[0m';
4
5
 
5
6
  export default async ORM => {
6
7
  const MAX_RETRIES = 5, INITIAL_WAIT = 100;
7
- let connectionRetries = 0, waitTime = INITIAL_WAIT;
8
+ let connectionRetries = 0, waitTime = INITIAL_WAIT, CONNECTED = false;
8
9
 
9
10
  while (connectionRetries < MAX_RETRIES) {
10
11
  try {
11
- LOG_MODULE.post({ META_MODULE_NAME, META_METHOD_NAME, AUTH_INTENT: connectionRetries });
12
+ LOG_MODULE.postINFO(`DB connection attempt:`,
13
+ `${YELLOW}${connectionRetries}${END}`);
12
14
  await ORM.authenticate();
15
+ CONNECTED = true;
13
16
  break;
14
17
 
15
18
  } catch (META_METHOD_ERROR) {
19
+ if (connectionRetries >= MAX_RETRIES-1) {
20
+ break;
21
+ }
22
+
16
23
  await new Promise(resolve => setTimeout(resolve, waitTime));
17
24
 
18
25
  connectionRetries += 1;
19
26
  const jitter = Math.random() * 0.3 + 0.7;
20
- waitTime = Math.min(2 * waitTime * jitter, INITIAL_WAIT * Math.pow(2, MAX_RETRIES));
27
+ waitTime = Math.min(2 * waitTime * jitter,
28
+ INITIAL_WAIT * Math.pow(2, MAX_RETRIES));
21
29
  }
22
30
  }
23
31
 
32
+ if (CONNECTED) LOG_MODULE.postINFO(`DB connected`);
33
+ else {
34
+ throw new Error(`DB conection attempts ran out `+
35
+ `${connectionRetries}/${MAX_RETRIES}`);
36
+ }
37
+
24
38
  return connectionRetries;
25
39
  };
@@ -1,9 +1,9 @@
1
1
  const methodsAvailable = ['post', 'get', 'patch', 'put', 'delete'];
2
2
 
3
- export default METHOD_NAME => {
4
- const methodMatched = METHOD_NAME.match(/^[a-z]*/);
3
+ export default ENDPOINT_NAME => {
4
+ const methodMatched = ENDPOINT_NAME.match(/^[a-z]*/);
5
5
  if (!methodMatched || !methodsAvailable.includes(methodMatched[0]))
6
- throw new Error('UNKNOWN_METHOD_TYPE: ' + METHOD_NAME);
6
+ throw new Error('UNKNOWN_ENDPOINT_TYPE: ' + ENDPOINT_NAME);
7
7
 
8
8
  return methodMatched[0];
9
9
  }
@@ -0,0 +1,16 @@
1
+ export default (MODULE_NAME, ENDPOINT_CODE) => {
2
+ const methodPathParts = ENDPOINT_CODE.split(":");
3
+ const pathParams = methodPathParts.slice(1).map(param => `:${param}`);
4
+ const rawEndpointName = methodPathParts[0];
5
+ const ENDPOINT_NAME = rawEndpointName[0] === '!' ?
6
+ rawEndpointName.substring(1) : rawEndpointName;
7
+
8
+ const relativeEndpointRoute =
9
+ `${rawEndpointName}${pathParams.length > 0 ? '/' : ''}`+
10
+ `${pathParams.join("/")}`;
11
+ const ENDPOINT_ROUTE = relativeEndpointRoute[0]==='!' ?
12
+ `/${relativeEndpointRoute.substring(1)}/${MODULE_NAME}` :
13
+ `/${MODULE_NAME}/${relativeEndpointRoute}`;
14
+
15
+ return { ENDPOINT_NAME, ENDPOINT_ROUTE };
16
+ };
@@ -0,0 +1,10 @@
1
+ const typeToResponseCode = {
2
+ post: 201,
3
+ delete: 204,
4
+ }
5
+
6
+ export default endpointType => {
7
+ if (endpointType in typeToResponseCode)
8
+ return typeToResponseCode[endpointType];
9
+ else return 200;
10
+ }
@@ -5,8 +5,8 @@ const getUpperSnakeCase = str => {
5
5
  .toUpperCase();
6
6
  }
7
7
 
8
- export default (MODULE_NAME, METHOD_NAME) => {
8
+ export default (MODULE_NAME, ENDPOINT_NAME) => {
9
9
  const snakeServiceName = getUpperSnakeCase(MODULE_NAME);
10
- const snakeMethodName = getUpperSnakeCase(METHOD_NAME);
11
- return snakeServiceName + '_' + snakeMethodName;
10
+ const snakeEndpointName = getUpperSnakeCase(ENDPOINT_NAME);
11
+ return snakeServiceName + '_' + snakeEndpointName;
12
12
  };
@@ -1,7 +1,8 @@
1
1
  import post from './post/index.js';
2
2
  import getParsedCode from './getParsedCode/index.js';
3
- import getType from './getType/index.js';
3
+ import getMethod from './getMethod/index.js';
4
4
  import getRouteCode from './getRouteCode/index.js';
5
5
  import postResponse from './postResponse/index.js';
6
6
 
7
- export default { post, getParsedCode, getType, getRouteCode, postResponse };
7
+ export default { post, getParsedCode, getMethod,
8
+ getRouteCode, postResponse };
@@ -0,0 +1,55 @@
1
+ import API_MODULE from '../../api/index.js';
2
+ import LOG_MODULE from '../../log/index.js';
3
+ import EVENT_MODULE from '../../event/index.js';
4
+ import ERROR_MODULE from '../../error/index.js';
5
+
6
+ import getParsedCode from '../getParsedCode/index.js';
7
+ import getRouteCode from '../getRouteCode/index.js';
8
+ import getMethod from '../getMethod/index.js';
9
+ import postResponse from '../postResponse/index.js';
10
+
11
+ const META_MODULE_NAME='ENDPOINT', META_ENDPOINT_NAME='POST';
12
+ const CYAN = '\x1b[36m', YELLOW = '\x1b[33m', END = '\x1b[0m';
13
+
14
+ export default async (MODULE_NAME, ENDPOINT_KEY, ENDPOINT) => {
15
+ const { ENDPOINT_NAME, ENDPOINT_ROUTE }
16
+ = getParsedCode(MODULE_NAME, ENDPOINT_KEY);
17
+ const ROUTE_CODE = getRouteCode(MODULE_NAME, ENDPOINT_NAME);
18
+ //LOG_MODULE.postINFO(
19
+ // ` FOUND_ENDPOINT: ${CYAN}${ENDPOINT_NAME}${END}`);
20
+
21
+ try {
22
+
23
+ // Register service within API
24
+ API_MODULE.get().MODULES[MODULE_NAME][ENDPOINT_NAME] =
25
+ async (body, claims=undefined) => {
26
+
27
+ const { MODELS, MODULES } = API_MODULE.get();
28
+ return await ENDPOINT({ MODELS, MODULES, ROUTE_CODE,
29
+ body, claims });
30
+ }
31
+
32
+ const ENDPOINT_METHOD = getMethod(ENDPOINT_NAME);
33
+
34
+ // Register route forn lAMBDA API
35
+ LOG_MODULE.postINFO(` ${YELLOW}${ENDPOINT_ROUTE}${END}`);
36
+
37
+ API_MODULE.get()[ENDPOINT_METHOD](
38
+ ENDPOINT_ROUTE, async (event, res) => {
39
+
40
+ const { body, claims } = EVENT_MODULE.getParsedEvent(event);
41
+
42
+ try {
43
+ await postResponse(res, ROUTE_CODE, ENDPOINT,
44
+ ENDPOINT_METHOD, body, claims);
45
+ }
46
+ catch (ENDPOINT_ERROR) {
47
+ ERROR_MODULE.postRuntime(res, ROUTE_CODE, ENDPOINT_ERROR);
48
+ }
49
+ });
50
+
51
+ } catch (META_ENDPOINT_ERROR) {
52
+ ERROR_MODULE.postCompiletime({ META_MODULE_NAME,
53
+ META_ENDPOINT_NAME, ROUTE_CODE, META_ENDPOINT_ERROR });
54
+ }
55
+ };
@@ -3,16 +3,19 @@ import LOG_MODULE from '../../log/index.js';
3
3
 
4
4
  import getResponseCode from '../getResponseCode/index.js';
5
5
 
6
- export default async (res, ROUTE_CODE, METHOD, METHOD_TYPE, body, claims) => {
6
+ export default async (res, ROUTE_CODE, ENDPOINT, ENDPOINT_TYPE,
7
+ body, claims) => {
7
8
 
8
9
  const { MODELS, MODULES } = API_MODULE.get();
9
10
 
10
- const METHOD_RESPONSE = await METHOD({ MODELS, MODULES, body, claims });
11
- const { status, body: responseBody, cookies, headers } = METHOD_RESPONSE;
11
+ const ENDPOINT_RESPONSE =
12
+ await ENDPOINT({ MODELS, MODULES, body, claims });
13
+ const { status, body: responseBody, cookies, headers } =
14
+ ENDPOINT_RESPONSE;
12
15
 
13
16
  // status
14
- let METHOD_RESPONSE_CODE = status;
15
- if (!status) METHOD_RESPONSE_CODE = getResponseCode(METHOD_TYPE);
17
+ let ENDPOINT_RESPONSE_CODE = status;
18
+ if (!status) ENDPOINT_RESPONSE_CODE = getResponseCode(ENDPOINT_TYPE);
16
19
 
17
20
  // headers
18
21
  if (headers) Object.entries(headers).forEach(([k, v]) => res.header(k, v));
@@ -23,7 +26,7 @@ export default async (res, ROUTE_CODE, METHOD, METHOD_TYPE, body, claims) => {
23
26
  //console.log('cookie', c.name);
24
27
  res.cookie(c.name, c.value, c.options || {}); });
25
28
 
26
- LOG_MODULE.post({ ROUTE_CODE, METHOD_RESPONSE });
29
+ LOG_MODULE.postINFO(`ENDPOINT response:`, ENDPOINT_RESPONSE);
27
30
 
28
- res.status(METHOD_RESPONSE_CODE).json(responseBody);
31
+ res.status(ENDPOINT_RESPONSE_CODE).json(responseBody);
29
32
  };
@@ -1,13 +1,9 @@
1
1
  import API_MODULE from '../../api/index.js';
2
2
  import LOG_MODULE from '../../log/index.js';
3
3
 
4
- export default ({ META_MODULE_NAME, META_METHOD_NAME, ROUTE_CODE, META_METHOD_ERROR }) => {
5
- LOG_MODULE.postERROR({
6
- META_MODULE_NAME,
7
- META_METHOD_NAME,
8
- ROUTE_CODE,
9
- META_METHOD_ERROR,
10
- STACK_TRACE: META_METHOD_ERROR?.stack,
11
- });
12
- if (API_MODULE.get().DEV_ENV==='TRUE') throw META_METHOD_ERROR;
4
+ export default ({ META_METHOD_ERROR }) => {
5
+ //LOG_MODULE.postERROR({ STACK_TRACE: META_METHOD_ERROR?.stack });
6
+ //throw META_METHOD_ERROR;
7
+ console.error(META_METHOD_ERROR?.stack);
8
+ process.exit(1);
13
9
  };
package/src/log/index.js CHANGED
@@ -1,5 +1,9 @@
1
- import post from './post/index.js';
2
1
  import postERROR from './postERROR/index.js';
2
+ import postDEBUG from './postDEBUG/index.js';
3
3
  import postINFO from './postINFO/index.js';
4
4
 
5
- export default { post, postERROR, postINFO };
5
+ export default {
6
+ postERROR,
7
+ postDEBUG,
8
+ postINFO,
9
+ };
@@ -0,0 +1,9 @@
1
+ import API_MODULE from '../../api/index.js';
2
+
3
+ const CYAN = '\x1b[36m', YELLOW = '\x1b[33m', END = '\x1b[0m';
4
+
5
+ export default (...MESSAGE) => {
6
+ if (API_MODULE.get().ERROR_LEVEL===0) {
7
+ console.log(`${YELLOW}DEBUG${END}:`, ...MESSAGE);
8
+ }
9
+ };
@@ -1,3 +1,7 @@
1
1
  import API_MODULE from '../../api/index.js';
2
2
 
3
- export default (...ERROR) => { console.error(...ERROR); };
3
+ const CYAN = '\x1b[36m', YELLOW = '\x1b[33m', END = '\x1b[0m';
4
+
5
+ export default (...ERROR) => {
6
+ console.error(...ERROR);
7
+ };
@@ -1,3 +1,9 @@
1
1
  import API_MODULE from '../../api/index.js';
2
2
 
3
- export default (...MESSAGE) => { if (API_MODULE.get().ERROR_LEVEL===0) { console.log(...MESSAGE); } };
3
+ const CYAN = '\x1b[36m', YELLOW = '\x1b[33m', END = '\x1b[0m';
4
+
5
+ export default (...MESSAGE) => {
6
+ if (API_MODULE.get().ERROR_LEVEL<=1) {
7
+ console.log(`${YELLOW}INFO${END}:`, ...MESSAGE);
8
+ }
9
+ };
@@ -7,7 +7,7 @@ export default () => {
7
7
 
8
8
  API_MODULE.get().use((req, res, next) => {
9
9
  if (req.headers['x-trigger-error']) {
10
- LOG_MODULE.post({ ROUTE_CODE: 'LAMBDA_API', MESSAGE: 'X_TRIGGER_ERROR_HEADER' });
10
+ LOG_MODULE.postINFO('Header de prueba: X_TRIGGER_ERROR_HEADER');
11
11
  throw new Error('X_TRIGGER_ERROR_HEADER');
12
12
  }
13
13
  res.cors(); next();
@@ -3,21 +3,32 @@ import { DataTypes } from 'sequelize';
3
3
  import API_MODULE from '../../api/index.js';
4
4
  import LOG_MODULE from '../../log/index.js';
5
5
 
6
- const META_MODULE='MODEL', META_METHOD='GET_SYNCED', META_ROUTE=META_MODULE+'_'+META_METHOD;
6
+ const META_MODULE='MODEL', META_METHOD='GET_SYNCED',
7
+ META_ROUTE=META_MODULE+'_'+META_METHOD;
8
+ const CYAN = '\x1b[36m', YELLOW = '\x1b[33m', END = '\x1b[0m';
7
9
 
8
10
  export default async () => {
9
11
  for (const MODULE_NAME of Object.keys(API_MODULE.get().SCHEMAS)) {
10
- LOG_MODULE.post({ META_ROUTE, LOADING: MODULE_NAME });
11
- API_MODULE.get().MODELS[MODULE_NAME] = API_MODULE.get().SCHEMAS[MODULE_NAME](API_MODULE.get().ORM, DataTypes);
12
+
13
+ LOG_MODULE.postINFO(`DB model being loaded:`,
14
+ `${CYAN}${MODULE_NAME}${END}`);
15
+
16
+ API_MODULE.get().MODELS[MODULE_NAME] =
17
+ API_MODULE.get().SCHEMAS[MODULE_NAME](
18
+ API_MODULE.get().ORM, DataTypes);
12
19
  }
13
20
 
14
- for (const [MODULE_NAME, MODEL] of Object.entries(API_MODULE.get().MODELS)) {
15
- LOG_MODULE.post({ META_ROUTE, ASSOCIATING: MODULE_NAME });
21
+ for (const [MODULE_NAME, MODEL] of
22
+ Object.entries(API_MODULE.get().MODELS)) {
23
+
24
+ LOG_MODULE.postINFO(`DB model being associated:`,
25
+ `${CYAN}${MODULE_NAME}${END}`);
16
26
  MODEL.associate(API_MODULE.get().MODELS);
17
27
  }
18
28
 
19
- LOG_MODULE.post({ META_ROUTE, SYNCING_ORM: 'WAITING_ORM_SYNCING' });
20
- //await API_MODULE.get().ORM.sync({ alter: process.env.MM_API_DEV_ENV==='TRUE' ? true : false });
29
+ LOG_MODULE.postINFO('DB synchronizing...');
30
+ //await API_MODULE.get().ORM.sync({
31
+ // alter: process.env.MM_API_DEV_ENV==='TRUE' ? true : false });
21
32
  await API_MODULE.get().ORM.sync();
22
- LOG_MODULE.post({ META_ROUTE, SYNCING_ORM: 'DONE_ORM_SYNCING' });
33
+ LOG_MODULE.postINFO('DB synchronized');
23
34
  };
@@ -7,10 +7,10 @@ const CYAN = '\x1b[36m', YELLOW = '\x1b[33m', END = '\x1b[0m';
7
7
 
8
8
  export default async (MODULE_NAME, MODEL) => {
9
9
  if (!MODEL) {
10
- LOG_MODULE.post(' MODEL_NOT_FOUND')
10
+ //LOG_MODULE.post(' MODEL_NOT_FOUND')
11
11
  return;
12
12
  }
13
13
 
14
- LOG_MODULE.post(` + MODEL: ${CYAN}${MODULE_NAME}${END}`)
14
+ LOG_MODULE.postINFO(` ${CYAN}${MODULE_NAME}${END}`)
15
15
  API_MODULE.get().SCHEMAS[MODULE_NAME] = MODEL(MODULE_NAME);
16
16
  }
@@ -63,7 +63,7 @@ export default lambdaHandler => async (req, res) => {
63
63
  res.end(lambdaRes.body);
64
64
 
65
65
  } catch(SERVER_ERROR) {
66
- LOG_MODULE.post({ SERVER_ERROR });
66
+ LOG_MODULE.postERROR({ SERVER_ERROR });
67
67
  res.statusCode = 500;
68
68
  for (let k in CORS_HEADERS) res.setHeader(k, CORS_HEADERS[k]);
69
69
  res.end(JSON.stringify({ REQUEST_PARSING_ERROR: SERVER_ERROR.toString() }));
@@ -1,3 +0,0 @@
1
- import API_MODULE from '../../api/index.js';
2
-
3
- export default (...MESSAGE) => { console.log(...MESSAGE); };
@@ -1,13 +0,0 @@
1
- export default (MODULE_NAME, METHOD_CODE) => {
2
- const methodPathParts = METHOD_CODE.split(":");
3
- const pathParams = methodPathParts.slice(1).map(param => `:${param}`);
4
- const rawMethodName = methodPathParts[0];
5
- const METHOD_NAME = rawMethodName[0] === '!' ? rawMethodName.substring(1) : rawMethodName;
6
-
7
- const relativeMethodRoute = `${rawMethodName}${pathParams.length > 0 ? '/' : ''}${pathParams.join("/")}`;
8
- const METHOD_ROUTE = relativeMethodRoute[0]==='!' ?
9
- `/${relativeMethodRoute.substring(1)}/${MODULE_NAME}` :
10
- `/${MODULE_NAME}/${relativeMethodRoute}`;
11
-
12
- return { METHOD_NAME, METHOD_ROUTE };
13
- };
@@ -1,9 +0,0 @@
1
- const typeToResponseCode = {
2
- post: 201,
3
- delete: 204,
4
- }
5
-
6
- export default methodType => {
7
- if (methodType in typeToResponseCode) return typeToResponseCode[methodType];
8
- else return 200;
9
- }
@@ -1,44 +0,0 @@
1
- import API_MODULE from '../../api/index.js';
2
- import LOG_MODULE from '../../log/index.js';
3
- import EVENT_MODULE from '../../event/index.js';
4
- import ERROR_MODULE from '../../error/index.js';
5
-
6
- import getParsedCode from '../getParsedCode/index.js';
7
- import getRouteCode from '../getRouteCode/index.js';
8
- import getType from '../getType/index.js';
9
- import postResponse from '../postResponse/index.js';
10
-
11
- const META_MODULE_NAME='METHOD', META_METHOD_NAME='POST';
12
- const CYAN = '\x1b[36m', YELLOW = '\x1b[33m', END = '\x1b[0m';
13
-
14
- export default async (MODULE_NAME, METHOD_KEY, METHOD) => {
15
- const { METHOD_NAME, METHOD_ROUTE } = getParsedCode(MODULE_NAME, METHOD_KEY);
16
- const ROUTE_CODE = getRouteCode(MODULE_NAME, METHOD_NAME);
17
- //LOG_MODULE.post(` FOUND_METHOD: ${CYAN}${METHOD_NAME}${END}`);
18
-
19
- try {
20
-
21
- // Register service within API
22
- API_MODULE.get().MODULES[MODULE_NAME][METHOD_NAME] = async (body, claims=undefined) => {
23
- const { MODELS, MODULES } = API_MODULE.get();
24
- return await METHOD({ MODELS, MODULES, ROUTE_CODE, body, claims });
25
- }
26
-
27
- const METHOD_TYPE = getType(METHOD_NAME);
28
-
29
- // Register route forn lAMBDA API
30
- LOG_MODULE.post(` · ${YELLOW}${METHOD_ROUTE}${END}`);
31
-
32
- API_MODULE.get()[METHOD_TYPE](METHOD_ROUTE, async (event, res) => {
33
- const { body, claims } = EVENT_MODULE.getParsedEvent(event);
34
-
35
- try {
36
- await postResponse(res, ROUTE_CODE, METHOD, METHOD_TYPE, body, claims);
37
- }
38
- catch (METHOD_ERROR) { ERROR_MODULE.postRuntime(res, ROUTE_CODE, METHOD_ERROR); }
39
- });
40
-
41
- } catch (META_METHOD_ERROR) {
42
- ERROR_MODULE.postCompiletime({ META_MODULE_NAME, META_METHOD_NAME, ROUTE_CODE, META_METHOD_ERROR });
43
- }
44
- };