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

Does Dynamoose support saving non attribute data? #107

Closed
kcjl519 opened this issue Feb 8, 2017 · 6 comments
Closed

Does Dynamoose support saving non attribute data? #107

kcjl519 opened this issue Feb 8, 2017 · 6 comments

Comments

@kcjl519
Copy link

kcjl519 commented Feb 8, 2017

I have defined some basic attributes of the model, for example.

var Cat = dynamoose.model('Cat', { id: Number, name: String });

But when I do something like

var oscar = new Cat({id: 1, name: 'oscar', info: {some: 1, other: 2, details: 3}});
oscar.save()

info, never gets saved. How can I save data that's not defined in the schema?

@brandongoode
Copy link
Contributor

Dynamoose doesn't support saving data not defined in the schema.

It could be done but would required modifying Schema.prototype.toDynamo and Schema.prototype.parseDynamo to handled all properties and apply a default Object attribute to the properties that don't match.

Something like the changes below. If you have time to test and document, I would be happy to merge a PR.

Schema.prototype.toDynamo = function(model) {

  var dynamoObj = {};

  for(var name in model) {
    var attr = this.attributes[name];

    if(!attr & this.options.saveUnknown) {
      attr = Attribute.create(this, name, Object);
      this.attributes[name] = attr;
    }

    if(attr) {
      attr.setDefault(model);
      var dynamoAttr = attr.toDynamo(model[name], undefined, model);
      if(dynamoAttr) {
        dynamoObj[attr.name] = dynamoAttr;
      }
    }
  }

  debug('toDynamo: %s', JSON.stringify(dynamoObj) );
  return dynamoObj;
};

Schema.prototype.parseDynamo = function(model, dynamoObj) {

  for(var name in dynamoObj) {
    var attr = this.attributes[name];

    if(!attr & this.options.saveUnknown) {
      attr = Attribute.create(this, name, Object);
      this.attributes[name] = attr;
    }

    if(attr) {
      var attrVal = attr.parseDynamo(dynamoObj[name]);
      if(attrVal !== undefined && attrVal !== null){
        model[name] = attrVal;
      }
    }
  }

  debug('parseDynamo: %s',JSON.stringify(model));

  return dynamoObj;

};

@kcjl519
Copy link
Author

kcjl519 commented Feb 11, 2017

Thanks! I'll have a look later next week.

@kcjl519
Copy link
Author

kcjl519 commented Feb 20, 2017

I'm assuming I'd also have to modify Model.js Operations.getUpdateExpression ?

@kcjl519
Copy link
Author

kcjl519 commented Feb 20, 2017

Upon some testing, it seems this involves a bigger change.

Hit problems where the data was not injected into the putSet, or were new attributes being created on the fly.

@brandongoode
Copy link
Contributor

brandongoode commented Feb 20, 2017

I don't think any changes are needed in the getUpdateExpression function, but changes are needed to the 3 loops that inject the changes in to the operations object.

The putAttr, deleteAttr, and addAttr all need to have the same check applied as in toDynamo above:

if(!attr & this.options.saveUnknown) {
  attr = Attribute.create(this, name, Object);
  this.attributes[name] = attr;
}

If you push your the changes to your fork, I would be happy to look them over and see if i can spot the issue.

@sdrioux
Copy link

sdrioux commented Apr 4, 2017

Bump. Ran into this same issue. This would be an awesome addition.

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

3 participants