I found a partial solution by myself. So if anyone found this post, the solution is to use the this.transformReponse.
I found this solution there: node.js - Entity Service VS super.find() VS Query Engine API, what is the differences? - Stack Overflow
like this:
const lon = ctx.query.filters['lon']
const lat = ctx.query.filters['lat']
const distance = ctx.query.filters['distance']
if (lon && lat && distance) {
// And updated version of the query, I improved it a bit since the last post
// This query will return all addresses IDs that are within the distance of the given coordinates
const req = await strapi.db.connection.raw(`
SELECT id
FROM addresses
WHERE ST_DWithin(
ST_Transform(ST_SetSRID(ST_MakePoint(lon, lat), 4326)),
ST_Transform(ST_SetSRID(ST_MakePoint(${lon}, ${lat}), 4326)), ${distance})
`);
// Get your start and limit from the query
const { start, limit } = ctx.query.filters;
// This query will return all data from address that match the IDs from the previous query
const entityData = await strapi.entityService.findMany('api::address.address', {
filters: {
id: {
// Here I'm querying the IDs from the previous query
$in: req.rows.map((row) => row.id)
},
},
// Here you can popylate the fields you want, like any other filter
populate: ['*'],
// For the pagination you need to pass the start and limit
// source: https://docs.strapi.io/dev-docs/api/entity-service/order-pagination#pagination
start,
limit
});
// This will transofrm into a response that match others strapi responses.
const data = await this.transformResponse(entityData);
return {
data,
meta: {
// Here you can create your own pagination
pagination: {
// You need to convert the start and limit to numbers
// I don't know why this is converted to string, but this works on the filter
start: Number(start),
limit: Number(limit),
// source: https://www.restack.io/docs/strapi-knowledge-strapi-findmany-pagination-guide
total: req.rows.length
}
}
};
}
Before this fix my response was like this:
[
{
id: 15,
(... address content)
createdAt: '2024-02-05T17:10:01.237Z',
updatedAt: '2024-02-05T17:10:01.237Z',
my_relation: [
{
id: 16,
(...relation content)
createdAt: '2024-02-05T17:10:01.256Z',
updatedAt: '2024-02-05T17:10:01.275Z',
publishedAt: '2024-02-05T17:10:01.254Z'
}
(...)
]
(...)
]
Now it is like this:
[
{
"id": 15,
"attributes": {
(... address content)
"createdAt": "2024-02-05T17:10:01.237Z",
"updatedAt": "2024-02-05T17:10:01.237Z",
"reviews": {
"data": [
{
"id": 16,
"attributes": {
(...relation content)
"createdAt": "2024-02-05T17:10:01.256Z",
"updatedAt": "2024-02-05T17:10:01.275Z",
"publishedAt": "2024-02-05T17:10:01.254Z"
}
},
]
}
}
}
]
But I still have one problem, my response is like this:
{
data: {
data: [ // Here is the problem
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object]
],
meta: {}
},
meta: { pagination: { start: 0, limit: 9, total: 64 } }
}
I can’t do this on strapi because it’s just break. xD
return {
data: data.data,
}