From 170dd40d3c14a465d6c0bcfbbf169a9ee93aa355 Mon Sep 17 00:00:00 2001 From: Ilya Diallo Date: Mon, 22 Feb 2016 12:57:18 +0100 Subject: [PATCH 1/4] Accept subdocuments keys ("object.subobject"), to allow atomic updates of an object field. --- src/Schema.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Schema.js b/src/Schema.js index 54aa3d9e..eb8f1c5b 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -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); From 831c2ee3decff98be12468ab43bfce09a34d596a Mon Sep 17 00:00:00 2001 From: Ilya Diallo Date: Mon, 22 Feb 2016 16:45:41 +0100 Subject: [PATCH 2/4] Update the tests --- spec/RestCreate.spec.js | 34 +++++++++++++++++++++++++++++++++- spec/Schema.spec.js | 11 +++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/spec/RestCreate.spec.js b/spec/RestCreate.spec.js index f4055974..1e79d2bf 100644 --- a/spec/RestCreate.spec.js +++ b/spec/RestCreate.spec.js @@ -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', diff --git a/spec/Schema.spec.js b/spec/Schema.spec.js index a5c28c5b..8be0a02e 100644 --- a/spec/Schema.spec.js +++ b/spec/Schema.spec.js @@ -32,6 +32,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}); From 7da7c43fcecdea4d40c573f5aba33fada93f4dae Mon Sep 17 00:00:00 2001 From: Ilya Diallo Date: Mon, 22 Feb 2016 22:40:01 +0100 Subject: [PATCH 3/4] #510 Detect when the port you are trying to run the server on is already in use. --- src/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/index.js b/src/index.js index 36f94284..344bac89 100644 --- a/src/index.js +++ b/src/index.js @@ -188,6 +188,17 @@ function ParseServer({ api.use(middlewares.handleParseErrors); + + process.on('uncaughtException', (err) => { + if( err.code === "EADDRINUSE" ) { // user-friendly message for this common error + console.log(`Unable to listen on port ${err.port}. The port is already in use.`); + process.exit(0); + } + else { + throw err; + } + }); + return api; } From 193f368686872d850260201d0e622f4a33f2198e Mon Sep 17 00:00:00 2001 From: Ilya Diallo Date: Mon, 22 Feb 2016 22:45:00 +0100 Subject: [PATCH 4/4] #510 Detect when the port you are trying to run the server on is already in use. (reverted from commit 7da7c43fcecdea4d40c573f5aba33fada93f4dae) --- src/index.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/index.js b/src/index.js index 344bac89..36f94284 100644 --- a/src/index.js +++ b/src/index.js @@ -188,17 +188,6 @@ function ParseServer({ api.use(middlewares.handleParseErrors); - - process.on('uncaughtException', (err) => { - if( err.code === "EADDRINUSE" ) { // user-friendly message for this common error - console.log(`Unable to listen on port ${err.port}. The port is already in use.`); - process.exit(0); - } - else { - throw err; - } - }); - return api; }