mirror of
https://github.com/strapi/strapi.git
synced 2025-11-09 22:59:14 +00:00
Add ordering option to populate
This commit is contained in:
parent
7c4b7e02c8
commit
15cb9900b4
@ -4,7 +4,6 @@ const sqlite = {
|
|||||||
filename: '.tmp/data.db',
|
filename: '.tmp/data.db',
|
||||||
},
|
},
|
||||||
useNullAsDefault: true,
|
useNullAsDefault: true,
|
||||||
debug: true,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const postgres = {
|
const postgres = {
|
||||||
|
|||||||
@ -2,23 +2,8 @@ const { createCoreController } = require('@strapi/strapi').factories;
|
|||||||
|
|
||||||
module.exports = createCoreController('api::address.address', {
|
module.exports = createCoreController('api::address.address', {
|
||||||
async find(ctx) {
|
async find(ctx) {
|
||||||
// const { results } = await strapi.service('api::address.address').find();
|
const { results } = await strapi.service('api::address.address').find();
|
||||||
|
|
||||||
const r = await strapi.db.query('api::address.address').load(
|
ctx.body = await this.sanitizeOutput(results);
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
},
|
|
||||||
'categories',
|
|
||||||
{
|
|
||||||
limit: 2,
|
|
||||||
orderBy: 'asc|desc',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(r);
|
|
||||||
|
|
||||||
ctx.body = r;
|
|
||||||
|
|
||||||
// ctx.body = await this.sanitizeOutput(results);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1063,10 +1063,6 @@ const createEntityManager = (db) => {
|
|||||||
return { ...entity, ...entry };
|
return { ...entity, ...entry };
|
||||||
},
|
},
|
||||||
|
|
||||||
async LoadPage() {
|
|
||||||
|
|
||||||
},
|
|
||||||
|
|
||||||
// TODO: add lifecycle events
|
// TODO: add lifecycle events
|
||||||
async load(uid, entity, fields, params) {
|
async load(uid, entity, fields, params) {
|
||||||
const { attributes } = db.metadata.get(uid);
|
const { attributes } = db.metadata.get(uid);
|
||||||
|
|||||||
@ -33,12 +33,7 @@ const processOrderBy = (orderBy, ctx) => {
|
|||||||
const attribute = attributes[key];
|
const attribute = attributes[key];
|
||||||
|
|
||||||
if (!attribute) {
|
if (!attribute) {
|
||||||
// throw new Error(`Attribute ${key} not found on model ${uid}`);
|
throw new Error(`Attribute ${key} not found on model ${uid}`);
|
||||||
|
|
||||||
return {
|
|
||||||
column: key,
|
|
||||||
order: direction,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (types.isScalar(attribute.type)) {
|
if (types.isScalar(attribute.type)) {
|
||||||
|
|||||||
@ -5,7 +5,7 @@ const _ = require('lodash/fp');
|
|||||||
const { fromRow } = require('../transform');
|
const { fromRow } = require('../transform');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populate X to One relation
|
* Populate oneToOne and manyToOne relation
|
||||||
* @param {*} input
|
* @param {*} input
|
||||||
* @param {*} ctx
|
* @param {*} ctx
|
||||||
* @returns
|
* @returns
|
||||||
@ -193,7 +193,7 @@ const oneToMany = async (input, ctx) => {
|
|||||||
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
|
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
|
||||||
rootTable: qb.alias,
|
rootTable: qb.alias,
|
||||||
on: joinTable.on,
|
on: joinTable.on,
|
||||||
orderBy: joinTable.orderBy,
|
orderBy: _.mapValues((v) => populateValue.ordering || v, joinTable.orderBy),
|
||||||
})
|
})
|
||||||
.addSelect(joinColAlias)
|
.addSelect(joinColAlias)
|
||||||
.where({ [joinColAlias]: referencedValues })
|
.where({ [joinColAlias]: referencedValues })
|
||||||
@ -276,7 +276,7 @@ const manyToMany = async (input, ctx) => {
|
|||||||
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
|
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
|
||||||
rootTable: populateQb.alias,
|
rootTable: populateQb.alias,
|
||||||
on: joinTable.on,
|
on: joinTable.on,
|
||||||
orderBy: joinTable.orderBy,
|
orderBy: _.mapValues((v) => populateValue.ordering || v, joinTable.orderBy),
|
||||||
})
|
})
|
||||||
.addSelect(joinColAlias)
|
.addSelect(joinColAlias)
|
||||||
.where({ [joinColAlias]: referencedValues })
|
.where({ [joinColAlias]: referencedValues })
|
||||||
@ -367,7 +367,7 @@ const morphX = async (input, ctx) => {
|
|||||||
...(joinTable.on || {}),
|
...(joinTable.on || {}),
|
||||||
field: attributeName,
|
field: attributeName,
|
||||||
},
|
},
|
||||||
orderBy: joinTable.orderBy,
|
orderBy: _.mapValues((v) => populateValue.ordering || v, joinTable.orderBy),
|
||||||
})
|
})
|
||||||
.addSelect([`${alias}.${idColumn.name}`, `${alias}.${typeColumn.name}`])
|
.addSelect([`${alias}.${idColumn.name}`, `${alias}.${typeColumn.name}`])
|
||||||
.where({
|
.where({
|
||||||
@ -552,6 +552,7 @@ const pickPopulateParams = _.pick([
|
|||||||
'limit',
|
'limit',
|
||||||
'offset',
|
'offset',
|
||||||
'filters',
|
'filters',
|
||||||
|
'ordering',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const applyPopulate = async (results, populate, ctx) => {
|
const applyPopulate = async (results, populate, ctx) => {
|
||||||
|
|||||||
@ -326,14 +326,14 @@ const createQueryBuilder = (uid, db, initialState = {}) => {
|
|||||||
processSelect() {
|
processSelect() {
|
||||||
state.select = state.select.map((field) => helpers.toColumnName(meta, field));
|
state.select = state.select.map((field) => helpers.toColumnName(meta, field));
|
||||||
|
|
||||||
// if (this.shouldUseDistinct()) {
|
if (this.shouldUseDistinct()) {
|
||||||
// const joinsOrderByColumns = state.joins.flatMap((join) => {
|
const joinsOrderByColumns = state.joins.flatMap((join) => {
|
||||||
// return _.keys(join.orderBy).map((key) => this.aliasColumn(key, join.alias));
|
return _.keys(join.orderBy).map((key) => this.aliasColumn(key, join.alias));
|
||||||
// });
|
});
|
||||||
// const orderByColumns = state.orderBy.map(({ column }) => column);
|
const orderByColumns = state.orderBy.map(({ column }) => column);
|
||||||
|
|
||||||
// state.select = _.uniq([...joinsOrderByColumns, ...orderByColumns, ...state.select]);
|
state.select = _.uniq([...joinsOrderByColumns, ...orderByColumns, ...state.select]);
|
||||||
// }
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getKnexQuery() {
|
getKnexQuery() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user