From 9d70d95cbc34a082b0d480398b2b30b53f86ee91 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 | 56 ++++++++++++++++++++++++ src/lib/table.ts | 21 ++++++++- 4 files changed, 113 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..e6ae6dc --- /dev/null +++ b/src/lib/methods/update-time-to-live.ts @@ -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 { + 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..f7ab2c6 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: { attribute: string; enabled: boolean }) { + return new UpdateTimeToLive(this, this.dynamodb).initialize(params); + } }