Skip to content

Commit 1d3dd62

Browse files
committed
using es6
1 parent 1acb5d9 commit 1d3dd62

10 files changed

+65
-79
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
.webpack

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ In order to install and run this example you need an AWS accounts credentials co
1414
git clone [email protected]:ankkho/aws-node-elasticache-redis.git
1515
cd aws-node-elasticache-redis
1616
npm install
17+
export REGION='your-desired-aws-region' eg: 'ap-south-1'
1718
```
1819

1920
2. Deploy using sls deploy

aws/redis.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
const redis = require('redis');
2-
const bluebird = require('bluebird');
3-
const currentRedisEndpoint = 'redis://localhost:6379';
1+
/*
2+
Returns promise functions for interacting with redis
3+
*/
44

5-
bluebird.promisifyAll(redis.RedisClient.prototype);
6-
bluebird.promisifyAll(redis.Multi.prototype);
5+
import redis from 'redis'
6+
import bluebird from 'bluebird'
77

8-
const createRedisClient = (endpoint) => {
9-
const clientObj = redis.createClient(false, endpoint, {
10-
no_ready_check: true,
11-
});
8+
bluebird.promisifyAll(redis.RedisClient.prototype)
9+
bluebird.promisifyAll(redis.Multi.prototype)
1210

13-
clientObj.on('error', (err) => {
14-
console.log(`Error while creating redis client: ${err}`);
15-
});
11+
const createRedisClient = () => {
12+
const clientObj = redis.createClient(6379, process.env.cacheURL, { no_ready_check: true })
1613

17-
return clientObj;
18-
};
14+
clientObj.on('error', (err) => {
15+
console.log(`Error while creating redis client: ${err}`)
16+
})
1917

20-
const client = createRedisClient(currentRedisEndpoint);
18+
return clientObj
19+
}
2120

21+
const client = createRedisClient()
2222

23-
module.exports = {
24-
client,
25-
};
23+
export default client

handler.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
const lib = require('./lib');
1+
import {
2+
getProductDataResponse,
3+
saveProductDataResponse,
4+
} from './lib'
25

3-
const getProductData = (event, context) =>
4-
lib.getProductDataResponse(event, (error, response) => context.done(error, response));
6+
const getProductData = (event, context) => getProductDataResponse(event, (error, response) => context.done(error, response))
7+
const saveProductData = (event, context) => saveProductDataResponse(event, (error, response) => context.done(error, response))
58

6-
module.exports = {
7-
getProductData,
8-
};
9+
export {
10+
getProductData,
11+
saveProductData,
12+
}

lib/functions/getProductData.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
const redis = require('../../aws/redis');
2-
const util = require('../util');
1+
import redis from '../../aws/redis'
2+
import { keyName } from '../util'
33

4-
const get = function* (productId) {
5-
const keyName = util.keyName(productId);
4+
function* get(productId) {
65
const name = {
76
name: 'foo',
87
};
9-
console.log('.......');
10-
yield redis.client.hmsetAsync(keyName, name);
8+
yield redis.client.hmsetAsync(keyName(productId), name);
119
const data = yield redis.client.hmgetallAsync(keyName);
12-
console.log({ data });
1310
return data ? { data: {} } : { data };
14-
};
11+
}
1512

16-
module.exports = { get };
13+
export default get

lib/functions/saveProductData.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
const redis = require('../../aws/redis');
2-
const util = require('../util');
1+
import redis from '../../aws/redis'
2+
import { keyName } from '../util'
33

4-
const save = function* (body) {
4+
function* save(body) {
55
const { productId } = body;
6-
const keyName = util.keyName(productId);
7-
return yield redis.hmsetAsync(keyName, body);
8-
};
6+
return yield redis.hmsetAsync(keyName(productId), body);
7+
}
98

10-
module.exports = { save };
9+
export default save

lib/index.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
const getProductData = require('./functions/getProductData');
2-
const saveProductData = require('./functions/saveProductData');
1+
import co from 'co'
2+
import getProductData from './functions/getProductData'
3+
import saveProductData from './functions/saveProductData'
34

4-
const util = require('./util');
5+
import { response } from './util'
56

67
// response for getAllProducts
78
const getProductDataResponse = (event, cb) => {
8-
const id = event.pathParameters.id;
9-
const resp = getProductData.get(id);
9+
co(function* () {
10+
const { pathParameters: { id } } = event
11+
const resp = yield getProductData(id);
1012

11-
return cb(null, util.response(resp));
13+
return cb(null, response(resp))
14+
})
1215
};
1316

1417
const saveProductDataResponse = (event, cb) => {
15-
const { body } = JSON.parse(event.body);
16-
saveProductData.save(body).then(resp => cb(null, util.response('done')));
18+
co(function* () {
19+
const { body } = JSON.parse(event.body);
20+
const resp = yield saveProductData(body)
21+
22+
return cb(null, response(resp))
23+
})
1724
};
1825

1926
export {

lib/util.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
const keyName = productId => `product:${productId}:data`;
2-
3-
const response = message => ({
4-
statusCode: 200,
5-
body: {
6-
message,
7-
},
8-
});
2+
const response = body => ({ statusCode: 200, body: JSON.stringify(body) })
93

104
export {
115
keyName,

serverless.yml

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ provider:
44
name: aws
55
runtime: nodejs6.10
66
stage: dev
7-
profile: disvoice-${self:provider.stage}
8-
region: ${env:REGION}
7+
region: 'ap-south-1'
98
memorySize: 128
109
timeout: 10
1110
cfLogs: true
@@ -15,14 +14,14 @@ provider:
1514
environment: dev
1615
serviceName: serverless-sample
1716
environment:
18-
cacheUrl: ${cf:serverless-sample-dev.cacheUrl}
19-
vpc:
20-
securityGroupIds:
21-
- ${cf:serverless-sample-dev.VPCSecurityGroup}
22-
- ${cf:serverless-sample-dev.RedisSecurityGroup}
23-
subnetIds:
24-
- ${cf:serverless-sample-dev.PrivateSubnetA}
25-
- ${cf:serverless-sample-dev.PublicSubnetB}
17+
# cacheUrl: ${cf:serverless-sample-dev.cacheUrl}
18+
# vpc:
19+
# securityGroupIds:
20+
# - ${cf:serverless-sample-dev.VPCSecurityGroup}
21+
# - ${cf:serverless-sample-dev.RedisSecurityGroup}
22+
# subnetIds:
23+
# - ${cf:serverless-sample-dev.PrivateSubnetA}
24+
# - ${cf:serverless-sample-dev.PublicSubnetB}
2625
package:
2726
exclude:
2827
- ./node_modules/**

webpack.config.js

-14
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ module.exports = {
2727
},
2828
plugins: [
2929
new webpack.NoEmitOnErrorsPlugin(),
30-
new webpack.optimize.UglifyJsPlugin({
31-
mangle: true,
32-
comments: false,
33-
compress: {
34-
warnings: false,
35-
pure_getters: true,
36-
unsafe: true,
37-
unsafe_comps: true,
38-
},
39-
output: {
40-
comments: false,
41-
},
42-
exclude: [/\.min\.js$/gi],
43-
}),
4430
new CompressionPlugin({ asset: '[path].gz[query]', algorithm: 'gzip', test: /\.js/, threshold: 10240, minRatio: 0 }),
4531
],
4632
};

0 commit comments

Comments
 (0)