Skip to content

Commit

Permalink
Actually fixed Model.js Model instance to handle if no data supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
wrenoud committed Sep 9, 2013
1 parent 1314937 commit f8a9edc
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ var index_suffix = '-index';

var Model = function Model(name, hashKey, schema, connection){
function instance(data){
this.data = data;

if(data){
this.data = data;
}else{
this.data = {};
}

// populate the default values
for(var key in schema.paths){
var path = schema.paths[key];
if(path.defaultValue != undefined && ((this.data && this.data[key] == undefined) || this.data == undefined)){
if(path.defaultValue != undefined && this.data && this.data[key] == undefined){
if(typeof path.defaultValue == 'function'){
this.data[key] = path.defaultValue();
}else{
Expand Down Expand Up @@ -57,11 +61,11 @@ var Model = function Model(name, hashKey, schema, connection){
this.connection.putItem(this.modelName, data, _dynamodb_options, function(err,res,cap){
if(err){
if(callback) callback(err, res, cap);
return;
return;
}else{
// no errors...
if(callback) callback(err, res, cap);
return;
return;
}
});

Expand Down Expand Up @@ -110,7 +114,7 @@ var Model = function Model(name, hashKey, schema, connection){
throw Error("Undefined document attribute { " + path +" : " + value + " }");
}
}

try{
for(var path in updates){
switch(path){
Expand All @@ -136,7 +140,7 @@ var Model = function Model(name, hashKey, schema, connection){
prepared_updates[path] = prep_update(path, updates[path], 'PUT');
}
}

key[this.hash] = schema.paths[this.hash].prep(conditions[this.hash]);
if(conditions[this.range]){
key[this.range] = schema.paths[this.range].prep(conditions[this.range]);
Expand All @@ -151,16 +155,16 @@ var Model = function Model(name, hashKey, schema, connection){
this.connection.updateItem(this.modelName, key, prepared_updates, {}, function(err,res,cap){
if(err){
if(callback) callback(err, res, cap);
return;
return;
}else{
// no errors...
if(callback) callback(err, res, cap);
return;
return;
}
});

};

instance.getItem = function(conditions, options, callback){
// apply setters and validation
var schema = this.schema;
Expand Down Expand Up @@ -230,7 +234,7 @@ var Model = function Model(name, hashKey, schema, connection){
});
};
instance.find = instance.query;

// create the table
instance.create = function(throughput, callback){
var connection = this.connection;
Expand Down Expand Up @@ -258,14 +262,14 @@ var Model = function Model(name, hashKey, schema, connection){

if(paths[this.hash]){
var hash = {hash: [
this.hash,
convertType(paths[this.hash].instance)
this.hash,
convertType(paths[this.hash].instance)
]};

if(this.range && paths[this.range]){
hash.range = [
this.range,
convertType(paths[this.range].instance)
this.range,
convertType(paths[this.range].instance)
]
}
// lets handle the LSIs
Expand Down

0 comments on commit f8a9edc

Please sign in to comment.