Skip to content

Commit 5d422b8

Browse files
committed
Load table items dynamically and enable nice formatting of data
Loading items dynamically gives impression of better responsiveness with big items that cause slow queries. Using fancy JSON formatter is probably gonna be slower than just using JSON.stringify() but I've deemed it worthy due to much better readability and being able to tell types of item values. Add possibility to go to previous page of results
1 parent 5d64adf commit 5d422b8

File tree

4 files changed

+662
-73
lines changed

4 files changed

+662
-73
lines changed

lib/backend.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,46 @@ exports.createServer = (dynamodb, docClient) => {
335335
app.get('/tables/:TableName', (req, res, next) => {
336336
const TableName = req.params.TableName
337337
req.query = pickBy(req.query)
338-
const filters = omit(req.query, ['_hash', 'range', 'startKey', 'pageNum'])
338+
const filters = omit(req.query, ['_hash', 'range', 'prevKey', 'startKey', 'pageNum'])
339+
340+
describeTable({ TableName })
341+
.then(description => {
342+
const pageNum = req.query.pageNum ? parseInt(req.query.pageNum) : 1
343+
const ExpressionAttributeNames = {}
344+
const ExpressionAttributeValues = {}
345+
const FilterExpressions = []
346+
347+
for (const key in filters) {
348+
const attributeDefinition = description.Table.AttributeDefinitions.find(
349+
definition => {
350+
return definition.AttributeName === key
351+
}
352+
)
353+
if (attributeDefinition && attributeDefinition.AttributeType === 'N') {
354+
req.query[key] = Number(req.query[key])
355+
}
356+
ExpressionAttributeNames[`#${key}`] = key
357+
ExpressionAttributeValues[`:${key}`] = req.query[key]
358+
359+
FilterExpressions.push(`#${key} = :${key}`)
360+
}
361+
362+
const data = Object.assign({}, description, {
363+
query: req.query,
364+
omit,
365+
filters,
366+
pageNum: pageNum,
367+
filterQueryString: querystring.stringify(filters),
368+
})
369+
res.render('scan', data)
370+
})
371+
.catch(next)
372+
})
373+
374+
app.get('/tables/:TableName/items', (req, res, next) => {
375+
const TableName = req.params.TableName
376+
req.query = pickBy(req.query)
377+
const filters = omit(req.query, ['_hash', 'range', 'prevKey', 'startKey', 'pageNum'])
339378

340379
describeTable({ TableName })
341380
.then(description => {
@@ -401,12 +440,14 @@ exports.createServer = (dynamodb, docClient) => {
401440
omit,
402441
filters,
403442
pageNum: pageNum,
443+
prevKey: encodeURIComponent(req.query.prevKey || ''),
444+
startKey: encodeURIComponent(req.query.startKey || ''),
404445
nextKey: nextKeyParam,
405446
filterQueryString: querystring.stringify(filters),
406447
Items,
407448
UniqueKeys,
408449
})
409-
res.render('scan', data)
450+
res.json(data)
410451
}
411452
)
412453
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Mohsen Azimi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

0 commit comments

Comments
 (0)