Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix loading document type attributes from DynamoDB when when saveUnknown=true #339

Merged
merged 2 commits into from
Jun 13, 2018
Merged

Fix loading document type attributes from DynamoDB when when saveUnknown=true #339

merged 2 commits into from
Jun 13, 2018

Conversation

jonathonlui
Copy link
Contributor

@jonathonlui jonathonlui commented Apr 13, 2018

This PR fixes an issue when using saveUnknown + useDocumentTypes and trying to retrieve a model that hasn't yet been "used" for example:

  var Cat = new Schema({
    id: Number,
  }, {
    useDocumentTypes: true,
    saveUnknown: true,
  });

  // Assume an item is in the Cat table:
  // {
  //   id: { N: '1' }
  //   petProperties: {
  //     M: {
  //       name: { S: 'Fluffy' },
  //       age: { N: 10 },
  //     }
  //   }
  // }

  Cat.get({ id: 1}};

  // throws SyntaxError: Unexpected token o in JSON at position 1

This is because since the Cat schema doesn't have getProperties attribute Schema.parseDynamo will create an new attribute with an Object type because Attribute.lookupType returns Object for Dynamo "'M" types.

This then causes the Attribute.parseDynamo to be parsed using JSON.parse instead of the correct mapify.

Interestingly this error doesn't happen if the model has been used to create (or put/save) before doing get/query/scan.

  var Cat = new Schema({
    id: Number,
  }, {
    useDocumentTypes: true,
    saveUnknown: true,
  });

  Cat.create({
    id : 1,
    petProperties: {
      name: 'Fluffy',
      age: 10,
    },
  );

  // Cat's schema now has a petProperties attribute

  Cat.get({ id: 1}};

  // No error

This is because when Cat.create runs, it creates the petProperties attribute in the model's schema, and when Cat.get runs, Schema.parseDynamo see that is has petProperties and doesn't try to create a new, incorrectly-typed Attribute.

Copy link
Member

@fishcharlie fishcharlie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fishcharlie
Copy link
Member

Thanks so much for this @jonathonlui. I'm going to have @brandongoode review this at some point before we get it merged in. We will aim to get this in version 0.9. Thanks again!

@fishcharlie fishcharlie added this to the v0.9.0 milestone Apr 15, 2018
@fishcharlie fishcharlie mentioned this pull request Apr 15, 2018
@fishcharlie fishcharlie modified the milestones: v0.9.0, v1.0 Jun 13, 2018
@fishcharlie fishcharlie mentioned this pull request Jun 13, 2018
@fishcharlie fishcharlie merged commit d9a852e into dynamoose:master Jun 13, 2018
@fishcharlie fishcharlie mentioned this pull request Oct 9, 2018
21 tasks
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants