import { PostSchema } from '#database/schema' import { column, scope } from '@adonisjs/lucid/orm' export type PostStatus = 'draft' | 'published' export default class Post extends PostSchema { // Override : le générateur infère `any` pour les enums + arrays Postgres, // on retype proprement. @column() declare status: PostStatus @column() declare tags: string[] /** * Articles publiés, du plus récent au plus ancien. * Utilisé par toutes les surfaces publiques (index, RSS, sitemap, related). */ static published = scope((query) => { query.where('status', 'published').whereNotNull('publishedAt').orderBy('publishedAt', 'desc') }) /** * Articles "liés" : intersection de tags non vide, hors article courant, * triés par récence. Limit côté appelant. */ static relatedTo = scope((query, post: Post) => { if (post.tags.length === 0) { query.whereRaw('1 = 0') return } query .where('status', 'published') .whereNot('id', post.id) .whereRaw('tags && ?::text[]', [post.tags]) .orderBy('publishedAt', 'desc') }) }