Support createOnlyInstance in model (#3548)
* setting up createOnlyInstance * add comment * fix eslint issue * new tests * Address code review comments
This commit is contained in:
parent
661f7741cd
commit
3651c09782
|
@ -758,6 +758,12 @@ module.exports = function(registry) {
|
||||||
var pathName =
|
var pathName =
|
||||||
(scope.options && scope.options.http && scope.options.http.path) || scopeName;
|
(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 isStatic = scope.isStatic;
|
||||||
var toModelName = scope.modelTo.modelName;
|
var toModelName = scope.modelTo.modelName;
|
||||||
|
|
||||||
|
@ -791,6 +797,7 @@ module.exports = function(registry) {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
allowArray: true,
|
allowArray: true,
|
||||||
model: toModelName,
|
model: toModelName,
|
||||||
|
createOnlyInstance: hasUpdateOnlyProps,
|
||||||
http: {source: 'body'},
|
http: {source: 'body'},
|
||||||
},
|
},
|
||||||
{arg: 'options', type: 'object', http: 'optionsFromRequest'},
|
{arg: 'options', type: 'object', http: 'optionsFromRequest'},
|
||||||
|
|
|
@ -627,6 +627,12 @@ module.exports = function(registry) {
|
||||||
var typeName = PersistedModel.modelName;
|
var typeName = PersistedModel.modelName;
|
||||||
var options = PersistedModel.settings;
|
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
|
// This is just for LB 3.x
|
||||||
options.replaceOnPUT = options.replaceOnPUT !== false;
|
options.replaceOnPUT = options.replaceOnPUT !== false;
|
||||||
|
|
||||||
|
@ -643,6 +649,7 @@ module.exports = function(registry) {
|
||||||
accepts: [
|
accepts: [
|
||||||
{
|
{
|
||||||
arg: 'data', type: 'object', model: typeName, allowArray: true,
|
arg: 'data', type: 'object', model: typeName, allowArray: true,
|
||||||
|
createOnlyInstance: hasUpdateOnlyProps,
|
||||||
description: 'Model instance data',
|
description: 'Model instance data',
|
||||||
http: {source: 'body'},
|
http: {source: 'body'},
|
||||||
},
|
},
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"name": "customerforceidfalse",
|
||||||
|
"base": "PersistedModel",
|
||||||
|
"forceId": false,
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"type": "string",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -65,5 +65,9 @@
|
||||||
"profile": {
|
"profile": {
|
||||||
"dataSource": "db",
|
"dataSource": "db",
|
||||||
"public": true
|
"public": true
|
||||||
|
},
|
||||||
|
"customerforceidfalse": {
|
||||||
|
"dataSource": "db",
|
||||||
|
"public": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -194,6 +194,21 @@ describe('remoting - integration', function() {
|
||||||
];
|
];
|
||||||
expect(methods).to.include.members(expectedMethods);
|
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() {
|
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) {
|
function getFormattedScopeMethods(methods) {
|
||||||
return methods.filter(function(m) {
|
return methods.filter(function(m) {
|
||||||
return m.name.indexOf('__') === 0;
|
return m.name.indexOf('__') === 0;
|
||||||
|
|
Loading…
Reference in New Issue