Skip to content

Commit

Permalink
Add describe time to live methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gndelia committed Dec 22, 2023
1 parent a321a7b commit 9d70d95
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/lib/methods/describe-time-to-live.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
DescribeTimeToLiveCommand,
DescribeTimeToLiveCommandOutput,
} from "@aws-sdk/client-dynamodb";
import { Method } from "./method";
import { Executable } from "./executable";
import { DynamoDB } from "../dynamodb";
import { Table } from "../table";

export class DescribeTimeToLive extends Method implements Executable {
constructor(table: Table, dynamodb: DynamoDB) {
super(table, dynamodb);
}

buildRawQuery() {
return {};
}

/**
* This method will execute the delete table request that was built up.
*/
async exec(): Promise<DescribeTimeToLiveCommandOutput> {
const client = this.dynamodb.client;

if (!client) {
throw new Error("Call .connect() before executing queries.");
}

return this.runQuery(() =>
client.send(
new DescribeTimeToLiveCommand({ TableName: this.table!.name })
)
);
}
}
2 changes: 2 additions & 0 deletions src/lib/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export { BaseQuery } from "./base-query";
export { CreateTable } from "./create-table";
export { DeleteItem } from "./delete-item";
export { DeleteTable } from "./delete-table";
export { DescribeTimeToLive } from "./describe-time-to-live";
export { Executable } from "./executable";
export { InsertItem } from "./insert-item";
export { ListTables } from "./list-tables";
Expand All @@ -21,4 +22,5 @@ export { Query } from "./query";
export { Scan } from "./scan";
export { UpdateTableConfig } from "./update-table-config";
export { UpdateItem } from "./update-item";
export { UpdateTimeToLive } from "./update-time-to-live";
export { BatchWrite } from "./batch/batch-write";
56 changes: 56 additions & 0 deletions src/lib/methods/update-time-to-live.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
UpdateTimeToLiveCommand,
UpdateTimeToLiveInput,
UpdateTimeToLiveCommandOutput,
} from "@aws-sdk/client-dynamodb";
import { Method } from "./method";
import { Executable } from "./executable";
import { DynamoDB } from "../dynamodb";
import { Table } from "../table";

export class UpdateTimeToLive extends Method implements Executable {
private input: { attribute: string; enabled: boolean } | undefined;

constructor(table: Table, dynamodb: DynamoDB) {
super(table, dynamodb);
}

/**
* Initialize the `UpdateTableConfig` object.
*
* @param schema The schema of the table.
*/
initialize(_input: { attribute: string; enabled: boolean }) {
// Set the schema as params object
this.input = _input;

// Return the object so that it can be chained
return this;
}

buildRawQuery(): UpdateTimeToLiveInput {
return {
...this.input,
TableName: this.table!.name,
TimeToLiveSpecification: {
AttributeName: this.input!.attribute,
Enabled: this.input!.enabled,
},
};
}

/**
* This method will execute the delete table request that was built up.
*/
async exec(): Promise<UpdateTimeToLiveCommandOutput> {
const client = this.dynamodb.client;

if (!client) {
throw new Error("Call .connect() before executing queries.");
}

return this.runQuery(() =>
client.send(new UpdateTimeToLiveCommand(this.buildRawQuery()))
);
}
}
21 changes: 20 additions & 1 deletion src/lib/table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { UpdateTableCommandInput } from "@aws-sdk/client-dynamodb";
import {
UpdateTableCommandInput,
UpdateTimeToLiveCommandInput,
} from "@aws-sdk/client-dynamodb";
import { DynamoDB } from "./dynamodb";
import {
Query,
Expand All @@ -7,8 +10,10 @@ import {
UpdateItem,
DeleteItem,
DeleteTable,
DescribeTimeToLive,
CreateTable,
UpdateTableConfig,
UpdateTimeToLive,
} from "./methods";
import * as table from "./utils/table";
import { operators as updateOperators } from "./utils/update";
Expand Down Expand Up @@ -206,10 +211,24 @@ export class Table {
return new CreateTable(this, this.dynamodb).initialize(schema);
}

/**
* This method will return the time to live status of the table.
*/
describeTimeToLive() {
return new DescribeTimeToLive(this, this.dynamodb);
}

/**
* This method updates the table configuration
*/
updateConfig(params: Omit<UpdateTableCommandInput, "TableName">) {
return new UpdateTableConfig(this, this.dynamodb).initialize(params);
}

/**
* This method updates the time to live configuration of the table
*/
updateTimeToLive(params: { attribute: string; enabled: boolean }) {
return new UpdateTimeToLive(this, this.dynamodb).initialize(params);
}
}

0 comments on commit 9d70d95

Please sign in to comment.