Skip to content

Commit

Permalink
fix: synchronous table creation and deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausTrainer committed Oct 7, 2016
1 parent f4d7e20 commit 64795e4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 33 deletions.
75 changes: 43 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const serialize = require('./serialize')
const deserialize = require('./deserialize')
const DynamoDBIterator = require('./iterator')

const MAX_BATCH_SIZE = 25

const globalStore = {}

AWS.config.apiVersions = {dynamodb: '2012-08-10'}
Expand Down Expand Up @@ -40,7 +42,8 @@ function hexEncodeTableName (str) {

DynamoDBDOWN.prototype._open = function (options, cb) {
if (!options.dynamodb) {
return cb(new Error('`open` requires `options` argument with "dynamodb" key'))
cb(new Error('`open` requires `options` argument with "dynamodb" key'))
return
}

if (typeof options.prefix === 'string') {
Expand All @@ -57,17 +60,17 @@ DynamoDBDOWN.prototype._open = function (options, cb) {

this.dynamoDb = new AWS.DynamoDB(dynamodbOptions)

if (options.createIfMissing) {
if (options.createIfMissing !== false) {
this.createTable({
ProvisionedThroughput: dynamodbOptions.ProvisionedThroughput
}, (err, data) => {
const exists = err && (err.code === 'ResourceInUseException')

if (options.errorIfExists && exists || err && !exists) {
return cb(err)
cb(err)
} else {
cb(null, this)
}

return cb(null, this)
})
} else {
cb(null, this)
Expand Down Expand Up @@ -102,14 +105,12 @@ DynamoDBDOWN.prototype._get = function (key, options, cb) {

this.dynamoDb.getItem(params, function (err, data) {
if (err) {
return cb(err)
}

if (!(data && data.Item && data.Item.value)) {
return cb(new Error('NotFound'))
cb(err)
} else if (!(data && data.Item && data.Item.value)) {
cb(new Error('NotFound'))
} else {
cb(null, deserialize(data.Item.value, options.asBuffer))
}

return cb(null, deserialize(data.Item.value, options.asBuffer))
})
}

Expand All @@ -122,13 +123,7 @@ DynamoDBDOWN.prototype._del = function (key, options, cb) {
}
}

this.dynamoDb.deleteItem(params, function (err, data) {
if (err) {
return cb(err)
}

cb(null, data)
})
this.dynamoDb.deleteItem(params, cb)
}

DynamoDBDOWN.prototype._batch = function (array, options, cb) {
Expand Down Expand Up @@ -183,7 +178,8 @@ DynamoDBDOWN.prototype._batch = function (array, options, cb) {

const loop = (err, data) => {
if (err) {
return cb(err)
cb(err)
return
}

const reqs = []
Expand All @@ -192,14 +188,14 @@ DynamoDBDOWN.prototype._batch = function (array, options, cb) {
reqs.push.apply(reqs, data.UnprocessedItems[this.encodedTableName])
}

reqs.push.apply(reqs, ops.splice(0, 25 - reqs.length))
reqs.push.apply(reqs, ops.splice(0, MAX_BATCH_SIZE - reqs.length))

if (reqs.length === 0) {
return cb()
cb()
} else {
params.RequestItems[this.encodedTableName] = reqs
this.dynamoDb.batchWriteItem(params, loop)
}

params.RequestItems[this.encodedTableName] = reqs
this.dynamoDb.batchWriteItem(params, loop)
}

loop()
Expand Down Expand Up @@ -227,23 +223,38 @@ DynamoDBDOWN.prototype.createTable = function (opts, cb) {
WriteCapacityUnits: 1
}

this.dynamoDb.createTable(params, cb)
this.dynamoDb.createTable(params, (err, data) => {
if (err) {
cb(err)
} else {
this.dynamoDb.waitFor('tableExists', {TableName: this.encodedTableName}, cb)
}
})
}

DynamoDBDOWN.destroy = function (name, callback) {
DynamoDBDOWN.destroy = function (name, cb) {
const store = globalStore[name]

if (store) {
store.dynamoDb.deleteTable({TableName: store.encodedTableName}, (err, data) => {
if (err) {
callback()
} else {
if (err && err.code === 'ResourceNotFoundException') {
delete globalStore[name]
callback()
cb()
} else if (err) {
cb(err)
} else {
store.dynamoDb.waitFor('tableNotExists', {TableName: store.encodedTableName}, (err, data) => {
if (err) {
cb(err)
} else {
delete globalStore[name]
cb()
}
})
}
})
} else {
callback(new Error('NotFound'))
cb(new Error('NotFound'))
}
}

Expand Down
7 changes: 6 additions & 1 deletion iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ DynamoDBIterator.prototype.createReadStream = function (opts) {

const onData = (err, data) => {
if (err) {
return stream.emit('error', err)
if (err.code === 'ResourceNotFoundException') {
stream.end()
} else {
stream.emit('error', err)
}
return stream
}

data.Items.forEach((item) => {
Expand Down
35 changes: 35 additions & 0 deletions test/node_modules/aws-sdk/apis/dynamodb-2012-08-10.waiters2.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 64795e4

Please sign in to comment.