Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Adding support for TTL #134

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/dynamodb-data-mapper/src/DataMapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,43 @@ describe('DataMapper', () => {
});
});

describe('#updateTimeToLive', () => {
const waitPromiseFunc = jest.fn(() => Promise.resolve());
const updateTimeToLivePromiseFunc = jest.fn(() => Promise.resolve({}));
const mockDynamoDbClient = {
config: {},
updateTimeToLive: jest.fn(() => ({promise: updateTimeToLivePromiseFunc})),
waitFor: jest.fn(() => ({promise: waitPromiseFunc})),
};

beforeEach(() => {
updateTimeToLivePromiseFunc.mockClear();
mockDynamoDbClient.updateTimeToLive.mockClear();
waitPromiseFunc.mockClear();
mockDynamoDbClient.waitFor.mockClear();
});

const mapper = new DataMapper({
client: mockDynamoDbClient as any,
});

class Item {
get [DynamoDbTable]() { return 'foo' }

get [DynamoDbSchema]() {
return { id: { type: 'String', keyType: 'HASH' }, date: {type: 'Date'} };
}
}

it('should create and send UpdateTimeToLive request',
async () => {
await mapper.updateTimeToLive(Item, true, 'date');

expect(mockDynamoDbClient.updateTimeToLive.mock.calls).toEqual([
[ { TableName: 'foo', TimeToLiveSpecification: { Enabled: true, AttributeName: 'date' } } ],
]);
});
});

describe('#ensureGlobalSecondaryIndexExists', () => {
const waitPromiseFunc = jest.fn(() => Promise.resolve());
Expand Down
21 changes: 21 additions & 0 deletions packages/dynamodb-data-mapper/src/DataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,27 @@ export class DataMapper {
await this.client.waitFor('tableNotExists', {TableName}).promise();
}

/**
* Perform a UpdateTimeToLive on provided table.
*
* According to AWS documentation, applying across all partitions can take 1 hour.
* That's why this feature doesn't wait for complete process.
*
* @param valueConstructor The constructor used for values in the table.
* @param Enabled Boolean value that represents desired state of TTL
* @param AttributeName "Expiration" attribute which is used for TTL mechanism
* Must be type of Number and contain unix epoch time of record's expiration
* @throws Throws exception if TTL is currently set to desired state or when you exceed maximum number of commands
* in some period of time
*/
async updateTimeToLive(valueConstructor: ZeroArgumentsConstructor<any>, Enabled: boolean, AttributeName: string) {
const TableName = this.getTableName(valueConstructor.prototype);
await this.client.updateTimeToLive({
TableName,
TimeToLiveSpecification: { Enabled, AttributeName }
}).promise();
}

/**
* If the table does not already exist, perform a CreateTable operation
* using the schema accessible via the {DynamoDbSchema} property and the
Expand Down