Skip to content
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

Add raw mode #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
130 changes: 89 additions & 41 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')
const fs = require('fs')
const os = require('os')
const errorhandler = require('errorhandler')
const { extractKey, extractKeysForItems, parseKey, doSearch } = require('./util')
const { extractKey, extractKeysForItems, parseKey, doSearch, parseRawKey} = require('./util')
const { purgeTable } = require('./actions/purgeTable')
const asyncMiddleware = require('./utils/asyncMiddleware')
const bodyParser = require('body-parser')
Expand Down Expand Up @@ -129,6 +129,8 @@ exports.createServer = (dynamodb, docClient) => {

const listTables = (...args) => dynamodb.listTables(...args).promise()
const describeTable = (...args) => dynamodb.describeTable(...args).promise()
const getRawItem = (...args) => dynamodb.getItem(...args).promise()
const putRawItem = (...args) => dynamodb.putItem(...args).promise()
const getItem = (...args) => docClient.get(...args).promise()
const putItem = (...args) => docClient.put(...args).promise()
const deleteItem = (...args) => docClient.delete(...args).promise()
Expand Down Expand Up @@ -553,7 +555,7 @@ exports.createServer = (dynamodb, docClient) => {
})
}))

app.delete('/tables/:TableName/items/:key', asyncMiddleware((req, res) => {
app.delete('/tables/:TableName/*items/:key', asyncMiddleware((req, res) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In raw page, the delete requests are send as DELETE /tables/:TableName/raw-items/:key.

const TableName = req.params.TableName
return describeTable({ TableName })
.then(result => {
Expand All @@ -568,7 +570,7 @@ exports.createServer = (dynamodb, docClient) => {
})
}))

app.get('/tables/:TableName/add-item', asyncMiddleware((req, res) => {
function addItemPage(req, res, isRaw) {
const TableName = req.params.TableName
return describeTable({ TableName })
.then(result => {
Expand All @@ -578,90 +580,136 @@ exports.createServer = (dynamodb, docClient) => {
const definition = table.AttributeDefinitions.find(attribute => {
return attribute.AttributeName === key.AttributeName
})
Item[key.AttributeName] = definition.AttributeType === 'S' ? '' : 0

if (isRaw) {
Item[key.AttributeName] = definition.AttributeType === 'S' ? {S: ''} : {'N': 0}
} else {
Item[key.AttributeName] = definition.AttributeType === 'S' ? '' : 0
}
})

res.render('item', {
Table: table,
isRaw: isRaw,
TableName: req.params.TableName,
Item: Item,
isNew: true
})
})
}

app.get('/tables/:TableName/add-raw-item', asyncMiddleware((req, res) => {
addItemPage(req, res, true)
}))

app.get('/tables/:TableName/items/:key', asyncMiddleware((req, res) => {
app.get('/tables/:TableName/add-item', asyncMiddleware((req, res) => {
addItemPage(req, res, false)
}))

function getItemPage(req, res, isRaw) {
const TableName = req.params.TableName
return describeTable({ TableName })
.then(result => {
const params = {
TableName,
Key: parseKey(req.params.key, result.Table)
Key: (isRaw ? parseRawKey : parseKey)(req.params.key, result.Table)
}

return getItem(params).then(response => {
return (isRaw ? getRawItem : getItem)(params).then(response => {
if (!response.Item) {
return res.status(404).send('Not found')
}
res.render('item', {
Table: result.Table,
isRaw: isRaw,
TableName: req.params.TableName,
Item: response.Item,
isNew: false
})
})
})
}

app.get('/tables/:TableName/items/:key', asyncMiddleware((req, res) => {
return getItemPage(req, res, false)
}))

app.put(
'/tables/:TableName/add-item',
bodyParser.json({ limit: '500kb' }),
asyncMiddleware((req, res) => {
const TableName = req.params.TableName
return describeTable({ TableName })
.then(description => {
app.get('/tables/:TableName/raw-items/:key', asyncMiddleware((req, res) => {
return getItemPage(req, res, true)
}))

function createNewItem(req, res, isRaw) {
const TableName = req.params.TableName
return describeTable({ TableName })
.then(description => {
const params = {
TableName,
Item: req.body
}

return (isRaw ? putRawItem : putRawItem)(params).then(() => {
const Key = extractKey(req.body, description.Table.KeySchema)
const params = {
TableName,
Item: req.body
Key
}

return putItem(params).then(() => {
const Key = extractKey(req.body, description.Table.KeySchema)
const params = {
TableName,
Key
return (isRaw ? getRawItem : getItem)(params).then(response => {
if (!response.Item) {
return res.status(404).send('Not found')
}
return getItem(params).then(response => {
if (!response.Item) {
return res.status(404).send('Not found')
}
return res.json(Key)
})
return res.json(Key)
})
})
})
}

app.put(
'/tables/:TableName/add-item',
bodyParser.json({ limit: '500kb' }),
asyncMiddleware((req, res) => {
createNewItem(req, res, false)
}))

app.put(
'/tables/:TableName/items/:key',
'/tables/:TableName/add-raw-item',
bodyParser.json({ limit: '500kb' }),
asyncMiddleware((req, res) => {
const TableName = req.params.TableName
return describeTable({ TableName })
.then(result => {
createNewItem(req, res, true)
}))

function updateItem(req, res, isRaw) {
const TableName = req.params.TableName
return describeTable({ TableName })
.then(result => {
const params = {
TableName,
Item: req.body
}

return (isRaw ? putRawItem : putItem)(params).then(() => {
const params = {
TableName,
Item: req.body
Key: parseRawKey(req.params.key, result.Table)
}

return putItem(params).then(() => {
const params = {
TableName,
Key: parseKey(req.params.key, result.Table)
}
return getItem(params).then(response => {
return res.json(response.Item)
})
return (isRaw ? getRawItem : getItem)(params).then(response => {
return res.json(response.Item)
})
})
})
}

app.put(
'/tables/:TableName/raw-items/:key',
bodyParser.json({ limit: '500kb' }),
asyncMiddleware((req, res) => {
updateItem(req, res, true)
}))

app.put(
'/tables/:TableName/items/:key',
bodyParser.json({ limit: '500kb' }),
asyncMiddleware((req, res) => {
updateItem(req, res, false)
}))

app.use((err, req, res, next) => {
Expand Down
42 changes: 32 additions & 10 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ exports.extractKey = function(item, KeySchema) {
}

exports.parseKey = function(keys, table) {
const splitKeys = keys.split(',')
return doParseKey(keys, table, typecastKey)
}

return table.KeySchema.reduce((prev, current, index) => {
return Object.assign({}, prev, {
[current.AttributeName]: typecastKey(
current.AttributeName,
splitKeys[index],
table
)
})
}, {})
exports.parseRawKey = function(keys, table) {
return doParseKey(keys, table, typecastRawKey)
}

exports.extractKeysForItems = function(Items) {
Expand Down Expand Up @@ -110,6 +104,34 @@ function doSearch(docClient, tableName, scanParams, limit, startKey, progress,
return getNextBite(params)
}

function doParseKey(keys, table, typecastFunction) {
const splitKeys = keys.split(',')

return table.KeySchema.reduce((prev, current, index) => {
return Object.assign({}, prev, {
[current.AttributeName]: typecastFunction(
current.AttributeName,
splitKeys[index],
table
)
})
}, {})
}

function typecastRawKey(keyName, keyValue, table) {
const definition = table.AttributeDefinitions.find(attribute => {
return attribute.AttributeName === keyName
})
if (definition) {
switch (definition.AttributeType) {
case 'N':
return {'N': Number(keyValue) }
case 'S':
return { 'S': String(keyValue) }
}
}
return { 'S': String(keyValue) }
}

function typecastKey(keyName, keyValue, table) {
const definition = table.AttributeDefinitions.find(attribute => {
Expand Down
21 changes: 21 additions & 0 deletions views/item.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
})
}
</script>
<div>
<input type="checkbox"
id='rawCheckbox'
onclick='handleCheckboxClick()'
<% if (isRaw) { %>
checked
<% } %>
> Raw
</div>
<button
class='btn btn-primary'
id='saveButton'
Expand All @@ -79,6 +88,18 @@
</button>

<script>
function handleCheckboxClick (event) {
const checked = document.getElementById('rawCheckbox').checked
if (!checked && <%= isRaw %> && <%= !isNew %>) {
window.location.href = window.location.href.replace('/raw-items/', '/items/')
} else if (checked && <%= !isRaw %> && <%= !isNew %>) {
window.location.href = window.location.href.replace('/items/', '/raw-items/')
} else if (!checked && <%= isRaw %> && <%= isNew %>) {
window.location.href = window.location.href.replace('/add-raw-item', '/add-item')
} else if (checked && <%= !isRaw %> && <%= isNew %>) {
window.location.href = window.location.href.replace('/add-item', '/add-raw-item')
}
}
function handleDeleteClick (event) {
event.preventDefault()
fetch(document.location.pathname, {
Expand Down
3 changes: 2 additions & 1 deletion views/scan.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@
if (data.Items.length) {
$('#items-container').append(data.Items.map(item => {
const viewUrl = '/tables/<%= Table.TableName %>/items/' + encodeURIComponent(Object.values(item.__key).join(','))
const rowEl = $('<tr><td><a href="' + viewUrl + '">View</a></td></tr>')
const viewRawUrl = '/tables/<%= Table.TableName %>/raw-items/' + encodeURIComponent(Object.values(item.__key).join(','))
const rowEl = $('<tr><td><div class="btn-group"><a class="btn btn-primary" href="' + viewUrl + '">View</a><a class="btn btn-primary" href="' + viewRawUrl + '">View Raw</a></div></td></tr>')

for (const column of data.uniqueKeys) {
const columnEl = $('<td></td>')
Expand Down