Skip to content

Commit 28e588d

Browse files
authored
test(table/query): add test for nested attribute filters (dynamodb-toolbox#297)
1 parent d69d8ce commit 28e588d

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/__tests__/table.query.unit.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,50 @@ describe('query', () => {
130130
})
131131
})
132132

133+
it('queries a table with options including a nested attribute filter', () => {
134+
let result = TestTable.queryParams('test', {
135+
index: 'GSI1',
136+
limit: 10,
137+
reverse: true,
138+
consistent: true,
139+
capacity: 'total',
140+
select: 'all_attributes',
141+
eq: 'skVal',
142+
filters: { attr: 'test.a.b', eq: 'testFilter' },
143+
attributes: ['pk', 'sk', 'test'],
144+
startKey: { pk: 'test', sk: 'skVal2' },
145+
entity: TestEntity.name,
146+
execute: true,
147+
parse: true
148+
})
149+
150+
expect(result).toEqual({
151+
TableName: 'test-table',
152+
KeyConditionExpression: '#pk = :pk and #sk = :sk',
153+
ExpressionAttributeNames: {
154+
'#pk': 'GSI1pk',
155+
'#sk': 'GSIsk1',
156+
'#attr1_0': 'test',
157+
'#attr1_1': 'a',
158+
'#attr1_2': 'b',
159+
'#proj1': 'pk',
160+
'#proj2': 'sk',
161+
'#proj3': 'test',
162+
'#proj4': '_et'
163+
},
164+
ExpressionAttributeValues: { ':pk': 'test', ':sk': 'skVal', ':attr1': 'testFilter' },
165+
FilterExpression: '#attr1_0.#attr1_1.#attr1_2 = :attr1',
166+
ProjectionExpression: '#proj1,#proj2,#proj3,#proj4',
167+
IndexName: 'GSI1',
168+
Limit: '10',
169+
ScanIndexForward: false,
170+
ConsistentRead: true,
171+
ReturnConsumedCapacity: 'TOTAL',
172+
Select: 'ALL_ATTRIBUTES',
173+
ExclusiveStartKey: { pk: 'test', sk: 'skVal2' }
174+
})
175+
})
176+
133177
it('fails on an invalid option', () => {
134178
expect(() =>
135179
TestTable.queryParams(

src/lib/expressionBuilder.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,7 @@ const parseClause = <EntityTable extends TableDef | undefined = undefined>(
254254
// Add values and special key condition
255255
values[`:attr${grp}_0`] = value[0]
256256
values[`:attr${grp}_1`] = value[1]
257-
clause = `${
258-
size ? `size(${operand})` : `${operand}`
259-
} between :attr${grp}_0 and :attr${grp}_1`
257+
clause = `${size ? `size(${operand})` : operand} between :attr${grp}_0 and :attr${grp}_1`
260258
} else {
261259
error(`'between' conditions require an array with two values.`)
262260
}
@@ -292,7 +290,7 @@ const parseClause = <EntityTable extends TableDef | undefined = undefined>(
292290
// TODO: validate/convert types
293291
clause = `attribute_type(${operand},:attr${grp})`
294292
} else {
295-
clause = `${size ? `size(${operand})` : `${operand}`} ${operator} :attr${grp}`
293+
clause = `${size ? `size(${operand})` : operand} ${operator} :attr${grp}`
296294
}
297295
} // end if-else
298296

0 commit comments

Comments
 (0)