Support createOnlyInstance in model (#3548)

* setting up createOnlyInstance

* add comment

* fix eslint issue

* new tests

* Address code review comments
This commit is contained in:
Rashmi Hunt 2017-08-22 17:10:55 -07:00 committed by GitHub
parent 661f7741cd
commit 3651c09782
5 changed files with 50 additions and 0 deletions

View File

@ -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'},

View File

@ -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'},
},

View File

@ -0,0 +1,11 @@
{
"name": "customerforceidfalse",
"base": "PersistedModel",
"forceId": false,
"properties": {
"name": {
"type": "string",
"required": true
}
}
}

View File

@ -65,5 +65,9 @@
"profile": {
"dataSource": "db",
"public": true
},
"customerforceidfalse": {
"dataSource": "db",
"public": true
}
}

View File

@ -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;