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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when accessing nested objects #158

Open
pmuens opened this issue Jun 19, 2018 · 1 comment
Open

Error when accessing nested objects #158

pmuens opened this issue Jun 19, 2018 · 1 comment

Comments

@pmuens
Copy link

pmuens commented Jun 19, 2018

Hey there 馃憢
Dynogels is pretty awesome! However I get the following error when trying to project fields in a deeply nested object via attributes:

ExpressionAttributeNames contains invalid key: Syntax error; key: \"#data.provider.name\"

Here's the query I'm constructing with Dynogels:

const query = this.store
  .query('some-service')
  .filter('application')
  .equals(application)
  .attributes(['name', 'data.provider.name'])
  .descending()
  .loadAll()

I think I can pin point the issue to the expressionAttributeNames creation here:

https://github.com/clarkie/dynogels/blob/v9.0.0/lib/query.js#L166-L171

Looks like the generated #data.provider.name doesn't validate since it includes dots.
Creating something like #dataProviderName when . are detected could be a potential solution here.

@pmuens
Copy link
Author

pmuens commented Jun 19, 2018

Just looked into this and here's the quick hack I use to solved it for now:

const { reduce, assoc, merge } = require('ramda')

function createExpressionAttributeNames (attributes, attributeNamesObject = {}) {
  return reduce(
    (accum, attribute) => {
      if (attribute.includes('.')) {
        const subAttributes = attribute.split('.')
        return merge(
          reduce(
            (accum, subAttribute) =>
              assoc(`#${subAttribute}`, subAttribute, accum),
            {},
            subAttributes
          ),
          accum
        )
      }

      return assoc(`#${attribute}`, attribute, accum)
    },
    attributeNamesObject,
    attributes
  )
}

module.exports = createExpressionAttributeNames
const { map } = require('ramda')

function createProjectionExpression (attributes) {
  return map(attribute => {
    if (attribute.includes('.')) {
      const attributeParts = attribute.split('.')
      const preparedAttributeParts = map(
        attributePart => `#${attributePart}`,
        attributeParts
      )
      return preparedAttributeParts.join('.')
    }
    return `#${attribute}`
  }, attributes).join(',')
}

module.exports = createProjectionExpression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant