@xube/kit-aws 0.0.22

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.
@@ -0,0 +1,4 @@
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";
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./resources/dynamodb/get"), exports);
18
+ __exportStar(require("./resources/dynamodb/put"), exports);
19
+ __exportStar(require("./resources/dynamodb/schema/item"), exports);
20
+ __exportStar(require("./resources/dynamodb/schema/keys"), exports);
@@ -0,0 +1,4 @@
1
+ export declare const getItemFromTable: (tableName: string, key: {
2
+ PK: string;
3
+ SK: string;
4
+ }) => Promise<Record<string, any> | undefined>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getItemFromTable = void 0;
4
+ const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
5
+ const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
6
+ const ddbClient = new client_dynamodb_1.DynamoDBClient({
7
+ region: process.env.AWS_REGION,
8
+ });
9
+ const ddbDocClient = lib_dynamodb_1.DynamoDBDocumentClient.from(ddbClient);
10
+ const getItemFromTable = async (tableName, key) => {
11
+ const params = {
12
+ TableName: tableName,
13
+ Key: key,
14
+ };
15
+ const result = await ddbDocClient.send(new lib_dynamodb_1.GetCommand(params));
16
+ return result.Item;
17
+ };
18
+ exports.getItemFromTable = getItemFromTable;
@@ -0,0 +1,7 @@
1
+ import { XubeResponse } from "@xube/kit-request";
2
+ import { XubeLog } from "@xube/kit-log";
3
+ export declare const putItemInTable: <TItem extends {
4
+ id: string;
5
+ PK: string;
6
+ SK: string;
7
+ }>(table: string, item: TItem, log?: XubeLog) => Promise<XubeResponse<boolean>>;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.putItemInTable = void 0;
4
+ const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
5
+ const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
6
+ const kit_request_1 = require("@xube/kit-request");
7
+ const kit_constants_1 = require("@xube/kit-constants");
8
+ const kit_log_1 = require("@xube/kit-log");
9
+ const ddbClient = new client_dynamodb_1.DynamoDBClient({
10
+ region: process.env.AWS_REGION,
11
+ });
12
+ const ddbDocClient = lib_dynamodb_1.DynamoDBDocumentClient.from(ddbClient);
13
+ const putItemInTable = async (table, item, log = kit_log_1.XubeLog.getInstance()) => {
14
+ const params = {
15
+ TableName: table,
16
+ Item: item,
17
+ };
18
+ const putItemCommand = new lib_dynamodb_1.PutCommand(params);
19
+ try {
20
+ const result = await ddbDocClient.send(putItemCommand);
21
+ return new kit_request_1.XubeResponse({
22
+ statusCode: kit_constants_1.StatusCode.OK,
23
+ data: true,
24
+ });
25
+ }
26
+ catch (e) {
27
+ log.error(`Error occurred when adding item to table.\n${JSON.stringify(e, null, 2)}`);
28
+ }
29
+ return new kit_request_1.XubeResponse({
30
+ statusCode: kit_constants_1.StatusCode.InternalError,
31
+ error: `Error occurred when adding item to table`,
32
+ });
33
+ };
34
+ exports.putItemInTable = putItemInTable;
@@ -0,0 +1 @@
1
+ export declare const ID_FIELD = "id";
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ID_FIELD = void 0;
4
+ exports.ID_FIELD = "id";
@@ -0,0 +1,15 @@
1
+ import { z } from "zod";
2
+ export declare const TableItemSchema: z.ZodObject<{
3
+ PK: z.ZodString;
4
+ SK: z.ZodString;
5
+ id: z.ZodString;
6
+ }, "strip", z.ZodTypeAny, {
7
+ id: string;
8
+ PK: string;
9
+ SK: string;
10
+ }, {
11
+ id: string;
12
+ PK: string;
13
+ SK: string;
14
+ }>;
15
+ export type TableItem = z.infer<typeof TableItemSchema>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TableItemSchema = void 0;
4
+ const fields_1 = require("./fields");
5
+ const keys_1 = require("./keys");
6
+ const zod_1 = require("zod");
7
+ exports.TableItemSchema = zod_1.z.object({
8
+ [keys_1.PARTITION_KEY]: zod_1.z.string(),
9
+ [keys_1.SORT_KEY]: zod_1.z.string(),
10
+ [fields_1.ID_FIELD]: zod_1.z.string(),
11
+ });
@@ -0,0 +1,2 @@
1
+ export declare const PARTITION_KEY = "PK";
2
+ export declare const SORT_KEY = "SK";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SORT_KEY = exports.PARTITION_KEY = void 0;
4
+ exports.PARTITION_KEY = "PK";
5
+ exports.SORT_KEY = "SK";
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@xube/kit-aws",
3
+ "version": "0.0.22",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+ssh://git@github.com/XubeLtd/dev-kit.git"
12
+ },
13
+ "author": "Xube Pty Ltd",
14
+ "license": "MIT",
15
+ "bugs": {
16
+ "url": "https://github.com/XubeLtd/dev-kit/issues"
17
+ },
18
+ "homepage": "https://github.com/XubeLtd/dev-kit#readme",
19
+ "devDependencies": {
20
+ "@types/aws-lambda": "^8.10.119",
21
+ "@xube/kit-build": "^0.0.22"
22
+ },
23
+ "dependencies": {
24
+ "@aws-sdk/client-dynamodb": "^3.427.0",
25
+ "@aws-sdk/lib-dynamodb": "^3.427.0",
26
+ "@xube/kit-log": "^0.0.22",
27
+ "@xube/kit-request": "^0.0.22",
28
+ "zod": "^3.22.4"
29
+ }
30
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
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"
@@ -0,0 +1,24 @@
1
+ import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBDocumentClient, GetCommand } from "@aws-sdk/lib-dynamodb";
3
+
4
+ const ddbClient = new DynamoDBClient({
5
+ region: process.env.AWS_REGION,
6
+ });
7
+
8
+ const ddbDocClient = DynamoDBDocumentClient.from(ddbClient);
9
+
10
+ export const getItemFromTable = async (
11
+ tableName: string,
12
+ key: {
13
+ PK: string;
14
+ SK: string;
15
+ }
16
+ ): Promise<Record<string, any> | undefined> => {
17
+ const params = {
18
+ TableName: tableName,
19
+ Key: key,
20
+ };
21
+
22
+ const result = await ddbDocClient.send(new GetCommand(params));
23
+ return result.Item;
24
+ };
@@ -0,0 +1,47 @@
1
+ import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2
+ import {
3
+ DynamoDBDocumentClient,
4
+ PutCommand,
5
+ PutCommandOutput,
6
+ } from "@aws-sdk/lib-dynamodb";
7
+ import { XubeResponse } from "@xube/kit-request";
8
+ import { TableItem } from "./schema/item";
9
+ import { StatusCode } from "@xube/kit-constants";
10
+ import { XubeLog } from "@xube/kit-log";
11
+
12
+ const ddbClient = new DynamoDBClient({
13
+ region: process.env.AWS_REGION,
14
+ });
15
+
16
+ const ddbDocClient = DynamoDBDocumentClient.from(ddbClient);
17
+
18
+ export const putItemInTable = async <TItem extends TableItem>(
19
+ table: string,
20
+ item: TItem,
21
+ log: XubeLog = XubeLog.getInstance()
22
+ ): Promise<XubeResponse<boolean>> => {
23
+ const params = {
24
+ TableName: table,
25
+ Item: item,
26
+ };
27
+
28
+ const putItemCommand: PutCommand = new PutCommand(params);
29
+
30
+ try {
31
+ const result: PutCommandOutput = await ddbDocClient.send(putItemCommand);
32
+
33
+ return new XubeResponse({
34
+ statusCode: StatusCode.OK,
35
+ data: true,
36
+ });
37
+ } catch (e) {
38
+ log.error(
39
+ `Error occurred when adding item to table.\n${JSON.stringify(e, null, 2)}`
40
+ );
41
+ }
42
+
43
+ return new XubeResponse({
44
+ statusCode: StatusCode.InternalError,
45
+ error: `Error occurred when adding item to table`,
46
+ });
47
+ };
@@ -0,0 +1 @@
1
+ export const ID_FIELD = "id"
@@ -0,0 +1,10 @@
1
+ import { ID_FIELD } from "./fields";
2
+ import { PARTITION_KEY, SORT_KEY } from "./keys";
3
+ import { z } from "zod";
4
+
5
+ export const TableItemSchema = z.object({
6
+ [PARTITION_KEY]: z.string(),
7
+ [SORT_KEY]: z.string(),
8
+ [ID_FIELD]: z.string(),
9
+ });
10
+ export type TableItem = z.infer<typeof TableItemSchema>;
@@ -0,0 +1,2 @@
1
+ export const PARTITION_KEY = "PK";
2
+ export const SORT_KEY = "SK";
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "extends": "../kit-build/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist"
6
+ },
7
+ "exclude": ["**/*.test.ts", "**/*.mock.ts", "dist"],
8
+ "references": [
9
+ {
10
+ "path": "../kit-constants"
11
+ },
12
+ {
13
+ "path": "../kit-log"
14
+ },
15
+ {
16
+ "path": "../kit-request"
17
+ }
18
+ ]
19
+ }