Fix issue when the relation is populated or not (Bookshelf)

This commit is contained in:
Kamal Bennani 2019-02-02 14:17:03 +01:00 committed by Alexandre Bodin
parent 19c696ff25
commit 0d9626b165
2 changed files with 36 additions and 3 deletions

View File

@ -14,9 +14,23 @@ class QueryBuilder {
let fieldKey = `${model.collectionName}.${attributeKey}`;
if (association) {
if (association.nature === 'manyToMany') {
const { attribute, column } = model.attributes[key];
const { attribute, column } = model.attributes[association.via];
fieldKey = `${association.tableCollectionName}.${attribute}_${column}`;
}
/**
* If for instance we want to filter on the model relation, such as
* {
* product: {
* method: 'whereIn',
* value: ["1", "2", "3"]
* }
* }
* the fieldKey should be "products.id" and not "products.product"
*/
if (attributeKey === key) {
fieldKey = `${model.collectionName}.${model.primaryKey}`;
}
}
if (value.symbol) {
@ -108,7 +122,7 @@ class QueryBuilder {
qb.innerJoin(
relationTable,
`${association.tableCollectionName}.${strapiModel.attributes[key].attribute}_${strapiModel.attributes[key].column}`,
`${association.tableCollectionName}.${strapiModel.attributes[association.alias].attribute}_${strapiModel.attributes[association.alias].column}`,
`${relationTable}.${astModel.primaryKey}`,
);
} else {

View File

@ -97,7 +97,26 @@ module.exports = {
return data
.filter(entry => {
const entryValue = entry[ids.alias][astModel.primaryKey];
/**
* In some cases (Mongoose for instance) the entry will be populated so we need to access its value using the primary key
* Example
* // Mongoose
* entry = {
* product: {
* _id: "some_id", // the value is entry["product"]["_id"]
* price: 10
* },
* company: "strapi"
* }
* // Bookshelf
* entry = {
* product: 1, // the value is entry["product"]
* company: "strapi"
* }
*/
const entryValue = _.isUndefined(entry[ids.alias][astModel.primaryKey])
? entry[ids.alias]
: entry[ids.alias][astModel.primaryKey];
return entryValue.toString() === ids.value.toString();
})
.slice(skip, skip + limit);