Skip to content

Commit

Permalink
Personal changes, sort, pick, omit, double click to filter etc
Browse files Browse the repository at this point in the history
  • Loading branch information
mokkabonna committed Aug 19, 2022
1 parent c5fb6ec commit 98fed57
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 35 deletions.
69 changes: 61 additions & 8 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { purgeTable } = require('./actions/purgeTable')
const asyncMiddleware = require('./utils/asyncMiddleware')
const bodyParser = require('body-parser')
const pickBy = require('lodash.pickby')
const omit = require('lodash.omit');
const clc = require('cli-color')
const cookieParser = require('cookie-parser')
const DEFAULT_THEME = process.env.DEFAULT_THEME || 'light'
Expand Down Expand Up @@ -420,7 +421,8 @@ exports.createServer = (dynamodb, docClient) => {
'<=': '<=',
'>': '>',
'<': '<',
'begins_with': 'begins_with'
'begins_with': 'begins_with',
'contains': 'contains'
},
attributeTypes: {
'S': 'String',
Expand Down Expand Up @@ -486,8 +488,14 @@ exports.createServer = (dynamodb, docClient) => {

if (filters[key].operator === 'begins_with') {
FilterExpressions.push(`${filters[key].operator} ( #key${i} , :key${i})`)
} else if (filters[key].operator === 'contains') {
FilterExpressions.push(
`${filters[key].operator} ( #key${i} , :key${i})`
)
} else {
FilterExpressions.push(`#key${i} ${filters[key].operator} :key${i}`)
FilterExpressions.push(
`#key${i} ${filters[key].operator} :key${i}`
)
}
}
// Increment the unique ID variable
Expand Down Expand Up @@ -522,24 +530,67 @@ exports.createServer = (dynamodb, docClient) => {
: undefined

return getPage(docClient, description.Table.KeySchema, TableName,
params, 25, startKey, req.query.operationType)
params, 100, startKey, req.query.operationType)
.then(results => {
const { pageItems, nextKey } = results

const primaryKeys = description.Table.KeySchema.map(
schema => schema.AttributeName)
const omittedProps = (req.query.omit || '').split(',').map(x => x.trim()).filter(x => x)
const pickedProps = (req.query.pick || '').split(',').map(x => x.trim()).filter(x => x)
const sortBy = req.query.sort
const isDescending = (req.query.direction || 'asc') === 'desc'
let filteredItems = pageItems.map((item) =>
omit(item, omittedProps)
)

if (pickedProps.length) {
filteredItems = filteredItems.map((item) =>
pickBy(item, (val, key) => pickedProps.includes(key) || primaryKeys.includes(key))
)
}

if (sortBy) {
filteredItems.sort((a, b) => {
const aVal = isDescending ? b[sortBy] : a[sortBy]
const bVal = isDescending ? a[sortBy]: b[sortBy]
if (aVal === undefined && bVal === undefined) {
return 0
}
if (aVal === undefined) {
return 1
}
if (bVal === undefined) {
return -1
}
const compare = (aVal || '').localeCompare(bVal || '')
return compare
})
}

const nextKeyParam = nextKey
? encodeURIComponent(JSON.stringify(nextKey))
: null

const primaryKeys = description.Table.KeySchema.map(
schema => schema.AttributeName)
const sortedFirstKeys = ['_type']

const restOfAllKeys = extractKeysForItems(filteredItems)
.filter(key => !primaryKeys.includes(key)
&& !sortedFirstKeys.includes(key)).sort((a, b) => a.localeCompare(b))

const dateKeys = restOfAllKeys.filter(key => /At$/i.test(key))
const restOfKeys = restOfAllKeys.filter(key => !/At$/i.test(key))

// Primary keys are listed first.
const uniqueKeys = [
...primaryKeys,
...extractKeysForItems(pageItems).filter(key => !primaryKeys.includes(key)),
...sortedFirstKeys,
...restOfKeys,
...dateKeys,
]

// Append the item key.
for (const item of pageItems) {
for (const item of filteredItems) {
item.__key = extractKey(item, description.Table.KeySchema)
}

Expand All @@ -550,8 +601,10 @@ exports.createServer = (dynamodb, docClient) => {
startKey: encodeURIComponent(req.query.startKey || ''),
nextKey: nextKeyParam,
filterQueryString: encodeURIComponent(req.query.filters || ''),
Items: pageItems,
Items: filteredItems,
uniqueKeys,
pickedProps,
omittedProps
})

res.json(data)
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bin": "./bin/dynamodb-admin.js",
"scripts": {
"dev": "nodemon bin/dynamodb-admin.js",
"start": "node bin/dynamodb-admin.js",
"start": "DYNAMO_ENDPOINT=http://localhost:5600 node bin/dynamodb-admin.js",
"lint": "eslint --ext .js . && ejslint views",
"fix": "eslint --ext .js --fix .",
"test": "jest",
Expand Down Expand Up @@ -40,6 +40,7 @@
"ejs": "^3.1.6",
"errorhandler": "^1.5.1",
"express": "^4.17.1",
"lodash.omit": "^4.5.0",
"lodash.pickby": "^4.6.0",
"open": "^8.2.1"
},
Expand Down
Loading

0 comments on commit 98fed57

Please sign in to comment.