strapi-utils
Advanced tools
Comparing version 3.0.0-alpha.5.5 to 3.0.0-alpha.6
@@ -19,3 +19,3 @@ 'use strict'; | ||
if (_.isObject(controller) && controller.hasOwnProperty('identity')) { | ||
controller = controller.identity; | ||
controller = controller.identity.toLowerCase(); | ||
} else if (_.isString(controller)) { | ||
@@ -22,0 +22,0 @@ controller = controller.toLowerCase(); |
@@ -75,3 +75,3 @@ 'use strict'; | ||
getNature: (association, key, models) => { | ||
getNature: (association, key, models, currentModelName) => { | ||
const types = { | ||
@@ -102,3 +102,3 @@ current: '', | ||
// We have to find if they are a model linked to this key | ||
_.forIn(models, model => { | ||
_.forIn(_.omit(models, currentModelName || ''), model => { | ||
_.forIn(model.attributes, attribute => { | ||
@@ -172,3 +172,3 @@ if (attribute.hasOwnProperty('via') && attribute.via === key && attribute.hasOwnProperty('collection')) { | ||
}; | ||
} else if (types.current === 'model' && types.other === 'collection') { | ||
} else if ((types.current === 'model' || types.current === 'modelD') && types.other === 'collection') { | ||
return { | ||
@@ -178,2 +178,7 @@ nature: 'oneToMany', | ||
}; | ||
} else if (types.current === 'modelD' && types.other === 'collection') { | ||
return { | ||
nature: 'oneToMany', | ||
verbose: 'belongsTo' | ||
}; | ||
} else if (types.current === 'collection' && types.other === 'model') { | ||
@@ -233,3 +238,4 @@ return { | ||
// Get relation nature | ||
const infos = this.getNature(association, key); | ||
const infos = this.getNature(association, key, undefined, model.toLowerCase()); | ||
const details = _.get(strapi.models, `${association.model || association.collection}.attributes.${association.via}`, {}); | ||
@@ -244,3 +250,4 @@ // Build associations object | ||
nature: infos.nature, | ||
autoPopulate: (_.get(association, 'autoPopulate') || _.get(strapi.config, 'jsonapi.enabled')) === true | ||
autoPopulate: (_.get(association, 'autoPopulate') || _.get(strapi.config, 'jsonapi.enabled')) === true, | ||
dominant: details.dominant !== true | ||
}); | ||
@@ -254,3 +261,4 @@ } else if (association.hasOwnProperty('model')) { | ||
nature: infos.nature, | ||
autoPopulate: (_.get(association, 'autoPopulate') || _.get(strapi.config, 'jsonapi.enabled')) === true | ||
autoPopulate: (_.get(association, 'autoPopulate') || _.get(strapi.config, 'jsonapi.enabled')) === true, | ||
dominant: details.dominant !== true | ||
}); | ||
@@ -262,3 +270,57 @@ } | ||
return _.findKey(strapi.models[association.model || association.collection].attributes, {via: attribute}); | ||
}, | ||
convertParams: (entity, params) => { | ||
if (!entity) { | ||
throw new Error('You can\'t call the convert params method without passing the model\'s name as a first argument.'); | ||
} | ||
const model = entity.toLowerCase(); | ||
if (!strapi.models.hasOwnProperty(model)) { | ||
return this.log.error(`The model ${model} can't be found.`); | ||
} | ||
const connector = strapi.models[model].orm; | ||
if (!connector) { | ||
throw new Error(`Impossible to determine the use ORM for the model ${model}.`); | ||
} | ||
const convertor = strapi.hook[connector].load().getQueryParams; | ||
const convertParams = { | ||
where: {}, | ||
sort: '', | ||
start: 0, | ||
limit: 100 | ||
}; | ||
_.forEach(params, (value, key) => { | ||
let result; | ||
if (_.includes(['_start', '_limit'], key)) { | ||
result = convertor(value, key); | ||
} else if (key === '_sort') { | ||
const [attr, order] = value.split(':'); | ||
result = convertor(order, key, attr); | ||
} else { | ||
const suffix = key.split('_'); | ||
let type; | ||
if (_.includes(['ne', 'lt', 'gt', 'lte', 'gte'], _.last(suffix))) { | ||
type = `_${_.last(suffix)}`; | ||
key = _.dropRight(suffix).join('_'); | ||
} else { | ||
type = '='; | ||
} | ||
result = convertor(value, type, key); | ||
} | ||
_.set(convertParams, result.key, result.value); | ||
}); | ||
return convertParams; | ||
} | ||
}; |
{ | ||
"name": "strapi-utils", | ||
"version": "3.0.0-alpha.5.5", | ||
"version": "3.0.0-alpha.6", | ||
"description": "Shared utilities for the Strapi packages", | ||
@@ -5,0 +5,0 @@ "homepage": "http://strapi.io", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21338
16
581
8
1