From 14256f78b2f2a63e08dc1c2a4853cfd7fefe4409 Mon Sep 17 00:00:00 2001 From: Peter Jankovsky Date: Thu, 19 May 2016 15:57:56 -0600 Subject: [PATCH] update to use promises --- index.js | 41 ++------------ node_modules/tg-node-lib/lib/tg-config.js | 68 ++++++++++++----------- node_modules/tg-node-lib/package.json | 6 +- 3 files changed, 45 insertions(+), 70 deletions(-) diff --git a/index.js b/index.js index ead5e2f..a6f95e9 100644 --- a/index.js +++ b/index.js @@ -2,44 +2,15 @@ const tgConfig = require('tg-node-lib/lib/tg-config'); exports.handler = function(event, context, callback) { - var res; if (typeof event.key === 'undefined') { // grab the whole table - res = tgConfig.getAllConfig(); - callback(res[0], res[1]); - // dynamodb.scan({ - // TableName: 'TokenGoodsConfig' - // }, function(err, data){ - // if (err) - // callback(err); - // else { - // var config = {}; - // for (var i=0; i < data.Items.length; i++) { - // config[data.Items[i].key.S] = data.Items[i].value.S; - // } - // callback(null, config); - // } - // }); + tgConfig.getAllConfig() + .then((config) => callback(null, config)) + .catch((err) => callback(err, null)); } else { // grab just a single key - res = tgConfig.getConfig(event.key); - callback(res[0], res[1]); - // dynamodb.getItem({ - // Key: { - // key: { - // S: String(event.key) - // } - // }, - // TableName: 'TokenGoodsConfig' - // }, function(err, data){ - // if (err) - // callback(err); - // else { - // if (typeof data.Item === 'undefined') - // callback('Config value not found'); - // else - // callback(null, data.Item.value.S); - // } - // }); + tgConfig.getConfig(event.key) + .then((value) => callback(null, value)) + .catch((err) => callback(err, null)); } }; diff --git a/node_modules/tg-node-lib/lib/tg-config.js b/node_modules/tg-node-lib/lib/tg-config.js index adc0e7e..df9f5c5 100644 --- a/node_modules/tg-node-lib/lib/tg-config.js +++ b/node_modules/tg-node-lib/lib/tg-config.js @@ -7,44 +7,48 @@ module.exports = { getAllConfig: function () { - // grab the whole table - var config = {}; - var err = null; - dynamodb.scan({ - TableName: 'TokenGoodsConfig' - }, function (_err, data) { - if (_err) - err = _err; - else { - for (var i = 0; i < data.Items.length; i++) { - config[data.Items[i].key.S] = data.Items[i].value.S; + return new Promise(function (resolve, reject) { + // grab the whole table + dynamodb.scan({ + TableName: 'TokenGoodsConfig' + }, function (_err, data) { + if (_err) { + reject(_err); + } else { + var config = {}; + for (var i = 0; i < data.Items.length; i++) { + config[data.Items[i].key.S] = data.Items[i].value.S; + } + resolve(config); } - } + }); }); - return [err, config]; + }, getConfig: function (configKey) { - // grab just a single key - var value = null; - var err = null; - dynamodb.getItem({ - Key: { - key: { - S: String(configKey) + return new Promise(function (resolve, reject) { + // grab just a single key + var value = null; + dynamodb.getItem({ + Key: { + key: { + S: String(configKey) + } + }, + TableName: TABLE_NAME + }, function (_err, data) { + if (_err) + reject(_err); + else { + if (typeof data.Item === 'undefined') { + value = 'Config value not found'; + } else { + value = data.Item.value.S; + } + resolve(value); } - }, - TableName: TABLE_NAME - }, function (_err, data) { - if (_err) - err = _err; - else { - if (typeof data.Item === 'undefined') - value = 'Config value not found'; - else - value = data.Item.value.S; - } + }); }); - return [err, value]; } } diff --git a/node_modules/tg-node-lib/package.json b/node_modules/tg-node-lib/package.json index 89e5899..6390132 100644 --- a/node_modules/tg-node-lib/package.json +++ b/node_modules/tg-node-lib/package.json @@ -31,8 +31,8 @@ "_requiredBy": [ "/" ], - "_resolved": "git+ssh://git@github.com/pjankovsky/tg-node-lib.git#6442ca1fc41b146c77c144fdb01e969c92d14471", - "_shasum": "888fc46eafc7575cc42fb9163b99cb8dc08c2910", + "_resolved": "git+ssh://git@github.com/pjankovsky/tg-node-lib.git#3527f85a62edf07178b217fd135b6018c538d268", + "_shasum": "efe56cf9e7c8be473a4bb0de61a4cf9934935b8d", "_shrinkwrap": null, "_spec": "github:pjankovsky/tg-node-lib", "_where": "/Users/peterj/Code/TG/tg-lambda-config", @@ -45,7 +45,7 @@ }, "description": "Common lib data for node js lambda functions ======", "devDependencies": {}, - "gitHead": "6442ca1fc41b146c77c144fdb01e969c92d14471", + "gitHead": "3527f85a62edf07178b217fd135b6018c538d268", "license": "MIT", "main": "index.js", "name": "tg-node-lib",