Skip to content

Commit

Permalink
Added afterSave hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Burch committed Feb 20, 2012
1 parent f12e51d commit 10bc507
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Base.js
Expand Up @@ -35,7 +35,11 @@ module.exports = function(db, name, config) {
that.id = res.id;
that.rev = res.rev
}
callback(err, res);
if(err) {
callback(err, res);
} else {
that.afterSave ? that.afterSave(res, callback) : callback(err, res);
}
});
}

Expand Down
11 changes: 10 additions & 1 deletion README.md
Expand Up @@ -82,7 +82,12 @@ Next, create your domain files in ../testDomain like this:
}, function(that) {
this.beforeSave = function() {
that.fullName = that.firstName + ' ' + that.lastName;
}
},
that.afterSave = function(res, callback) {
// do something and callback with (err, res)
callback(undefined, res);
};

return that;
});

Expand All @@ -106,6 +111,10 @@ After running init() the domain constructors are available from the couch-ar obj
domain constructors can include a hook beforeSave() that will be run before a document
is saved or updated in the DB.

## After Hooks

domain constructors can include a hook afterSave(res, cb) that will run after a document is saved. See the previous example for the format.


## create()

Expand Down
14 changes: 14 additions & 0 deletions test/spec/TestUserSpec.js
Expand Up @@ -58,6 +58,19 @@ describe('couch-ar', function () {
asyncSpecWait();
});

it('should have called afterSave after saving and before calling the callback', function() {
expect(user.afterSaveRun).toBe(true);
});

it('should call afterSave method after writing to the db', function() {
domain.TestUser.findByUsername('tester1', function(u) {
expect(u.afterSaveRun).toBe(undefined);
asyncSpecDone();
});
asyncSpecWait();
});


it('should allow us to create more than one', function () {
var u = domain.TestUser.create({username:'tester2', firstName:'Test2', lastName:'Tester2', dbName:testConfig.domainDbName});
u.save(function (err, res) {
Expand Down Expand Up @@ -96,6 +109,7 @@ describe('couch-ar', function () {
})
asyncSpecWait();
});

});

describe('findByUsername() method', function () {
Expand Down
7 changes: 7 additions & 0 deletions test/testDomain/TestUser.js
Expand Up @@ -26,6 +26,13 @@ domain.create('TestUser',{
this.beforeSave = function() {
that.fullName = that.firstName + ' ' + that.lastName;
}
that.afterSave = function(res, callback) {
setTimeout(function() {
that.afterSaveRun = true;
callback(undefined, res);
},200);
};

});


10 changes: 9 additions & 1 deletion test/testDomain2/testUser.js
Expand Up @@ -7,7 +7,8 @@ domain.create('TestUser',{
password: {},
firstName:{},
lastName: {},
fullName: {finders:false}
fullName: {finders:false},
afterSaveRun: {}
},
hasMany: {
phoneNumbers: 'PhoneNumber',
Expand All @@ -26,6 +27,13 @@ domain.create('TestUser',{
that.beforeSave = function() {
that.fullName = that.firstName + ' ' + that.lastName;
};

that.afterSave = function(res, callback) {
setTimeout(function() {
that.afterSaveRun = true;
callback(undefined, res);
},200);
};
return that;
});

Expand Down

0 comments on commit 10bc507

Please sign in to comment.