Looking to enhance the capabilities of the AWS SDK DynamoDB
class by creating a new implementation for the scan
method that can overcome the 1 MB limitations. I came across some helpful resources such as the AWS documentation and this insightful Stack Overflow post. After researching typescript module augmentation, following examples in the official documentation and various relevant threads on Stack Overflow, I attempted to extend the class accordingly. Unfortunately, my solution did not work even though it was similar to a successful third party class extension example found.
import * as AWS from "aws-sdk";
declare module "aws-sdk" {
namespace AWS {
interface DynamoDB {
scanAll(params: any): any;
}
}
}
AWS.DynamoDB.prototype.scanAll = async function(params: AWS.DynamoDB.Types.ScanInput) {
let items : any[];
let db = <AWS.DynamoDB> this;
var result = await db.scan(params).promise();
if (result.Items)
items.concat(result.Items);
while (result.LastEvaluatedKey) {
params.ExclusiveStartKey = result.LastEvaluatedKey;
result = await db.scan(params).promise();
if (result.Items) {
items.concat(result.Items);
}
}
}
export {}
An error is triggered during the Typescript build process at the line AWS.DynamoDB.prototype.scanAll =
.
TS2339: Property 'scanAll' does not exist on type 'DynamoDB'.
It seems like the issue might be related to the namespace. Any ideas on how to resolve this effectively?
I attempted to omit the namespace as shown below:
import * as AWS from "aws-sdk";
declare module "aws-sdk" {
interface DynamoDB {
scanAll(params: any): any;
}
}
DynamoDB.prototype.scanAll = async function(params: AWS.DynamoDB.Types.ScanInput) {
let items : any[];
let db = <AWS.DynamoDB> this;
var result = await db.scan(params).promise();
if (result.Items)
items.concat(result.Items);
while (result.LastEvaluatedKey) {
params.ExclusiveStartKey = result.LastEvaluatedKey;
result = await db.scan(params).promise();
if (result.Items) {
items.concat(result.Items);
}
}
}
export {}
However, this approach led to Typescript being unable to recognize the type DynamoDB
, resulting in the following error:
TS2304: Cannot find name 'DynamoDB'.