From 3651c09782757e6b49e6ed845ed975d8e29ef1de Mon Sep 17 00:00:00 2001 From: Rashmi Hunt Date: Tue, 22 Aug 2017 17:10:55 -0700 Subject: [PATCH] Support createOnlyInstance in model (#3548) * setting up createOnlyInstance * add comment * fix eslint issue * new tests * Address code review comments --- lib/model.js | 7 +++++++ lib/persisted-model.js | 7 +++++++ .../common/models/customer-forceid.json | 11 ++++++++++ .../server/model-config.json | 4 ++++ test/remoting.integration.js | 21 +++++++++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 test/fixtures/simple-integration-app/common/models/customer-forceid.json diff --git a/lib/model.js b/lib/model.js index 37bd024b..a3edb4a1 100644 --- a/lib/model.js +++ b/lib/model.js @@ -758,6 +758,12 @@ module.exports = function(registry) { var pathName = (scope.options && scope.options.http && scope.options.http.path) || scopeName; + // if there is atleast one updateOnly property, then we set + // createOnlyInstance flag in __create__ to indicate loopback-swagger + // code to create a separate model instance for create operation only + const updateOnlyProps = this.getUpdateOnlyProperties(); + const hasUpdateOnlyProps = updateOnlyProps && updateOnlyProps.length > 0; + var isStatic = scope.isStatic; var toModelName = scope.modelTo.modelName; @@ -791,6 +797,7 @@ module.exports = function(registry) { type: 'object', allowArray: true, model: toModelName, + createOnlyInstance: hasUpdateOnlyProps, http: {source: 'body'}, }, {arg: 'options', type: 'object', http: 'optionsFromRequest'}, diff --git a/lib/persisted-model.js b/lib/persisted-model.js index e83c80f6..de676a6d 100644 --- a/lib/persisted-model.js +++ b/lib/persisted-model.js @@ -627,6 +627,12 @@ module.exports = function(registry) { var typeName = PersistedModel.modelName; var options = PersistedModel.settings; + // if there is atleast one updateOnly property, then we set + // createOnlyInstance flag in __create__ to indicate loopback-swagger + // code to create a separate model instance for create operation only + const updateOnlyProps = this.getUpdateOnlyProperties(); + const hasUpdateOnlyProps = updateOnlyProps && updateOnlyProps.length > 0; + // This is just for LB 3.x options.replaceOnPUT = options.replaceOnPUT !== false; @@ -643,6 +649,7 @@ module.exports = function(registry) { accepts: [ { arg: 'data', type: 'object', model: typeName, allowArray: true, + createOnlyInstance: hasUpdateOnlyProps, description: 'Model instance data', http: {source: 'body'}, }, diff --git a/test/fixtures/simple-integration-app/common/models/customer-forceid.json b/test/fixtures/simple-integration-app/common/models/customer-forceid.json new file mode 100644 index 00000000..87f75b5c --- /dev/null +++ b/test/fixtures/simple-integration-app/common/models/customer-forceid.json @@ -0,0 +1,11 @@ +{ + "name": "customerforceidfalse", + "base": "PersistedModel", + "forceId": false, + "properties": { + "name": { + "type": "string", + "required": true + } + } +} \ No newline at end of file diff --git a/test/fixtures/simple-integration-app/server/model-config.json b/test/fixtures/simple-integration-app/server/model-config.json index 1eb76159..da3b3fed 100644 --- a/test/fixtures/simple-integration-app/server/model-config.json +++ b/test/fixtures/simple-integration-app/server/model-config.json @@ -65,5 +65,9 @@ "profile": { "dataSource": "db", "public": true + }, + "customerforceidfalse": { + "dataSource": "db", + "public": true } } diff --git a/test/remoting.integration.js b/test/remoting.integration.js index 90bb0b20..ad2dd316 100644 --- a/test/remoting.integration.js +++ b/test/remoting.integration.js @@ -194,6 +194,21 @@ describe('remoting - integration', function() { ]; expect(methods).to.include.members(expectedMethods); }); + + describe('createOnlyInstance', function() { + it('sets createOnlyInstance to true if id is generated and forceId is not set to false', + function() { + var storeClass = findClass('store'); + var createMethod = getCreateMethod(storeClass.methods); + assert(createMethod.accepts[0].createOnlyInstance === true); + }); + + it('sets createOnlyInstance to false if forceId is set to false in the model', function() { + var customerClass = findClass('customerforceidfalse'); + var createMethod = getCreateMethod(customerClass.methods); + assert(createMethod.accepts[0].createOnlyInstance === false); + }); + }); }); describe('With model.settings.replaceOnPUT false', function() { @@ -316,6 +331,12 @@ function getFormattedMethodsExcludingRelations(methods) { }); } +function getCreateMethod(methods) { + return methods.find(function(m) { + return (m.name === 'create'); + }); +} + function getFormattedScopeMethods(methods) { return methods.filter(function(m) { return m.name.indexOf('__') === 0;