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

Nested Attributes in Dynamoose #1589

Open
fishcharlie opened this issue Apr 16, 2023 · 2 comments
Open

Nested Attributes in Dynamoose #1589

fishcharlie opened this issue Apr 16, 2023 · 2 comments

Comments

@fishcharlie
Copy link
Member

Nested Attributes aren't living up to what users expect in Dynamoose. Almost anytime you want to reference a nested attribute in Dynamoose, it fails. This includes conditional logic, updating a nested attribute, projected attributes (only returning a nested attribute from an item), etc.

From a syntax perspective, it's possible to represent these nested attributes using a .. However, the question then becomes what if a non nested attribute has a . in its attribute name. Dynamoose somehow has to be smart enough to handle that.

We could of course block usage of .'s in attribute names, but that would be a breaking change. Additionally, it would be a breaking change without recourse for users (ie. there'd be no way around it).

Any feedback here would be greatly appreciated. How should Dynamoose handle this? How should we handle the edge cases that get exposed here?

One more side note, we do represent nested attributes in saveUnknown using .'s. Which I'm not sure was thought about enough in terms of edge cases.

References:

@lewjc
Copy link

lewjc commented Aug 2, 2023

Do you need to block it, or can you set a preference of taking properties with a dot in their name over nested objects. I think that some responsibility should be put onto the user defining their data models, its quite easy to avoid the situation where we have something like the below I think.

const schema = new dynamoose.Schema(
  {
     "hello.world": String,
     "hello: {
          "world": String
      }
  }
);

When performing an action on the model, we can do an iterative check against the dots, with a preference of taking keys with dots in them first. e.g.

// Using above schema

const attributes = ["hello.world"];

// This will only take the hello.world attribute.
Model.query({ }).attributes(attributes)

// When attributes are mapped somewhere in the code.. 
for(const attribute of attributes) {
  if(attribute.includes(".")) {
     if(attribute in model.schemal { // will return true for a key of hello.world and false for a key of hello, with a child object with { world: string }
      // This is what we take first,
    } else {
      attribute.split(".")[1] // take the next child(s)
      .. call this function again and check for the presence of 'world', if we had some key like 'hello.world.foo'
     }
  }
}

I haven't checked the code so I'm sorry if this isn't too applicable, it was just a thought I had when looking through the docs / trying to use nested keys.

@rhh4x0r
Copy link

rhh4x0r commented Aug 28, 2023

Hey y'all, any update on being able to set nested attributes within a map? I have the schema correctly but still can't do set mapname.attribute. It just creates "setname.attribute" in the db.

Looking through the docs and issues here I haven't found any solutions here.

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

No branches or pull requests

3 participants