Skip to content

Commit

Permalink
Update lint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanblock committed Sep 3, 2023
1 parent cecc3e1 commit 246f021
Show file tree
Hide file tree
Showing 59 changed files with 9,072 additions and 8,970 deletions.
46 changes: 0 additions & 46 deletions .eslintrc

This file was deleted.

35 changes: 18 additions & 17 deletions actions/batchGetItem.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
var async = require('async'),
getItem = require('./getItem'),
db = require('../db')
getItem = require('./getItem'),
db = require('../db')

module.exports = function batchGetItem(store, data, cb) {
module.exports = function batchGetItem (store, data, cb) {
var requests = {}

async.series([
async.each.bind(async, Object.keys(data.RequestItems), addTableRequests),
async.parallel.bind(async, requests),
], function(err, responses) {
], function (err, responses) {
if (err) return cb(err)
var res = {Responses: {}, UnprocessedKeys: {}}, table, tableResponses = responses[1], totalSize = 0, capacities = {}
var res = { Responses: {}, UnprocessedKeys: {} }, table, tableResponses = responses[1], totalSize = 0, capacities = {}

for (table in tableResponses) {
// Order is pretty random
// Assign keys before we shuffle
tableResponses[table].forEach(function(tableRes, ix) { tableRes._key = data.RequestItems[table].Keys[ix] }) // eslint-disable-line no-loop-func
tableResponses[table].forEach(function (tableRes, ix) { tableRes._key = data.RequestItems[table].Keys[ix] }) // eslint-disable-line no-loop-func
shuffle(tableResponses[table])
res.Responses[table] = tableResponses[table].map(function(tableRes) { // eslint-disable-line no-loop-func
res.Responses[table] = tableResponses[table].map(function (tableRes) { // eslint-disable-line no-loop-func
if (tableRes.Item) {
// TODO: This is totally inefficient - should fix this
var newSize = totalSize + db.itemSize(tableRes.Item)
if (newSize > (1024 * 1024 + store.options.maxItemSize - 3)) {
if (!res.UnprocessedKeys[table]) {
res.UnprocessedKeys[table] = {Keys: []}
res.UnprocessedKeys[table] = { Keys: [] }
if (data.RequestItems[table].AttributesToGet)
res.UnprocessedKeys[table].AttributesToGet = data.RequestItems[table].AttributesToGet
if (data.RequestItems[table].ConsistentRead)
Expand All @@ -44,46 +44,47 @@ module.exports = function batchGetItem(store, data, cb) {
}).filter(Boolean)
}

if (~['TOTAL', 'INDEXES'].indexOf(data.ReturnConsumedCapacity)) {
res.ConsumedCapacity = Object.keys(tableResponses).map(function(table) {
if (~[ 'TOTAL', 'INDEXES' ].indexOf(data.ReturnConsumedCapacity)) {
res.ConsumedCapacity = Object.keys(tableResponses).map(function (table) {
return {
CapacityUnits: capacities[table],
TableName: table,
Table: data.ReturnConsumedCapacity == 'INDEXES' ? {CapacityUnits: capacities[table]} : undefined,
Table: data.ReturnConsumedCapacity == 'INDEXES' ? { CapacityUnits: capacities[table] } : undefined,
}
})
}

cb(null, res)
})

function addTableRequests(tableName, cb) {
store.getTable(tableName, function(err, table) {
function addTableRequests (tableName, cb) {
store.getTable(tableName, function (err, table) {
if (err) return cb(err)

var req = data.RequestItems[tableName], i, key, options, gets = []

for (i = 0; i < req.Keys.length; i++) {
key = req.Keys[i]

if ((err = db.validateKey(key, table)) != null) return cb(err)
let invalid = db.validateKey(key, table)
if (invalid != null) return cb(invalid)

options = {TableName: tableName, Key: key}
options = { TableName: tableName, Key: key }
if (req._projection) options._projection = req._projection
if (req.AttributesToGet) options.AttributesToGet = req.AttributesToGet
if (req.ConsistentRead) options.ConsistentRead = req.ConsistentRead
if (data.ReturnConsumedCapacity) options.ReturnConsumedCapacity = data.ReturnConsumedCapacity
gets.push(options)
}

requests[tableName] = async.map.bind(async, gets, function(data, cb) { return getItem(store, data, cb) })
requests[tableName] = async.map.bind(async, gets, function (data, cb) { return getItem(store, data, cb) })

cb()
})
}
}

function shuffle(arr) {
function shuffle (arr) {
var i, j, temp
for (i = arr.length - 1; i >= 1; i--) {
j = Math.floor(Math.random() * (i + 1))
Expand Down
35 changes: 19 additions & 16 deletions actions/batchWriteItem.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,67 @@
var async = require('async'),
putItem = require('./putItem'),
deleteItem = require('./deleteItem'),
db = require('../db')
putItem = require('./putItem'),
deleteItem = require('./deleteItem'),
db = require('../db')

module.exports = function batchWriteItem(store, data, cb) {
module.exports = function batchWriteItem (store, data, cb) {
var actions = []

async.series([
async.each.bind(async, Object.keys(data.RequestItems), addTableActions),
async.parallel.bind(async, actions),
], function(err, responses) {
], function (err, responses) {
if (err) {
if (err.body && (/Missing the key/.test(err.body.message) || /Type mismatch for key/.test(err.body.message)))
err.body.message = 'The provided key element does not match the schema'
return cb(err)
}
var res = {UnprocessedItems: {}}, tableUnits = {}
var res = { UnprocessedItems: {} }, tableUnits = {}

if (~['TOTAL', 'INDEXES'].indexOf(data.ReturnConsumedCapacity)) {
responses[1].forEach(function(action) {
if (~[ 'TOTAL', 'INDEXES' ].indexOf(data.ReturnConsumedCapacity)) {
responses[1].forEach(function (action) {
var table = action.ConsumedCapacity.TableName
if (!tableUnits[table]) tableUnits[table] = 0
tableUnits[table] += action.ConsumedCapacity.CapacityUnits
})
res.ConsumedCapacity = Object.keys(tableUnits).map(function(table) {
res.ConsumedCapacity = Object.keys(tableUnits).map(function (table) {
return {
CapacityUnits: tableUnits[table],
TableName: table,
Table: data.ReturnConsumedCapacity == 'INDEXES' ? {CapacityUnits: tableUnits[table]} : undefined,
Table: data.ReturnConsumedCapacity == 'INDEXES' ? { CapacityUnits: tableUnits[table] } : undefined,
}
})
}

cb(null, res)
})

function addTableActions(tableName, cb) {
store.getTable(tableName, function(err, table) {
function addTableActions (tableName, cb) {
store.getTable(tableName, function (err, table) {
if (err) return cb(err)

var reqs = data.RequestItems[tableName], i, req, key, seenKeys = {}, options

for (i = 0; i < reqs.length; i++) {
req = reqs[i]

options = {TableName: tableName}
options = { TableName: tableName }
if (data.ReturnConsumedCapacity) options.ReturnConsumedCapacity = data.ReturnConsumedCapacity

if (req.PutRequest) {

if ((err = db.validateItem(req.PutRequest.Item, table)) != null) return cb(err)
let invalid = db.validateItem(req.PutRequest.Item, table)
if (invalid != null) return cb(invalid)

options.Item = req.PutRequest.Item
actions.push(putItem.bind(null, store, options))

key = db.createKey(options.Item, table)

} else if (req.DeleteRequest) {
}
else if (req.DeleteRequest) {

if ((err = db.validateKey(req.DeleteRequest.Key, table) != null)) return cb(err)
let invalid = db.validateKey(req.DeleteRequest.Key, table)
if (invalid != null) return cb(invalid)

options.Key = req.DeleteRequest.Key
actions.push(deleteItem.bind(null, store, options))
Expand Down
30 changes: 15 additions & 15 deletions actions/createTable.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var crypto = require('crypto')

module.exports = function createTable(store, data, cb) {
module.exports = function createTable (store, data, cb) {

var key = data.TableName, tableDb = store.tableDb

tableDb.lock(key, function(release) {
tableDb.lock(key, function (release) {
cb = release(cb)

tableDb.get(key, function(err) {
tableDb.get(key, function (err) {
if (err && err.name != 'NotFoundError') return cb(err)
if (!err) {
err = new Error
Expand All @@ -24,47 +24,47 @@ module.exports = function createTable(store, data, cb) {
data.CreationDateTime = Date.now() / 1000
data.ItemCount = 0
if (!data.ProvisionedThroughput) {
data.ProvisionedThroughput = {ReadCapacityUnits: 0, WriteCapacityUnits: 0}
data.ProvisionedThroughput = { ReadCapacityUnits: 0, WriteCapacityUnits: 0 }
}
data.ProvisionedThroughput.NumberOfDecreasesToday = 0
data.TableSizeBytes = 0
data.TableStatus = 'CREATING'
if (data.BillingMode == 'PAY_PER_REQUEST') {
data.BillingModeSummary = {BillingMode: 'PAY_PER_REQUEST'}
data.TableThroughputModeSummary = {TableThroughputMode: 'PAY_PER_REQUEST'}
data.BillingModeSummary = { BillingMode: 'PAY_PER_REQUEST' }
data.TableThroughputModeSummary = { TableThroughputMode: 'PAY_PER_REQUEST' }
delete data.BillingMode
}
if (data.LocalSecondaryIndexes) {
data.LocalSecondaryIndexes.forEach(function(index) {
data.LocalSecondaryIndexes.forEach(function (index) {
index.IndexArn = 'arn:aws:dynamodb:' + tableDb.awsRegion + ':' + tableDb.awsAccountId + ':table/' +
data.TableName + '/index/' + index.IndexName
index.IndexSizeBytes = 0
index.ItemCount = 0
})
}
if (data.GlobalSecondaryIndexes) {
data.GlobalSecondaryIndexes.forEach(function(index) {
data.GlobalSecondaryIndexes.forEach(function (index) {
index.IndexArn = 'arn:aws:dynamodb:' + tableDb.awsRegion + ':' + tableDb.awsAccountId + ':table/' +
data.TableName + '/index/' + index.IndexName
index.IndexSizeBytes = 0
index.ItemCount = 0
index.IndexStatus = 'CREATING'
if (!index.ProvisionedThroughput) {
index.ProvisionedThroughput = {ReadCapacityUnits: 0, WriteCapacityUnits: 0}
index.ProvisionedThroughput = { ReadCapacityUnits: 0, WriteCapacityUnits: 0 }
}
index.ProvisionedThroughput.NumberOfDecreasesToday = 0
})
}

tableDb.put(key, data, function(err) {
tableDb.put(key, data, function (err) {
if (err) return cb(err)

setTimeout(function() {
setTimeout(function () {

// Shouldn't need to lock/fetch as nothing should have changed
data.TableStatus = 'ACTIVE'
if (data.GlobalSecondaryIndexes) {
data.GlobalSecondaryIndexes.forEach(function(index) {
data.GlobalSecondaryIndexes.forEach(function (index) {
index.IndexStatus = 'ACTIVE'
})
}
Expand All @@ -73,21 +73,21 @@ module.exports = function createTable(store, data, cb) {
data.BillingModeSummary.LastUpdateToPayPerRequestDateTime = data.CreationDateTime
}

tableDb.put(key, data, function(err) {
tableDb.put(key, data, function (err) {
// eslint-disable-next-line no-console
if (err && !/Database is not open/.test(err)) console.error(err.stack || err)
})

}, store.options.createTableMs)

cb(null, {TableDescription: data})
cb(null, { TableDescription: data })
})
})
})

}

function uuidV4() {
function uuidV4 () {
var bytes = crypto.randomBytes(14).toString('hex')
return bytes.slice(0, 8) + '-' + bytes.slice(8, 12) + '-4' + bytes.slice(13, 16) + '-' +
bytes.slice(16, 20) + '-' + bytes.slice(20, 28)
Expand Down
18 changes: 10 additions & 8 deletions actions/deleteItem.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
var db = require('../db')

module.exports = function deleteItem(store, data, cb) {
module.exports = function deleteItem (store, data, cb) {

store.getTable(data.TableName, function(err, table) {
store.getTable(data.TableName, function (err, table) {
if (err) return cb(err)

if ((err = db.validateKey(data.Key, table)) != null) return cb(err)
let invalid = db.validateKey(data.Key, table)
if (invalid != null) return cb(invalid)

var itemDb = store.getItemDb(data.TableName), key = db.createKey(data.Key, table)

itemDb.lock(key, function(release) {
itemDb.lock(key, function (release) {
cb = release(cb)

itemDb.get(key, function(err, existingItem) {
itemDb.get(key, function (err, existingItem) {
if (err && err.name != 'NotFoundError') return cb(err)

if ((err = db.checkConditional(data, existingItem)) != null) return cb(err)
let invalid = db.checkConditional(data, existingItem)
if (invalid != null) return cb(invalid)

var returnObj = {}

Expand All @@ -24,10 +26,10 @@ module.exports = function deleteItem(store, data, cb) {

returnObj.ConsumedCapacity = db.addConsumedCapacity(data, false, existingItem)

db.updateIndexes(store, table, existingItem, null, function(err) {
db.updateIndexes(store, table, existingItem, null, function (err) {
if (err) return cb(err)

itemDb.del(key, function(err) {
itemDb.del(key, function (err) {
if (err) return cb(err)
cb(null, returnObj)
})
Expand Down
Loading

0 comments on commit 246f021

Please sign in to comment.