Skip to content

Commit

Permalink
Merge 3342718 into 5f713f0
Browse files Browse the repository at this point in the history
  • Loading branch information
b-admike committed Dec 6, 2018
2 parents 5f713f0 + 3342718 commit d631f02
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/connectors/memory.js
Expand Up @@ -255,6 +255,21 @@ Memory.prototype._createSync = function(model, data, fn) {
this.collection(model, {});
}

var indexes = this._models[model].model.definition.indexes();
for (var idx in indexes) {
var idxPropName = idx.substring(0, idx.indexOf('_index'));
if (indexes[idx].unique === true) {
for (var instId in this.cache[model]) {
var inst = JSON.parse(this.cache[model][instId]);
if (inst[idxPropName] === data[idxPropName]) {
var duplicateIndexError = new Error(g.f('Duplicate entry for %s.%s', model, idxPropName));
duplicateIndexError.statusCode = duplicateIndexError.status = 409;
return fn(duplicateIndexError);
}
}
}
}

if (this.collection(model)[id]) {
var error = new Error(g.f('Duplicate entry for %s.%s', model, idName));
error.statusCode = error.status = 409;
Expand Down
18 changes: 18 additions & 0 deletions test/memory.test.js
Expand Up @@ -677,6 +677,24 @@ describe('Memory connector', function() {
});
});

it('should refuse to create object with duplicate unique index', function(done) {
var ds = new DataSource({connector: 'memory'});
var Product = ds.define('ProductTest', {name: {type: String, index: {unique: true}}}, {forceId: false});
ds.automigrate('ProductTest', function(err) {
if (err) return done(err);

Product.create({name: 'a-name'}, function(err, p) {
if (err) return done(err);
Product.create({name: 'a-name'}, function(err) {
should.exist(err);
err.message.should.match(/Duplicate/i);
err.statusCode.should.equal(409);
done();
});
});
});
});

describe('automigrate', function() {
var ds;
beforeEach(function() {
Expand Down

0 comments on commit d631f02

Please sign in to comment.