1

I am trying to optimise the following scans into a single scan (or query). The only way I see is to use to use a OR comparator using DynamoDB. I am using dynogels (fork of vogels) in my application but sadly I am not aware of any OR query functionality in there.

let arrivals = yield Reservation.scan()
  .where('room').equals(room)
  .where('arrival').between(from, to)
  .execAsync().then((reply) => reply.Items.map((item) => item.get()));

let departures = yield Reservation.scan()
  .where('room').equals(room)
  .where('departure').between(from, to)
  .execAsync().then((reply) => reply.Items.map((item) => item.get()));

let combined = arrivals.concat(departures);
return Promise.resolve(combined);

Proposed optimisation:

return Reservation.scan()
  .where('room').equals(room)
  .where('arrival').between(from, to)
  .or.where('departure').between(from, to)
  .execAsync().then((reply) => reply.Items.map((item) => item.get()));

The scans get me the reservations that end (departure) or start (arrival) (or both) in a specified date range (from, to).

Community
  • 1
  • 1
btype
  • 1,593
  • 19
  • 32

1 Answers1

3

I think this can be achieved using filterexpression.

Sample code using filterexpression:-

const scanItem = Reservation.scan().filterExpression('room = :idVal AND ((arrival BETWEEN :arrDateVal1 AND :arrDateVal2) OR (departure BETWEEN :depDateVal1 AND :depDateVal2)) ')
                .expressionAttributeValues({ ':idVal' : '2', ':arrDateVal1' : '21-APR-2017', ':arrDateVal2' : '23-APR-2017'
            ,':depDateVal1' : '21-APR-2017', ':depDateVal2' : '23-APR-2017'});                

scanItem.exec((err, result) => {if(!err) {console.log(JSON.stringify(result,undefined, 2))}});

Note:-

Not sure whether you wanted to specifically use where rather than filterexpression (or) just wanted to achieve the result regardless of where or filterexpression.

notionquest
  • 37,595
  • 6
  • 111
  • 105
  • Thanks! Will try it out. Wasn't aware of the `filterExpression` syntax and just used the nice `dynogels` provided syntax. – btype Apr 25 '17 at 14:17
  • I made a pull request to add this feature in, and im dependent on this feature so you might like it: https://github.com/sp90/dynogels – Simon Dragsbæk Jun 29 '18 at 08:47