-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBase.js
More file actions
123 lines (103 loc) · 2.83 KB
/
Copy pathBase.js
File metadata and controls
123 lines (103 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
/**
* DynamoDB service base class
*
* @module services/dynamodb/Base
*
*/
const rootPrefix = '../..',
logger = require(rootPrefix + '/lib/logger/customConsoleLogger'),
responseHelper = require(rootPrefix + '/lib/formatter/response'),
OSTBase = require('@ostdotcom/base'),
coreConstant = require(rootPrefix + '/config/coreConstant');
const InstanceComposer = OSTBase.InstanceComposer;
require(rootPrefix + '/lib/dynamodb/base');
/**
* Constructor for base service class
*
* @param {String} methodName - DDB method name
* @param {Object} params - DDB method params
* @param {String} serviceType - type of service supported
*
* @constructor
*/
const DDBServiceBase = function(methodName, params, serviceType) {
const oThis = this;
oThis.params = params;
oThis.methodName = methodName;
oThis.serviceType = serviceType;
};
DDBServiceBase.prototype = {
/**
* Perform method
*
* @return {Promise<result>}
*
*/
perform: async function() {
const oThis = this;
return oThis.asyncPerform().catch(function(err) {
logger.error('services/dynamodb/Base.js:perform inside catch ', err);
return responseHelper.error({
internal_error_identifier: 's_dy_b_perform_1',
api_error_identifier: 'exception',
debug_options: { error: err.stack },
error_config: coreConstant.ERROR_CONFIG
});
});
},
/**
* Async Perform
*
* @return {Promise<*>}
*/
asyncPerform: async function() {
const oThis = this;
let r = oThis.validateParams();
if (r.isFailure()) return r;
r = await oThis.executeDdbRequest();
return r;
},
/**
* Validation of params
*
* @return {result}
*
*/
validateParams: function() {
const oThis = this;
if (!oThis.methodName) {
return responseHelper.error({
internal_error_identifier: 'l_dy_b_validateParams_1',
api_error_identifier: 'invalid_method_name',
debug_options: {},
error_config: coreConstant.ERROR_CONFIG
});
}
if (!oThis.params) {
return responseHelper.error({
internal_error_identifier: 'l_dy_b_validateParams_3',
api_error_identifier: 'invalid_params',
debug_options: {},
error_config: coreConstant.ERROR_CONFIG
});
}
return responseHelper.successWithData({});
},
/**
* Execute dynamoDB request
*
* @return {promise<result>}
*
*/
executeDdbRequest: async function() {
const oThis = this;
// Last parameter is service type (dax or dynamoDB)
return await oThis
.ic()
.getInstanceFor(coreConstant.icNameSpace, 'libDynamoDBBase')
.queryDdb(oThis.methodName, oThis.serviceType, oThis.params);
}
};
InstanceComposer.registerAsShadowableClass(DDBServiceBase, coreConstant.icNameSpace, 'DDBServiceBase');
module.exports = DDBServiceBase;