Skip to content

Instantly share code, notes, and snippets.

@mfbx9da4
Created October 4, 2021 21:06
Show Gist options
  • Save mfbx9da4/d379a3da0f2034a59bf798d0fcc9ba19 to your computer and use it in GitHub Desktop.
Save mfbx9da4/d379a3da0f2034a59bf798d0fcc9ba19 to your computer and use it in GitHub Desktop.
import AWS from 'aws-sdk'
import { uuid } from 'short-uuid'
import { yesno } from 'yesno-http'
yesno.spy()
const documentClient = new AWS.DynamoDB.DocumentClient({
apiVersion: 'latest',
})
const putItem = (Item: any, table: string) =>
documentClient.put({ TableName: table, Item: Item }).promise()
const getItem = (Key: any, table: string) =>
documentClient.get({ TableName: table, Key, ConsistentRead: true }).promise()
const transactPutItem = (Item: any, table: string) =>
documentClient
.transactWrite({ TransactItems: [{ Put: { TableName: table, Item: Item } }] })
.promise()
const deleteItem = (Key: any, table: string) =>
documentClient.delete({ TableName: table, Key }).promise()
const runTest = async (i: number) => {
const tableName = 'onin-dev-Accounts-2'
const item = { accountID: uuid() }
console.log('putting', i)
const logIntercepted = () => {
const i = yesno.intercepted()
console.log(
'i.',
JSON.stringify(
i.map(x => ({ id: x.__id, status: x.response })),
null,
2
)
)
yesno.clear()
}
await transactPutItem(item, tableName).catch(e => {
console.log(e)
logIntercepted()
throw e
})
logIntercepted()
}
export const transactionTest2 = async () => {
const errors = []
const iters = Array.from({ length: 200 }).map((x, i) => i)
for (const i of iters) {
await runTest(i).catch(e => errors.push(e))
}
console.log('errors', errors.length)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment