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 | 22x 22x 22x 17x 17x 17x 85x 73x 73x 21x 17x 17x 54x 25x 17x 17x 125x 125x 96x 125x 125x 125x 125x 125x 105x 105x 105x 105x 105x 125x 418x 126x 126x 126x 126x 117x 9x 4x 5x 2x 3x 3x 126x 126x 246x 246x 246x 126x 418x 18x 18x 18x 18x 18x | import { Injectable } from '@nestjs/common';
import {
DocumentClient,
} from 'aws-sdk/clients/dynamodb';
import AWS from 'aws-sdk';
import { JsonObject } from '../../types.d';
@Injectable()
export class DynamodbUtils {
convertDbItemToDto<T>(
item: DocumentClient.AttributeMap,
dbToDtoNameMapping: { [key: string]: string },
attributesToDelete?: string[],
): T {
let dto: any = {};
const mapValue: JsonObject<string> = {};
Object.keys(item)
.filter((key) => !key.startsWith('aws:rep'))
.forEach((key) => {
dto[key] = item[key];
if (key in dbToDtoNameMapping) {
mapValue[dbToDtoNameMapping[key]] = item[key];
}
});
Eif (
typeof attributesToDelete !== 'undefined' &&
attributesToDelete.length > 0
) {
attributesToDelete.forEach((key) => {
if (key in dto) {
delete dto[key];
}
});
}
dto = { ...dto, ...mapValue };
return dto;
}
/**
*
* @param params This is the QueryInput parameter that will be populated with the provided arguments
* @param timestampFilter
* @param filter
* @param nextToken
*/
prepareMultiQueryParams(
queryInput: DocumentClient.QueryInput,
type: string,
timestampFilter: string,
nextToken?: DocumentClient.Key,
filter?: string,
) {
const params = queryInput;
if (nextToken) {
params.ExclusiveStartKey = nextToken;
}
// add timestamp
const timestampExpression = this.convertTimestampFilter(timestampFilter, type);
params.KeyConditionExpression += ` AND ${timestampExpression.KeyConditionExpression}`;
params.ExpressionAttributeValues = {
...params.ExpressionAttributeValues,
...timestampExpression.ExpressionAttributeValues,
};
params.ExpressionAttributeNames = {
...params.ExpressionAttributeNames,
...timestampExpression.ExpressionAttributeNames,
};
if (filter) {
console.log('query filter', filter);
const jfilter = JSON.parse(filter);
params.FilterExpression = jfilter.expression;
params.ExpressionAttributeValues = {
...params.ExpressionAttributeValues,
...AWS.DynamoDB.Converter.unmarshall(jfilter.expressionValues),
};
params.ExpressionAttributeNames = {
...params.ExpressionAttributeNames,
...jfilter.expressionNames,
};
}
return params;
}
convertTimestampFilter = (filter: string, type: string): DocumentClient.QueryInput => {
const jFilter = JSON.parse(filter);
const input: DocumentClient.QueryInput = {
TableName: '',
ExpressionAttributeValues: {},
};
const values = AWS.DynamoDB.Converter.unmarshall(jFilter.expressionValues);
if (
':timestampFilter_gt' in jFilter.expressionValues &&
':timestampFilter_lt' in jFilter.expressionValues
) {
input.KeyConditionExpression =
'#type BETWEEN :timestampFilter_gt AND :timestampFilter_lt';
} else if (':timestampFilter_gt' in jFilter.expressionValues) {
input.KeyConditionExpression = '#type > :timestampFilter_gt';
} else if (':timestampFilter_lt' in jFilter.expressionValues) {
input.KeyConditionExpression = '#type < :timestampFilter_lt';
} else Eif (
':timestampFilter_between_start' in jFilter.expressionValues &&
':timestampFilter_between_end' in jFilter.expressionValues
) {
input.KeyConditionExpression =
'(#type BETWEEN :timestampFilter_between_start AND :timestampFilter_between_end)';
}
const valueEntries = Object.entries(values);
valueEntries.forEach(([key, value]) => {
const time = new Date(value).getTime();
Eif (typeof input.ExpressionAttributeValues !== 'undefined') {
input.ExpressionAttributeValues[key] = `${type}.${time}`;
}
});
return input;
};
/**
* create a last year time window if the request doesn't have timestamp in the filter
*/
createLastYearQuery = (): string => {
const now = new Date();
const lastYear = new Date();
lastYear.setFullYear(lastYear.getFullYear() - 1);
const tFilter = {
expression:
'#timestampFilter < :timestampFilter_lt AND #timestampFilter > :timestampFilter_gt',
expressionNames: { '#timestampFilter': 'timestamp' },
expressionValues: {
':timestampFilter_gt': { S: lastYear.toISOString() },
':timestampFilter_lt': { S: now.toISOString() },
},
};
return JSON.stringify(tFilter);
};
}
|