Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
  • Loading branch information
biggora committed Feb 1, 2017
2 parents 6521254 + 6d749f9 commit 6fff2bc
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,11 @@ AbstractClass.create = function (data, callback) {
// validation required
obj.isValid(function (valid) {
if (!valid) {
p.reject(new Error('Validation error'));
return callback && callback(new Error('Validation error'), obj);
var err = new Error(modelName + ' validation error');
p.reject(err);
return callback && callback(err, obj);
} else {
create();
return create();
}
});
}
Expand Down Expand Up @@ -423,26 +424,24 @@ AbstractClass.findOrCreate = function findOrCreate(query, data, callback) {
callback = data;
data = {};
}
var self = this;
var p = deferPromise();
this.findOne({where: query}, function (err, record) {
var self = this, p = deferPromise();
self.findOne({where: query}, function (err, record) {
if (err) {
p.reject(err);
return callback && callback(err);
}
if (record) {
} else if (record) {
p.resolve(record);
return callback && callback(null, record);
}
data = helpers.merge(query, data);
self.create(data, function (err, record) {
if (err) {
p.reject(err);
} else {
} else {
data = helpers.merge(query, data);
self.create(data).then(function (record) {
p.resolve(record);
}
return callback && callback(err, record);
});
return callback && callback(null, record);
}).catch(function (err) {
p.reject(err);
return callback && callback(err, null);
});
}
});
return p.promise;
};
Expand Down Expand Up @@ -577,26 +576,28 @@ AbstractClass.findOne = function findOne(params, callback) {
if (stillConnecting(this.schema, this, arguments)) {
return null;
}

var self = this;
if (typeof params === 'undefined') {
this.q.params.limit = 1;
return this;
self.q.params.limit = 1;
return self;
} else {
var p = deferPromise();
if (typeof params === 'function') {
callback = params;
params = {};
}
params = buildQuery(params, this);
params = buildQuery(params, self);
params.limit = 1;
this.all(params, function (err, collection) {
if (err || !collection || !collection.length > 0) {
self
.all(params)
.then(function (collection) {
p.resolve(collection[0]);
return callback && callback(null, collection[0] || null);
})
.catch(function (err) {
p.reject(err);
return callback && callback(err, null);
}
p.resolve(collection[0]);
return callback && callback(err, collection[0]);
});
});
return p.promise;
}
}
Expand Down

0 comments on commit 6fff2bc

Please sign in to comment.