mirror of
https://github.com/strapi/strapi.git
synced 2025-11-29 16:41:19 +00:00
Fix distinct
This commit is contained in:
parent
b23f090709
commit
549773698a
@ -31,6 +31,7 @@ const adminAuthStrategy = {
|
|||||||
|
|
||||||
ctx.state.userAbility = userAbility;
|
ctx.state.userAbility = userAbility;
|
||||||
ctx.state.user = user;
|
ctx.state.user = user;
|
||||||
|
ctx.state.isAuthenticatedAdmin = true;
|
||||||
|
|
||||||
return { authenticated: true, credentials: user };
|
return { authenticated: true, credentials: user };
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,6 +54,7 @@ const createJoin = (ctx, { alias, attributeName, attribute }) => {
|
|||||||
return alias;
|
return alias;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: toColumnName for orderBy & on
|
||||||
const applyJoin = (qb, join) => {
|
const applyJoin = (qb, join) => {
|
||||||
const {
|
const {
|
||||||
method = 'leftJoin',
|
method = 'leftJoin',
|
||||||
|
|||||||
@ -205,11 +205,29 @@ const createQueryBuilder = (uid, db) => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
processState() {
|
processState() {
|
||||||
state.select = state.select.map(field => helpers.toColumnName(meta, field));
|
|
||||||
state.orderBy = helpers.processOrderBy(state.orderBy, { qb: this, uid, db });
|
state.orderBy = helpers.processOrderBy(state.orderBy, { qb: this, uid, db });
|
||||||
state.where = helpers.processWhere(state.where, { qb: this, uid, db });
|
state.where = helpers.processWhere(state.where, { qb: this, uid, db });
|
||||||
state.populate = helpers.processPopulate(state.populate, { qb: this, uid, db });
|
state.populate = helpers.processPopulate(state.populate, { qb: this, uid, db });
|
||||||
state.data = helpers.toRow(meta, state.data);
|
state.data = helpers.toRow(meta, state.data);
|
||||||
|
|
||||||
|
this.processSelect();
|
||||||
|
},
|
||||||
|
|
||||||
|
shouldUseDistinct() {
|
||||||
|
return state.joins.length > 0 && _.isEmpty(state.groupBy);
|
||||||
|
},
|
||||||
|
|
||||||
|
processSelect() {
|
||||||
|
state.select = state.select.map(field => helpers.toColumnName(meta, field));
|
||||||
|
|
||||||
|
if (this.shouldUseDistinct()) {
|
||||||
|
const joinsOrderByColumns = state.joins.flatMap(join => {
|
||||||
|
return _.keys(join.orderBy).map(key => this.aliasColumn(key, join.alias));
|
||||||
|
});
|
||||||
|
const orderByColumns = state.orderBy.map(({ column }) => column);
|
||||||
|
|
||||||
|
state.select = _.uniq([...state.select, ...orderByColumns, ...joinsOrderByColumns]);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getKnexQuery() {
|
getKnexQuery() {
|
||||||
@ -229,20 +247,12 @@ const createQueryBuilder = (uid, db) => {
|
|||||||
|
|
||||||
switch (state.type) {
|
switch (state.type) {
|
||||||
case 'select': {
|
case 'select': {
|
||||||
if (state.select.length === 0) {
|
|
||||||
state.select = ['*'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state.joins.length > 0 && _.isEmpty(state.groupBy)) {
|
|
||||||
// add a discting when making joins and if we don't have a groupBy
|
|
||||||
// TODO: make sure we return the right data
|
|
||||||
qb.distinct(this.aliasColumn('id'));
|
|
||||||
|
|
||||||
// TODO: add column if they aren't there already
|
|
||||||
state.select.unshift(...state.orderBy.map(({ column }) => column));
|
|
||||||
}
|
|
||||||
|
|
||||||
qb.select(state.select.map(column => this.aliasColumn(column)));
|
qb.select(state.select.map(column => this.aliasColumn(column)));
|
||||||
|
|
||||||
|
if (this.shouldUseDistinct()) {
|
||||||
|
qb.distinct();
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'count': {
|
case 'count': {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user