@factorearth/recordmiddleware-dataaccesslayer 0.0.1-alpha.12 → 0.0.1-alpha.13

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,2 @@
1
+ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
2
+ export declare const dynamoDbDocClient: DynamoDBDocumentClient;
@@ -0,0 +1,5 @@
1
+ import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2
+ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
3
+ const dynamoDbClient = new DynamoDBClient();
4
+ export const dynamoDbDocClient = DynamoDBDocumentClient.from(dynamoDbClient);
5
+ //# sourceMappingURL=aws-services.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aws-services.js","sourceRoot":"","sources":["../lib/aws-services.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAE/D,MAAM,cAAc,GAAG,IAAI,cAAc,EAAE,CAAC;AAC5C,MAAM,CAAC,MAAM,iBAAiB,GAAG,sBAAsB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { BatchDeleteInput } from "index";
2
+ export declare function batchDeleteItems(items: BatchDeleteInput[], tableName: string): Promise<void>;
@@ -0,0 +1,30 @@
1
+ import { BatchWriteCommand } from "@aws-sdk/lib-dynamodb";
2
+ import { dynamoDbDocClient } from "aws-services";
3
+ import chunkBatch from "chunkBatch";
4
+ import { MAX_ITEMS_BATCH_OPERATIONS } from "index";
5
+ export async function batchDeleteItems(items, tableName) {
6
+ const chunks = chunkBatch(items, MAX_ITEMS_BATCH_OPERATIONS);
7
+ for (const chunk of chunks) {
8
+ const deleteRequests = chunk.map((id) => {
9
+ return {
10
+ DeleteRequest: {
11
+ Key: { id }
12
+ }
13
+ };
14
+ });
15
+ const commandInput = {
16
+ RequestItems: {
17
+ [tableName]: deleteRequests
18
+ }
19
+ };
20
+ const command = new BatchWriteCommand(commandInput);
21
+ try {
22
+ await dynamoDbDocClient.send(command);
23
+ }
24
+ catch (err) {
25
+ console.error(`ERROR BATCH DELETING FROM TABLE ${tableName}`, err);
26
+ console.log('IDS', deleteRequests);
27
+ }
28
+ }
29
+ }
30
+ //# sourceMappingURL=batchDeleteItems.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"batchDeleteItems.js","sourceRoot":"","sources":["../lib/batchDeleteItems.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAA0B,MAAM,uBAAuB,CAAC;AAClF,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,UAAU,MAAM,YAAY,CAAC;AACpC,OAAO,EAAoB,0BAA0B,EAAE,MAAM,OAAO,CAAC;AAErE,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,KAAyB,EAAE,SAAiB;IAClF,MAAM,MAAM,GAAG,UAAU,CAAC,KAAK,EAAE,0BAA0B,CAAC,CAAC;IAC7D,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC5B,MAAM,cAAc,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE;YACvC,OAAO;gBACN,aAAa,EAAE;oBACd,GAAG,EAAE,EAAE,EAAE,EAAE;iBACX;aACD,CAAC;QACH,CAAC,CAAC,CAAC;QACH,MAAM,YAAY,GAA2B;YAC5C,YAAY,EAAE;gBACb,CAAC,SAAS,CAAC,EAAE,cAAc;aAC3B;SACD,CAAC;QACF,MAAM,OAAO,GAAsB,IAAI,iBAAiB,CAAC,YAAY,CAAC,CAAC;QACvE,IAAI,CAAC;YACJ,MAAM,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,OAAO,CAAC,KAAK,CAAC,mCAAmC,SAAS,EAAE,EAAE,GAAG,CAAC,CAAC;YACnE,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;QACpC,CAAC;IACF,CAAC;AACF,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { BatchDeleteInput } from "index";
2
+ export declare function batchPutItems(items: BatchDeleteInput[], tableName: string): Promise<void>;
@@ -0,0 +1,30 @@
1
+ import { BatchWriteCommand } from "@aws-sdk/lib-dynamodb";
2
+ import { dynamoDbDocClient } from "./aws-services";
3
+ import chunkBatch from "./chunkBatch";
4
+ import { MAX_ITEMS_BATCH_OPERATIONS } from "index";
5
+ export async function batchPutItems(items, tableName) {
6
+ const chunks = chunkBatch(items, MAX_ITEMS_BATCH_OPERATIONS);
7
+ for (const chunk of chunks) {
8
+ const putRequests = chunk.map((Item) => {
9
+ return {
10
+ PutRequest: {
11
+ Item
12
+ }
13
+ };
14
+ });
15
+ const commandInput = {
16
+ RequestItems: {
17
+ [tableName]: putRequests
18
+ }
19
+ };
20
+ const command = new BatchWriteCommand(commandInput);
21
+ try {
22
+ await dynamoDbDocClient.send(command);
23
+ }
24
+ catch (err) {
25
+ console.error(`ERROR BATCH PUTTING DOCUMENTS IN ${tableName}`, err);
26
+ console.error(`DOCUMENTS: `, putRequests);
27
+ }
28
+ }
29
+ }
30
+ //# sourceMappingURL=batchPutItems.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"batchPutItems.js","sourceRoot":"","sources":["../lib/batchPutItems.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAA0B,MAAM,uBAAuB,CAAC;AAClF,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,UAAU,MAAM,cAAc,CAAC;AACtC,OAAO,EAAoB,0BAA0B,EAAE,MAAM,OAAO,CAAC;AAErE,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,KAAyB,EAAE,SAAiB;IAC/E,MAAM,MAAM,GAAG,UAAU,CAAC,KAAK,EAAE,0BAA0B,CAAC,CAAC;IAC7D,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC5B,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;YACtC,OAAO;gBACN,UAAU,EAAE;oBACX,IAAI;iBACJ;aACD,CAAC;QACH,CAAC,CAAC,CAAC;QACH,MAAM,YAAY,GAA2B;YAC5C,YAAY,EAAE;gBACb,CAAC,SAAS,CAAC,EAAE,WAAW;aACxB;SACD,CAAC;QACF,MAAM,OAAO,GAAsB,IAAI,iBAAiB,CAAC,YAAY,CAAC,CAAC;QACvE,IAAI,CAAC;YACJ,MAAM,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,OAAO,CAAC,KAAK,CAAC,qCAAqC,SAAS,EAAE,EAAE,GAAG,CAAC,CAAC;YACrE,OAAO,CAAC,KAAK,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;QAC3C,CAAC;IACF,CAAC;AACF,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Given an array of items, this module chunks them out into smaller arrays of {@link itemMax} length
3
+ * @param array An array of items that we want to divide into a subset of arrays
4
+ * @param itemMax The maximum number of items that can be in each array
5
+ * @returns An array of item arrays
6
+ */
7
+ export default function chunkBatch<T>(array: T[], itemMax: number): T[][];
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Given an array of items, this module chunks them out into smaller arrays of {@link itemMax} length
3
+ * @param array An array of items that we want to divide into a subset of arrays
4
+ * @param itemMax The maximum number of items that can be in each array
5
+ * @returns An array of item arrays
6
+ */
7
+ export default function chunkBatch(array, itemMax) {
8
+ const final = [];
9
+ let chunk = [];
10
+ for (let i = 0; i < array.length; i++) {
11
+ const element = array[i];
12
+ if (i !== 0 && (i % itemMax === 0)) {
13
+ // Its the beginning of the next one
14
+ final.push(chunk);
15
+ chunk = [element];
16
+ }
17
+ else {
18
+ chunk.push(element);
19
+ }
20
+ }
21
+ if (chunk.length > 0) {
22
+ final.push(chunk);
23
+ }
24
+ return final;
25
+ }
26
+ //# sourceMappingURL=chunkBatch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunkBatch.js","sourceRoot":"","sources":["../lib/chunkBatch.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAAI,KAAU,EAAE,OAAe;IAChE,MAAM,KAAK,GAAU,EAAE,CAAC;IACxB,IAAI,KAAK,GAAQ,EAAE,CAAC;IACpB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACzB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,OAAO,KAAK,CAAC,CAAC,EAAE,CAAC;YACpC,oCAAoC;YACpC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAClB,KAAK,GAAG,CAAC,OAAO,CAAC,CAAC;QAEnB,CAAC;aAAM,CAAC;YACP,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACrB,CAAC;IACF,CAAC;IAED,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function deleteItem(id: string, tableName: string): Promise<void>;
@@ -0,0 +1,19 @@
1
+ import { DeleteCommand } from "@aws-sdk/lib-dynamodb";
2
+ import { dynamoDbDocClient } from "aws-services";
3
+ export async function deleteItem(id, tableName) {
4
+ const commandInput = {
5
+ TableName: tableName,
6
+ Key: {
7
+ id
8
+ }
9
+ };
10
+ const command = new DeleteCommand(commandInput);
11
+ try {
12
+ await dynamoDbDocClient.send(command);
13
+ }
14
+ catch (err) {
15
+ console.error(`ERROR DELETING DOCUMENT ${id} in ${tableName}`);
16
+ console.error(err);
17
+ }
18
+ }
19
+ //# sourceMappingURL=deleteItem.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deleteItem.js","sourceRoot":"","sources":["../lib/deleteItem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAsB,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,EAAU,EAAE,SAAiB;IAC7D,MAAM,YAAY,GAAuB;QACxC,SAAS,EAAE,SAAS;QACpB,GAAG,EAAE;YACJ,EAAE;SACF;KACD,CAAC;IACF,MAAM,OAAO,GAAkB,IAAI,aAAa,CAAC,YAAY,CAAC,CAAC;IAC/D,IAAI,CAAC;QACJ,MAAM,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,OAAO,SAAS,EAAE,CAAC,CAAC;QAC/D,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC;AACF,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const getItem: (id: string, tableName: string) => Promise<Record<string, any> | null | undefined>;
@@ -0,0 +1,19 @@
1
+ import { dynamoDbDocClient } from "./aws-services";
2
+ import { GetCommand } from "@aws-sdk/lib-dynamodb";
3
+ export const getItem = async (id, tableName) => {
4
+ const params = {
5
+ TableName: tableName,
6
+ Key: {
7
+ id
8
+ }
9
+ };
10
+ try {
11
+ const data = await dynamoDbDocClient.send(new GetCommand(params));
12
+ return data.Item;
13
+ }
14
+ catch (err) {
15
+ console.error("Error", err);
16
+ return null;
17
+ }
18
+ };
19
+ //# sourceMappingURL=getItem.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getItem.js","sourceRoot":"","sources":["../lib/getItem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,EAAE,EAAU,EAAE,SAAiB,EAAE,EAAE;IAC9D,MAAM,MAAM,GAAG;QACd,SAAS,EAAE,SAAS;QACpB,GAAG,EAAE;YACJ,EAAE;SACF;KACD,CAAC;IACF,IAAI,CAAC;QACJ,MAAM,IAAI,GAAG,MAAM,iBAAiB,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;QAClE,OAAO,IAAI,CAAC,IAAI,CAAC;IAClB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAC5B,OAAO,IAAI,CAAC;IACb,CAAC;AACF,CAAC,CAAA"}
package/dist/index.d.ts CHANGED
@@ -1 +1,5 @@
1
- export * from "./getProject";
1
+ export * from "./getItem";
2
+ export declare const MAX_ITEMS_BATCH_OPERATIONS = 25;
3
+ export interface BatchDeleteInput {
4
+ id: string;
5
+ }
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
- export * from "./getProject";
1
+ export * from "./getItem";
2
+ export const MAX_ITEMS_BATCH_OPERATIONS = 25;
2
3
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAE1B,MAAM,CAAC,MAAM,0BAA0B,GAAG,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@factorearth/recordmiddleware-dataaccesslayer",
3
- "version": "0.0.1-alpha.12",
3
+ "version": "0.0.1-alpha.13",
4
4
  "description": "A module for accessing dynamdb efficiently",
5
5
  "author": "madtrx <marlin.makori@gmail.com>",
6
6
  "homepage": "https://github.com/FactorEarth/RecordMiddleware#readme",
@@ -28,5 +28,5 @@
28
28
  "access": "public",
29
29
  "registry": "https://registry.npmjs.org/"
30
30
  },
31
- "gitHead": "8498879d10c3f30d5167ce8fb5b9fe0d21d863e3"
31
+ "gitHead": "ac74f89b3c1db1ce3433cbafc2752b586d4270a0"
32
32
  }
@@ -1 +0,0 @@
1
- export declare function getProject(id: string): Promise<string>;
@@ -1,5 +0,0 @@
1
- export async function getProject(id) {
2
- console.log("getProject");
3
- return "hello world";
4
- }
5
- //# sourceMappingURL=getProject.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getProject.js","sourceRoot":"","sources":["../lib/getProject.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,EAAU;IAC1C,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;IAC1B,OAAO,aAAa,CAAC;AACtB,CAAC"}