minimonolith 0.6.6 → 0.7.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,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
 
3
- import { createAPI } from './src/createAPI/index.js';
3
+ import { createAPI } from './src/api/index.js';
4
4
  import { runLambdaServer, loadEnvFile } from './src/server/index.js';
5
5
 
6
6
  export { runLambdaServer, loadEnvFile, createAPI, z };
@@ -0,0 +1,32 @@
1
+ name: NPM Publish
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master # Change this to the branch where you want to trigger the workflow
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/checkout@v3
14
+
15
+ - name: Set up Node.js
16
+ uses: actions/setup-node@v3
17
+ with:
18
+ node-version: 18 # Change this to the Node.js version you need
19
+
20
+ - name: Install dependencies
21
+ run: yarn install
22
+
23
+ - name: Run tests
24
+ run: yarn test
25
+
26
+ - name: Upload coverage to Codecov
27
+ uses: codecov/codecov-action@v3
28
+
29
+ - name: Publish to NPM
30
+ run: yarn publish
31
+ env:
32
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # Set up the NPM_TOKEN in your GitHub repository secrets
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "minimonolith",
3
3
  "type": "module",
4
- "version": "0.6.6",
4
+ "version": "0.7.0",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -2,9 +2,12 @@ import lambdaAPICreateAPI from 'lambda-api';
2
2
 
3
3
  import { registerHealthService } from '../health/index.js';
4
4
  import { registerDatabaseService } from '../database/index.js';
5
+ import { loadAndSyncModels } from '../model/index.js';
5
6
  import { registerService } from '../service/index.js';
6
7
  import { apiHandler } from './apiHandler.js';
7
8
 
9
+ const ROUTE_CODE = 'CREATE_API';
10
+
8
11
  const addCORS = API => {
9
12
  API.use((req, res, next) => { res.cors(); next(); });
10
13
  API.use((err, req, res, next) => { res.cors(); next(); });
@@ -18,10 +21,6 @@ const addCORS = API => {
18
21
  });
19
22
  };
20
23
 
21
- const addService = API => {
22
-
23
- }
24
-
25
24
  const createAPI = () => {
26
25
  const API = lambdaAPICreateAPI();
27
26
 
@@ -34,7 +33,11 @@ const createAPI = () => {
34
33
 
35
34
  API.registerDatabaseService = async () => { await registerDatabaseService(API) };
36
35
 
37
- API.handler = () => apiHandler(API);
36
+ API.handler = async () => {
37
+ if (API.ORM) API.MODELS = await loadAndSyncModels(API);
38
+ else console.log({ ROUTE_CODE, INFO: 'NO_DATABASE_REGISTERED' });
39
+ return apiHandler(API);
40
+ }
38
41
 
39
42
  API.SERVICES = {};
40
43
 
@@ -1,29 +1,16 @@
1
1
  import Sequelize from 'sequelize';
2
2
  //import SequelizeDynamo from 'dynamo-sequelize';
3
- import { loadAndSyncModels } from './loadModels.js';
4
3
 
5
4
  const ROUTE_CODE = 'DB_CONNECTION';
6
5
 
7
- const createORMInstance = () => {
8
- if (process.env.TEST_ENV) return new Sequelize('sqlite::memory:', { logging: false });
9
-
10
- const { DB_DB, DB_USER, DB_PASS, DB_HOST } = process.env;
11
- const SEQUELIZE_OPTIONS = { host: DB_HOST, dialect: 'mysql', logging: false };
12
-
13
- if (process.env.DEV_ENV) console.log({ ROUTE_CODE, DB_VARS: { DB_USER, DB_PASS, DB_HOST, DB_DB, }});
14
- return new Sequelize(DB_DB, DB_USER, DB_PASS, SEQUELIZE_OPTIONS);
15
- };
16
-
17
-
18
- const establishConnection = async () => {
6
+ const establishConnection = async ORM => {
19
7
  const MAX_RETRIES = 5, INITIAL_WAIT = 100;
20
8
  let connectionRetries = 0, waitTime = INITIAL_WAIT;
21
- const orm = createORMInstance();
22
9
 
23
10
  while (connectionRetries < MAX_RETRIES) {
24
11
  try {
25
- if (!process.env.TEST_ENV) console.log({ ROUTE_CODE, AUTH_INTENT: connectionRetries });
26
- await orm.authenticate();
12
+ console.log({ ROUTE_CODE, AUTH_INTENT: connectionRetries });
13
+ await ORM.authenticate();
27
14
  break;
28
15
 
29
16
  } catch (DATABASE_CONNECTION_ERROR) {
@@ -35,15 +22,19 @@ const establishConnection = async () => {
35
22
  }
36
23
  }
37
24
 
38
- return orm;
25
+ return connectionRetries;
39
26
  };
40
27
 
41
28
  const registerDatabaseService = async API => {
42
29
  try {
43
- const orm = await establishConnection();
44
- API.MODELS = await loadAndSyncModels(orm, Sequelize);
30
+ const { DB_DIALECT, DB_DB, DB_USER, DB_PASS, DB_HOST } = process.env;
31
+ const SEQUELIZE_OPTIONS = { host: DB_HOST, dialect: DB_DIALECT, logging: false };
32
+
33
+ if (!process.env.PROD_ENV) console.log({ ROUTE_CODE, DB_VARS: { DB_USER, DB_PASS, DB_HOST, DB_DB, }});
34
+ API.ORM = new Sequelize(DB_DB, DB_USER, DB_PASS, SEQUELIZE_OPTIONS);
35
+ establishConnection(API.ORM);
45
36
 
46
37
  } catch (DB_ERROR) { console.error({ ROUTE_CODE, DB_ERROR }); }
47
38
  };
48
39
 
49
- export { createORMInstance, establishConnection, registerDatabaseService };
40
+ export { establishConnection, registerDatabaseService };
@@ -1,4 +1,3 @@
1
1
  import { registerDatabaseService } from './databaseService.js';
2
- import { registerModel } from './loadModels.js';
3
2
 
4
- export { registerDatabaseService, registerModel };
3
+ export { registerDatabaseService };
@@ -5,7 +5,7 @@ const healthHandler = () => {
5
5
  const registerHealthService = API => {
6
6
  API.get('/', async (req, res) => {
7
7
  const SERVICE_RESPONSE = healthHandler();
8
- if (!process.env.TEST_ENV) console.log({ SERVICE_RESPONSE });
8
+ console.log({ SERVICE_RESPONSE });
9
9
  return { SERVICE_RESPONSE };
10
10
  });
11
11
  }
@@ -0,0 +1,3 @@
1
+ import { registerModel, loadAndSyncModels } from './loadModels.js';
2
+
3
+ export { registerModel, loadAndSyncModels };
@@ -0,0 +1,27 @@
1
+ import { DataTypes } from 'sequelize';
2
+
3
+ const ROUTE_CODE = 'LOAD_MODELS';
4
+
5
+ const modelSchemas = {};
6
+
7
+ const registerModel = async (SERVICE_NAME, SERVICE_URL) => {
8
+ const SERVICE_MODEL_MODULE = await import(`${SERVICE_URL}model.js`);
9
+ modelSchemas[SERVICE_NAME] = SERVICE_MODEL_MODULE.modelSchema(SERVICE_NAME);
10
+ }
11
+
12
+ const loadAndSyncModels = async API => {
13
+ console.log({ ROUTE_CODE, LOADING_MODELS: 'LOADING_MODELS' });
14
+ const MODELS = Object.keys(modelSchemas).reduce((loadedModels, serviceName) => {
15
+ loadedModels[serviceName] = modelSchemas[serviceName](API.ORM, DataTypes);
16
+ return loadedModels;
17
+ }, {});
18
+
19
+ Object.entries(MODELS).forEach(([serviceName, model]) => { model.associate(MODELS); });
20
+
21
+ console.log({ ROUTE_CODE, SYNCING_ORM: 'SYNCING_ORM' });
22
+ await API.ORM.sync({ alter: process.env.LOCAL_ENV ? true : false });
23
+
24
+ return MODELS;
25
+ };
26
+
27
+ export { registerModel, loadAndSyncModels };
@@ -4,18 +4,18 @@ const methodHandler = async (event, API, METHOD, ROUTE_CODE) => {
4
4
  try {
5
5
  const VALIDATION_ERROR = await validationHandler(event, METHOD, API.MODELS, ROUTE_CODE);
6
6
  if (VALIDATION_ERROR) {
7
- if (!process.env.TEST_ENV) console.error({ ROUTE_CODE, VALIDATION_ERROR });
7
+ console.error({ ROUTE_CODE, VALIDATION_ERROR: VALIDATION_ERROR.toString() });
8
8
  return VALIDATION_ERROR;
9
9
  }
10
10
 
11
11
  const EVENT_CONTEXT = { event, MODELS: API.MODELS, SERVICES: API.SERVICES, ROUTE_CODE };
12
12
  const METHOD_RESPONSE = await METHOD.handler(EVENT_CONTEXT);
13
13
 
14
- if (!process.env.TEST_ENV) console.log({ ROUTE_CODE, METHOD_RESPONSE });
14
+ console.log({ ROUTE_CODE, METHOD_RESPONSE });
15
15
  return METHOD_RESPONSE;
16
16
 
17
17
  } catch (METHOD_ERROR) {
18
- if (!process.env.TEST_ENV) console.error({ ROUTE_CODE, METHOD_ERROR });
18
+ console.error({ ROUTE_CODE, METHOD_ERROR });
19
19
  return { statusCode: 500, body: { ROUTE_CODE, METHOD_ERROR: METHOD_ERROR.toString() } };
20
20
  }
21
21
  };
@@ -1,7 +1,7 @@
1
1
  import url from 'url'
2
2
 
3
3
  import { getProjectRoot } from '../path/index.js';
4
- import { registerModel } from '../database/index.js';
4
+ import { registerModel } from '../model/index.js';
5
5
  import { methodHandler } from './methodHandler.js';
6
6
  import { getMethodType } from './getMethodType.js';
7
7
  import { getMethodRouteCode } from './getMethodRouteCode.js';
@@ -19,17 +19,20 @@ const registerService = async (API, SERVICE_NAME, SRC_FOLDER, MODULE_FOLDER) =>
19
19
 
20
20
  API.SERVICES[SERVICE_NAME] = {};
21
21
  Object.keys(SERVICE_METHODS).forEach(METHOD_NAME => {
22
+ const ROUTE_CODE = getMethodRouteCode(SERVICE_NAME, METHOD_NAME);
22
23
 
23
- API.SERVICES[SERVICE_NAME][METHOD_NAME] = async (event, res) => {
24
- const ROUTE_CODE = getMethodRouteCode(SERVICE_NAME, METHOD_NAME);
25
- const { statusCode, body } =
26
- await methodHandler(event, API, SERVICE_METHODS[METHOD_NAME], ROUTE_CODE);
27
- res.status(statusCode).json(body);
24
+ API.SERVICES[SERVICE_NAME][METHOD_NAME] = async body => {
25
+ const eventResponse =
26
+ await methodHandler({ body }, API, SERVICE_METHODS[METHOD_NAME], ROUTE_CODE);
27
+ return eventResponse.body;
28
28
  }
29
29
 
30
30
  const methodType = getMethodType(METHOD_NAME);
31
- API[methodType](`/${SERVICE_NAME}/${METHOD_NAME}`,
32
- API.SERVICES[SERVICE_NAME][METHOD_NAME]);
31
+ API[methodType](`/${SERVICE_NAME}/${METHOD_NAME}`, async (event, res) => {
32
+ const { statusCode, body } =
33
+ await methodHandler(event, API, SERVICE_METHODS[METHOD_NAME], ROUTE_CODE);
34
+ res.status(statusCode).json(body);
35
+ });
33
36
  });
34
37
  } catch (SERVICE_HANDLER_ERROR) {
35
38
  console.error({
@@ -1,30 +0,0 @@
1
- const ROUTE_CODE = 'LOAD_MODELS';
2
-
3
- const modelSchemas = {};
4
-
5
- export const registerModel = async (SERVICE_NAME, SERVICE_URL) => {
6
- const SERVICE_MODEL_MODULE = await import(`${SERVICE_URL}model.js`);
7
- modelSchemas[SERVICE_NAME] = SERVICE_MODEL_MODULE.modelSchema(SERVICE_NAME);
8
- }
9
-
10
- const loadModels = (orm, types) => {
11
- const MODELS = Object.keys(modelSchemas).reduce((loadedModels, serviceName) => {
12
- loadedModels[serviceName] = modelSchemas[serviceName](orm, types);
13
- return loadedModels;
14
- }, {});
15
-
16
- Object.entries(MODELS).forEach(([serviceName, model]) => { model.associate(MODELS); });
17
- return MODELS;
18
- }
19
-
20
- const loadAndSyncModels = async (orm, types) => {
21
- if (!process.env.TEST_ENV) console.log({ ROUTE_CODE, LOADING_MODELS: 'LOADING_MODELS' });
22
- const MODELS = loadModels(orm, types);
23
-
24
- if (!process.env.TEST_ENV) console.log({ ROUTE_CODE, SYNCING_ORM: 'SYNCING_ORM' });
25
- await orm.sync({ alter: process.env.LOCAL_ENV ? true : false });
26
-
27
- return MODELS;
28
- };
29
-
30
- export { loadAndSyncModels };
File without changes
File without changes