@xube/kit-aws 0.0.33 → 0.0.35
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 +2 -0
- package/dist/index.js +2 -0
- package/dist/resources/dynamodb/query.d.ts +5 -0
- package/dist/resources/dynamodb/query.js +56 -0
- package/dist/resources/dynamodb/schema/item.d.ts +11 -1
- package/dist/resources/dynamodb/schema/item.js +11 -5
- package/dist/resources/dynamodb/schema/keys.d.ts +24 -0
- package/dist/resources/dynamodb/schema/keys.js +14 -1
- package/dist/resources/dynamodb/transform.d.ts +1 -0
- package/dist/resources/dynamodb/transform.js +8 -0
- package/package.json +5 -4
- package/src/index.ts +6 -4
- package/src/resources/dynamodb/query.ts +69 -0
- package/src/resources/dynamodb/schema/item.ts +13 -7
- package/src/resources/dynamodb/schema/keys.ts +19 -1
- package/src/resources/dynamodb/transform.ts +5 -0
package/dist/index.d.ts
CHANGED
|
@@ -2,3 +2,5 @@ export * from "./resources/dynamodb/get";
|
|
|
2
2
|
export * from "./resources/dynamodb/put";
|
|
3
3
|
export * from "./resources/dynamodb/schema/item";
|
|
4
4
|
export * from "./resources/dynamodb/schema/keys";
|
|
5
|
+
export * from "./resources/dynamodb/query";
|
|
6
|
+
export * from "./resources/dynamodb/transform";
|
package/dist/index.js
CHANGED
|
@@ -18,3 +18,5 @@ __exportStar(require("./resources/dynamodb/get"), exports);
|
|
|
18
18
|
__exportStar(require("./resources/dynamodb/put"), exports);
|
|
19
19
|
__exportStar(require("./resources/dynamodb/schema/item"), exports);
|
|
20
20
|
__exportStar(require("./resources/dynamodb/schema/keys"), exports);
|
|
21
|
+
__exportStar(require("./resources/dynamodb/query"), exports);
|
|
22
|
+
__exportStar(require("./resources/dynamodb/transform"), exports);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { TableItem } from "./schema/item";
|
|
2
|
+
import { PartialTableKey } from "./schema/keys";
|
|
3
|
+
import { XubeResponse } from "@xube/kit-request";
|
|
4
|
+
import { XubeLog } from "@xube/kit-log";
|
|
5
|
+
export declare const queryItemsFromTable: (tableName: string, keys: PartialTableKey, indexName: string | undefined, log?: XubeLog) => Promise<XubeResponse<TableItem[]>>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.queryItemsFromTable = void 0;
|
|
4
|
+
const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
|
|
5
|
+
const item_1 = require("./schema/item");
|
|
6
|
+
const keys_1 = require("./schema/keys");
|
|
7
|
+
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
8
|
+
const kit_request_1 = require("@xube/kit-request");
|
|
9
|
+
const kit_log_1 = require("@xube/kit-log");
|
|
10
|
+
const ddbClient = new client_dynamodb_1.DynamoDBClient({ region: process.env.AWS_REGION });
|
|
11
|
+
const ddbDocClient = lib_dynamodb_1.DynamoDBDocumentClient.from(ddbClient);
|
|
12
|
+
const queryItemsFromTable = async (tableName, keys, indexName, log = kit_log_1.XubeLog.getInstance()) => {
|
|
13
|
+
let KeyConditionExpression = "#pk = :pkValue";
|
|
14
|
+
let ExpressionAttributeNames = {
|
|
15
|
+
"#pk": keys_1.PARTITION_KEY,
|
|
16
|
+
};
|
|
17
|
+
let ExpressionAttributeValues = {
|
|
18
|
+
":pkValue": keys[keys_1.PARTITION_KEY],
|
|
19
|
+
};
|
|
20
|
+
if (keys[keys_1.SORT_KEY]) {
|
|
21
|
+
KeyConditionExpression += " AND begins_with(#sk, :skPrefix)";
|
|
22
|
+
ExpressionAttributeNames["#sk"] = keys_1.SORT_KEY;
|
|
23
|
+
ExpressionAttributeValues[":skPrefix"] = keys[keys_1.SORT_KEY];
|
|
24
|
+
}
|
|
25
|
+
const params = {
|
|
26
|
+
TableName: tableName,
|
|
27
|
+
IndexName: indexName,
|
|
28
|
+
KeyConditionExpression,
|
|
29
|
+
ExpressionAttributeNames,
|
|
30
|
+
ExpressionAttributeValues,
|
|
31
|
+
};
|
|
32
|
+
try {
|
|
33
|
+
const response = await ddbDocClient.send(new lib_dynamodb_1.QueryCommand(params));
|
|
34
|
+
if (!response.Items || !response.Items.length) {
|
|
35
|
+
return new kit_request_1.XubeResponse({
|
|
36
|
+
statusCode: 404,
|
|
37
|
+
error: `No items found.`,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (!(0, item_1.areTableItems)(response.Items)) {
|
|
41
|
+
return new kit_request_1.XubeResponse({
|
|
42
|
+
statusCode: 500,
|
|
43
|
+
error: `Items returned from DynamoDB are not valid TableItems.`,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return new kit_request_1.XubeResponse({
|
|
47
|
+
statusCode: 200,
|
|
48
|
+
data: response.Items,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error("Error querying DynamoDB:", error);
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.queryItemsFromTable = queryItemsFromTable;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
export declare const TableItemSchema: z.ZodObject<{
|
|
3
|
+
id: z.ZodString;
|
|
3
4
|
PK: z.ZodString;
|
|
4
5
|
SK: z.ZodString;
|
|
5
|
-
id: z.ZodString;
|
|
6
6
|
}, "strip", z.ZodTypeAny, {
|
|
7
7
|
id: string;
|
|
8
8
|
PK: string;
|
|
@@ -13,3 +13,13 @@ export declare const TableItemSchema: z.ZodObject<{
|
|
|
13
13
|
SK: string;
|
|
14
14
|
}>;
|
|
15
15
|
export type TableItem = z.infer<typeof TableItemSchema>;
|
|
16
|
+
export declare const isTableItem: (obj: unknown) => obj is {
|
|
17
|
+
id: string;
|
|
18
|
+
PK: string;
|
|
19
|
+
SK: string;
|
|
20
|
+
};
|
|
21
|
+
export declare const areTableItems: (obj: unknown) => obj is {
|
|
22
|
+
id: string;
|
|
23
|
+
PK: string;
|
|
24
|
+
SK: string;
|
|
25
|
+
}[];
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TableItemSchema = void 0;
|
|
3
|
+
exports.areTableItems = exports.isTableItem = exports.TableItemSchema = void 0;
|
|
4
4
|
const fields_1 = require("./fields");
|
|
5
5
|
const keys_1 = require("./keys");
|
|
6
6
|
const zod_1 = require("zod");
|
|
7
|
-
exports.TableItemSchema = zod_1.z
|
|
8
|
-
|
|
9
|
-
[keys_1.SORT_KEY]: zod_1.z.string(),
|
|
7
|
+
exports.TableItemSchema = zod_1.z
|
|
8
|
+
.object({
|
|
10
9
|
[fields_1.ID_FIELD]: zod_1.z.string(),
|
|
11
|
-
})
|
|
10
|
+
})
|
|
11
|
+
.merge(keys_1.TableKeySchema);
|
|
12
|
+
const isTableItem = (obj) => exports.TableItemSchema.passthrough().safeParse(obj).success;
|
|
13
|
+
exports.isTableItem = isTableItem;
|
|
14
|
+
const areTableItems = (obj) => {
|
|
15
|
+
return zod_1.z.array(exports.TableItemSchema.passthrough()).safeParse(obj).success;
|
|
16
|
+
};
|
|
17
|
+
exports.areTableItems = areTableItems;
|
|
@@ -1,2 +1,26 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
export declare const PARTITION_KEY = "PK";
|
|
2
3
|
export declare const SORT_KEY = "SK";
|
|
4
|
+
export declare const TableKeySchema: z.ZodObject<{
|
|
5
|
+
PK: z.ZodString;
|
|
6
|
+
SK: z.ZodString;
|
|
7
|
+
}, "strip", z.ZodTypeAny, {
|
|
8
|
+
PK: string;
|
|
9
|
+
SK: string;
|
|
10
|
+
}, {
|
|
11
|
+
PK: string;
|
|
12
|
+
SK: string;
|
|
13
|
+
}>;
|
|
14
|
+
export type TableKey = z.infer<typeof TableKeySchema>;
|
|
15
|
+
export declare const PartialTableKey: z.ZodObject<{
|
|
16
|
+
PK: z.ZodString;
|
|
17
|
+
SK: z.ZodOptional<z.ZodString>;
|
|
18
|
+
}, "strip", z.ZodTypeAny, {
|
|
19
|
+
PK: string;
|
|
20
|
+
SK?: string | undefined;
|
|
21
|
+
}, {
|
|
22
|
+
PK: string;
|
|
23
|
+
SK?: string | undefined;
|
|
24
|
+
}>;
|
|
25
|
+
export type PartialTableKey = z.infer<typeof PartialTableKey>;
|
|
26
|
+
export declare const getAggregateKeyFromTableKey: (tableKey: TableKey) => string;
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SORT_KEY = exports.PARTITION_KEY = void 0;
|
|
3
|
+
exports.getAggregateKeyFromTableKey = exports.PartialTableKey = exports.TableKeySchema = exports.SORT_KEY = exports.PARTITION_KEY = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
4
5
|
exports.PARTITION_KEY = "PK";
|
|
5
6
|
exports.SORT_KEY = "SK";
|
|
7
|
+
exports.TableKeySchema = zod_1.z.object({
|
|
8
|
+
[exports.PARTITION_KEY]: zod_1.z.string(),
|
|
9
|
+
[exports.SORT_KEY]: zod_1.z.string(),
|
|
10
|
+
});
|
|
11
|
+
exports.PartialTableKey = zod_1.z.object({
|
|
12
|
+
[exports.PARTITION_KEY]: zod_1.z.string(),
|
|
13
|
+
[exports.SORT_KEY]: zod_1.z.string().optional(),
|
|
14
|
+
});
|
|
15
|
+
const getAggregateKeyFromTableKey = (tableKey) => {
|
|
16
|
+
return `${tableKey[exports.PARTITION_KEY]}##${tableKey[exports.SORT_KEY]}`;
|
|
17
|
+
};
|
|
18
|
+
exports.getAggregateKeyFromTableKey = getAggregateKeyFromTableKey;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const unmarshallItem: (item: Record<string, any>) => Record<string, any>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unmarshallItem = void 0;
|
|
4
|
+
const util_dynamodb_1 = require("@aws-sdk/util-dynamodb");
|
|
5
|
+
const unmarshallItem = (item) => {
|
|
6
|
+
return (0, util_dynamodb_1.unmarshall)(item);
|
|
7
|
+
};
|
|
8
|
+
exports.unmarshallItem = unmarshallItem;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xube/kit-aws",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.35",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,13 +18,14 @@
|
|
|
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.
|
|
21
|
+
"@xube/kit-build": "^0.0.35"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@aws-sdk/client-dynamodb": "^3.427.0",
|
|
25
25
|
"@aws-sdk/lib-dynamodb": "^3.427.0",
|
|
26
|
-
"@
|
|
27
|
-
"@xube/kit-
|
|
26
|
+
"@aws-sdk/util-dynamodb": "^3.427.0",
|
|
27
|
+
"@xube/kit-log": "^0.0.35",
|
|
28
|
+
"@xube/kit-request": "^0.0.35",
|
|
28
29
|
"zod": "^3.22.4"
|
|
29
30
|
}
|
|
30
31
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export * from "./resources/dynamodb/get"
|
|
2
|
-
export * from "./resources/dynamodb/put"
|
|
3
|
-
export * from "./resources/dynamodb/schema/item"
|
|
4
|
-
export * from "./resources/dynamodb/schema/keys"
|
|
1
|
+
export * from "./resources/dynamodb/get";
|
|
2
|
+
export * from "./resources/dynamodb/put";
|
|
3
|
+
export * from "./resources/dynamodb/schema/item";
|
|
4
|
+
export * from "./resources/dynamodb/schema/keys";
|
|
5
|
+
export * from "./resources/dynamodb/query";
|
|
6
|
+
export * from "./resources/dynamodb/transform";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import { TableItem, areTableItems } from "./schema/item";
|
|
3
|
+
import {
|
|
4
|
+
PARTITION_KEY,
|
|
5
|
+
PartialTableKey,
|
|
6
|
+
SORT_KEY,
|
|
7
|
+
TableKey,
|
|
8
|
+
} from "./schema/keys";
|
|
9
|
+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
|
|
10
|
+
import { XubeResponse } from "@xube/kit-request";
|
|
11
|
+
import { XubeLog } from "@xube/kit-log";
|
|
12
|
+
|
|
13
|
+
const ddbClient = new DynamoDBClient({ region: process.env.AWS_REGION });
|
|
14
|
+
const ddbDocClient = DynamoDBDocumentClient.from(ddbClient);
|
|
15
|
+
|
|
16
|
+
export const queryItemsFromTable = async (
|
|
17
|
+
tableName: string,
|
|
18
|
+
keys: PartialTableKey,
|
|
19
|
+
indexName: string | undefined,
|
|
20
|
+
log: XubeLog = XubeLog.getInstance()
|
|
21
|
+
): Promise<XubeResponse<TableItem[]>> => {
|
|
22
|
+
let KeyConditionExpression = "#pk = :pkValue";
|
|
23
|
+
let ExpressionAttributeNames: { [key: string]: string } = {
|
|
24
|
+
"#pk": PARTITION_KEY,
|
|
25
|
+
};
|
|
26
|
+
let ExpressionAttributeValues: { [key: string]: string } = {
|
|
27
|
+
":pkValue": keys[PARTITION_KEY],
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
if (keys[SORT_KEY]) {
|
|
31
|
+
KeyConditionExpression += " AND begins_with(#sk, :skPrefix)";
|
|
32
|
+
ExpressionAttributeNames["#sk"] = SORT_KEY;
|
|
33
|
+
ExpressionAttributeValues[":skPrefix"] = keys[SORT_KEY];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const params = {
|
|
37
|
+
TableName: tableName,
|
|
38
|
+
IndexName: indexName,
|
|
39
|
+
KeyConditionExpression,
|
|
40
|
+
ExpressionAttributeNames,
|
|
41
|
+
ExpressionAttributeValues,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const response = await ddbDocClient.send(new QueryCommand(params));
|
|
46
|
+
|
|
47
|
+
if (!response.Items || !response.Items.length) {
|
|
48
|
+
return new XubeResponse({
|
|
49
|
+
statusCode: 404,
|
|
50
|
+
error: `No items found.`,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!areTableItems(response.Items)) {
|
|
55
|
+
return new XubeResponse({
|
|
56
|
+
statusCode: 500,
|
|
57
|
+
error: `Items returned from DynamoDB are not valid TableItems.`,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return new XubeResponse({
|
|
62
|
+
statusCode: 200,
|
|
63
|
+
data: response.Items,
|
|
64
|
+
});
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error("Error querying DynamoDB:", error);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { ID_FIELD } from "./fields";
|
|
2
|
-
import { PARTITION_KEY, SORT_KEY } from "./keys";
|
|
2
|
+
import { PARTITION_KEY, SORT_KEY, TableKeySchema } from "./keys";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
|
|
5
|
-
export const TableItemSchema = z
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export type TableItem = z.infer<typeof TableItemSchema>;
|
|
5
|
+
export const TableItemSchema = z
|
|
6
|
+
.object({
|
|
7
|
+
[ID_FIELD]: z.string(),
|
|
8
|
+
})
|
|
9
|
+
.merge(TableKeySchema);
|
|
10
|
+
export type TableItem = z.infer<typeof TableItemSchema>;
|
|
11
|
+
export const isTableItem = (obj: unknown): obj is TableItem =>
|
|
12
|
+
TableItemSchema.passthrough().safeParse(obj).success;
|
|
13
|
+
|
|
14
|
+
export const areTableItems = (obj: unknown): obj is TableItem[] => {
|
|
15
|
+
return z.array(TableItemSchema.passthrough()).safeParse(obj).success;
|
|
16
|
+
};
|
|
@@ -1,2 +1,20 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
1
3
|
export const PARTITION_KEY = "PK";
|
|
2
|
-
export const SORT_KEY = "SK";
|
|
4
|
+
export const SORT_KEY = "SK";
|
|
5
|
+
|
|
6
|
+
export const TableKeySchema = z.object({
|
|
7
|
+
[PARTITION_KEY]: z.string(),
|
|
8
|
+
[SORT_KEY]: z.string(),
|
|
9
|
+
});
|
|
10
|
+
export type TableKey = z.infer<typeof TableKeySchema>;
|
|
11
|
+
|
|
12
|
+
export const PartialTableKey = z.object({
|
|
13
|
+
[PARTITION_KEY]: z.string(),
|
|
14
|
+
[SORT_KEY]: z.string().optional(),
|
|
15
|
+
});
|
|
16
|
+
export type PartialTableKey = z.infer<typeof PartialTableKey>;
|
|
17
|
+
|
|
18
|
+
export const getAggregateKeyFromTableKey = (tableKey: TableKey) => {
|
|
19
|
+
return `${tableKey[PARTITION_KEY]}##${tableKey[SORT_KEY]}`;
|
|
20
|
+
};
|