Skip to content

Commit

Permalink
Merge branch 'master' of github.com:nearinfinity/node-persist
Browse files Browse the repository at this point in the history
  • Loading branch information
joeferner committed Jun 22, 2012
2 parents 47097d1 + d84a4ef commit fadf65e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -66,6 +66,7 @@ You can install using Node Package Manager (npm):
* [delete](#modelDelete)
* [getById](#modelGetById)
* [onSave](#modelOnSave)
* [onLoad](#modelOnLoad)
* [Associated Object Properties](#associatedObjectProperties)

## Query
Expand Down Expand Up @@ -504,6 +505,22 @@ __Example__
obj.lastUpdated = new Date();
};

<a name="modelOnLoad" />
### Model.onLoad(obj)

If preset this function will be called after an object is loaded from the database. You would typically
create this method in your model file.

__Arguments__

* obj - The object that was just loaded from the database.

__Example__

Person.onLoad = function(obj) {
obj.fullName = obj.firstName + ' ' + obj.lastName;
};

<a name="associatedObjectProperties" />
### Associated Object Properties

Expand Down
5 changes: 4 additions & 1 deletion lib/sqltree.js
Expand Up @@ -28,7 +28,7 @@ var SqlTree = Class.extend({
for (i = 0; i < this.columns.length; i++) {
var column = this.columns[i];
if ((column.model.modelName === modelName || column.model.tableName === modelName)
&& (column.propertyName === columnName || column.dbColumnName === columnName)) {
&& (column.propertyName === columnName || column.dbColumnName === columnName)) {
return column;
}
}
Expand Down Expand Up @@ -153,6 +153,9 @@ var SqlTree = Class.extend({
}
result[column.propertyName] = val;
}
if (result._getModel().onLoad) {
result._getModel().onLoad(result);
}
return result;
}
});
Expand Down
21 changes: 13 additions & 8 deletions test/select.js
Expand Up @@ -17,6 +17,10 @@ exports['Select'] = nodeunit.testCase({
"age": type.INTEGER
}).hasMany(this.Phone);

this.Person.onLoad = function (person) {
person.nameAndAge = person.name + ": " + person.age;
};

this.Company = persist.define("Company", {
"name": type.STRING
}).hasMany(this.Person, { through: "CompanyPerson" });
Expand Down Expand Up @@ -61,7 +65,8 @@ exports['Select'] = nodeunit.testCase({
}
test.equals(people.length, 2);
test.equals(people[0].name, "Bob O'Neill");
test.equals(JSON.stringify(people[0]), '{"phones":{},"companies":{},"modifiedBy":{},"name":"Bob O\'Neill","age":21,"id":' + people[0].id + '}');
console.log(people[0].nameAndAge, "Bob O\'Neill: 21");
test.equals(JSON.stringify(people[0]), '{"phones":{},"companies":{},"modifiedBy":{},"name":"Bob O\'Neill","age":21,"id":' + people[0].id + ',"nameAndAge":"Bob O\'Neill: 21"}');
test.equals(people[1].name, 'john');

test.done();
Expand Down Expand Up @@ -208,15 +213,15 @@ exports['Select'] = nodeunit.testCase({

this.Person
.getById(this.connection, person1Id, function (err, person) {
if (err) {
console.error(err);
return;
}
if (err) {
console.error(err);
return;
}

test.equals(person.name, "Bob O'Neill");
test.equals(person.name, "Bob O'Neill");

test.done();
});
test.done();
});
},

"get by id with include": function (test) {
Expand Down

0 comments on commit fadf65e

Please sign in to comment.