@xube/kit-aws 0.0.85 → 0.0.86

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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./resources/cognito/authenticate";
2
2
  export * from "./resources/cognito/register";
3
+ export * from "./resources/dynamodb/batch";
3
4
  export * from "./resources/dynamodb/get";
4
5
  export * from "./resources/dynamodb/put";
5
6
  export * from "./resources/dynamodb/query";
package/dist/index.js CHANGED
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./resources/cognito/authenticate"), exports);
18
18
  __exportStar(require("./resources/cognito/register"), exports);
19
+ __exportStar(require("./resources/dynamodb/batch"), exports);
19
20
  __exportStar(require("./resources/dynamodb/get"), exports);
20
21
  __exportStar(require("./resources/dynamodb/put"), exports);
21
22
  __exportStar(require("./resources/dynamodb/query"), exports);
@@ -0,0 +1,16 @@
1
+ import { WriteRequest } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
3
+ import { TableKey } from "@xube/kit-aws-schema";
4
+ export interface BatchPutPropsOutput {
5
+ success: boolean;
6
+ unprocessed?: Record<string, WriteRequest[]>;
7
+ }
8
+ export type UnprocessedItems = TableKey[];
9
+ export declare const putItems: <T extends {
10
+ id: string;
11
+ PK: string;
12
+ SK: string;
13
+ type: string;
14
+ s: number;
15
+ us?: number | undefined;
16
+ }>(tableName: string, items: T[], ddbDocClient?: DynamoDBDocumentClient) => Promise<BatchPutPropsOutput>;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.putItems = void 0;
4
+ const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
5
+ const client_1 = require("./client");
6
+ const kit_aws_schema_1 = require("@xube/kit-aws-schema");
7
+ const putItems = async (tableName, items, ddbDocClient = (0, client_1.getDynamodbClient)()) => {
8
+ let unprocessedItems = {};
9
+ try {
10
+ for (let i = 0; i < items.length; i += kit_aws_schema_1.MAX_NUMBER_BATCH_WRITES) {
11
+ const currentBatchPutItemsBatch = items.slice(i, Math.min(kit_aws_schema_1.MAX_NUMBER_BATCH_WRITES, items.length));
12
+ const batchPutItemsCommand = new lib_dynamodb_1.BatchWriteCommand({
13
+ RequestItems: {
14
+ [tableName]: currentBatchPutItemsBatch.map((currentBatchPutItemProps) => ({
15
+ PutRequest: {
16
+ Item: {
17
+ ...currentBatchPutItemProps,
18
+ },
19
+ },
20
+ })),
21
+ },
22
+ });
23
+ const batchPutItemsCommandOutput = await ddbDocClient.send(batchPutItemsCommand);
24
+ if (batchPutItemsCommandOutput.UnprocessedItems &&
25
+ Object.keys(batchPutItemsCommandOutput.UnprocessedItems).length) {
26
+ if (batchPutItemsCommandOutput.UnprocessedItems &&
27
+ Object.keys(batchPutItemsCommandOutput).length) {
28
+ unprocessedItems = {
29
+ ...unprocessedItems,
30
+ ...batchPutItemsCommandOutput.UnprocessedItems,
31
+ };
32
+ }
33
+ }
34
+ }
35
+ if (Object.keys(unprocessedItems).length > 0) {
36
+ console.error(`There were ${Object.keys(unprocessedItems).length} unprocessed items when attempting to batch put items: ${JSON.stringify(unprocessedItems, null, 2)}`);
37
+ return {
38
+ success: false,
39
+ unprocessed: unprocessedItems,
40
+ };
41
+ }
42
+ return {
43
+ success: true,
44
+ };
45
+ }
46
+ catch (e) {
47
+ console.error("Error occurred when batch writing items. " + JSON.stringify(e));
48
+ return { success: false };
49
+ }
50
+ };
51
+ exports.putItems = putItems;
@@ -0,0 +1,4 @@
1
+ import { DynamoDBClientConfig } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
3
+ export declare const getDynamodbClientConfig: () => DynamoDBClientConfig;
4
+ export declare const getDynamodbClient: () => DynamoDBDocumentClient;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDynamodbClient = exports.getDynamodbClientConfig = void 0;
4
+ const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
5
+ const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
6
+ const getDynamodbClientConfig = () => ({
7
+ region: process.env.AWS_REGION,
8
+ });
9
+ exports.getDynamodbClientConfig = getDynamodbClientConfig;
10
+ const ddbClient = new client_dynamodb_1.DynamoDBClient((0, exports.getDynamodbClientConfig)());
11
+ const ddbDocClient = lib_dynamodb_1.DynamoDBDocumentClient.from(ddbClient, {
12
+ marshallOptions: {
13
+ removeUndefinedValues: true,
14
+ convertClassInstanceToMap: true,
15
+ },
16
+ });
17
+ const getDynamodbClient = () => {
18
+ return ddbDocClient;
19
+ };
20
+ exports.getDynamodbClient = getDynamodbClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xube/kit-aws",
3
- "version": "0.0.85",
3
+ "version": "0.0.86",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -18,7 +18,7 @@
18
18
  "homepage": "https://github.com/XubeLtd/dev-kit#readme",
19
19
  "devDependencies": {
20
20
  "@types/aws-lambda": "^8.10.119",
21
- "@xube/kit-build": "^0.0.85"
21
+ "@xube/kit-build": "^0.0.86"
22
22
  },
23
23
  "dependencies": {
24
24
  "@aws-sdk/client-cognito-identity-provider": "^3.433.0",
@@ -26,10 +26,10 @@
26
26
  "@aws-sdk/client-ssm": "^3.433.0",
27
27
  "@aws-sdk/lib-dynamodb": "^3.433.0",
28
28
  "@aws-sdk/util-dynamodb": "^3.433.0",
29
- "@xube/kit-aws-schema": "^0.0.85",
30
- "@xube/kit-log": "^0.0.85",
31
- "@xube/kit-request": "^0.0.85",
32
- "@xube/kit-schema": "^0.0.85",
29
+ "@xube/kit-aws-schema": "^0.0.86",
30
+ "@xube/kit-log": "^0.0.86",
31
+ "@xube/kit-request": "^0.0.86",
32
+ "@xube/kit-schema": "^0.0.86",
33
33
  "zod": "^3.22.4"
34
34
  }
35
35
  }
package/src/index.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  export * from "./resources/cognito/authenticate";
2
2
  export * from "./resources/cognito/register";
3
+
4
+ export * from "./resources/dynamodb/batch";
3
5
  export * from "./resources/dynamodb/get";
4
6
  export * from "./resources/dynamodb/put";
5
7
  export * from "./resources/dynamodb/query";
6
8
  export * from "./resources/dynamodb/transform";
9
+
7
10
  export * from "./resources/parameter-store/get";
@@ -0,0 +1,90 @@
1
+ import { WriteRequest } from "@aws-sdk/client-dynamodb";
2
+ import {
3
+ DynamoDBDocumentClient,
4
+ BatchWriteCommand,
5
+ BatchWriteCommandOutput,
6
+ } from "@aws-sdk/lib-dynamodb";
7
+ import { getDynamodbClient } from "./client";
8
+ import {
9
+ MAX_NUMBER_BATCH_WRITES,
10
+ TableItem,
11
+ TableKey,
12
+ } from "@xube/kit-aws-schema";
13
+
14
+ export interface BatchPutPropsOutput {
15
+ success: boolean;
16
+ unprocessed?: Record<string, WriteRequest[]>;
17
+ }
18
+
19
+ export type UnprocessedItems = TableKey[];
20
+
21
+ export const putItems = async <T extends TableItem>(
22
+ tableName: string,
23
+ items: T[],
24
+ ddbDocClient: DynamoDBDocumentClient = getDynamodbClient()
25
+ ): Promise<BatchPutPropsOutput> => {
26
+ let unprocessedItems: Record<string, WriteRequest[]> = {};
27
+ try {
28
+ for (let i = 0; i < items.length; i += MAX_NUMBER_BATCH_WRITES) {
29
+ const currentBatchPutItemsBatch: T[] = items.slice(
30
+ i,
31
+ Math.min(MAX_NUMBER_BATCH_WRITES, items.length)
32
+ );
33
+
34
+ const batchPutItemsCommand = new BatchWriteCommand({
35
+ RequestItems: {
36
+ [tableName]: currentBatchPutItemsBatch.map(
37
+ (currentBatchPutItemProps) => ({
38
+ PutRequest: {
39
+ Item: {
40
+ ...currentBatchPutItemProps,
41
+ },
42
+ },
43
+ })
44
+ ),
45
+ },
46
+ });
47
+
48
+ const batchPutItemsCommandOutput: BatchWriteCommandOutput =
49
+ await ddbDocClient.send(batchPutItemsCommand);
50
+
51
+ if (
52
+ batchPutItemsCommandOutput.UnprocessedItems &&
53
+ Object.keys(batchPutItemsCommandOutput.UnprocessedItems).length
54
+ ) {
55
+ if (
56
+ batchPutItemsCommandOutput.UnprocessedItems &&
57
+ Object.keys(batchPutItemsCommandOutput).length
58
+ ) {
59
+ unprocessedItems = {
60
+ ...unprocessedItems,
61
+ ...batchPutItemsCommandOutput.UnprocessedItems,
62
+ };
63
+ }
64
+ }
65
+ }
66
+
67
+ if (Object.keys(unprocessedItems).length > 0) {
68
+ console.error(
69
+ `There were ${Object.keys(unprocessedItems).length} unprocessed items when attempting to batch put items: ${JSON.stringify(
70
+ unprocessedItems,
71
+ null,
72
+ 2
73
+ )}`
74
+ );
75
+ return {
76
+ success: false,
77
+ unprocessed: unprocessedItems,
78
+ };
79
+ }
80
+
81
+ return {
82
+ success: true,
83
+ };
84
+ } catch (e) {
85
+ console.error(
86
+ "Error occurred when batch writing items. " + JSON.stringify(e)
87
+ );
88
+ return { success: false };
89
+ }
90
+ };
@@ -0,0 +1,17 @@
1
+ import { DynamoDBClient, DynamoDBClientConfig } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
3
+
4
+ export const getDynamodbClientConfig = (): DynamoDBClientConfig => ({
5
+ region: process.env.AWS_REGION,
6
+ });
7
+
8
+ const ddbClient = new DynamoDBClient(getDynamodbClientConfig());
9
+ const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, {
10
+ marshallOptions: {
11
+ removeUndefinedValues: true,
12
+ convertClassInstanceToMap: true,
13
+ },
14
+ });
15
+ export const getDynamodbClient = () => {
16
+ return ddbDocClient;
17
+ };