lesgo 0.7.0 → 0.7.4

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.
@@ -8,7 +8,7 @@ set -Eeuo pipefail
8
8
  # #
9
9
  ###############################################################################
10
10
 
11
- usage="$(basename "$0") [-f] [-s] [-t] [-h] [-d] [-y] -- script to deploy serverless functions
11
+ usage="$(basename "$0") [-f] [-s] [-t] [-h] [-d] [-c] [-y] -- script to deploy serverless functions
12
12
 
13
13
  where:
14
14
  -t define the type of action to be taken (build, deploy, invoke, logs, destroy)
@@ -17,22 +17,24 @@ where:
17
17
  -h show this help text
18
18
  -l to invoke a local function
19
19
  -d set data parameters for invoke function
20
+ -c set config as per --config path-to-serverless-yml
20
21
  -y no question/prompt for ci/cd"
21
22
 
22
23
  # arg options
23
- BUILD=0; # serverless build without deploy
24
- DEPLOY=0; # serverless deploy
25
- INVOKE=0; # serverless invoke of specific function
26
- LOGS=0; # serverless stream log of specific function
27
- DESTROY=0; # serverless remove entire service
28
- FUNCTION=''; # specify function to involve
29
- STAGE=''; # deploy specific stage/environment
30
- INVOKE_LOCAL=0; # default to non local execution
31
- DATA='' # set the data parameters for invoke function
32
- NO_QUESTION=0; # default to prompt
24
+ BUILD=0; # serverless build without deploy
25
+ DEPLOY=0; # serverless deploy
26
+ INVOKE=0; # serverless invoke of specific function
27
+ LOGS=0; # serverless stream log of specific function
28
+ DESTROY=0; # serverless remove entire service
29
+ FUNCTION=''; # specify function to involve
30
+ STAGE=''; # deploy specific stage/environment
31
+ INVOKE_LOCAL=0; # default to non local execution
32
+ DATA='' # set the data parameters for invoke function
33
+ NO_QUESTION=0; # default to prompt
34
+ CONFIG='./serverless.yml'; # specify serverless config file to deploy, default to root
33
35
 
34
36
  # parse the options
35
- while getopts "lhs:f:t:d:y" OPT ; do
37
+ while getopts "lhs:f:t:d:c:y" OPT ; do
36
38
  case ${OPT} in
37
39
  f) FUNCTION=${OPTARG} ;;
38
40
  s) STAGE=${OPTARG} ;;
@@ -53,6 +55,7 @@ while getopts "lhs:f:t:d:y" OPT ; do
53
55
  fi;;
54
56
  l) INVOKE_LOCAL=1 ;;
55
57
  d) DATA=${OPTARG} ;;
58
+ c) CONFIG=${OPTARG} ;;
56
59
  y) NO_QUESTION=1 ;;
57
60
  h)
58
61
  echo "${usage}"
@@ -102,8 +105,8 @@ function deploy_func_check ()
102
105
 
103
106
  function deploy_func ()
104
107
  {
105
- echo -e "${YELLOW}Deploying ${FUNCTION} to ${STAGE}${NC}"
106
- sls deploy -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE}
108
+ echo -e "${YELLOW}Deploying ${FUNCTION} to ${STAGE}${NC} using ${CONFIG}"
109
+ sls deploy -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
107
110
  }
108
111
 
109
112
  function prompt_confirmation_deploy_all ()
@@ -112,7 +115,7 @@ function prompt_confirmation_deploy_all ()
112
115
  deploy_full;
113
116
  else
114
117
  while true; do
115
- read -p "Confirm deploy service to ${STAGE} with .env.${ENVFILE}? [Y|N] " yn
118
+ read -p "Confirm deploy service to ${STAGE} with .env.${ENVFILE} using ${CONFIG}? [Y|N] " yn
116
119
  case ${yn} in
117
120
  [Yy] | yes | Yes | YES ) deploy_full; break;;
118
121
  [Nn] | no | No | NO ) echo -e "${YELLOW}Cancelled deploying service to [${STAGE}]${NC}"; exit;;
@@ -125,7 +128,7 @@ function prompt_confirmation_deploy_all ()
125
128
  function prompt_confirmation_deploy_function ()
126
129
  {
127
130
  while true; do
128
- read -p "Confirm deploy function ${FUNCTION} to ${STAGE} with .env.${ENVFILE}? [Y|N] " yn
131
+ read -p "Confirm deploy function ${FUNCTION} to ${STAGE} with .env.${ENVFILE} using ${CONFIG}? [Y|N] " yn
129
132
  case ${yn} in
130
133
  [Yy] | yes | Yes | YES ) deploy_func; break;;
131
134
  [Nn] | no | No | NO ) echo -e "${YELLOW}Cancelled deploying function ${FUNCTION} to [${STAGE}]${NC}"; exit;;
@@ -137,35 +140,35 @@ function prompt_confirmation_deploy_function ()
137
140
  function deploy_full ()
138
141
  {
139
142
  echo -e "${YELLOW}Deploying service to ${STAGE}${NC}"
140
- sls deploy --stage ${STAGE} --env ${ENVFILE}
143
+ sls deploy --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
141
144
  }
142
145
 
143
146
  function invoke_func ()
144
147
  {
145
- echo -e "${YELLOW}Invoking function ${FUNCTION} on ${STAGE}${NC}"
148
+ echo -e "${YELLOW}Invoking function ${FUNCTION} on ${STAGE}${NC} using ${CONFIG}"
146
149
  if [ ${INVOKE_LOCAL} == 1 ]; then
147
- sls invoke local -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -d ${DATA} -l
150
+ sls invoke local -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -d ${DATA} -l --config ${CONFIG}
148
151
  else
149
- sls invoke -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -d ${DATA} -l
152
+ sls invoke -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -d ${DATA} -l --config ${CONFIG}
150
153
  fi
151
154
  }
152
155
 
153
156
  function log_stream_func ()
154
157
  {
155
- echo -e "${YELLOW}Log Streaming function ${FUNCTION} on ${STAGE}${NC}"
156
- sls logs -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -t
158
+ echo -e "${YELLOW}Log Streaming function ${FUNCTION} on ${STAGE}${NC} using ${CONFIG}"
159
+ sls logs -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -t --config ${CONFIG}
157
160
  }
158
161
 
159
162
  function build ()
160
163
  {
161
- echo -e "${YELLOW}Building bundle without deployment${NC}"
162
- sls webpack --stage ${STAGE} --env ${ENVFILE}
164
+ echo -e "${YELLOW}Building bundle without deployment${NC} using ${CONFIG}"
165
+ sls webpack --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
163
166
  }
164
167
 
165
168
  function prompt_confirmation_destroy_service ()
166
169
  {
167
170
  while true; do
168
- read -p "Confirm destroy service to ${STAGE} with .env.${ENVFILE}? Note: This action will remove entire service from AWS. Ensure no other applications are using this service. [Y|N] " yn
171
+ read -p "Confirm destroy service to ${STAGE} with .env.${ENVFILE} using ${CONFIG}? Note: This action will remove entire service from AWS. Ensure no other applications are using this service. [Y|N] " yn
169
172
  case ${yn} in
170
173
  [Yy] | yes | Yes | YES ) destroy_service; break;;
171
174
  [Nn] | no | No | NO ) echo -e "${YELLOW}Cancelled destroying service to [${STAGE}]${NC}"; exit;;
@@ -177,7 +180,7 @@ function prompt_confirmation_destroy_service ()
177
180
  function destroy_service ()
178
181
  {
179
182
  echo -e "${YELLOW}Removing service to ${STAGE}${NC}"
180
- sls remove --stage ${STAGE} --env ${ENVFILE}
183
+ sls remove --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
181
184
  }
182
185
 
183
186
  ###############################################################################
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lesgo",
3
- "version": "0.7.0",
3
+ "version": "0.7.4",
4
4
  "description": "Core framework for lesgo node.js serverless framework.",
5
5
  "main": "./src/index.js",
6
6
  "author": "Sufiyan Rahmat",
@@ -51,7 +51,7 @@
51
51
  "prettier": "^1.18.2"
52
52
  },
53
53
  "dependencies": {
54
- "@elastic/elasticsearch": "^7.4.0",
54
+ "@elastic/elasticsearch": "^7.16.0",
55
55
  "@firebase/app": "^0.6.12",
56
56
  "data-api-client": "^1.1.0",
57
57
  "firebase-admin": "^9.3.0",
@@ -38,24 +38,18 @@ class ElasticsearchService {
38
38
  return this.client;
39
39
  }
40
40
 
41
- search(body) {
42
- return new Promise((resolve, reject) => {
43
- const param = {
44
- index: this.index,
45
- type: this.type,
46
- body,
47
- };
48
- this.client.search(param, (err, response) => {
49
- /* istanbul ignore next */
50
- if (err) {
51
- reject(err);
52
- }
53
-
54
- this.result = response;
55
-
56
- resolve(response);
57
- });
58
- });
41
+ async search(body) {
42
+ const param = {
43
+ index: this.index,
44
+ type: this.type,
45
+ body,
46
+ };
47
+
48
+ const response = await this.client.search(param);
49
+
50
+ this.result = response;
51
+
52
+ return response;
59
53
  }
60
54
 
61
55
  /**
@@ -75,12 +69,7 @@ class ElasticsearchService {
75
69
  body: settings,
76
70
  };
77
71
 
78
- return new Promise((resolve, reject) => {
79
- this.client.indices.create(params, (err, response) => {
80
- // eslint-disable-next-line no-unused-expressions
81
- err ? /* istanbul ignore next */ reject(err) : resolve(response);
82
- });
83
- });
72
+ return this.client.indices.create(params);
84
73
  }
85
74
 
86
75
  deleteIndices(index, options = {}) {
@@ -89,32 +78,20 @@ class ElasticsearchService {
89
78
  ...options,
90
79
  };
91
80
 
92
- return new Promise((resolve, reject) => {
93
- this.client.indices.delete(params, (err, response) => {
94
- // eslint-disable-next-line no-unused-expressions
95
- err ? /* istanbul ignore next */ reject(err) : resolve(response);
96
- });
97
- });
81
+ return this.client.indices.delete(params);
98
82
  }
99
83
 
100
- existIndices(index, options = {}) {
84
+ async existIndices(index, options = {}) {
101
85
  const params = { index, ...options };
102
- return new Promise((resolve, reject) => {
103
- this.client.indices.exists(params, (err, response) => {
104
- // eslint-disable-next-line no-unused-expressions
105
- err ? /* istanbul ignore next */ reject(err) : resolve(response.body);
106
- });
107
- });
86
+
87
+ const response = await this.client.indices.exists(params);
88
+ return response.body;
108
89
  }
109
90
 
110
91
  putMapping(index, type, body) {
111
92
  const params = { index, type, body: { properties: body } };
112
- return new Promise((resolve, reject) => {
113
- this.client.indices.putMapping(params, (err, response) => {
114
- // eslint-disable-next-line no-unused-expressions
115
- err ? /* istanbul ignore next */ reject(err) : resolve(response);
116
- });
117
- });
93
+
94
+ return this.client.indices.putMapping(params);
118
95
  }
119
96
 
120
97
  get(id) {
@@ -124,12 +101,7 @@ class ElasticsearchService {
124
101
  id,
125
102
  };
126
103
 
127
- return new Promise((resolve, reject) => {
128
- this.client.get(params, (err, response) => {
129
- // eslint-disable-next-line no-unused-expressions
130
- err ? /* istanbul ignore next */ reject(err) : resolve(response);
131
- });
132
- });
104
+ return this.client.get(params);
133
105
  }
134
106
 
135
107
  indexOrCreateById(body, refresh = false) {
@@ -141,24 +113,11 @@ class ElasticsearchService {
141
113
  refresh,
142
114
  };
143
115
 
144
- return new Promise((resolve, reject) => {
145
- this.client.index(params, (err, response) => {
146
- // eslint-disable-next-line no-unused-expressions
147
- err ? /* istanbul ignore next */ reject(err) : resolve(response);
148
- });
149
- });
116
+ return this.client.index(params);
150
117
  }
151
118
 
152
119
  bulkIndex(bodies) {
153
- return new Promise((resolve, reject) => {
154
- this.client.bulk(
155
- { body: this.constructBulkIndex(bodies) },
156
- (err, response) => {
157
- // eslint-disable-next-line no-unused-expressions
158
- err ? /* istanbul ignore next */ reject(err) : resolve(response);
159
- }
160
- );
161
- });
120
+ return this.client.bulk({ body: this.constructBulkIndex(bodies) });
162
121
  }
163
122
 
164
123
  create(id, body) {
@@ -169,12 +128,7 @@ class ElasticsearchService {
169
128
  body,
170
129
  };
171
130
 
172
- return new Promise((resolve, reject) => {
173
- this.client.index(params, (err, response) => {
174
- // eslint-disable-next-line no-unused-expressions
175
- err ? /* istanbul ignore next */ reject(err) : resolve(response);
176
- });
177
- });
131
+ return this.client.index(params);
178
132
  }
179
133
 
180
134
  updateById(id) {
@@ -184,12 +138,7 @@ class ElasticsearchService {
184
138
  id,
185
139
  };
186
140
 
187
- return new Promise((resolve, reject) => {
188
- this.client.get(params, (err, response) => {
189
- // eslint-disable-next-line no-unused-expressions
190
- err ? /* istanbul ignore next */ reject(err) : resolve(response);
191
- });
192
- });
141
+ return this.client.get(params);
193
142
  }
194
143
 
195
144
  /**
@@ -32,6 +32,8 @@ describe('test LengthAwarePaginator instantiate', () => {
32
32
  expect(await paginator.lastItem()).toMatchObject(mockDataLastItem);
33
33
  expect(paginator.perPage()).toEqual(5);
34
34
  expect(await paginator.total()).toEqual(30);
35
+
36
+ expect(db.select).toHaveBeenCalled();
35
37
  });
36
38
  it('should not throw exception when instantiating with current page', async () => {
37
39
  const paginator = new LengthAwarePaginator(
@@ -51,6 +53,8 @@ describe('test LengthAwarePaginator instantiate', () => {
51
53
  expect(await paginator.lastItem()).toMatchObject(mockDataLastItem);
52
54
  expect(paginator.perPage()).toEqual(5);
53
55
  expect(await paginator.total()).toEqual(30);
56
+
57
+ expect(db.select).toHaveBeenCalled();
54
58
  });
55
59
  it('should default perPage to 10 when instantiating without perPage', async () => {
56
60
  const paginator = new LengthAwarePaginator(
@@ -66,6 +70,8 @@ describe('test LengthAwarePaginator instantiate', () => {
66
70
  expect(paginator.currentPage()).toEqual(1);
67
71
  expect(paginator.perPage()).toEqual(10);
68
72
  expect(await paginator.total()).toEqual(30);
73
+
74
+ expect(db.select).toHaveBeenCalled();
69
75
  });
70
76
  it('should throw exception if total is not a number', async () => {
71
77
  try {
@@ -116,6 +122,43 @@ describe('test LengthAwarePaginator instantiate', () => {
116
122
  { ...mockDataLastItem },
117
123
  ],
118
124
  });
125
+
126
+ expect(db.select).toHaveBeenCalled();
127
+ });
128
+
129
+ it('should simply return an empty paginator object if total is explicitly zero', async () => {
130
+ const paginator = new LengthAwarePaginator(
131
+ db,
132
+ 'SELECT * FROM tests',
133
+ {},
134
+ {
135
+ perPage: 5,
136
+ currentPage: 1,
137
+ total: 0,
138
+ }
139
+ );
140
+
141
+ expect(await paginator.count()).toEqual(0);
142
+ expect(await paginator.previousPage()).toEqual(false);
143
+ expect(paginator.currentPage()).toEqual(1);
144
+ expect(await paginator.nextPage()).toEqual(false);
145
+ expect(await paginator.firstItem()).toBe(undefined);
146
+ expect(await paginator.lastItem()).toBe(undefined);
147
+ expect(paginator.perPage()).toEqual(5);
148
+ expect(await paginator.total()).toEqual(0);
149
+
150
+ expect(await paginator.toObject()).toMatchObject({
151
+ count: 0,
152
+ previous_page: false,
153
+ current_page: 1,
154
+ next_page: false,
155
+ per_page: 5,
156
+ last_page: 0,
157
+ total: 0,
158
+ items: [],
159
+ });
160
+
161
+ expect(db.select).not.toHaveBeenCalled();
119
162
  });
120
163
  });
121
164
 
@@ -138,7 +181,7 @@ describe('test total() usage', () => {
138
181
  });
139
182
 
140
183
  describe('test lastPage() usage', () => {
141
- it('should get the last page using supplied paramater as total data', async () => {
184
+ it('should get the last page using supplied parameter as total data', async () => {
142
185
  const paginator1 = new LengthAwarePaginator(
143
186
  db,
144
187
  'SELECT * FROM total_tests',
@@ -217,11 +217,17 @@ export default class Paginator {
217
217
  }
218
218
 
219
219
  async executeQuery() {
220
- this.response = await this.dbProp.select(
221
- this.generatePaginationSqlSnippet(),
222
- this.sqlParamsProp,
223
- this.connection
224
- );
220
+ const total = this.totalProp;
221
+ if (
222
+ (typeof total === 'number' && total > 0) ||
223
+ (typeof total !== 'number' && !total)
224
+ ) {
225
+ this.response = await this.dbProp.select(
226
+ this.generatePaginationSqlSnippet(),
227
+ this.sqlParamsProp,
228
+ this.connection
229
+ );
230
+ }
225
231
 
226
232
  this.hasNext = this.response.length > this.perPage();
227
233
  if (this.hasNext) {