Skip to content

Commit 8935b97

Browse files
committed
forceId=true with auto-increment db
1 parent 83a2826 commit 8935b97

File tree

6 files changed

+29
-6
lines changed

6 files changed

+29
-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: 2 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) {

0 commit comments

Comments
 (0)