From 02f15b564fc94e6647d20885a7d857f6f14f2f5a Mon Sep 17 00:00:00 2001 From: vazarely Date: Fri, 11 Dec 2015 07:06:28 +0200 Subject: [PATCH] Adds PR#3228 (https://github.com/balderdashy/sails/pull/3228) on top of sails.js rel0.12-rc3 --- README.md | 4 ++++ lib/hooks/blueprints/actionUtil.js | 12 +++++++++++ lib/hooks/blueprints/actions/create.js | 2 +- lib/hooks/pubsub/index.js | 22 ++++++++++++++++++++- test/integration/router.APIScaffold.test.js | 15 ++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 84fa639bf8..dce67b60ed 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ Sails.js is a web framework that makes it easy to build custom, enterprise-grade Node.js apps. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It's especially good for building realtime features like chat. +## NOTE   +2015-Dec-11: This fork adds the following PRs on top of sails.js rel 0.12-rc3 +- [PR#3228: Added support for JSON arrays](https://github.com/balderdashy/sails/pull/3228) + ## Installation   **With [node](http://nodejs.org) [installed](http://sailsjs.org/#!documentation/new-to-nodejs):** diff --git a/lib/hooks/blueprints/actionUtil.js b/lib/hooks/blueprints/actionUtil.js index 105b659116..cd9dc5f75e 100644 --- a/lib/hooks/blueprints/actionUtil.js +++ b/lib/hooks/blueprints/actionUtil.js @@ -270,6 +270,18 @@ var actionUtil = { throw new Error('Invalid `req.options.values.blacklist`. Should be an array of strings (parameter names.)'); } + //Special handling if the body is an array, does what it would do usally but on each array object. + if(_.isArray(req.body)){ + sails.log.silly("Reqest body is an array"); + var values = []; + _.each(req.body, function(element, key){ + values[key] = mergeDefaults(element, _.omit(req.options.values, 'blacklist')); + values[key] = _.omit(values[key], blacklist || []); + values[key] = _.omit(values[key], function (p){ if (_.isUndefined(p)) return true; }); + }); + return values; + } + // Merge params into req.options.values, omitting the blacklist. var values = mergeDefaults(req.params.all(), _.omit(req.options.values, 'blacklist')); diff --git a/lib/hooks/blueprints/actions/create.js b/lib/hooks/blueprints/actions/create.js index 3f0dc74ea5..ef0f97922d 100644 --- a/lib/hooks/blueprints/actions/create.js +++ b/lib/hooks/blueprints/actions/create.js @@ -40,7 +40,7 @@ module.exports = function createRecord (req, res) { Model.subscribe(req, newInstance); Model.introduce(newInstance); } - Model.publishCreate(newInstance.toJSON(), !req.options.mirror && req); + Model.publishCreate(newInstance, !req.options.mirror && req); } // Send JSONP-friendly response if it's supported diff --git a/lib/hooks/pubsub/index.js b/lib/hooks/pubsub/index.js index c87cb3bcc6..98b93e3b5b 100644 --- a/lib/hooks/pubsub/index.js +++ b/lib/hooks/pubsub/index.js @@ -1077,6 +1077,26 @@ module.exports = function(sails) { }, + /** + * Publish the creation of model or an array of models + * + * @param {[Object]|Object} models + * - the data to publish + * + * @param {Request|Socket} req - Optional request for broadcast. + * @api private + */ + publishCreate: function(models, req, options){ + var self = this; + + // Pluralize so we can use this method regardless of it is an array or not + models = self.pluralize(models); + + //Publish all models + _.each(models, function(values){ + self.publishCreateSingle(values.toJSON(), req, options); + }) + }, /** * Publish the creation of a model @@ -1088,7 +1108,7 @@ module.exports = function(sails) { * @api private */ - publishCreate: function(values, req, options) { + publishCreateSingle: function(values, req, options) { var self = this; var reverseAssociation; diff --git a/test/integration/router.APIScaffold.test.js b/test/integration/router.APIScaffold.test.js index 0ebb5bc8c0..05340f3a04 100644 --- a/test/integration/router.APIScaffold.test.js +++ b/test/integration/router.APIScaffold.test.js @@ -183,6 +183,21 @@ describe('router :: ', function() { }); }); + describe('a post of JSON array request to /:controller/create', function() { + it('should return JSON array for a newly created instances of the test model', function (done) { + httpHelper.testRoute('post', { + url: 'empty/create', + json: true, + body: [{stuff: "bad"}, {stuff: "ghuud"}] + }, function (err, response) { + if (err) return done(new Error(err)); + + assert(response.body instanceof Array && response.body[1].stuff === "ghuud", Err.badResponse(response)); + done(); + }); + }); + }); + describe('with pluralize turned on', function() { before(function() {