Skip to content

Commit c4b2921

Browse files
author
Janny
authored
Merge pull request #982 from strongloop/fix/auto-increment-db
forceId=true with auto-increment db
2 parents 8e81185 + baec1b5 commit c4b2921

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

lib/datasource.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,11 @@ DataSource.prototype.setupDataAccess = function(modelClass, settings) {
515515
idProp.type = idType;
516516
modelClass.definition.rawProperties[idName].type = idType;
517517
modelClass.definition.properties[idName].type = idType;
518-
if (settings.forceId) {
518+
var forceId = settings.forceId;
519+
if (idProp.generated && forceId !== false) {
520+
forceId = true;
521+
}
522+
if (forceId) {
519523
modelClass.validatesAbsenceOf(idName, { if: 'isNewRecord' });
520524
}
521525
}

test/crud-with-options.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('crud-with-options', function() {
1313
before(function(done) {
1414
db = getSchema();
1515
User = db.define('User', {
16+
id: { type: Number, id: true },
1617
seq: { type: Number, index: true },
1718
name: { type: String, index: true, sort: true },
1819
email: { type: String, index: true },

test/default-scope.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ describe('default scope', function() {
9494

9595
Person = db.define('Person', { name: String }, {
9696
scope: { include: 'things' },
97+
forceId: false,
9798
});
9899

99100
// inst is only valid for instance methods

test/loopback-dl.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ describe('Models attached to a dataSource', function() {
766766
title: { type: String, length: 255, index: true },
767767
content: { type: String },
768768
comments: [String],
769-
});
769+
}, { forceId: false });
770770
});
771771

772772
beforeEach(function(done) {

test/manipulation.test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ describe('manipulation', function() {
6161
Person.destroyAll(done);
6262
});
6363

64+
describe('forceId', function() {
65+
before(function(done) {
66+
TestForceId = db.define('TestForceId');
67+
db.automigrate('TestForceId', done);
68+
});
69+
70+
it('it defaults to forceId:true for generated id property', function(done) {
71+
TestForceId.create({ id: 1 }, function(err, t) {
72+
should.exist(err);
73+
err.message.should.match(/can\'t be set/);
74+
done();
75+
});
76+
});
77+
});
78+
6479
it('should create instance', function(done) {
6580
Person.create({ name: 'Anatoliy' }, function(err, p) {
6681
p.name.should.equal('Anatoliy');
@@ -308,7 +323,7 @@ describe('manipulation', function() {
308323
it('should refuse to create object with duplicate id', function(done) {
309324
// NOTE(bajtos) We cannot reuse Person model here,
310325
// `settings.forceId` aborts the CREATE request at the validation step.
311-
var Product = db.define('ProductTest', { name: String });
326+
var Product = db.define('ProductTest', { name: String }, { forceId: false });
312327
db.automigrate('ProductTest', function(err) {
313328
if (err) return done(err);
314329

@@ -825,7 +840,7 @@ describe('manipulation', function() {
825840
title: { type: String, length: 255, index: true },
826841
content: { type: String },
827842
comments: [String],
828-
});
843+
}, { forceId: false });
829844
ds.automigrate('Post', done);
830845
});
831846

@@ -1046,6 +1061,8 @@ describe('manipulation', function() {
10461061
ds.automigrate('Post', done);
10471062
});
10481063
beforeEach(function(done) {
1064+
// TODO(bajtos) add API to lib/observer - remove observers for all hooks
1065+
Post._observers = {};
10491066
Post.destroyAll(function() {
10501067
Post.create({ title: 'a', content: 'AAA' }, function(err, p) {
10511068
if (err) return done(err);

test/relations.test.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4190,7 +4190,7 @@ describe('relations', function() {
41904190
tmp = getTransientDataSource();
41914191
// db = getSchema();
41924192
Person = db.define('Person', { name: String });
4193-
Address = tmp.define('Address', { street: String });
4193+
Address = tmp.define('Address', { street: String }, { forceId: false });
41944194
Address.validatesPresenceOf('street');
41954195

41964196
db.automigrate(['Person'], done);
@@ -4488,7 +4488,7 @@ describe('relations', function() {
44884488
// db = getSchema();
44894489
Category = db.define('Category', { name: String });
44904490
Job = db.define('Job', { name: String });
4491-
Link = db.define('Link', { name: String, notes: String });
4491+
Link = db.define('Link', { name: String, notes: String }, { forceId: false });
44924492
});
44934493

44944494
it('can be declared', function(done) {
@@ -4509,10 +4509,13 @@ describe('relations', function() {
45094509

45104510
it('should setup related items', function(done) {
45114511
Job.create({ name: 'Job 1' }, function(err, p) {
4512+
if (err) return done(err);
45124513
job1 = p;
45134514
Job.create({ name: 'Job 2' }, function(err, p) {
4515+
if (err) return done(err);
45144516
job2 = p;
45154517
Job.create({ name: 'Job 3' }, function(err, p) {
4518+
if (err) return done(err);
45164519
job3 = p;
45174520
done();
45184521
});
@@ -4522,11 +4525,13 @@ describe('relations', function() {
45224525

45234526
it('should associate items on scope', function(done) {
45244527
Category.create({ name: 'Category A' }, function(err, cat) {
4528+
if (err) return done(err);
45254529
var link = cat.items.build();
45264530
link.job(job1);
45274531
var link = cat.items.build();
45284532
link.job(job2);
45294533
cat.save(function(err, cat) {
4534+
if (err) return done(err);
45304535
var job = cat.items.at(0);
45314536
job.should.be.instanceof(Link);
45324537
job.should.not.have.property('jobId');
@@ -4542,6 +4547,7 @@ describe('relations', function() {
45424547

45434548
it('should include related items on scope', function(done) {
45444549
Category.findOne(function(err, cat) {
4550+
if (err) return done(err);
45454551
cat.links.should.have.length(2);
45464552

45474553
// denormalized properties:
@@ -4556,6 +4562,7 @@ describe('relations', function() {
45564562
should.not.exist(cat.items.at(1).job());
45574563

45584564
cat.items(function(err, items) {
4565+
if (err) return done(err);
45594566
cat.items.at(0).job().should.be.instanceof(Job);
45604567
cat.items.at(1).job().should.be.instanceof(Job);
45614568
cat.items.at(1).job().name.should.equal('Job 2');
@@ -4566,8 +4573,10 @@ describe('relations', function() {
45664573

45674574
it('should remove embedded items by id', function(done) {
45684575
Category.findOne(function(err, cat) {
4576+
if (err) return done(err);
45694577
cat.links.should.have.length(2);
45704578
cat.items.destroy(job1.id, function(err) {
4579+
if (err) return done(err);
45714580
should.not.exist(err);
45724581
cat.links.should.have.length(1);
45734582
done();
@@ -4577,6 +4586,7 @@ describe('relations', function() {
45774586

45784587
it('should find items on scope', function(done) {
45794588
Category.findOne(function(err, cat) {
4589+
if (err) return done(err);
45804590
cat.links.should.have.length(1);
45814591
cat.items.at(0).id.should.eql(job2.id);
45824592
cat.items.at(0).name.should.equal(job2.name);
@@ -4585,6 +4595,7 @@ describe('relations', function() {
45854595
should.not.exist(cat.items.at(0).job());
45864596

45874597
cat.items(function(err, items) {
4598+
if (err) return done(err);
45884599
cat.items.at(0).job().should.be.instanceof(Job);
45894600
cat.items.at(0).job().name.should.equal('Job 2');
45904601
done();
@@ -4594,8 +4605,10 @@ describe('relations', function() {
45944605

45954606
it('should add related items to scope', function(done) {
45964607
Category.findOne(function(err, cat) {
4608+
if (err) return done(err);
45974609
cat.links.should.have.length(1);
45984610
cat.items.add(job3, function(err, link) {
4611+
if (err) return done(err);
45994612
link.should.be.instanceof(Link);
46004613
link.id.should.eql(job3.id);
46014614
link.name.should.equal('Job 3');
@@ -4608,6 +4621,7 @@ describe('relations', function() {
46084621

46094622
it('should find items on scope', function(done) {
46104623
Category.findOne(function(err, cat) {
4624+
if (err) return done(err);
46114625
cat.links.should.have.length(2);
46124626

46134627
cat.items.at(0).should.be.instanceof(Link);
@@ -4622,8 +4636,10 @@ describe('relations', function() {
46224636

46234637
it('should remove embedded items by reference id', function(done) {
46244638
Category.findOne(function(err, cat) {
4639+
if (err) return done(err);
46254640
cat.links.should.have.length(2);
46264641
cat.items.remove(job2.id, function(err) {
4642+
if (err) return done(err);
46274643
should.not.exist(err);
46284644
cat.links.should.have.length(1);
46294645
done();
@@ -4633,6 +4649,7 @@ describe('relations', function() {
46334649

46344650
it('should have removed embedded items by reference id', function(done) {
46354651
Category.findOne(function(err, cat) {
4652+
if (err) return done(err);
46364653
cat.links.should.have.length(1);
46374654
done();
46384655
});
@@ -4642,9 +4659,11 @@ describe('relations', function() {
46424659

46434660
it('should create items on scope', function(done) {
46444661
Category.create({ name: 'Category B' }, function(err, cat) {
4662+
if (err) return done(err);
46454663
category = cat;
46464664
var link = cat.items.build({ notes: 'Some notes...' });
46474665
link.job.create({ name: 'Job 1' }, function(err, p) {
4666+
if (err) return done(err);
46484667
jobId = p.id;
46494668
cat.links[0].id.should.eql(p.id);
46504669
cat.links[0].name.should.equal('Job 1'); // denormalized
@@ -4657,12 +4676,14 @@ describe('relations', function() {
46574676

46584677
it('should find items on scope', function(done) {
46594678
Category.findById(category.id, function(err, cat) {
4679+
if (err) return done(err);
46604680
cat.name.should.equal('Category B');
46614681
cat.links.toObject().should.eql([
46624682
{ id: jobId, name: 'Job 1', notes: 'Some notes...' },
46634683
]);
46644684
cat.items.at(0).should.equal(cat.links[0]);
46654685
cat.items(function(err, items) { // alternative access
4686+
if (err) return done(err);
46664687
items.should.be.an.array;
46674688
items.should.have.length(1);
46684689
items[0].job(function(err, p) {
@@ -4675,8 +4696,10 @@ describe('relations', function() {
46754696

46764697
it('should update items on scope - and save parent', function(done) {
46774698
Category.findById(category.id, function(err, cat) {
4699+
if (err) return done(err);
46784700
var link = cat.items.at(0);
46794701
link.updateAttributes({ notes: 'Updated notes...' }, function(err, link) {
4702+
if (err) return done(err);
46804703
link.notes.should.equal('Updated notes...');
46814704
done();
46824705
});
@@ -4685,6 +4708,7 @@ describe('relations', function() {
46854708

46864709
it('should find items on scope - verify update', function(done) {
46874710
Category.findById(category.id, function(err, cat) {
4711+
if (err) return done(err);
46884712
cat.name.should.equal('Category B');
46894713
cat.links.toObject().should.eql([
46904714
{ id: jobId, name: 'Job 1', notes: 'Updated notes...' },
@@ -4695,7 +4719,9 @@ describe('relations', function() {
46954719

46964720
it('should remove items from scope - and save parent', function(done) {
46974721
Category.findById(category.id, function(err, cat) {
4722+
if (err) return done(err);
46984723
cat.items.at(0).destroy(function(err, link) {
4724+
if (err) return done(err);
46994725
cat.links.should.eql([]);
47004726
done();
47014727
});
@@ -4704,6 +4730,7 @@ describe('relations', function() {
47044730

47054731
it('should find items on scope - verify destroy', function(done) {
47064732
Category.findById(category.id, function(err, cat) {
4733+
if (err) return done(err);
47074734
cat.name.should.equal('Category B');
47084735
cat.links.should.eql([]);
47094736
done();

0 commit comments

Comments
 (0)