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

Generated attributes: Pass current model to attribute default function. #124

Closed
vangorra opened this issue Apr 11, 2017 · 7 comments · Fixed by #127
Closed

Generated attributes: Pass current model to attribute default function. #124

vangorra opened this issue Apr 11, 2017 · 7 comments · Fixed by #127

Comments

@vangorra
Copy link

It would be awesome if we could specify a default function for an attribute and get the current instance of the model as a parameter. That would allow us to populate the secondary index in a better way.

const schema = {
  _id: String,
  country: String,
  state: String,
  surName: String,
  surNameIndex: {
    index: {
      default: model => {
        return `${model.country}_${model.state}_${model.surName}`;
      }
    }
  }
}

Seems passing the model here would do the trick:
https://github.com/automategreen/dynamoose/blob/108fcaa91fc1785dc4d3623231f6b7a16f877dc4/lib/Attribute.js#L280

More generically however, having a forceDefault boolean for the attribute to ensure the default is applied every time would also be useful. This would ensure the secondary index data is properly updated when the data is persisted.

@brandongoode
Copy link
Contributor

Passing the model sounds like a great idea. Want to submit it as a PR?

Not sure I follow the forceDefault. Would this overwrite the value with the default, regardless of if there's currently a value?

Also, default would not be in the index. It should be applied directly to the attribute:

const schema = {
  _id: String,
  country: String,
  state: String,
  surName: String,
  surNameIndex: {
    index: true,
    default: model => {
      return `${model.country}_${model.state}_${model.surName}`;
    }
  }
}

@vangorra
Copy link
Author

vangorra commented Apr 12, 2017 via email

@troyswanson
Copy link
Contributor

Is the idea for the generated value be stored in the DynamoDB table?

@vangorra
Copy link
Author

@troyswanson Yes.

@troyswanson
Copy link
Contributor

Would it be possible for something like #120 (comment) to be included with this?

@vangorra
Copy link
Author

That looks out of scope of what I'm trying to accomplish here.

@troyswanson
Copy link
Contributor

Fair enough.

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

Successfully merging a pull request may close this issue.

3 participants