Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-viegas committed Sep 9, 2019
1 parent 8426784 commit bf25f75
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 162 deletions.
58 changes: 42 additions & 16 deletions README.md
Expand Up @@ -10,26 +10,52 @@ functions for a data store api

## Usage

- depends on winston, thus it needs a logger configuration being handed over:
```
const logger = winston.createLogger(config['WINSTON_CONFIG']);
```
- depends on '@jtviegas/dyndbstore' thus it needs the dynamoDb config being handed over too:
```
const store = require('@jtviegas/dyndbstore');
...
store.init({ apiVersion: config.DB_API_VERSION , region: config.DB_API_REGION , endpoint: config.DB_ENDPOINT
, accessKeyId: config.DB_API_ACCESS_KEY_ID , secretAccessKey: config.DB_API_ACCESS_KEY } );
```
- check `test_index.js` for usage:
```
const index = require("..")(config);
```
### required environment variables or configuration properties
- STOREFUNCTIONS_AWS_REGION
- STOREFUNCTIONS_AWS_DB_ENDPOINT - optional, used for local testing;
- STOREFUNCTIONS_AWS_ACCESS_KEY_ID
- STOREFUNCTIONS_AWS_ACCESS_KEY
- STOREFUNCTIONS_ENTITY_LIST
- STOREFUNCTIONS_TENANT
- STOREFUNCTIONS_ENV_LIST

### code snippet example

let config = {
STOREFUNCTIONS_AWS_REGION: 'eu-west-1'
, STOREFUNCTIONS_AWS_ACCESS_KEY_ID: process.env.ACCESS_KEY_ID
, STOREFUNCTIONS_AWS_ACCESS_KEY: process.env.ACCESS_KEY
, STOREFUNCTIONS_ENTITY_LIST: 'item, part'
, STOREFUNCTIONS_TENANT: 'xpto'
, STOREFUNCTIONS_ENV_LIST: 'production,development'
};

const functions = require('@jtviegas/store-functions')(config);
let event = {
httpMethod: 'GET'
, pathParameters: {
entity: 'item'
}
, queryStringParameters: {
env: 'development'
}
};
functions.handler(event, context, (e,d)=>{
if(e)
done(e);
else {
let r=JSON.parse(d.body);
expect(r.length).to.equal(A_NUMBER);
done(null);
}
});

Check the test folder in source tree.

## Tests

`npm test`

## Contributing

https://github.com/jtviegas/store-functions
just help yourself and submit a pull request
28 changes: 0 additions & 28 deletions common.js

This file was deleted.

28 changes: 18 additions & 10 deletions index.js
Expand Up @@ -2,22 +2,26 @@

const winston = require('winston');
const ServerError = require('@jtviegas/jscommons').ServerError;
const commons = require('@jtviegas/jscommons').commons;

const storeFunctions = (config) => {

if (!config)
throw new Error('!!! must provide config to initialize module !!!');

const DEFAULT_WINSTON_CONFIG = {
format: winston.format.combine(winston.format.splat(), winston.format.simple()),
transports: [new winston.transports.Console()]
const CONFIGURATION_SPEC = {
DYNDBSTORE_AWS_REGION: 'STOREFUNCTIONS_AWS_REGION'
, DYNDBSTORE_AWS_ACCESS_KEY_ID: 'STOREFUNCTIONS_AWS_ACCESS_KEY_ID'
, DYNDBSTORE_AWS_ACCESS_KEY: 'STOREFUNCTIONS_AWS_ACCESS_KEY'
, DYNDBSTORE_AWS_DB_ENDPOINT: 'STOREFUNCTIONS_AWS_DB_ENDPOINT'
, STOREFUNCTIONS_ENTITY_LIST: 'STOREFUNCTIONS_ENTITY_LIST'
, STOREFUNCTIONS_TENANT: 'STOREFUNCTIONS_TENANT'
, STOREFUNCTIONS_ENV_LIST: 'STOREFUNCTIONS_ENV_LIST'
};

if (! config['WINSTON_CONFIG'])
config['WINSTON_CONFIG'] = DEFAULT_WINSTON_CONFIG;

const logger = winston.createLogger(config['WINSTON_CONFIG']);
const service = require("./service")(config);
const logger = winston.createLogger(commons.getDefaultWinstonConfig());
let configuration = commons.getConfiguration(CONFIGURATION_SPEC, config, commons.handleListVariables);
const service = require("./service")(configuration);

let handler = (event, context, callback) => {
logger.info("[handler|in] <= %s", JSON.stringify(event));
Expand All @@ -36,10 +40,14 @@ const storeFunctions = (config) => {
throw new ServerError(`Unsupported method "${event.httpMethod}"`, 400);
else {
if( event.pathParameters && event.pathParameters.entity ) {
let env = null;
if( event.queryStringParameters && event.queryStringParameters.env )
env = event.queryStringParameters.env;

if( event.pathParameters.id )
service.entityRetrieval(event.pathParameters.entity, parseInt(event.pathParameters.id), done);
service.entityRetrieval(event.pathParameters.entity, parseInt(event.pathParameters.id), env, done);
else
service.entitiesRetrieval(event.pathParameters.entity, done);
service.entitiesRetrieval(event.pathParameters.entity, env, done);
}
else
throw new ServerError("Unsupported path", 404);
Expand Down
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@jtviegas/store-functions",
"version": "0.0.9",
"version": "1.0.0",
"description": "functions for a data store api",
"main": "index.js",
"scripts": {
Expand All @@ -25,8 +25,8 @@
"node": ">=4.2.4"
},
"dependencies": {
"@jtviegas/dyndbstore": "^1.0.6",
"@jtviegas/jscommons": "0.0.1",
"@jtviegas/dyndbstore": "^1.0.11",
"@jtviegas/jscommons": "0.0.12",
"winston": "^3.2.1"
},
"devDependencies": {
Expand Down
41 changes: 27 additions & 14 deletions service.js
@@ -1,21 +1,33 @@
'use strict';
const winston = require('winston');
const commons = require('@jtviegas/jscommons').commons;
const ServerError = require('@jtviegas/jscommons').ServerError;
const store = require('@jtviegas/dyndbstore');

const service_module = (config) => {

const logger = winston.createLogger(config['WINSTON_CONFIG']);
const logger = winston.createLogger(commons.getDefaultWinstonConfig());
logger.info("...initializing service module...");
const common = require("./common")(config);
store.init( config );

let storeConfig = { apiVersion: config.DB_API_VERSION , region: config.DB_API_REGION
, accessKeyId: config.DB_API_ACCESS_KEY_ID , secretAccessKey: config.DB_API_ACCESS_KEY };
var getTableFromEntity = (entity, env) => {
logger.info("[getTableFromEntity|in] (%s,%s)", entity, env);

if( -1 === config.STOREFUNCTIONS_ENTITY_LIST.indexOf(entity) )
throw new ServerError('!!! table not enabled: ' + entity + ' !!!', 400);

let result = `${config.STOREFUNCTIONS_TENANT}_${entity}`;
if (null !== env){
if( -1 === config.STOREFUNCTIONS_ENV_LIST.indexOf(env) )
throw new ServerError('!!! table not enabled: ' + entity + ' !!!', 400);
result += '_' + env;
}

logger.info("[getTableFromEntity|out] => %s", result);
return result;
}

if( config.DB_ENDPOINT )
storeConfig['endpoint'] = config.DB_ENDPOINT;

store.init( storeConfig );
logger.info("...initialized the store successfully !");

var confirmTable = (table, callback) => {
logger.debug("[confirmTable|in] (%s)", table);
Expand All @@ -41,10 +53,10 @@ const service_module = (config) => {
}


var entityRetrieval = (entity, id, callback) => {
logger.debug("[entityRetrieval|in] (%s,%s)", entity, id);
var entityRetrieval = (entity, id, env, callback) => {
logger.debug("[entityRetrieval|in] (%s,%s,%s)", entity, id, env);

let table = common.tableFromEntity(entity);
let table = getTableFromEntity(entity, env);

confirmTable(table, (e) => {
if(e)
Expand All @@ -61,9 +73,9 @@ const service_module = (config) => {
logger.debug("[entityRetrieval|out]");
}

var entitiesRetrieval = (entity, callback) => {
logger.debug("[entitiesRetrieval|in] (%s)", entity);
let table = common.tableFromEntity(entity);
var entitiesRetrieval = (entity, env, callback) => {
logger.debug("[entitiesRetrieval|in] (%s,%s)", entity, env);
let table = getTableFromEntity(entity, env);

confirmTable(table, (e) => {
if(e)
Expand All @@ -80,6 +92,7 @@ const service_module = (config) => {
return {
entityRetrieval: entityRetrieval
, entitiesRetrieval: entitiesRetrieval
, getTableFromEntity: getTableFromEntity
, confirmTable: confirmTable
};

Expand Down
75 changes: 0 additions & 75 deletions test/config.js

This file was deleted.

0 comments on commit bf25f75

Please sign in to comment.