@xube/kit-aws 0.0.70 → 0.0.71

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.
@@ -1,7 +1,25 @@
1
1
  import { XubeResponse } from "@xube/kit-request";
2
2
  import { XubeLog } from "@xube/kit-log";
3
- import { PartialTableKey, TableItem } from "@xube/kit-aws-schema";
4
- export declare const queryItemsFromTable: (tableName: string, keys: PartialTableKey, indexName: string | undefined, keyLabels?: {
3
+ import { TableItem } from "@xube/kit-aws-schema";
4
+ type PartialTableKey = {
5
+ PK: string;
6
+ SK?: string;
7
+ };
8
+ export declare const queryItemsFromTableBetween: (tableName: string, keys: {
9
+ PK: string;
10
+ SKstart: string;
11
+ SKend: string;
12
+ }, indexName?: string, keyLabels?: {
5
13
  partitionKey: string;
6
14
  sortKey?: string;
7
15
  }, log?: XubeLog) => Promise<XubeResponse<TableItem[]>>;
16
+ export declare const queryItemsFromTableBeginsWith: (tableName: string, keys: PartialTableKey, indexName: string | undefined, keyLabels?: {
17
+ partitionKey: string;
18
+ sortKey?: string;
19
+ }, log?: XubeLog) => Promise<XubeResponse<TableItem[]>>;
20
+ export declare const queryItemsFromTableUsingPKOnly: (tableName: string, keys: {
21
+ PK: string;
22
+ }, indexName: string | undefined, keyLabels?: {
23
+ partitionKey: string;
24
+ }, log?: XubeLog) => Promise<XubeResponse<TableItem[]>>;
25
+ export {};
@@ -1,52 +1,56 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.queryItemsFromTable = void 0;
3
+ exports.queryItemsFromTableUsingPKOnly = exports.queryItemsFromTableBeginsWith = exports.queryItemsFromTableBetween = void 0;
4
4
  const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
5
5
  const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
6
6
  const kit_request_1 = require("@xube/kit-request");
7
7
  const kit_log_1 = require("@xube/kit-log");
8
8
  const kit_aws_schema_1 = require("@xube/kit-aws-schema");
9
+ const kit_constants_1 = require("@xube/kit-constants");
9
10
  const ddbClient = new client_dynamodb_1.DynamoDBClient({ region: process.env.AWS_REGION });
10
11
  const ddbDocClient = lib_dynamodb_1.DynamoDBDocumentClient.from(ddbClient);
11
- const queryItemsFromTable = async (tableName, keys, indexName, keyLabels = {
12
- partitionKey: kit_aws_schema_1.PARTITION_KEY,
13
- sortKey: kit_aws_schema_1.SORT_KEY,
14
- }, log = kit_log_1.XubeLog.getInstance()) => {
15
- let KeyConditionExpression = "#pk = :pkValue";
16
- let ExpressionAttributeNames = {
12
+ const buildQueryParams = (tableName, keys, indexName, keyLabels, condition) => {
13
+ const ExpressionAttributeNames = {
17
14
  "#pk": keyLabels.partitionKey,
18
15
  };
19
- let ExpressionAttributeValues = {
20
- ":pkValue": keys[kit_aws_schema_1.PARTITION_KEY],
16
+ const ExpressionAttributeValues = {
17
+ ":pkValue": keys.PK,
21
18
  };
22
- if (keys[kit_aws_schema_1.SORT_KEY]) {
23
- KeyConditionExpression += " AND begins_with(#sk, :skPrefix)";
24
- ExpressionAttributeNames["#sk"] = keyLabels.sortKey ?? kit_aws_schema_1.SORT_KEY;
25
- ExpressionAttributeValues[":skPrefix"] = keys[kit_aws_schema_1.SORT_KEY];
19
+ let conditionExpression = "";
20
+ let additionalExpressionValues = {};
21
+ if (condition) {
22
+ const conditionResult = condition(ExpressionAttributeNames);
23
+ conditionExpression = conditionResult.conditionExpression;
24
+ additionalExpressionValues = conditionResult.expressionValues;
26
25
  }
27
- const params = {
26
+ return {
28
27
  TableName: tableName,
29
28
  IndexName: indexName,
30
- KeyConditionExpression,
29
+ KeyConditionExpression: `#pk = :pkValue ${conditionExpression}`.trim(),
31
30
  ExpressionAttributeNames,
32
- ExpressionAttributeValues,
31
+ ExpressionAttributeValues: {
32
+ ...ExpressionAttributeValues,
33
+ ...additionalExpressionValues,
34
+ },
33
35
  };
36
+ };
37
+ const executeQuery = async (params) => {
34
38
  try {
35
39
  const response = await ddbDocClient.send(new lib_dynamodb_1.QueryCommand(params));
36
40
  if (!response.Items || !response.Items.length) {
37
41
  return new kit_request_1.XubeResponse({
38
- statusCode: 404,
42
+ statusCode: kit_constants_1.StatusCode.NotFound,
39
43
  error: `No items found.`,
40
44
  });
41
45
  }
42
46
  if (!(0, kit_aws_schema_1.areTableItems)(response.Items)) {
43
47
  return new kit_request_1.XubeResponse({
44
- statusCode: 500,
48
+ statusCode: kit_constants_1.StatusCode.InternalError,
45
49
  error: `Items returned from DynamoDB are not valid TableItems.`,
46
50
  });
47
51
  }
48
52
  return new kit_request_1.XubeResponse({
49
- statusCode: 200,
53
+ statusCode: kit_constants_1.StatusCode.OK,
50
54
  data: response.Items,
51
55
  });
52
56
  }
@@ -55,4 +59,45 @@ const queryItemsFromTable = async (tableName, keys, indexName, keyLabels = {
55
59
  throw error;
56
60
  }
57
61
  };
58
- exports.queryItemsFromTable = queryItemsFromTable;
62
+ const queryItemsFromTableBetween = async (tableName, keys, indexName, keyLabels = {
63
+ partitionKey: kit_aws_schema_1.PARTITION_KEY,
64
+ sortKey: kit_aws_schema_1.SORT_KEY,
65
+ }, log = kit_log_1.XubeLog.getInstance()) => {
66
+ const params = buildQueryParams(tableName, keys, indexName, keyLabels, (ExpressionAttributeNames) => {
67
+ ExpressionAttributeNames["#sk"] = keyLabels.sortKey ?? kit_aws_schema_1.SORT_KEY;
68
+ return {
69
+ conditionExpression: "AND #sk BETWEEN :skStart AND :skEnd",
70
+ expressionValues: { ":skStart": keys.SKstart, ":skEnd": keys.SKend },
71
+ };
72
+ });
73
+ return executeQuery(params);
74
+ };
75
+ exports.queryItemsFromTableBetween = queryItemsFromTableBetween;
76
+ const queryItemsFromTableBeginsWith = async (tableName, keys, indexName, keyLabels = {
77
+ partitionKey: kit_aws_schema_1.PARTITION_KEY,
78
+ sortKey: kit_aws_schema_1.SORT_KEY,
79
+ }, log = kit_log_1.XubeLog.getInstance()) => {
80
+ let condition;
81
+ if (keys.SK) {
82
+ condition = (ExpressionAttributeNames) => {
83
+ ExpressionAttributeNames["#sk"] = keyLabels.sortKey ?? kit_aws_schema_1.SORT_KEY;
84
+ return {
85
+ conditionExpression: "AND begins_with(#sk, :skPrefix)",
86
+ expressionValues: { ":skPrefix": keys.SK },
87
+ };
88
+ };
89
+ }
90
+ else {
91
+ condition = undefined;
92
+ }
93
+ const params = buildQueryParams(tableName, keys, indexName, keyLabels, condition);
94
+ return executeQuery(params);
95
+ };
96
+ exports.queryItemsFromTableBeginsWith = queryItemsFromTableBeginsWith;
97
+ const queryItemsFromTableUsingPKOnly = async (tableName, keys, indexName, keyLabels = {
98
+ partitionKey: kit_aws_schema_1.PARTITION_KEY,
99
+ }, log = kit_log_1.XubeLog.getInstance()) => {
100
+ const params = buildQueryParams(tableName, keys, indexName, keyLabels);
101
+ return executeQuery(params);
102
+ };
103
+ exports.queryItemsFromTableUsingPKOnly = queryItemsFromTableUsingPKOnly;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xube/kit-aws",
3
- "version": "0.0.70",
3
+ "version": "0.0.71",
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.70"
21
+ "@xube/kit-build": "^0.0.71"
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.70",
30
- "@xube/kit-log": "^0.0.70",
31
- "@xube/kit-request": "^0.0.70",
32
- "@xube/kit-schema": "^0.0.70",
29
+ "@xube/kit-aws-schema": "^0.0.71",
30
+ "@xube/kit-log": "^0.0.71",
31
+ "@xube/kit-request": "^0.0.71",
32
+ "@xube/kit-schema": "^0.0.71",
33
33
  "zod": "^3.22.4"
34
34
  }
35
35
  }
@@ -2,65 +2,97 @@ import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
2
2
  import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
3
3
  import { XubeResponse } from "@xube/kit-request";
4
4
  import { XubeLog } from "@xube/kit-log";
5
- import { PARTITION_KEY, PartialTableKey, SORT_KEY, TableItem, areTableItems } from "@xube/kit-aws-schema";
5
+ import {
6
+ PARTITION_KEY,
7
+ SORT_KEY,
8
+ TableItem,
9
+ areTableItems,
10
+ } from "@xube/kit-aws-schema";
11
+ import { StatusCode } from "@xube/kit-constants";
6
12
 
7
13
  const ddbClient = new DynamoDBClient({ region: process.env.AWS_REGION });
8
14
  const ddbDocClient = DynamoDBDocumentClient.from(ddbClient);
15
+ type DynamoDbParams = {
16
+ TableName: string;
17
+ IndexName?: string;
18
+ KeyConditionExpression: string;
19
+ ExpressionAttributeNames: { [key: string]: string };
20
+ ExpressionAttributeValues: { [key: string]: string };
21
+ };
22
+
23
+ type ConditionReturn = {
24
+ conditionExpression: string;
25
+ expressionValues: { [key: string]: string };
26
+ };
27
+
28
+ type PartialTableKey = {
29
+ PK: string;
30
+ SK?: string;
31
+ };
9
32
 
10
- export const queryItemsFromTable = async (
33
+ const buildQueryParams = (
11
34
  tableName: string,
12
35
  keys: PartialTableKey,
13
36
  indexName: string | undefined,
14
37
  keyLabels: {
15
38
  partitionKey: string;
16
39
  sortKey?: string;
17
- } = {
18
- partitionKey: PARTITION_KEY,
19
- sortKey: SORT_KEY,
20
40
  },
21
- log: XubeLog = XubeLog.getInstance()
22
- ): Promise<XubeResponse<TableItem[]>> => {
23
- let KeyConditionExpression = "#pk = :pkValue";
24
- let ExpressionAttributeNames: { [key: string]: string } = {
41
+ condition?: (ExpressionAttributeNames: {
42
+ [key: string]: string;
43
+ }) => ConditionReturn
44
+ ): DynamoDbParams => {
45
+ const ExpressionAttributeNames: { [key: string]: string } = {
25
46
  "#pk": keyLabels.partitionKey,
26
47
  };
27
- let ExpressionAttributeValues: { [key: string]: string } = {
28
- ":pkValue": keys[PARTITION_KEY],
48
+
49
+ const ExpressionAttributeValues: { [key: string]: string } = {
50
+ ":pkValue": keys.PK,
29
51
  };
30
52
 
31
- if (keys[SORT_KEY]) {
32
- KeyConditionExpression += " AND begins_with(#sk, :skPrefix)";
33
- ExpressionAttributeNames["#sk"] = keyLabels.sortKey ?? SORT_KEY;
34
- ExpressionAttributeValues[":skPrefix"] = keys[SORT_KEY];
53
+ let conditionExpression = "";
54
+ let additionalExpressionValues: { [key: string]: string } = {};
55
+
56
+ if (condition) {
57
+ const conditionResult = condition(ExpressionAttributeNames);
58
+ conditionExpression = conditionResult.conditionExpression;
59
+ additionalExpressionValues = conditionResult.expressionValues;
35
60
  }
36
61
 
37
- const params = {
62
+ return {
38
63
  TableName: tableName,
39
64
  IndexName: indexName,
40
- KeyConditionExpression,
65
+ KeyConditionExpression: `#pk = :pkValue ${conditionExpression}`.trim(),
41
66
  ExpressionAttributeNames,
42
- ExpressionAttributeValues,
67
+ ExpressionAttributeValues: {
68
+ ...ExpressionAttributeValues,
69
+ ...additionalExpressionValues,
70
+ },
43
71
  };
72
+ };
44
73
 
74
+ const executeQuery = async (
75
+ params: DynamoDbParams
76
+ ): Promise<XubeResponse<TableItem[]>> => {
45
77
  try {
46
78
  const response = await ddbDocClient.send(new QueryCommand(params));
47
79
 
48
80
  if (!response.Items || !response.Items.length) {
49
81
  return new XubeResponse({
50
- statusCode: 404,
82
+ statusCode: StatusCode.NotFound,
51
83
  error: `No items found.`,
52
84
  });
53
85
  }
54
86
 
55
87
  if (!areTableItems(response.Items)) {
56
88
  return new XubeResponse({
57
- statusCode: 500,
89
+ statusCode: StatusCode.InternalError,
58
90
  error: `Items returned from DynamoDB are not valid TableItems.`,
59
91
  });
60
92
  }
61
93
 
62
94
  return new XubeResponse({
63
- statusCode: 200,
95
+ statusCode: StatusCode.OK,
64
96
  data: response.Items,
65
97
  });
66
98
  } catch (error) {
@@ -68,3 +100,90 @@ export const queryItemsFromTable = async (
68
100
  throw error;
69
101
  }
70
102
  };
103
+
104
+ export const queryItemsFromTableBetween = async (
105
+ tableName: string,
106
+ keys: {
107
+ PK: string;
108
+ SKstart: string;
109
+ SKend: string;
110
+ },
111
+ indexName?: string,
112
+ keyLabels: {
113
+ partitionKey: string;
114
+ sortKey?: string;
115
+ } = {
116
+ partitionKey: PARTITION_KEY,
117
+ sortKey: SORT_KEY,
118
+ },
119
+ log: XubeLog = XubeLog.getInstance()
120
+ ): Promise<XubeResponse<TableItem[]>> => {
121
+ const params = buildQueryParams(
122
+ tableName,
123
+ keys,
124
+ indexName,
125
+ keyLabels,
126
+ (ExpressionAttributeNames) => {
127
+ ExpressionAttributeNames["#sk"] = keyLabels.sortKey ?? SORT_KEY;
128
+ return {
129
+ conditionExpression: "AND #sk BETWEEN :skStart AND :skEnd",
130
+ expressionValues: { ":skStart": keys.SKstart, ":skEnd": keys.SKend },
131
+ };
132
+ }
133
+ );
134
+
135
+ return executeQuery(params);
136
+ };
137
+
138
+ export const queryItemsFromTableBeginsWith = async (
139
+ tableName: string,
140
+ keys: PartialTableKey,
141
+ indexName: string | undefined,
142
+ keyLabels: {
143
+ partitionKey: string;
144
+ sortKey?: string;
145
+ } = {
146
+ partitionKey: PARTITION_KEY,
147
+ sortKey: SORT_KEY,
148
+ },
149
+ log: XubeLog = XubeLog.getInstance()
150
+ ): Promise<XubeResponse<TableItem[]>> => {
151
+ let condition;
152
+
153
+ if (keys.SK) {
154
+ condition = (ExpressionAttributeNames) => {
155
+ ExpressionAttributeNames["#sk"] = keyLabels.sortKey ?? SORT_KEY;
156
+ return {
157
+ conditionExpression: "AND begins_with(#sk, :skPrefix)",
158
+ expressionValues: { ":skPrefix": keys.SK },
159
+ };
160
+ };
161
+ } else {
162
+ condition = undefined;
163
+ }
164
+
165
+ const params = buildQueryParams(
166
+ tableName,
167
+ keys,
168
+ indexName,
169
+ keyLabels,
170
+ condition
171
+ );
172
+
173
+ return executeQuery(params);
174
+ };
175
+
176
+ export const queryItemsFromTableUsingPKOnly = async (
177
+ tableName: string,
178
+ keys: { PK: string },
179
+ indexName: string | undefined,
180
+ keyLabels: {
181
+ partitionKey: string;
182
+ } = {
183
+ partitionKey: PARTITION_KEY,
184
+ },
185
+ log: XubeLog = XubeLog.getInstance()
186
+ ): Promise<XubeResponse<TableItem[]>> => {
187
+ const params = buildQueryParams(tableName, keys, indexName, keyLabels);
188
+ return executeQuery(params);
189
+ };