@factorearth/recordmiddleware-dataaccesslayer 0.0.1-y.23 → 0.0.1-y.25
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/aws-services.d.ts +2 -0
- package/dist/aws-services.js +4 -0
- package/dist/batchDeleteItems.d.ts +2 -0
- package/dist/batchDeleteItems.js +29 -0
- package/dist/batchGetItems.d.ts +2 -0
- package/dist/batchGetItems.js +24 -0
- package/dist/batchPutItems.d.ts +2 -0
- package/dist/batchPutItems.js +29 -0
- package/dist/chunkBatch.d.ts +7 -0
- package/dist/chunkBatch.js +25 -0
- package/dist/deleteItem.d.ts +1 -0
- package/dist/deleteItem.js +18 -0
- package/dist/getItem.d.ts +2 -0
- package/dist/getItem.js +42 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +11 -0
- package/dist/putItem.d.ts +1 -0
- package/dist/putItem.js +17 -0
- package/dist/updateItem.d.ts +1 -0
- package/dist/updateItem.js +17 -0
- package/dist/utils.d.ts +9 -0
- package/dist/utils.js +13 -0
- package/package.json +2 -2
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BatchWriteCommand } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import { dynamoDbDocClient } from "./aws-services.js";
|
|
3
|
+
import chunkBatch from "./chunkBatch.js";
|
|
4
|
+
import { MAX_ITEMS_BATCH_OPERATIONS } from "./index.js";
|
|
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
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BatchGetCommand } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import { dynamoDbDocClient } from "./aws-services.js";
|
|
3
|
+
export async function batchGetItems(ids, tableName) {
|
|
4
|
+
const commandInput = {
|
|
5
|
+
RequestItems: {
|
|
6
|
+
[tableName]: {
|
|
7
|
+
Keys: ids
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const command = new BatchGetCommand(commandInput);
|
|
12
|
+
try {
|
|
13
|
+
const sendResult = await dynamoDbDocClient.send(command);
|
|
14
|
+
const items = sendResult.Responses?.[tableName];
|
|
15
|
+
if (items)
|
|
16
|
+
return items;
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
console.error(`ERROR BATCH GETTING ITEMS FROM ${tableName}`);
|
|
20
|
+
console.error("IDS ", ids);
|
|
21
|
+
console.error(err);
|
|
22
|
+
}
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BatchWriteCommand } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import { dynamoDbDocClient } from "./aws-services.js";
|
|
3
|
+
import chunkBatch from "./chunkBatch.js";
|
|
4
|
+
import { MAX_ITEMS_BATCH_OPERATIONS } from "./index.js";
|
|
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
|
+
}
|
|
@@ -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,25 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deleteItem(id: string, tableName: string): Promise<void>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DeleteCommand } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import { dynamoDbDocClient } from "./aws-services.js";
|
|
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
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const getItem: (id: string, tableName: string) => Promise<Record<string, any> | null | undefined>;
|
|
2
|
+
export declare const getItemsByIndex: (indexName: string, tableName: string, attributeValue: string, attribute: string, columns?: string[]) => Promise<Record<string, any>[]>;
|
package/dist/getItem.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { dynamoDbDocClient } from "./aws-services.js";
|
|
2
|
+
import { GetCommand, QueryCommand } from "@aws-sdk/lib-dynamodb";
|
|
3
|
+
import { _columnsAndAttributeToReturn } from "./utils.js";
|
|
4
|
+
export const getItem = async (id, tableName) => {
|
|
5
|
+
const params = {
|
|
6
|
+
TableName: tableName,
|
|
7
|
+
Key: {
|
|
8
|
+
id
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
try {
|
|
12
|
+
const data = await dynamoDbDocClient.send(new GetCommand(params));
|
|
13
|
+
return data.Item;
|
|
14
|
+
}
|
|
15
|
+
catch (err) {
|
|
16
|
+
console.error("Error", err);
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
export const getItemsByIndex = async (indexName, tableName, attributeValue, attribute, columns) => {
|
|
21
|
+
const { expressionAttributeNames, columnsToReturn } = _columnsAndAttributeToReturn({ attribute, columns });
|
|
22
|
+
const params = {
|
|
23
|
+
TableName: tableName,
|
|
24
|
+
IndexName: indexName,
|
|
25
|
+
KeyConditionExpression: `#fk = :fkval`,
|
|
26
|
+
ExpressionAttributeNames: expressionAttributeNames,
|
|
27
|
+
ExpressionAttributeValues: {
|
|
28
|
+
":fkval": attributeValue
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
if (columnsToReturn) {
|
|
32
|
+
params.ProjectionExpression = columnsToReturn;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const data = await dynamoDbDocClient.send(new QueryCommand(params));
|
|
36
|
+
return data.Items || [];
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
console.error("Error", err);
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./aws-services";
|
|
2
|
+
export * from "./batchDeleteItems";
|
|
3
|
+
export * from "./batchGetItems";
|
|
4
|
+
export * from "./batchPutItems";
|
|
5
|
+
export * from "./chunkBatch";
|
|
6
|
+
export * from "./deleteItem";
|
|
7
|
+
export * from "./getItem";
|
|
8
|
+
export * from "./putItem";
|
|
9
|
+
export * from "./updateItem";
|
|
10
|
+
export * from "./utils";
|
|
11
|
+
export declare const MAX_ITEMS_BATCH_OPERATIONS = 25;
|
|
12
|
+
export interface BatchInput {
|
|
13
|
+
id: string;
|
|
14
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./aws-services";
|
|
2
|
+
export * from "./batchDeleteItems";
|
|
3
|
+
export * from "./batchGetItems";
|
|
4
|
+
export * from "./batchPutItems";
|
|
5
|
+
export * from "./chunkBatch";
|
|
6
|
+
export * from "./deleteItem";
|
|
7
|
+
export * from "./getItem";
|
|
8
|
+
export * from "./putItem";
|
|
9
|
+
export * from "./updateItem";
|
|
10
|
+
export * from "./utils";
|
|
11
|
+
export const MAX_ITEMS_BATCH_OPERATIONS = 25;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function putItem(item: Record<string, any>, tableName: string): Promise<void>;
|
package/dist/putItem.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PutCommand } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import { dynamoDbDocClient } from "./aws-services.js";
|
|
3
|
+
export async function putItem(item, tableName) {
|
|
4
|
+
const commandInput = {
|
|
5
|
+
Item: item,
|
|
6
|
+
TableName: tableName
|
|
7
|
+
};
|
|
8
|
+
const command = new PutCommand(commandInput);
|
|
9
|
+
try {
|
|
10
|
+
await dynamoDbDocClient.send(command);
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
console.error(`ERROR PUTTING ITEM IN ${tableName}`);
|
|
14
|
+
console.error("ITEM: ", item);
|
|
15
|
+
console.error(err);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function updateItem(item: Record<string, any>, tableName: string): Promise<void>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { UpdateCommand } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import { dynamoDbDocClient } from "./aws-services.js";
|
|
3
|
+
export async function updateItem(item, tableName) {
|
|
4
|
+
const commandInput = {
|
|
5
|
+
TableName: tableName,
|
|
6
|
+
Key: item
|
|
7
|
+
};
|
|
8
|
+
const command = new UpdateCommand(commandInput);
|
|
9
|
+
try {
|
|
10
|
+
await dynamoDbDocClient.send(command);
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
console.error(`ERROR UPDATING ITEM IN TABLE ${tableName}`);
|
|
14
|
+
console.error(`ITEM: `, item);
|
|
15
|
+
console.error(err);
|
|
16
|
+
}
|
|
17
|
+
}
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const _columnsAndAttributeToReturn = ({ attribute, columns, }) => {
|
|
2
|
+
const expressionAttributeNames = {
|
|
3
|
+
"#fk": `${attribute}`,
|
|
4
|
+
};
|
|
5
|
+
const columnsToReturn = columns
|
|
6
|
+
?.map((col, index) => {
|
|
7
|
+
const placeholder = `#col${index}`;
|
|
8
|
+
expressionAttributeNames[placeholder] = col;
|
|
9
|
+
return placeholder;
|
|
10
|
+
})
|
|
11
|
+
.join(", ");
|
|
12
|
+
return { expressionAttributeNames, columnsToReturn };
|
|
13
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@factorearth/recordmiddleware-dataaccesslayer",
|
|
3
|
-
"version": "0.0.1-y.
|
|
3
|
+
"version": "0.0.1-y.25",
|
|
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",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"access": "public",
|
|
32
32
|
"registry": "https://registry.npmjs.org/"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "45082bcb7811baf349b0febc1bf1a568b3d3e418"
|
|
35
35
|
}
|