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

(feat): support for "begins_with" operator searches #196

Merged
merged 10 commits into from
Feb 1, 2022
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
node_modules
yarn.lock
.idea/
.vscode/
.npmrc
26 changes: 20 additions & 6 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ exports.createServer = (dynamodb, docClient) => {
}
}


if (secondaryIndex.IndexType === 'global') {
index.ProvisionedThroughput = {
ReadCapacityUnits: req.body.ReadCapacityUnits,
Expand Down Expand Up @@ -400,6 +399,7 @@ exports.createServer = (dynamodb, docClient) => {
'<=': '<=',
'>': '>',
'<': '<',
'begins_with': 'begins_with'
},
attributeTypes: {
'S': 'String',
Expand Down Expand Up @@ -441,20 +441,33 @@ exports.createServer = (dynamodb, docClient) => {

// Create a variable to uniquely identify each expression attribute
let i = 0

for (const key in filters) {
if (filters[key].type === 'N') {
filters[key].value = Number(filters[key].value)
}

ExpressionAttributeNames[`#key${i}`] = key
ExpressionAttributeValues[`:key${i}`] = filters[key].value
if (indexBeingUsed && indexBeingUsed.KeySchema.find(
(keySchemaItem) => keySchemaItem.AttributeName === key)
) {
KeyConditionExpression.push(`#key${i} ${filters[key].operator} :key${i}`)
const matchedKeySchema = indexBeingUsed ? indexBeingUsed.KeySchema.find(
(keySchemaItem) => keySchemaItem.AttributeName === key) : undefined

if (matchedKeySchema) {
// Only the Range key can support begins_with operator
if (matchedKeySchema.KeyType === 'RANGE' && filters[key].operator === 'begins_with') {
KeyConditionExpression.push(`${filters[key].operator} ( #key${i} , :key${i})`)
} else {
KeyConditionExpression.push(`#key${i} ${filters[key].operator} :key${i}`)
}
} else {
ExpressionAttributeNames[`#key${i}`] = key
ExpressionAttributeValues[`:key${i}`] = filters[key].value
FilterExpressions.push(`#key${i} ${filters[key].operator} :key${i}`)

if (filters[key].operator === 'begins_with') {
FilterExpressions.push(`${filters[key].operator} ( #key${i} , :key${i})`)
} else {
FilterExpressions.push(`#key${i} ${filters[key].operator} :key${i}`)
}
}
// Increment the unique ID variable
i = i + 1
Expand Down Expand Up @@ -658,6 +671,7 @@ exports.createServer = (dynamodb, docClient) => {

return app
}

function isAttributeNotAlreadyCreated(attributeDefinitions, attributeName) {
return !attributeDefinitions
.find(attributeDefinition => attributeDefinition.AttributeName === attributeName)
Expand Down
61 changes: 22 additions & 39 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion views/scan.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
<% } %>
</select>
</td>
<td style="width: 80px">
<td style="width: 140px">
<select class="form-control filter-row-operator">
<% for (const operator in operators) { %>
<option value="<%= operator %>"><%= operators[operator] %></option>
Expand Down