Skip to content

Commit

Permalink
default value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
brianc committed Jul 17, 2011
1 parent 0bc49f5 commit 6037e99
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
48 changes: 30 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//helper function:
//copies all items in 'souce' to 'destination'
//note: this overwrites any currently existing
//note: this overwrites any currently existing
//items in destination
var apply = function(destination, source) {
for (var key in source) {
Expand All @@ -14,22 +14,27 @@ var apply = function(destination, source) {
//takes a config object containing properties
//of the field
var Field = function(config) {
//this allows the field to be called in
//this allows the field to be called in
//the node.js style of `Field(config)` instead
//of the traditional `new Field(config)` style
if (!(this instanceof Field)) {
return new Field(config);
}
//copy all properties of config into this object
apply(this, config);
//don't use 'undefined' as a default value
if(typeof this.defaultValue === 'undefined') {
this.defaultValue = null;
}

}

//whether or not to include this field in
//whether or not to include this field in
//queries which insert data into the models' table
Field.prototype.allowInsert = function() {
if (this.id) return false;
if (this.allowInsert) return true;

return false;
}

Expand All @@ -39,12 +44,19 @@ Field.prototype.allowUpdate = function() {
return !this.id;
}

//returns true if this field is an
//returns true if this field is an
//indentity column, otherwise false
Field.prototype.isIdentity = function() {
return this.id;
}

//used to apply this field's default to a model
Field.prototype.setDefaultValue = function(model) {
if(typeof model[this.name] === 'undefined') {
model[this.name] = this.defaultValue;
}
}

var ModelPrototype = function() {}

//generates an object which contains
Expand All @@ -54,7 +66,7 @@ var ModelPrototype = function() {}
//configuration as its two parameters
var createClassMethods = function(model, config) {
var fields = [];

return {
//adds a field to the model definition
addField: function(field) {
Expand Down Expand Up @@ -83,7 +95,7 @@ var createClassMethods = function(model, config) {
fields.identity.names = fields.identity.map(function(field) {
field.name;
})

return fields;
},
getField: function(name) {
Expand Down Expand Up @@ -112,13 +124,13 @@ var registeredModels = {};
var Model = {
//the logger to use
logger: fakeLogger,

// Retrieves a defined model by it's name
get: function(name) {
return registeredModels[name];
},
//adds a plugin to the Model system which

//adds a plugin to the Model system which
//gets called for each defined model during
//the model's definition...
//this allows a plugin to modifiy the definition of each model
Expand All @@ -128,27 +140,27 @@ var Model = {
handler: handler
})
},

//define's a new model
define: function(name, config) {
Model.logger.debug('[model.js] Defining model ' + name)

var model = function(initialConfig) {
if (!(this instanceof model)) {
return new model(initialConfig);
}

var self = this;
var fields = self.model.getFields();

if (initialConfig) {
for (var i = 0, len = fields.length; i < len; i++) {
var field = fields[i];
this[field.name] = initialConfig[field.name];
}
}
}

config.name = name;
var p = model.prototype;
p.model = model;
Expand All @@ -157,7 +169,7 @@ var Model = {
var idFields = config.fields.filter(function(field) {
return field.id == true;
})

// If no identity field is found, default to id
if (!idFields.length) {
config.fields.unshift({
Expand All @@ -175,8 +187,8 @@ var Model = {
Model.logger.debug('[model.js] Applying plugin ' + plugin.name + ' to model ' + name);
plugin.handler(model, config);
});
registeredModels[name] = model;

registeredModels[name] = model;
return model;
}
}
Expand Down
9 changes: 7 additions & 2 deletions lib/plugin/storage/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ module.exports = function(ctor, config) {
self.model.query({
text: sequel,
values: values,
callback: function(err, users) {
callback: function(err, result) {
if (err) return callback(err);
self.id = users[0].id;
var me = result[0];
self.id = result[0].id;
for(var i = 0, len = insertable.length; i < len; i++) {
var field = insertable[i];
field.setDefaultValue(self)
}
callback(null, self)
}
})
Expand Down
15 changes: 15 additions & 0 deletions test/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,20 @@ test('User model', function(t) {
})
})
})

test('default values', function(t) {
var user = new User({email: 'test@example.com'});
user.create(function(err, u) {
if(err) throw err;
console.log(u);
t.strictEqual(u.firstName, null);
t.strictEqual(u.lastName, null);
t.strictEqual(u.active, false);
pg.end()
t.done();
})

})

t.done();
})

0 comments on commit 6037e99

Please sign in to comment.