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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nested attr paths (dot notation) for exists conditions #288

Merged

Conversation

rbdmorgan
Copy link
Contributor

@rbdmorgan rbdmorgan commented Jul 15, 2022

Fixes #214 by allowing nested attr paths (dot notation) for exists conditions.

e.g. currently this:

{
  conditions: {
    attr: 'a.b.c',
    exists: true
  }
}

Generates this (which doesn't work):

{
  ExpressionAttributeNames: {
    '#attr1': 'a.b.c'
  },
  ConditionExpression: 'attribute_exists(#attr_1)'
}

With this PR, it will instead generate this (which does work):

{
  ExpressionAttributeNames: {
    '#attr1_0': 'a',
    '#attr1_1': 'b',
    '#attr1_2': 'c'
  },
  ConditionExpression: 'attribute_exists(#attr_1_0.#attr_1_1.#attr_1_2)'
}

The AWS docs suggest that attribute_exists(a.b.c) should work but it doesn't, at least not via the JS SDK. I found out about the above syntax and workaround on Stack Overflow!

Although I only need this fixed for the exists conditions, for completeness it looks like I probably should implement this for the between, in, begins_with, contains, attribute_type and size conditions/operators as well.

Keen to hear thoughts!

Just to add - I considered using arrays for nested attr paths, e.g.

{
  conditions: {
    attr: ['a', 'b', 'c'],
    exists: true
  }
}

As technically, you could have a valid (non-map) attribute key that is 'a.b.c' and want to check if that exists.

However, I don't think many people use that syntax, and it seems dot notation is assumed as the convention for nested attributes in checkAttribute(), and I didn't think it made sense to introduce a different convention here.

@jeremydaly
Copy link
Collaborator

Thanks @rbdmorgan! This is great. I'll give it a look.

@rbdmorgan
Copy link
Contributor Author

@jeremydaly anything I can do to help move this along?

I'm happy to implement this for the other operators as well (between, in etc) but didn't want to go ahead unless you're happy with the approach I've taken.

Hoping to get this merged soon as I need it for a project I'm working on (currently I have these conditions commented out!)

Very much appreciated 😄

@jeremydaly
Copy link
Collaborator

HI @rbdmorgan, got a little overwhelmed last week. I'll review this today and try to get it merged! Then if you can extend to the other operators, that'd be great!

@rbdmorgan
Copy link
Contributor Author

@jeremydaly that would be immensely appreciated if you get time!

And then I'll extend to the other operators over the next day or two 👍🏼

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

Successfully merging this pull request may close these issues.

Conditions on nested fields do not work
2 participants