forceId=true with auto-increment db

This commit is contained in:
jannyHou 2016-06-17 18:17:20 -04:00
parent 83a2826a59
commit 8935b978f3
6 changed files with 29 additions and 6 deletions

View File

@ -515,7 +515,11 @@ DataSource.prototype.setupDataAccess = function(modelClass, settings) {
idProp.type = idType; idProp.type = idType;
modelClass.definition.rawProperties[idName].type = idType; modelClass.definition.rawProperties[idName].type = idType;
modelClass.definition.properties[idName].type = idType; modelClass.definition.properties[idName].type = idType;
if (settings.forceId) { var forceId = settings.forceId;
if (idProp.generated && forceId !== false) {
forceId = true;
}
if (forceId) {
modelClass.validatesAbsenceOf(idName, { if: 'isNewRecord' }); modelClass.validatesAbsenceOf(idName, { if: 'isNewRecord' });
} }
} }

View File

@ -13,6 +13,7 @@ describe('crud-with-options', function() {
before(function(done) { before(function(done) {
db = getSchema(); db = getSchema();
User = db.define('User', { User = db.define('User', {
id: { type: Number, id: true },
seq: { type: Number, index: true }, seq: { type: Number, index: true },
name: { type: String, index: true, sort: true }, name: { type: String, index: true, sort: true },
email: { type: String, index: true }, email: { type: String, index: true },

View File

@ -94,6 +94,7 @@ describe('default scope', function() {
Person = db.define('Person', { name: String }, { Person = db.define('Person', { name: String }, {
scope: { include: 'things' }, scope: { include: 'things' },
forceId: false,
}); });
// inst is only valid for instance methods // inst is only valid for instance methods

View File

@ -766,7 +766,7 @@ describe('Models attached to a dataSource', function() {
title: { type: String, length: 255, index: true }, title: { type: String, length: 255, index: true },
content: { type: String }, content: { type: String },
comments: [String], comments: [String],
}); }, { forceId: false });
}); });
beforeEach(function(done) { beforeEach(function(done) {

View File

@ -61,6 +61,21 @@ describe('manipulation', function() {
Person.destroyAll(done); Person.destroyAll(done);
}); });
describe('forceId', function() {
before(function(done) {
TestForceId = db.define('TestForceId');
db.automigrate('TestForceId', done);
});
it('it defaults to forceId:true for generated id property', function(done) {
TestForceId.create({ id: 1 }, function(err, t) {
should.exist(err);
err.message.should.match(/can\'t be set/);
done();
});
});
});
it('should create instance', function(done) { it('should create instance', function(done) {
Person.create({ name: 'Anatoliy' }, function(err, p) { Person.create({ name: 'Anatoliy' }, function(err, p) {
p.name.should.equal('Anatoliy'); p.name.should.equal('Anatoliy');
@ -308,7 +323,7 @@ describe('manipulation', function() {
it('should refuse to create object with duplicate id', function(done) { it('should refuse to create object with duplicate id', function(done) {
// NOTE(bajtos) We cannot reuse Person model here, // NOTE(bajtos) We cannot reuse Person model here,
// `settings.forceId` aborts the CREATE request at the validation step. // `settings.forceId` aborts the CREATE request at the validation step.
var Product = db.define('ProductTest', { name: String }); var Product = db.define('ProductTest', { name: String }, { forceId: false });
db.automigrate('ProductTest', function(err) { db.automigrate('ProductTest', function(err) {
if (err) return done(err); if (err) return done(err);
@ -825,7 +840,7 @@ describe('manipulation', function() {
title: { type: String, length: 255, index: true }, title: { type: String, length: 255, index: true },
content: { type: String }, content: { type: String },
comments: [String], comments: [String],
}); }, { forceId: false });
ds.automigrate('Post', done); ds.automigrate('Post', done);
}); });
@ -1046,6 +1061,8 @@ describe('manipulation', function() {
ds.automigrate('Post', done); ds.automigrate('Post', done);
}); });
beforeEach(function(done) { beforeEach(function(done) {
// TODO(bajtos) add API to lib/observer - remove observers for all hooks
Post._observers = {};
Post.destroyAll(function() { Post.destroyAll(function() {
Post.create({ title: 'a', content: 'AAA' }, function(err, p) { Post.create({ title: 'a', content: 'AAA' }, function(err, p) {
if (err) return done(err); if (err) return done(err);

View File

@ -4190,7 +4190,7 @@ describe('relations', function() {
tmp = getTransientDataSource(); tmp = getTransientDataSource();
// db = getSchema(); // db = getSchema();
Person = db.define('Person', { name: String }); Person = db.define('Person', { name: String });
Address = tmp.define('Address', { street: String }); Address = tmp.define('Address', { street: String }, { forceId: false });
Address.validatesPresenceOf('street'); Address.validatesPresenceOf('street');
db.automigrate(['Person'], done); db.automigrate(['Person'], done);
@ -4488,7 +4488,7 @@ describe('relations', function() {
// db = getSchema(); // db = getSchema();
Category = db.define('Category', { name: String }); Category = db.define('Category', { name: String });
Job = db.define('Job', { name: String }); Job = db.define('Job', { name: String });
Link = db.define('Link', { name: String, notes: String }); Link = db.define('Link', { name: String, notes: String }, { forceId: false });
}); });
it('can be declared', function(done) { it('can be declared', function(done) {