Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
script/
coverage.html
.tern-port
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added to ignore files made by tern.js, an autocomplete engine

8 changes: 7 additions & 1 deletion lib/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ module.exports = function(url) {

Model.save = function(cb) {
var self = this;
return db.insert(this.toJSON(), function(err, docs) {
var doc;
if (this.toMongo && typeof this.toMongo === 'function') {
doc = this.toMongo();
} else {
doc = this.toJSON();
}
return db.insert(doc, function(err, docs) {
var doc = docs ? docs[0] : null;
if(err) {
// Check for duplicate index
Expand Down
58 changes: 55 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var User = modella('User')
.attr('email', {unique: true})
.attr('password');

User.use(mongo);

var AtomicUser = modella('AtomicUser')
.attr('_id')
.attr('name')
Expand All @@ -24,10 +26,42 @@ var AtomicUser = modella('AtomicUser')
.attr('email', {unique: true})
.attr('password');


User.use(mongo);
AtomicUser.use(mongo);

var OverrideUser = modella('OverrideUser')
.attr('_id')
.attr('name')
.attr('password');

OverrideUser.use(mongo);

OverrideUser.prototype.toMongo = function() {
var dump = {};
var self = this;

Object.keys(this.attrs).forEach(function (key) {
var val = self.attrs[key];

dump[key] = !!val.toJSON ? val.toJSON() : modella.utils.clone(val);
});

return dump;
};

OverrideUser.prototype.toJSON = function() {
var dump = {};
var self = this;

Object.keys(this.attrs).forEach(function (key) {
if (key === 'password') return;
var val = self.attrs[key];

dump[key] = !!val.toJSON ? val.toJSON() : modella.utils.clone(val);
});

return dump;
};

/**
* Initialize
*/
Expand All @@ -36,12 +70,15 @@ var user = new User();

var col = db.collection("User");
var atomiccol = db.collection("AtomicUser");
var overridecol = db.collection("OverrideUser");


describe("Modella-Mongo", function() {
before(function(done) {
col.remove({}, function() {
atomiccol.remove({}, done);
atomiccol.remove({}, function() {
overridecol.remove({}, done);
});
});
});

Expand Down Expand Up @@ -77,6 +114,21 @@ describe("Modella-Mongo", function() {
});
});

it("saves the record using toMongo if present", function(done) {
var user = new OverrideUser({name: 'Ryan', email: 'ryan@slingingcode.com', password: 'foobar123'});
user.save(function(err, u) {
expect(user.primary()).to.be.ok();
overridecol.findOne({}, function(err, u) {
expect(u).to.be.ok();
expect(u).to.have.property('name', 'Ryan');
expect(u).to.have.property('password', 'foobar123');
var uJSON = user.toJSON();
expect(uJSON).to.not.have.property('password');
done();
});
});
});

it("triggers errors if there is an error", function(done) {
var user = new User({name: 'Ryan', email: 'ryan@slingingcode.com'});
user.save();
Expand Down