From f95194d474eb125ca2a80d5b228e3de27b88b41d Mon Sep 17 00:00:00 2001 From: Gonzalo D'Elia Date: Fri, 22 Dec 2023 17:58:19 -0300 Subject: [PATCH] Add describe time to live methods --- src/lib/methods/describe-time-to-live.ts | 35 ++++++++++++++++ src/lib/methods/index.ts | 2 + src/lib/methods/update-time-to-live.ts | 52 ++++++++++++++++++++++++ src/lib/table.ts | 21 +++++++++- 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/lib/methods/describe-time-to-live.ts create mode 100644 src/lib/methods/update-time-to-live.ts diff --git a/src/lib/methods/describe-time-to-live.ts b/src/lib/methods/describe-time-to-live.ts new file mode 100644 index 0000000..8f1c87c --- /dev/null +++ b/src/lib/methods/describe-time-to-live.ts @@ -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 { + 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 }) + ) + ); + } +} diff --git a/src/lib/methods/index.ts b/src/lib/methods/index.ts index 00a4c92..668a272 100644 --- a/src/lib/methods/index.ts +++ b/src/lib/methods/index.ts @@ -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"; @@ -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"; diff --git a/src/lib/methods/update-time-to-live.ts b/src/lib/methods/update-time-to-live.ts new file mode 100644 index 0000000..f094909 --- /dev/null +++ b/src/lib/methods/update-time-to-live.ts @@ -0,0 +1,52 @@ +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: Omit | undefined; + + constructor(table: Table, dynamodb: DynamoDB) { + super(table, dynamodb); + } + + /** + * Initialize the `UpdateTableConfig` object. + * + * @param schema The schema of the table. + */ + initialize(_input: Omit) { + // 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, + }; + } + + /** + * This method will execute the delete table request that was built up. + */ + async exec(): Promise { + const client = this.dynamodb.client; + + if (!client) { + throw new Error("Call .connect() before executing queries."); + } + + return this.runQuery(() => + client.send(new UpdateTimeToLiveCommand(this.buildRawQuery())) + ); + } +} diff --git a/src/lib/table.ts b/src/lib/table.ts index d58c200..2a93cc0 100644 --- a/src/lib/table.ts +++ b/src/lib/table.ts @@ -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, @@ -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"; @@ -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) { return new UpdateTableConfig(this, this.dynamodb).initialize(params); } + + /** + * This method updates the time to live configuration of the table + */ + updateTimeToLive(params: Omit) { + return new UpdateTimeToLive(this, this.dynamodb).initialize(params); + } }