Merge pull request #567 from IlyaDiallo/master

Accept subdocuments keys ("object.subobject"), to allow atomic updates of an object field.
This commit is contained in:
Fosco Marotto
2016-02-26 02:05:03 -08:00
3 changed files with 50 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
// These tests check the "create" functionality of the REST API.
// These tests check the "create" / "update" functionality of the REST API.
var auth = require('../src/Auth');
var cache = require('../src/cache');
var Config = require('../src/Config');
@@ -41,6 +41,38 @@ describe('rest create', () => {
});
});
it('handles object and subdocument', (done) => {
var obj = {
subdoc: {foo: 'bar', wu: 'tan'},
};
rest.create(config, auth.nobody(config), 'MyClass', obj).then(() => {
return database.mongoFind('MyClass', {}, {});
}).then((results) => {
expect(results.length).toEqual(1);
var mob = results[0];
expect(typeof mob.subdoc).toBe('object');
expect(mob.subdoc.foo).toBe('bar');
expect(mob.subdoc.wu).toBe('tan');
expect(typeof mob._id).toEqual('string');
var obj = {
'subdoc.wu': 'clan',
};
rest.update(config, auth.nobody(config), 'MyClass', mob._id, obj).then(() => {
return database.mongoFind('MyClass', {}, {});
}).then((results) => {
expect(results.length).toEqual(1);
var mob = results[0];
expect(typeof mob.subdoc).toBe('object');
expect(mob.subdoc.foo).toBe('bar');
expect(mob.subdoc.wu).toBe('clan');
done();
});
});
});
it('handles user signup', (done) => {
var user = {
username: 'asdf',

View File

@@ -29,6 +29,17 @@ describe('Schema', () => {
});
});
it('can validate one object with dot notation', (done) => {
config.database.loadSchema().then((schema) => {
return schema.validateObject('TestObjectWithSubDoc', {x: false, y: 'YY', z: 1, 'aObject.k1': 'newValue'});
}).then((schema) => {
done();
}, (error) => {
fail(error);
done();
});
});
it('can validate two objects in a row', (done) => {
config.database.loadSchema().then((schema) => {
return schema.validateObject('Foo', {x: true, y: 'yyy', z: 0});

View File

@@ -426,6 +426,12 @@ Schema.prototype.validateField = function(className, key, type, freeze) {
// Just to check that the key is valid
transform.transformKey(this, className, key);
if( key.indexOf(".") > 0 ) {
// subdocument key (x.y) => ok if x is of type 'object'
key = key.split(".")[ 0 ];
type = 'object';
}
var expected = this.data[className][key];
if (expected) {
expected = (expected === 'map' ? 'object' : expected);