node.jsamazon-dynamodbsails.jsadaptervogels

How to scan or query a Dynamodb table using Vogels by passing a JSON object of conditions?


I'm trying to implement a custom sails-dynamodb adapter, similar to this one but compatible with the new Sails version v1.0.

I got stuck in implementing the find() function, trying to use the passed map of conditions (the query.criteria.where parameter), to build a dynamic query to the Dynamodb database using Vogels library.

If I try building the query manually it works:

model.scan().where('email').equals('xyz@abc.com').exec((err, result) => {
  if (err) {
    sails.log.error(err);
  } else {
    sails.log.info(result);
  }
});

But what I'm trying to achieve is something similar to this:

model.scan({email: 'xyz@abc.com'}).exec((err, result) => {
  if (err) {
    sails.log.error(err);
  } else {
    sails.log.info(result);
  }
});

Sails version v1.0.2
Vogels version v2.2.0


Solution

  • Here's a work around for this issue, where I'm iterating over the query's where conditions one by one, and appending them to the scanner object.

    var _ = require('@sailshq/lodash');
    
    let scanner = model.scan();
    
    // Build query conditions
    _.forOwn(sq3.criteria.where, (value, key) => {
      scanner.where(key).equals(value);
    });
    
    scanner.exec((err, result) => {
      if (err) {
        sails.log.error(err);
      } else {
        sails.log.info(result);
      }
    });