All files / utils/backend dynamoTyped.ts

65.81% Statements 77/117
82.35% Branches 14/17
77.77% Functions 7/9
65.81% Lines 77/117

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 1641x 1x                                 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   29x 29x 29x 85x 85x 85x       29x   29x 29x   23x 23x 23x 23x   23x 1x 1x   23x 23x   13x 13x 13x 13x   13x 10x 10x   13x 13x   5x 5x 5x 5x   5x 1x 1x   5x 5x                                             11x 11x 11x 11x   11x 10x 10x   11x 11x                                             10x 10x 10x 10x   10x       10x 10x   17x 17x 17x 17x   17x       17x 17x  
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import {
  DeleteCommand,
  DynamoDBDocumentClient,
  GetCommand,
  PutCommand,
  QueryCommand,
  ScanCommand,
  UpdateCommand
} from '@aws-sdk/lib-dynamodb';
 
import {
  TypedDeleteItemInput, TypedDeleteItemOutput, TypedFullQueryScanOutput, TypedGetInput,
  TypedGetOutput, TypedPutItemInput,
  TypedPutItemOutput, TypedQueryInput, TypedQueryOutput, TypedScanInput, TypedScanOutput,
  TypedUpdateInput, TypedUpdateOutput
} from '@/types/backend/dynamo';
 
const client = new DynamoDBClient();
const dynamoDb = DynamoDBDocumentClient.from(client, {
  marshallOptions: {
    removeUndefinedValues: true,
  },
});
 
export const TABLE_DEVICES = process.env.TABLE_DEVICES;
export const TABLE_ERROR = process.env.TABLE_ERROR;
export const TABLE_FILE = process.env.TABLE_FILE;
export const TABLE_FILE_TRANSLATION = process.env.TABLE_DTR_TRANSLATION;
export const TABLE_RADIOS = process.env.TABLE_RADIOS;
export const TABLE_SITE = process.env.TABLE_SITE;
export const TABLE_STATUS = process.env.TABLE_STATUS;
export const TABLE_TALKGROUP = process.env.TABLE_TALKGROUP;
export const TABLE_TEXT = process.env.TABLE_TEXT;
export const TABLE_USER = process.env.TABLE_USER;
 
function removeSets<T extends object>(input: T): T {
  const output = { ...input, };
  (Object.keys(output) as (keyof T)[]).forEach(key => {
    if (!(output[key] instanceof Set)) {
      return;
    }
 
    output[key] =
      [ ...output[key], ] as any; // eslint-disable-line @typescript-eslint/no-explicit-any
  });
 
  return output;
}
 
export async function typedUpdate<T extends object>(
  config: TypedUpdateInput<T>
): Promise<TypedUpdateOutput<T>> {
  const output = (await dynamoDb.send(new UpdateCommand(config))) as TypedUpdateOutput<T>;
 
  if (output.Attributes) {
    output.Attributes = removeSets(output.Attributes);
  }
 
  return output;
}
 
export async function typedGet<T extends object>(
  config: TypedGetInput<T>
): Promise<TypedGetOutput<T>> {
  const output = (await dynamoDb.send(new GetCommand(config))) as TypedGetOutput<T>;
 
  if (output.Item) {
    output.Item = removeSets(output.Item);
  }
 
  return output;
}
 
export async function typedQuery<T extends object>(
  config: TypedQueryInput<T>
): Promise<TypedQueryOutput<T>> {
  const output = (await dynamoDb.send(new QueryCommand(config))) as TypedQueryOutput<T>;
 
  if (output.Items) {
    output.Items = output.Items.map(v => removeSets(v));
  }
 
  return output;
}
 
export async function typedFullQuery<T extends object>(
  config: TypedQueryInput<T>,
  maxRuns: number = 5
): Promise<TypedFullQueryScanOutput<T>> {
  const output: TypedFullQueryScanOutput<T> = {
    Runs: 0,
    LastEvaluatedKey: null,
    Items: [],
  };
 
  do {
    const query = await typedQuery(config);
    output.Items.push(...query.Items || []);
 
    output.Runs++;
    output.LastEvaluatedKey = query.LastEvaluatedKey || null;
  } while (output.LastEvaluatedKey !== null && output.Runs < maxRuns);
 
  return output;
}
 
export async function typedScan<T extends object>(
  config: TypedScanInput<T>
): Promise<TypedScanOutput<T>> {
  const output = (await dynamoDb.send(new ScanCommand(config))) as TypedScanOutput<T>;
 
  if (output.Items) {
    output.Items = output.Items.map(v => removeSets(v));
  }
 
  return output;
}
 
export async function typedFullScan<T extends object>(
  config: TypedScanInput<T>,
  maxRuns: number = 5
): Promise<TypedFullQueryScanOutput<T>> {
  const output: TypedFullQueryScanOutput<T> = {
    Runs: 0,
    LastEvaluatedKey: null,
    Items: [],
  };
 
  do {
    const scan = await typedScan(config);
    output.Items.push(...scan.Items || []);
 
    output.Runs++;
    output.LastEvaluatedKey = scan.LastEvaluatedKey || null;
  } while (output.LastEvaluatedKey !== null && output.Runs < maxRuns);
 
  return output;
}
 
export async function typedDeleteItem<T extends object>(
  config: TypedDeleteItemInput<T>
): Promise<TypedDeleteItemOutput<T>> {
  const output = (await dynamoDb.send(new DeleteCommand(config))) as TypedDeleteItemOutput<T>;
 
  if (output.Attributes) {
    output.Attributes = removeSets(output.Attributes);
  }
 
  return output;
}
 
export async function typedPutItem<T extends object>(
  config: TypedPutItemInput<T>
): Promise<TypedPutItemOutput<T>> {
  const output = (await dynamoDb.send(new PutCommand(config))) as TypedPutItemOutput<T>;
 
  if (output.Attributes) {
    output.Attributes = removeSets(output.Attributes);
  }
 
  return output;
}