-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added stream support #5
base: master
Are you sure you want to change the base?
Changes from all commits
6dfd145
acf543d
aa4bf7f
2c2e417
46a675e
68a7b58
7ac573a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ table: | |
component: ../ | ||
inputs: | ||
name: test-table | ||
delete: false | ||
delete: true | ||
stream: true | ||
streamViewType: NEW_IMAGE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,69 @@ | ||
const { not, equals, pick } = require('ramda') | ||
|
||
async function createTable({ dynamodb, name, attributeDefinitions, keySchema }) { | ||
const validate = { | ||
streamViewType: (inputs) => { | ||
if (!inputs.streamViewType) { | ||
return | ||
} | ||
|
||
const validStreamTypes = ['NEW_IMAGE', 'OLD_IMAGE', 'NEW_AND_OLD_IMAGES', 'KEYS_ONLY'] | ||
if (!validStreamTypes.includes(inputs.streamViewType)) { | ||
throw Error(`${inputs.streamViewType} is not a valid streamViewType.`) | ||
} | ||
}, | ||
|
||
streamViewTypeUpdate: (comp, previousTable, inputs) => { | ||
if (!previousTable.streamArn || !inputs.streamViewType) { | ||
return | ||
} | ||
|
||
if (comp.state.stream && inputs.stream && comp.state.streamViewType !== inputs.streamViewType) { | ||
throw Error(`You cannot change the view type of an existing DynamoDB stream.`) | ||
} | ||
} | ||
} | ||
|
||
async function getStreamArn ({dynamodb, name}) { | ||
const maxTries = 5 | ||
let tries = 0 | ||
|
||
const getStreamArn = async () => { | ||
if (tries > maxTries) { | ||
throw Error(`There was a problem getting the arn for your DynamoDB stream. Please try again.`) | ||
} | ||
|
||
const {streamArn } = await describeTable({ dynamodb, name}) | ||
if (!streamArn && tries <= maxTries) { | ||
tries++ | ||
const sleep = ms => new Promise(r => setTimeout(r,ms)) | ||
await sleep(3000) | ||
return await getStreamArn() | ||
} | ||
return streamArn | ||
} | ||
|
||
return await getStreamArn() | ||
} | ||
Comment on lines
+26
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is used in the update table function below. Anyways, that is a problem, because users will likely depend on getting a stream arn back from the outputs of this component. So the above function will try to get the stream arn, if it doesnt, it will wait 3 seconds and try again. It will do this 5 more times until it gets a stream arn. If it does not get it in 5 tries, it will throw an error. The error does not mean the stream was not created, it just means we did not get the stream arn, which might be really important if the user is depending on the stream arn in other components. |
||
|
||
async function createTable({ dynamodb, name, attributeDefinitions, keySchema, stream, streamViewType = false }) { | ||
const res = await dynamodb | ||
.createTable({ | ||
TableName: name, | ||
AttributeDefinitions: attributeDefinitions, | ||
KeySchema: keySchema, | ||
BillingMode: 'PAY_PER_REQUEST' | ||
BillingMode: 'PAY_PER_REQUEST', | ||
...(stream && { | ||
StreamSpecification: { | ||
StreamEnabled: true, | ||
StreamViewType: streamViewType | ||
} | ||
dodgeblaster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
}) | ||
.promise() | ||
return res.TableDescription.TableArn | ||
return { | ||
tableArn: res.TableDescription.TableArn, | ||
streamArn: res.TableDescription.LatestStreamArn || false | ||
} | ||
} | ||
|
||
async function describeTable({ dynamodb, name }) { | ||
|
@@ -21,7 +75,14 @@ async function describeTable({ dynamodb, name }) { | |
arn: data.Table.TableArn, | ||
name: data.Table.TableName, | ||
attributeDefinitions: data.Table.AttributeDefinitions, | ||
keySchema: data.Table.KeySchema | ||
keySchema: data.Table.KeySchema, | ||
streamArn: data.Table.LatestStreamArn, | ||
streamEnabled: data.Table.StreamSpecification | ||
? data.Table.StreamSpecification.StreamEnabled | ||
: false, | ||
streamViewType: data.Table.StreamSpecification | ||
? data.Table.StreamSpecification.StreamViewType | ||
: false | ||
} | ||
} catch (error) { | ||
if (error.code === 'ResourceNotFoundException') { | ||
|
@@ -32,15 +93,41 @@ async function describeTable({ dynamodb, name }) { | |
} | ||
} | ||
|
||
async function updateTable({ dynamodb, name, attributeDefinitions }) { | ||
async function updateTable({prevTable, dynamodb, name, attributeDefinitions, stream, streamViewType }) { | ||
const disableStream = prevTable.streamEnabled && !stream | ||
const enableStream = !prevTable.streamEnabled && stream | ||
|
||
const res = await dynamodb | ||
.updateTable({ | ||
TableName: name, | ||
AttributeDefinitions: attributeDefinitions, | ||
BillingMode: 'PAY_PER_REQUEST' | ||
BillingMode: 'PAY_PER_REQUEST', | ||
|
||
...(disableStream && { | ||
StreamSpecification: { | ||
StreamEnabled: false | ||
} | ||
}), | ||
Comment on lines
+106
to
+110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ive added this disableStream flag here because the SDK will error if you try to disable a non existing stream. So this flag insures we only add this to the object if the inputs are disabling a stream that exists. |
||
|
||
...(enableStream && { | ||
StreamSpecification: { | ||
StreamEnabled: true, | ||
StreamViewType: streamViewType | ||
} | ||
}) | ||
}) | ||
.promise() | ||
return res.TableDescription.TableArn | ||
|
||
|
||
let streamArn = res.TableDescription.LatestStreamArn || false | ||
if (stream && !res.TableDescription.LatestStreamArn) { | ||
streamArn = await getStreamArn({dynamodb, name}) | ||
} | ||
|
||
return { | ||
tableArn: res.TableDescription.TableArn, | ||
streamArn | ||
} | ||
} | ||
|
||
async function deleteTable({ dynamodb, name }) { | ||
|
@@ -60,16 +147,20 @@ async function deleteTable({ dynamodb, name }) { | |
} | ||
|
||
function configChanged(prevTable, table) { | ||
const prevInputs = pick(['name', 'attributeDefinitions'], prevTable) | ||
const inputs = pick(['name', 'attributeDefinitions'], table) | ||
const prevInputs = pick(['name', 'attributeDefinitions', 'streamArn', 'streamViewType', 'streamEnabled'], prevTable) | ||
const inputs = pick(['name', 'attributeDefinitions', 'streamArn', 'streamViewType', 'streamEnabled'], table) | ||
|
||
return not(equals(inputs, prevInputs)) | ||
} | ||
|
||
|
||
|
||
module.exports = { | ||
createTable, | ||
describeTable, | ||
updateTable, | ||
deleteTable, | ||
configChanged | ||
configChanged, | ||
validate, | ||
getStreamArn | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added validation around stream types for creating and updating tables.
streamViewType
just confirms they are using a correct view type, and will throw an error if they are notstreamViewTypeUpdate
is a bit more subtle. If you change the viewType on an table that already has a stream, it wont do anything. Its possible people might assume it all worked unless they see a message explaining nothing actually happened. So I made an error for it.@eahefnawy is throwing errors the best way to give messages like this in the CLI?