@pi-r/aws-v3 0.6.0 → 0.6.1

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.
package/client/index.js CHANGED
@@ -213,40 +213,42 @@ async function executeQuery(credential, data, sessionKey) {
213
213
  }
214
214
  exports.executeQuery = executeQuery;
215
215
  async function executeBatchQuery(credential, batch, sessionKey) {
216
+ var _a;
216
217
  const length = batch.length;
217
218
  const result = new Array(length);
218
219
  const caching = length > 0 && this.hasCache(batch[0].service, sessionKey);
219
220
  const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
220
221
  let client;
221
- const createClient = () => client || (client = createDatabaseClient.call(this, length === 1 ? credential : { ...credential }));
222
+ const createClient = () => client || (client = createDatabaseClient.call(this, credential));
222
223
  const closeClient = () => client?.[0].destroy();
224
+ (0, aws_1.setDatabaseEndpoint)(credential);
223
225
  for (let i = 0; i < length; ++i) {
224
226
  const item = batch[i];
225
- const { service, table, id = '', query, partitionKey, key = partitionKey, limit = 0, update, ignoreCache } = item;
226
- if (!table) {
227
- closeClient();
228
- throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
229
- }
230
- (0, aws_1.setDatabaseEndpoint)(credential);
231
- const renewCache = ignoreCache === 0;
227
+ let { service, table: TableName, id, query, partitionKey, key = partitionKey, limit = 0, update, ignoreCache } = item;
228
+ const useCache = caching && ignoreCache !== true;
232
229
  const getCache = (value) => {
233
230
  if (ignoreCache !== 1) {
234
- cacheValue.renewCache = renewCache;
231
+ cacheValue.renewCache = ignoreCache === 0;
235
232
  return this.getQueryResult(service, credential, value, cacheValue);
236
233
  }
237
234
  };
238
- let rows, queryString = caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache ? table + '_' : '';
235
+ cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
236
+ let rows, queryString = '';
239
237
  if (key && (id || (0, types_1.isPlainObject)(key))) {
240
- if (queryString) {
241
- queryString += Module.asString(key, true) + id;
238
+ if (!TableName) {
239
+ closeClient();
240
+ throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
241
+ }
242
+ if (useCache) {
243
+ queryString = TableName + '_' + Module.asString(key, true) + (id !== undefined ? '_' + Module.asString(id, true) : '');
242
244
  if (!update && (rows = getCache(queryString))) {
243
245
  result[i] = rows;
244
246
  continue;
245
247
  }
246
248
  }
247
249
  const [db, AWS] = createClient();
248
- const Key = (0, types_1.isPlainObject)(key) ? key : { [key]: id };
249
- const command = { TableName: table, Key };
250
+ const Key = (0, types_1.isPlainObject)(key) ? key : { [key]: (0, aws_1.parseAttributeValue)(id) };
251
+ const command = { TableName, Key };
250
252
  if (update) {
251
253
  await db.send(new AWS.UpdateCommand({ ...command, ...update }));
252
254
  }
@@ -256,18 +258,65 @@ async function executeBatchQuery(credential, batch, sessionKey) {
256
258
  }
257
259
  }
258
260
  else if ((0, types_1.isPlainObject)(query)) {
259
- if (queryString && (rows = getCache(queryString += Module.asString(query, true) + limit))) {
260
- result[i] = rows;
261
- continue;
261
+ if (TableName) {
262
+ query.TableName = TableName;
262
263
  }
263
- query.TableName = table;
264
264
  if (limit > 0) {
265
265
  query.Limit = limit;
266
266
  }
267
+ if (!query.TableName) {
268
+ closeClient();
269
+ throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
270
+ }
271
+ if (useCache && (rows = getCache(queryString = Module.asString(query, true)))) {
272
+ result[i] = rows;
273
+ continue;
274
+ }
275
+ const [db, AWS] = createClient();
276
+ const { Count, Items } = await db.send(new AWS.QueryCommand(query));
277
+ if (Count && Items) {
278
+ rows = Items;
279
+ }
280
+ }
281
+ else if ((0, types_1.isArray)(query)) {
282
+ let params = (item.params || {});
283
+ if (!(0, types_1.isPlainObject)(params.RequestItems)) {
284
+ params.RequestItems = {};
285
+ }
286
+ TableName || (TableName = Object.keys(params.RequestItems)[0]);
287
+ if (!TableName) {
288
+ throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
289
+ }
290
+ // @ts-ignore
291
+ const Item = (_a = params.RequestItems)[TableName] || (_a[TableName] = {});
292
+ Item.Keys = query;
293
+ params = { RequestItems: { [TableName]: Item } };
294
+ if (useCache && (rows = getCache(queryString = Module.asString(params, true)))) {
295
+ result[i] = rows;
296
+ continue;
297
+ }
298
+ const [db, AWS] = createClient();
299
+ const { Responses } = await db.send(new AWS.BatchGetCommand(params));
300
+ if (Responses) {
301
+ rows = Responses[TableName];
302
+ }
303
+ }
304
+ else if (TableName) {
305
+ let params = item.params;
306
+ if ((0, types_1.isPlainObject)(params)) {
307
+ params.TableName = TableName;
308
+ }
309
+ else {
310
+ params = { TableName };
311
+ }
312
+ if (useCache && (rows = getCache(queryString = Module.asString(params, true)))) {
313
+ result[i] = rows;
314
+ continue;
315
+ }
267
316
  const [db, AWS] = createClient();
268
- const output = await db.send(new AWS.QueryCommand(query));
269
- if (output.Count && output.Items) {
270
- rows = output.Items;
317
+ const { Count, Items } = await db.send(new AWS.ScanCommand(params));
318
+ if (Count && Items) {
319
+ rows = Items;
271
320
  }
272
321
  }
273
322
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/aws-v3",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "AWS V3 cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -20,12 +20,12 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/cloud": "^0.8.0",
24
- "@e-mc/module": "^0.8.0",
25
- "@e-mc/types": "^0.8.0",
26
- "@pi-r/aws": "^0.6.0",
27
- "@aws-sdk/client-dynamodb": "^3.478.0",
28
- "@aws-sdk/client-s3": "^3.478.0",
29
- "@aws-sdk/lib-dynamodb": "^3.478.0"
23
+ "@e-mc/cloud": "^0.8.1",
24
+ "@e-mc/module": "^0.8.1",
25
+ "@e-mc/types": "^0.8.1",
26
+ "@pi-r/aws": "^0.6.1",
27
+ "@aws-sdk/client-dynamodb": "^3.485.0",
28
+ "@aws-sdk/client-s3": "^3.485.0",
29
+ "@aws-sdk/lib-dynamodb": "^3.485.0"
30
30
  }
31
31
  }
package/upload/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const path = require("path");
4
- const fs = require("fs");
5
4
  const stream = require("stream");
6
5
  const util_1 = require("@e-mc/cloud/util");
7
6
  const aws_1 = require("@pi-r/aws");
@@ -80,15 +79,10 @@ function upload(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
80
79
  const Body = [data.buffer];
81
80
  const ContentType = [contentType];
82
81
  if (fileGroup) {
83
- for (const [content, ext, localFile] of fileGroup) {
84
- try {
85
- Body.push(typeof content === 'string' ? fs.readFileSync(content) : content);
86
- Key.push(ext === '.map' && localFile ? path.basename(localFile) : filename + ext);
87
- }
88
- catch (err) {
89
- addLog(err);
90
- }
91
- }
82
+ const [key, body, type] = (0, util_1.createKeyAndBody)(filename, fileGroup, addLog);
83
+ Key.push(...key);
84
+ Body.push(...body);
85
+ ContentType.push(...type);
92
86
  }
93
87
  for (let i = 0; i < Key.length; ++i) {
94
88
  const first = i === 0;