Merci pour votre réponse Gordon25,
Pour votre question : rien de tout cela, c'est du pur amateurisme.
C'est à dire que je suis un peu jusque-boutiste sur les bords, et comme je suis têtu j'ai tout de même persévéré dans le sens de la mono-requête (au pire : 2).
Auparavant je m'étais intéressé aux modélisations de base de données, je ne m'intéresse sérieusement aux requêtes SQL que depuis deux mois. Je n'aime pas les ORMs. En effet, entre se taper la compréhension d'un ORM qui va me limiter/poser des problèmes de perf's dans certains cas, et attaquer directement le SQL, je préfère nettement attaquer bas niveau avec SQL. Je ne trouve pas cela si difficile au final. Je tâtonne un peu bien sûr, codant seul dans mon coin je manque de feedback, d'où ce post...
Pour vous montrer où j'en suis, voici une requête sous Postgres pour GET un article :
SELECT
a.*,
jsonb_build_object(
'_given_name', p._given_name,
'_additional_name', p._additional_name,
'_family_name', p._family_name,
'_usual_name', p._usual_name,
'_nickname', p._nickname,
'_prefix', p._prefix,
'_suffix', p._suffix,
'_url', p._url,
'_name', CONCAT_WS(' ', p._given_name, p._family_name),
'_full_name', CONCAT_WS(', ', CONCAT_WS(' ', p._prefix, p._given_name, p._additional_name, p._family_name), p._suffix),
'_media' , (
SELECT
jsonb_build_object(
'_name', m._name,
'_type', m._type,
'_url', m._url,
'_description', m._description
)
FROM __media m
WHERE m._id = ac._media_id
),
'_username', ac._username,
'_display_name', ac._display_name
) AS _author,
(
SELECT jsonb_agg(
jsonb_build_object(
'_id', c._id,
'_content', c._content,
'_created_at', c._created_at,
'_modified_at', c._modified_at,
'_commentator_id', c._account_id,
'_author' , jsonb_build_object(
'_display_name', ac._display_name,
'_username', ac._username,
'_given_name', p._given_name,
'_additional_name', p._additional_name,
'_family_name', p._family_name,
'_usual_name', p._usual_name,
'_nickname', p._nickname,
'_prefix', p._prefix,
'_suffix', p._suffix,
'_media_id', p._media_id,
'_name', CONCAT_WS(' ', p._given_name, p._family_name),
'_full_name', CONCAT_WS(', ', CONCAT_WS(' ', p._prefix, p._given_name, p._additional_name, p._family_name), p._suffix),
'_media', (
SELECT jsonb_build_object(
'_name', m._name,
'_type', m._type,
'_url', m._url,
'_description', m._description
)
FROM __media m
WHERE m._id = ac._media_id
)
)
)
ORDER BY c._id
)
FROM __comment c
LEFT JOIN __account ac ON c._account_id = ac._id
LEFT JOIN __person p ON c._account_id = p._id
WHERE c._article_id = a._id
) AS _comments,
(
SELECT jsonb_agg(
jsonb_build_object(
'_name', t._name,
'_slug', t._slug
)
)
FROM __tag_to_article ta
LEFT JOIN __tag t ON ta._tag_id = t._id
WHERE ta._article_id = a._id
) AS _tags,
(
SELECT jsonb_agg(
jsonb_build_object(
'_name', m._name,
'_type', m._type,
'_url', m._url,
'_description', m._description
)
)
FROM __media_to_article ma
LEFT JOIN __media m ON ma._media_id = m._id
WHERE ma._article_id = a._id
) AS medias
FROM
__article a
LEFT JOIN
__account ac ON a._author_id = ac._id
LEFT JOIN
__person p ON ac._person_id = p._id
WHERE
a._slug = 'le-slug-de-article'
GROUP BY
a._id, p._id, ac._id;
Ce qui donnera cette réponse à exploiter (après traitement des clefs, des dates et autres bricoles via JS) :
{
"id": 5,
"name": "Agent 327",
"alternativeHeadline": "",
"slug": "agent-327",
"content": "Un contenu...",
"createdAt": "2022-04-16T17:10:25.000Z",
"modifiedAt": "2022-04-16T18:15:22.000Z",
"type": null,
"description": "Test pour une vidéo...",
"authorId": 4,
"status": 1,
"isReadable": true,
"isCommentable": true,
"isVisibleComments": true,
"add": null,
"author": {
"url": null,
"name": "",
"media": {
"url": "/medias/images/uploads/charles-de-foucauld-original.webp",
"name": null,
"type": null,
"description": null
},
"prefix": null,
"suffix": null,
"nickname": null,
"username": "Iota",
"fullName": "",
"givenName": null,
"usualName": null,
"familyName": null,
"displayName": "username",
"additionalName": null
},
"comments": [
{
"id": 1,
"author": {
"name": "Jeanne d'Arc",
"media": {
"url": "/medias/images/uploads/jeanne-d-arc-original.webp",
"name": null,
"type": null,
"description": null
},
"prefix": "Ste",
"suffix": null,
"mediaId": 2,
"nickname": "La Pucelle d'Orléans",
"username": "Beta",
"fullName": "Ste Jeanne d'Arc",
"givenName": "Jeanne",
"usualName": null,
"familyName": "d'Arc",
"displayName": "nickname",
"additionalName": null
},
"content": "Un petit commentaire. Un.",
"createdAt": "2022-04-16T19:10:25",
"modifiedAt": "2022-04-16T19:17:22",
"commentatorId": 2
},
{
"id": 2,
"author": {
"name": "Charles de Gaulle",
"media": {
"url": "/medias/images/uploads/charles-de-gaulle-original.webp",
"name": null,
"type": null,
"description": null
},
"prefix": "Général",
"suffix": null,
"mediaId": 3,
"nickname": "Le Général",
"username": "Delta",
"fullName": "Général Charles André Joseph Marie de Gaulle",
"givenName": "Charles",
"usualName": null,
"familyName": "de Gaulle",
"displayName": "name",
"additionalName": "André Joseph Marie"
},
"content": "Un petit commentaire. Deux.",
"createdAt": "2022-04-16T19:15:25",
"modifiedAt": "2022-04-16T20:25:22",
"commentatorId": 3
},
{
"id": 3,
"author": {
"name": "",
"media": {
"url": "/medias/images/uploads/charles-de-foucauld-original.webp",
"name": null,
"type": null,
"description": null
},
"prefix": null,
"suffix": null,
"mediaId": null,
"nickname": "El Comandante",
"username": "Iota",
"fullName": "",
"givenName": null,
"usualName": null,
"familyName": null,
"displayName": "username",
"additionalName": null
},
"content": "Un petit commentaire. Trois.",
"createdAt": "2022-04-16T21:35:25",
"modifiedAt": "2022-04-16T22:15:22",
"commentatorId": 4
},
{
"id": 4,
"author": {
"name": "Henri de Lubac",
"media": {
"url": "/medias/images/uploads/henri-de-lubac-original.webp",
"name": null,
"type": null,
"description": null
},
"prefix": "P.",
"suffix": "s.j.",
"mediaId": 1,
"nickname": null,
"username": "Alpha",
"fullName": "P. Henri Sonier de Lubac, s.j.",
"givenName": "Henri",
"usualName": null,
"familyName": "de Lubac",
"displayName": "fullName",
"additionalName": "Sonier"
},
"content": "Un petit commentaire. Quatre.",
"createdAt": "2022-04-17T10:45:25",
"modifiedAt": "2022-04-17T10:47:12",
"commentatorId": 1
},
{
"id": 5,
"author": {
"name": "Jeanne d'Arc",
"media": {
"url": "/medias/images/uploads/jeanne-d-arc-original.webp",
"name": null,
"type": null,
"description": null
},
"prefix": "Ste",
"suffix": null,
"mediaId": 2,
"nickname": "La Pucelle d'Orléans",
"username": "Beta",
"fullName": "Ste Jeanne d'Arc",
"givenName": "Jeanne",
"usualName": null,
"familyName": "d'Arc",
"displayName": "nickname",
"additionalName": null
},
"content": "Un petit commentaire. Cinq.",
"createdAt": "2022-05-02T09:10:25",
"modifiedAt": "2022-05-02T10:07:22",
"commentatorId": 2
}
],
"tags": [
{
"name": "Anthropologie",
"slug": "anthropologie"
},
{
"name": "Paroles des Pères",
"slug": "paroles-des-peres"
},
{
"name": "Ressources en ligne",
"slug": "ressources-en-ligne"
}
],
"medias": null
}
Lorsque le projet sera stable, je convertirai peut-être tout cela en fonction stockée.
Modifié par Olivier C (04 May 2024 - 15:40)