Skip to content

Commit 5c7fd98

Browse files
committed
Added option for defining timeToLiveSpecification as part of table definition
1 parent 4719759 commit 5c7fd98

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ inputs:
113113
KeyType: HASH
114114
Projection:
115115
ProjectionType: 'ALL'
116+
timeToLiveSpecification: # (optional)
117+
AttributeName: attribute2
118+
Enabled: false
116119
region: us-east-1
117120
```
118121

src/serverless.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { mergeDeepRight, pick } = require('ramda');
55
const AWS = require('aws-sdk');
66
// eslint-disable-next-line import/no-unresolved
77
const { Component } = require('@serverless/core');
8-
const { log, createTable, deleteTable, describeTable, updateTable } = require('./utils');
8+
const { log, createTable, deleteTable, describeTable, updateTable, updateTimeToLive } = require('./utils');
99

1010
const outputsList = ['name', 'arn', 'region'];
1111

@@ -27,6 +27,7 @@ const defaults = {
2727
name: null,
2828
region: 'us-east-1',
2929
deletionPolicy: 'delete',
30+
timeToLiveSpecification: undefined
3031
};
3132

3233
class AwsDynamoDb extends Component {
@@ -88,6 +89,11 @@ class AwsDynamoDb extends Component {
8889
await updateTable.call(this, { dynamodb, prevGlobalSecondaryIndexes, ...config });
8990
}
9091

92+
if (config.timeToLiveSpecification) {
93+
await updateTimeToLive({dynamodb, ...config})
94+
95+
}
96+
9197
log(`Table ${config.name} was successfully deployed to the ${config.region} region.`);
9298

9399
this.state.arn = config.arn;

src/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,25 @@ async function deleteTable({ dynamodb, name }) {
119119
return !!res;
120120
}
121121

122+
async function updateTimeToLive({dynamodb, name, timeToLiveSpecification= {}}) {
123+
return await dynamodb.waitFor('tableExists', { TableName: name}, async function(err, data) {
124+
if (err) throw err;
125+
return await dynamodb
126+
.updateTimeToLive({
127+
TableName: name,
128+
TimeToLiveSpecification: {
129+
AttributeName: timeToLiveSpecification.AttributeName,
130+
Enabled: timeToLiveSpecification.Enabled,
131+
}
132+
}).promise();
133+
}).promise();
134+
}
135+
122136
module.exports = {
123137
log,
124138
createTable,
125139
describeTable,
126140
updateTable,
127141
deleteTable,
142+
updateTimeToLive,
128143
};

test/integration.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { sleep, generateId, getCredentials, getServerlessSdk, getTable } = require('./utils');
3+
const { sleep, generateId, getCredentials, getServerlessSdk, getTable, getTableTimeToLive } = require('./utils');
44

55
// set enough timeout for deployment to finish
66
jest.setTimeout(30000);
@@ -16,6 +16,10 @@ const instanceYaml = {
1616
stage: 'dev',
1717
inputs: {
1818
deletionPolicy: 'delete',
19+
timeToLiveSpecification: {
20+
AttributeName : 'expires',
21+
Enabled: true
22+
},
1923
attributeDefinitions: [
2024
{
2125
AttributeName: 'attribute1',
@@ -75,6 +79,8 @@ it('should successfully deploy dynamodb table and local index', async () => {
7579

7680
const res = await getTable(credentials, name);
7781

82+
//const resTTL = await getTableTimeToLive(credentials, name);
83+
7884
expect(instance.outputs.name).toBeDefined();
7985
expect(instance.outputs.arn).toBeDefined();
8086
expect(res.Table.AttributeDefinitions.length).toEqual(2);

test/utils.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const getCredentials = () => {
2828
};
2929

3030
if (!credentials.aws.accessKeyId || !credentials.aws.accessKeyId) {
31-
throw new Error('Unable to run tests. AWS credentials not found in the envionrment');
31+
throw new Error('Unable to run tests. AWS credentials not found in the environment');
3232
}
3333

3434
return credentials;
@@ -67,4 +67,23 @@ const getTable = async (credentials, tableName) => {
6767
.promise();
6868
};
6969

70-
module.exports = { sleep, generateId, getCredentials, getServerlessSdk, getTable };
70+
/*
71+
* Fetches a DynamoDB timeToLive for a table from aws for validation
72+
* @param ${object} credentials - the cross provider credentials object
73+
* @param ${string} tableName - the name of the dynamodb table
74+
*/
75+
const getTableTimeToLive = async (credentials, tableName) => {
76+
const config = {
77+
credentials: credentials.aws,
78+
region: 'us-east-1',
79+
};
80+
const dynamodb = new AWS.DynamoDB(config);
81+
82+
return dynamodb
83+
.describeTimeToLive({
84+
TableName: tableName,
85+
})
86+
.promise();
87+
};
88+
89+
module.exports = { sleep, generateId, getCredentials, getServerlessSdk, getTable, getTableTimeToLive };

0 commit comments

Comments
 (0)