Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const AWS = require('aws-sdk')
const { Component } = require('@serverless/core')
const { createTable, deleteTable, describeTable, updateTable, configChanged } = require('./utils')

const outputsList = ['name', 'arn', 'region']
const outputsList = ['name', 'arn', 'region', 'stream']

const defaults = {
attributeDefinitions: [
Expand All @@ -18,7 +18,8 @@ const defaults = {
KeyType: 'HASH'
}
],
region: 'us-east-1'
region: 'us-east-1',
stream: false
}

class AwsDynamoDb extends Component {
Expand Down Expand Up @@ -47,11 +48,14 @@ class AwsDynamoDb extends Component {
this.context.status('Creating')
this.context.debug(`Table ${config.name} does not exist. Creating...`)

config.arn = await createTable({ dynamodb, ...config })
const createResponse = await createTable({ dynamodb, ...config })
config.arn = createResponse.tableArn
config.stream = createResponse.streamArn
} else {
this.context.debug(`Table ${config.name} already exists. Comparing config changes...`)

config.arn = prevTable.arn
config.stream = prevTable.streamArn

if (configChanged(prevTable, config)) {
this.context.status('Updating')
Expand All @@ -72,11 +76,11 @@ class AwsDynamoDb extends Component {

this.state.arn = config.arn
this.state.name = config.name
this.state.stream = config.stream
this.state.region = config.region
await this.save()

const outputs = pick(outputsList, config)

return outputs
}

Expand Down
33 changes: 26 additions & 7 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
const { not, equals, pick } = require('ramda')

async function createTable({ dynamodb, name, attributeDefinitions, keySchema }) {
async function createTable({ dynamodb, name, attributeDefinitions, keySchema, stream }) {
const res = await dynamodb
.createTable({
TableName: name,
AttributeDefinitions: attributeDefinitions,
KeySchema: keySchema,
BillingMode: 'PAY_PER_REQUEST'
BillingMode: 'PAY_PER_REQUEST',
...(stream && {
StreamSpecification: {
StreamEnabled: true,
StreamViewType: 'NEW_IMAGE'
}
})
})
.promise()
return res.TableDescription.TableArn
return {
tableArn: res.TableDescription.TableArn,
streamArn: res.TableDescription.LatestStreamArn || false
}
}

async function describeTable({ dynamodb, name }) {
Expand All @@ -21,7 +30,8 @@ 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
}
} catch (error) {
if (error.code === 'ResourceNotFoundException') {
Expand All @@ -32,15 +42,24 @@ async function describeTable({ dynamodb, name }) {
}
}

async function updateTable({ dynamodb, name, attributeDefinitions }) {
async function updateTable({ dynamodb, name, attributeDefinitions, stream }) {
const res = await dynamodb
.updateTable({
TableName: name,
AttributeDefinitions: attributeDefinitions,
BillingMode: 'PAY_PER_REQUEST'
BillingMode: 'PAY_PER_REQUEST',
...(stream && {
StreamSpecification: {
StreamEnabled: true,
StreamViewType: 'NEW_IMAGE'
}
})
})
.promise()
return res.TableDescription.TableArn
return {
tableArn: res.TableDescription.TableArn,
streamArn: res.TableDescription.LatestStreamArn || false
}
}

async function deleteTable({ dynamodb, name }) {
Expand Down