@factorearth/recordmiddleware-dataaccesslayer 0.0.1-y.21 → 0.0.1-y.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.
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@factorearth/recordmiddleware-dataaccesslayer",
3
- "version": "0.0.1-y.21",
3
+ "version": "0.0.1-y.22",
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",
7
7
  "license": "ISC",
8
- "type": "module",
9
- "main": "dist/index.js",
10
- "types": "dist/index.d.ts",
8
+ "main": "dist/index",
9
+ "types": "dist/index",
10
+ "files": [
11
+ "dist"
12
+ ],
11
13
  "repository": {
12
14
  "type": "git",
13
15
  "url": "git+https://github.com/FactorEarth/RecordMiddleware.git"
@@ -29,5 +31,5 @@
29
31
  "access": "public",
30
32
  "registry": "https://registry.npmjs.org/"
31
33
  },
32
- "gitHead": "009749636b5343aa1c8092435037dc58829b7005"
34
+ "gitHead": "a259809dbea65ff3fb42b5ead58aa37648c83a02"
33
35
  }
@@ -1,2 +0,0 @@
1
- import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
2
- export declare const dynamoDbDocClient: DynamoDBDocumentClient;
@@ -1,4 +0,0 @@
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);
@@ -1,2 +0,0 @@
1
- import { BatchInput } from "./index.js";
2
- export declare function batchDeleteItems(items: BatchInput[], tableName: string): Promise<void>;
@@ -1,29 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- import { BatchInput } from "./index.js";
2
- export declare function batchGetItems(ids: BatchInput[], tableName: string): Promise<Record<string, any>[]>;
@@ -1,24 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- import { BatchInput } from "./index.js";
2
- export declare function batchPutItems(items: BatchInput[], tableName: string): Promise<void>;
@@ -1,29 +0,0 @@
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
- }
@@ -1,7 +0,0 @@
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[][];
@@ -1,25 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export declare function deleteItem(id: string, tableName: string): Promise<void>;
@@ -1,18 +0,0 @@
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
- }
package/dist/getItem.d.ts DELETED
@@ -1,2 +0,0 @@
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 DELETED
@@ -1,42 +0,0 @@
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 DELETED
@@ -1,14 +0,0 @@
1
- export * from "./aws-services.js";
2
- export * from "./batchDeleteItems.js";
3
- export * from "./batchGetItems.js";
4
- export * from "./batchPutItems.js";
5
- export * from "./chunkBatch.js";
6
- export * from "./deleteItem.js";
7
- export * from "./getItem.js";
8
- export * from "./putItem.js";
9
- export * from "./updateItem.js";
10
- export * from "./utils.js";
11
- export declare const MAX_ITEMS_BATCH_OPERATIONS = 25;
12
- export interface BatchInput {
13
- id: string;
14
- }
package/dist/index.js DELETED
@@ -1,11 +0,0 @@
1
- export * from "./aws-services.js";
2
- export * from "./batchDeleteItems.js";
3
- export * from "./batchGetItems.js";
4
- export * from "./batchPutItems.js";
5
- export * from "./chunkBatch.js";
6
- export * from "./deleteItem.js";
7
- export * from "./getItem.js";
8
- export * from "./putItem.js";
9
- export * from "./updateItem.js";
10
- export * from "./utils.js";
11
- export const MAX_ITEMS_BATCH_OPERATIONS = 25;
package/dist/putItem.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function putItem(item: Record<string, any>, tableName: string): Promise<void>;
package/dist/putItem.js DELETED
@@ -1,17 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export declare function updateItem(item: Record<string, any>, tableName: string): Promise<void>;
@@ -1,17 +0,0 @@
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 DELETED
@@ -1,9 +0,0 @@
1
- export declare const _columnsAndAttributeToReturn: ({ attribute, columns, }: {
2
- attribute: string;
3
- columns?: string[];
4
- }) => {
5
- expressionAttributeNames: {
6
- [key: string]: string;
7
- };
8
- columnsToReturn: string | undefined;
9
- };
package/dist/utils.js DELETED
@@ -1,13 +0,0 @@
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
- };
@@ -1,5 +0,0 @@
1
- import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2
- import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
3
-
4
- const dynamoDbClient = new DynamoDBClient();
5
- export const dynamoDbDocClient = DynamoDBDocumentClient.from(dynamoDbClient);
@@ -1,29 +0,0 @@
1
- import { BatchWriteCommand, BatchWriteCommandInput } from "@aws-sdk/lib-dynamodb";
2
- import { dynamoDbDocClient } from "./aws-services.js";
3
- import chunkBatch from "./chunkBatch.js";
4
- import { BatchInput, MAX_ITEMS_BATCH_OPERATIONS } from "./index.js";
5
-
6
- export async function batchDeleteItems(items: BatchInput[], tableName: string) {
7
- const chunks = chunkBatch(items, MAX_ITEMS_BATCH_OPERATIONS);
8
- for (const chunk of chunks) {
9
- const deleteRequests = chunk.map((id) => {
10
- return {
11
- DeleteRequest: {
12
- Key: { id }
13
- }
14
- };
15
- });
16
- const commandInput: BatchWriteCommandInput = {
17
- RequestItems: {
18
- [tableName]: deleteRequests
19
- }
20
- };
21
- const command: BatchWriteCommand = new BatchWriteCommand(commandInput);
22
- try {
23
- await dynamoDbDocClient.send(command);
24
- } catch (err) {
25
- console.error(`ERROR BATCH DELETING FROM TABLE ${tableName}`, err);
26
- console.log('IDS', deleteRequests);
27
- }
28
- }
29
- }
@@ -1,24 +0,0 @@
1
- import { BatchGetCommand, BatchGetCommandInput } from "@aws-sdk/lib-dynamodb";
2
- import { dynamoDbDocClient } from "./aws-services.js";
3
- import { BatchInput } from "./index.js";
4
-
5
- export async function batchGetItems(ids: BatchInput[], tableName: string) {
6
- const commandInput: BatchGetCommandInput = {
7
- RequestItems: {
8
- [tableName]: {
9
- Keys: ids
10
- }
11
- }
12
- };
13
- const command: BatchGetCommand = new BatchGetCommand(commandInput);
14
- try {
15
- const sendResult = await dynamoDbDocClient.send(command);
16
- const items = sendResult.Responses?.[tableName];
17
- if (items) return items;
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
- }
@@ -1,29 +0,0 @@
1
- import { BatchWriteCommand, BatchWriteCommandInput } from "@aws-sdk/lib-dynamodb";
2
- import { dynamoDbDocClient } from "./aws-services.js";
3
- import chunkBatch from "./chunkBatch.js";
4
- import { BatchInput, MAX_ITEMS_BATCH_OPERATIONS } from "./index.js";
5
-
6
- export async function batchPutItems(items: BatchInput[], tableName: string) {
7
- const chunks = chunkBatch(items, MAX_ITEMS_BATCH_OPERATIONS);
8
- for (const chunk of chunks) {
9
- const putRequests = chunk.map((Item) => {
10
- return {
11
- PutRequest: {
12
- Item
13
- }
14
- };
15
- });
16
- const commandInput: BatchWriteCommandInput = {
17
- RequestItems: {
18
- [tableName]: putRequests
19
- }
20
- };
21
- const command: BatchWriteCommand = new BatchWriteCommand(commandInput);
22
- try {
23
- await dynamoDbDocClient.send(command);
24
- } catch (err) {
25
- console.error(`ERROR BATCH PUTTING DOCUMENTS IN ${tableName}`, err);
26
- console.error(`DOCUMENTS `, putRequests);
27
- }
28
- }
29
- }
package/lib/chunkBatch.ts DELETED
@@ -1,27 +0,0 @@
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[][] {
8
- const final: T[][] = [];
9
- let chunk: T[] = [];
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
-
22
- if (chunk.length > 0) {
23
- final.push(chunk);
24
- }
25
-
26
- return final;
27
- }
package/lib/deleteItem.ts DELETED
@@ -1,18 +0,0 @@
1
- import { DeleteCommand, DeleteCommandInput } from "@aws-sdk/lib-dynamodb";
2
- import { dynamoDbDocClient } from "./aws-services.js";
3
-
4
- export async function deleteItem(id: string, tableName: string) {
5
- const commandInput: DeleteCommandInput = {
6
- TableName: tableName,
7
- Key: {
8
- id
9
- }
10
- };
11
- const command: DeleteCommand = new DeleteCommand(commandInput);
12
- try {
13
- await dynamoDbDocClient.send(command);
14
- } catch (err) {
15
- console.error(`ERROR DELETING DOCUMENT ${id} in ${tableName}`);
16
- console.error(err);
17
- }
18
- }
package/lib/getItem.ts DELETED
@@ -1,45 +0,0 @@
1
- import { dynamoDbDocClient } from "./aws-services.js";
2
- import { GetCommand, GetCommandInput, QueryCommand, QueryCommandInput } from "@aws-sdk/lib-dynamodb";
3
- import { _columnsAndAttributeToReturn } from "./utils.js";
4
-
5
- export const getItem = async (id: string, tableName: string ) => {
6
- const params: GetCommandInput = {
7
- TableName: tableName,
8
- Key: {
9
- id
10
- }
11
- };
12
- try {
13
- const data = await dynamoDbDocClient.send(new GetCommand(params));
14
- return data.Item;
15
- } catch (err) {
16
- console.error("Error", err);
17
- return null;
18
- }
19
- }
20
-
21
- export const getItemsByIndex = async (indexName: string, tableName: string, attributeValue:string, attribute: string, columns?: string[]) => {
22
- const { expressionAttributeNames, columnsToReturn } = _columnsAndAttributeToReturn({attribute, columns});
23
-
24
- const params: QueryCommandInput = {
25
- TableName: tableName,
26
- IndexName: indexName,
27
- KeyConditionExpression: `#fk = :fkval`,
28
- ExpressionAttributeNames: expressionAttributeNames,
29
- ExpressionAttributeValues: {
30
- ":fkval": attributeValue
31
- }
32
- };
33
-
34
- if (columnsToReturn) {
35
- params.ProjectionExpression = columnsToReturn;
36
- }
37
-
38
- try {
39
- const data = await dynamoDbDocClient.send(new QueryCommand(params));
40
- return data.Items || [];
41
- } catch (err) {
42
- console.error("Error", err);
43
- return [];
44
- }
45
- }
package/lib/index.ts DELETED
@@ -1,16 +0,0 @@
1
- export * from "./aws-services.js";
2
- export * from "./batchDeleteItems.js";
3
- export * from "./batchGetItems.js";
4
- export * from "./batchPutItems.js";
5
- export * from "./chunkBatch.js";
6
- export * from "./deleteItem.js";
7
- export * from "./getItem.js";
8
- export * from "./putItem.js";
9
- export * from "./updateItem.js";
10
- export * from "./utils.js";
11
-
12
- export const MAX_ITEMS_BATCH_OPERATIONS = 25;
13
-
14
- export interface BatchInput {
15
- id: string;
16
- }
package/lib/putItem.ts DELETED
@@ -1,17 +0,0 @@
1
- import { PutCommand, PutCommandInput } from "@aws-sdk/lib-dynamodb";
2
- import { dynamoDbDocClient } from "./aws-services.js";
3
-
4
- export async function putItem(item: Record<string, any>, tableName: string) {
5
- const commandInput: PutCommandInput = {
6
- Item: item,
7
- TableName: tableName
8
- };
9
- const command: PutCommand = new PutCommand(commandInput);
10
- try {
11
- await dynamoDbDocClient.send(command);
12
- } catch (err) {
13
- console.error(`ERROR PUTTING ITEM IN ${tableName}`);
14
- console.error("ITEM: ", item);
15
- console.error(err);
16
- }
17
- }
package/lib/updateItem.ts DELETED
@@ -1,17 +0,0 @@
1
- import { UpdateCommand, UpdateCommandInput } from "@aws-sdk/lib-dynamodb";
2
- import { dynamoDbDocClient } from "./aws-services.js";
3
-
4
- export async function updateItem(item: Record<string, any>, tableName: string) {
5
- const commandInput: UpdateCommandInput = {
6
- TableName: tableName,
7
- Key: item
8
- };
9
- const command: UpdateCommand = new UpdateCommand(commandInput);
10
- try {
11
- await dynamoDbDocClient.send(command);
12
- } catch (err) {
13
- console.error(`ERROR UPDATING ITEM IN TABLE ${tableName}`);
14
- console.error(`ITEM: `, item);
15
- console.error(err);
16
- }
17
- }
package/lib/utils.ts DELETED
@@ -1,21 +0,0 @@
1
- export const _columnsAndAttributeToReturn = ({
2
- attribute,
3
- columns,
4
- }: {
5
- attribute: string;
6
- columns?: string[];
7
- }) => {
8
- const expressionAttributeNames: { [key: string]: string } = {
9
- "#fk": `${attribute}`,
10
- };
11
-
12
- const columnsToReturn = columns
13
- ?.map((col, index) => {
14
- const placeholder = `#col${index}`;
15
- expressionAttributeNames[placeholder] = col;
16
- return placeholder;
17
- })
18
- .join(", ");
19
-
20
- return { expressionAttributeNames, columnsToReturn };
21
- };
package/tsconfig.json DELETED
@@ -1,33 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "baseUrl": "lib",
4
- "rootDir": "lib",
5
- "outDir": "dist",
6
- "target": "ESNext",
7
- "types": [
8
- "node"
9
- ],
10
- "lib": [
11
- "ESNext",
12
- "DOM",
13
- "DOM.Iterable"
14
- ],
15
- "module": "ESNext",
16
- "moduleResolution": "node",
17
- "skipLibCheck": true,
18
- "strict": true,
19
- "esModuleInterop": true,
20
- "forceConsistentCasingInFileNames": true,
21
- "declaration": true,
22
- "allowJs": true,
23
- "resolveJsonModule": true,
24
- },
25
- "include": [
26
- "lib/**/*.d.ts",
27
- "lib/**/*.ts",
28
- "lib/**/*.tsx"
29
- ],
30
- "exclude": [
31
- "./**/*.test.tsx"
32
- ],
33
- }