Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'},
Expand Down
7 changes: 7 additions & 0 deletions lib/persisted-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "customerforceidfalse",
"base": "PersistedModel",
"forceId": false,
"properties": {
"name": {
"type": "string",
"required": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@
"profile": {
"dataSource": "db",
"public": true
},
"customerforceidfalse": {
"dataSource": "db",
"public": true
}
}
21 changes: 21 additions & 0 deletions test/remoting.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -316,6 +331,12 @@ function getFormattedMethodsExcludingRelations(methods) {
});
}

function getCreateMethod(methods) {
return methods.find(function(m) {
return (m.name === 'create');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, this can be simplified to a one-liner:

return methods.find(m => m.name === 'create');

});
}

function getFormattedScopeMethods(methods) {
return methods.filter(function(m) {
return m.name.indexOf('__') === 0;
Expand Down