Set strict to false by default for non-relational data sources

This commit is contained in:
Raymond Feng 2013-08-26 13:38:24 -07:00
parent ecc142b4d2
commit 59841a04a8
6 changed files with 163 additions and 7 deletions

View File

@ -59,7 +59,8 @@ DataAccessObject._forDB = function (data) {
}
var res = {};
Object.keys(data).forEach(function (propName) {
if (this.whatTypeName(propName) === 'JSON' || data[propName] instanceof Array) {
var type = this.whatTypeName(propName);
if (type === 'JSON' || type === 'Any' || type === 'Object' || data[propName] instanceof Array) {
res[propName] = JSON.stringify(data[propName]);
} else {
res[propName] = data[propName];

View File

@ -319,6 +319,16 @@ DataSource.prototype.createModel = DataSource.prototype.define = function define
properties = properties || {};
settings = settings || {};
if(this.isRelational()) {
// Set the strict mode to be true for relational DBs by default
if(settings.strict === undefined || settings.strict === null) {
settings.strict = true;
}
if(settings.strict === false) {
settings.strict = 'throw';
}
}
var NewClass = ModelBuilder.prototype.define.call(this, className, properties, settings);
// add data access objects

View File

@ -94,6 +94,11 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
properties = properties || {};
settings = settings || {};
// Set the strict mode to be false by default
if(settings.strict === undefined || settings.strict === null) {
settings.strict = false;
}
this.buildSchema(className, properties);
// every class can receive hash of data as optional param

View File

@ -163,8 +163,13 @@ ModelBaseClass.defineProperty = function (prop, params) {
ModelBaseClass.whatTypeName = function (propName) {
var prop = this.properties[propName];
if (!prop || !prop.type) {
throw new Error('Undefined type for ' + this.modelName + ':' + propName);
if(!prop) {
// The property is not part of the definition
return null;
}
if (!prop.type) {
throw new Error('Type not defined for property ' + this.modelName + '.' + propName);
// return null;
}
return prop.type.name;
};

View File

@ -320,12 +320,12 @@ function seed(done) {
var beatles = [
{
name: 'John Lennon',
mail: 'john@b3atl3s.co.uk',
email: 'john@b3atl3s.co.uk',
role: 'lead',
order: 2
}, {
name: 'Paul McCartney',
mail: 'paul@b3atl3s.co.uk',
email: 'paul@b3atl3s.co.uk',
role: 'lead',
order: 1
},

View File

@ -42,7 +42,6 @@ describe('ModelBuilder define model', function () {
it('should not take unknown properties in strict mode', function (done) {
var modelBuilder = new ModelBuilder();
// simplier way to describe model
var User = modelBuilder.define('User', {name: String, bio: String}, {strict: true});
var user = new User({name: 'Joe', age: 20});
@ -59,10 +58,24 @@ describe('ModelBuilder define model', function () {
done(null, User);
});
it('should throw when unknown properties are used if strict=throw', function (done) {
var modelBuilder = new ModelBuilder();
var User = modelBuilder.define('User', {name: String, bio: String}, {strict: 'throw'});
try {
var user = new User({name: 'Joe', age: 20});
assert(false, 'The code should have thrown an error');
} catch(e) {
assert(true, 'The code is expected to throw an error');
}
// console.log(user);
done(null, User);
});
it('should be able to define open models', function (done) {
var modelBuilder = new ModelBuilder();
// simplier way to describe model
var User = modelBuilder.define('User', {}, {strict: false});
var user = new User({name: 'Joe', age: 20});
@ -76,6 +89,22 @@ describe('ModelBuilder define model', function () {
done(null, User);
});
it('should use false as the default value for strict', function (done) {
var modelBuilder = new ModelBuilder();
var User = modelBuilder.define('User', {});
var user = new User({name: 'Joe', age: 20});
// console.log(user);
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
done(null, User);
});
it('should be able to define nesting models', function (done) {
var modelBuilder = new ModelBuilder();
@ -244,6 +273,112 @@ describe('DataSource define model', function () {
});
});
it('should not take unknown properties in strict mode', function (done) {
var ds = new DataSource('memory');
var User = ds.define('User', {name: String, bio: String}, {strict: true});
User.create({name: 'Joe', age: 20}, function (err, user) {
// console.log(user);
User.modelName.should.equal('User');
user.should.be.a('object');
// console.log(user);
assert(user.name === 'Joe');
assert(user.age === undefined);
assert(user.toObject().age === undefined);
assert(user.toObject(true).age === undefined);
assert(user.bio === undefined);
done(null, User);
});
});
it('should throw when unknown properties are used if strict=throw', function (done) {
var ds = new DataSource('memory');
var User = ds.define('User', {name: String, bio: String}, {strict: 'throw'});
try {
var user = new User({name: 'Joe', age: 20});
assert(false, 'The code should have thrown an error');
} catch(e) {
assert(true, 'The code is expected to throw an error');
}
// console.log(user);
done(null, User);
});
it('should be able to define open models', function (done) {
var ds = new DataSource('memory');
var User = ds.define('User', {}, {strict: false});
User.create({name: 'Joe', age: 20}, function (err, user) {
// console.log(user);
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
done(null, User);
});
});
it('should use false as the default value for strict', function (done) {
var ds = new DataSource('memory');
var User = ds.define('User', {});
User.create({name: 'Joe', age: 20}, function (err, user) {
// console.log(user);
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
done(null, User);
});
});
it('should use true as the default value for strict for relational DBs', function (done) {
var ds = new DataSource('memory');
ds.connector.relational = true; // HACK
var User = ds.define('User', {name: String, bio: String}, {strict: true});
var user = new User({name: 'Joe', age: 20});
// console.log(user);
User.modelName.should.equal('User');
user.should.be.a('object');
// console.log(user);
assert(user.name === 'Joe');
assert(user.age === undefined);
assert(user.toObject().age === undefined);
assert(user.toObject(true).age === undefined);
assert(user.bio === undefined);
done(null, User);
});
it('should throw when unknown properties are used if strict=false for relational DBs', function (done) {
var ds = new DataSource('memory');
ds.connector.relational = true; // HACK
var User = ds.define('User', {name: String, bio: String}, {strict: 'throw'});
try {
var user = new User({name: 'Joe', age: 20});
assert(false, 'The code should have thrown an error');
} catch(e) {
assert(true, 'The code is expected to throw an error');
}
// console.log(user);
done(null, User);
});
});