Comparing version 2.2.4 to 2.3.0
{ | ||
"name": "dynaql", | ||
"version": "2.2.4", | ||
"version": "2.3.0", | ||
"description": "Dynamo library to make interacting with dynamo easier", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -34,2 +34,28 @@ const buildQueryExpression = require('../build-query-expression'); | ||
}); | ||
it('builds custom range key expressions', () => { | ||
let params = buildQueryExpression( | ||
{ | ||
id: '1', | ||
date: { | ||
'>': '2', | ||
'>=': '3', | ||
}, | ||
}, | ||
{ hash: 'id', range: 'date' } | ||
); | ||
expect(params).toEqual({ | ||
KeyConditionExpression: | ||
'#HASH = :HVALUE AND #RANGE > :RVALUE0 AND #RANGE >= :RVALUE1', | ||
ExpressionAttributeNames: { | ||
'#HASH': 'id', | ||
'#RANGE': 'date', | ||
}, | ||
ExpressionAttributeValues: { | ||
':HVALUE': '1', | ||
':RVALUE0': '2', | ||
':RVALUE1': '3', | ||
}, | ||
}); | ||
}); | ||
}); |
@@ -13,5 +13,13 @@ module.exports = function buildExpression(key, index) { | ||
if (key[index.range]) { | ||
params.KeyConditionExpression += ' AND #RANGE = :RVALUE'; | ||
(params.ExpressionAttributeNames['#RANGE'] = index.range), | ||
(params.ExpressionAttributeValues[':RVALUE'] = key[index.range]); | ||
if (typeof key[index.range] === 'object') { | ||
Object.keys(key[index.range]).forEach((rangeKey, i) => { | ||
params.KeyConditionExpression += ` AND #RANGE ${rangeKey} :RVALUE${i}`; | ||
params.ExpressionAttributeValues[`:RVALUE${i}`] = | ||
key[index.range][rangeKey]; | ||
}); | ||
} else { | ||
params.KeyConditionExpression += ' AND #RANGE = :RVALUE'; | ||
params.ExpressionAttributeValues[':RVALUE'] = key[index.range]; | ||
} | ||
params.ExpressionAttributeNames['#RANGE'] = index.range; | ||
} | ||
@@ -18,0 +26,0 @@ |
@@ -8,2 +8,3 @@ const uuid = require('uuid/v4'); | ||
let tableName = 'queryTestTable'; | ||
let rangeTableName = 'rangeTestTable'; | ||
let items = [...Array(5)].map(() => ({ | ||
@@ -13,9 +14,21 @@ id: uuid(), | ||
})); | ||
let rangeItems = [...Array(5)].map((_, i) => ({ | ||
id: '1', | ||
number: i, | ||
})); | ||
beforeAll(async () => { | ||
await testHelpers.createTable(tableName); | ||
await testHelpers.createTableWithRange(rangeTableName); | ||
await writeAll(testHelpers.clientPromise, 'PutRequest', tableName, items); | ||
await writeAll( | ||
testHelpers.clientPromise, | ||
'PutRequest', | ||
rangeTableName, | ||
rangeItems | ||
); | ||
}); | ||
afterAll(async () => { | ||
await testHelpers.deleteTable(tableName); | ||
await testHelpers.deleteTable(rangeTableName); | ||
}); | ||
@@ -40,3 +53,22 @@ | ||
it('can sort forward', async () => {}); | ||
it('can sort forward', async () => { | ||
let { result } = await query( | ||
testHelpers.clientPromise, | ||
rangeTableName, | ||
{ id: '1' }, | ||
{ sortForward: false } | ||
); | ||
expect(result[0].number).toEqual(4); | ||
}); | ||
it('can do a custom query on the range key', async () => { | ||
let { result } = await query(testHelpers.clientPromise, rangeTableName, { | ||
id: '1', | ||
number: { | ||
'>': 3, | ||
}, | ||
}); | ||
expect(result.length).toEqual(1); | ||
expect(result[0].number).toEqual(4); | ||
}); | ||
}); |
@@ -59,2 +59,34 @@ const createClients = require('../src/lib/create-clients'); | ||
async function createTableWithRangeHelper(TableName) { | ||
await createTable(clientPromise, { | ||
TableName, | ||
ProvisionedThroughput: { | ||
ReadCapacityUnits: 1, | ||
WriteCapacityUnits: 1, | ||
}, | ||
KeySchema: [ | ||
{ | ||
AttributeName: 'id', | ||
KeyType: 'HASH', | ||
}, | ||
{ | ||
AttributeName: 'number', | ||
KeyType: 'RANGE', | ||
}, | ||
], | ||
AttributeDefinitions: [ | ||
{ | ||
AttributeName: 'id', | ||
AttributeType: 'S', | ||
}, | ||
{ | ||
AttributeName: 'number', | ||
AttributeType: 'N', | ||
}, | ||
], | ||
}); | ||
await waitFor(clientPromise, TableName, 'tableExists'); | ||
} | ||
async function deleteTableHelper(TableName) { | ||
@@ -67,4 +99,5 @@ await deleteTable(clientPromise, { TableName }); | ||
createTable: createTableHelper, | ||
createTableWithRange: createTableWithRangeHelper, | ||
deleteTable: deleteTableHelper, | ||
clientPromise, | ||
}; |
160039
1197