feat: use many to one relationship for categories in directus

This commit is contained in:
2026-03-05 18:56:44 -06:00
parent f4676d151f
commit e7c660c142
4 changed files with 13 additions and 13 deletions

View File

@@ -10,7 +10,7 @@ import { timeago } from '@support/time';
const posts = await directus.request( const posts = await directus.request(
readItems('posts', { readItems('posts', {
filter: { published: { _eq: true } }, filter: { published: { _eq: true } },
fields: ['*'], fields: ['*', 'category.slug'],
sort: ['-published_date'], sort: ['-published_date'],
}) })
); );
@@ -31,13 +31,16 @@ const layoutPattern = [
const postMap: Map<string, Post[]> = posts const postMap: Map<string, Post[]> = posts
.sort((a: Post, b: Post) => b.published_date.valueOf() - a.published_date.valueOf()) .sort((a: Post, b: Post) => b.published_date.valueOf() - a.published_date.valueOf())
.reduce((acc, obj) => { .reduce((acc, obj) => {
let posts = acc.get(obj.category); const categorySlug = obj.category?.slug;
if (!categorySlug) return acc;
let posts = acc.get(categorySlug);
if (!posts) { if (!posts) {
posts = []; posts = [];
} }
posts.push(obj); posts.push(obj);
acc.set(obj.category, posts); acc.set(categorySlug, posts);
return acc; return acc;
}, new Map<string, Post[]>()); }, new Map<string, Post[]>());

View File

@@ -37,7 +37,7 @@ export type Post = {
title: string; title: string;
description: string; description: string;
tags: string[]; tags: string[];
category: string; category: Category;
selected: boolean; selected: boolean;
published: boolean; published: boolean;
content: string; content: string;

View File

@@ -17,7 +17,9 @@ import { getDirectusImageURL } from '@/support/url';
const post = Astro.props; const post = Astro.props;
export async function getStaticPaths() { export async function getStaticPaths() {
const posts = await directus.request(readItems('posts')); const posts = await directus.request(readItems('posts', {
fields: ['*', 'category.*'],
}));
return posts.map((post) => ({ return posts.map((post) => ({
params: { slug: post.slug }, params: { slug: post.slug },
props: post, props: post,
@@ -25,12 +27,7 @@ export async function getStaticPaths() {
} }
const global = await directus.request(readSingleton('site_global')); const global = await directus.request(readSingleton('site_global'));
const [category] = post.category ? await directus.request( const category = post.category;
readItems('categories', {
filter: { slug: { _eq: post.category },},
limit: 1,
}))
: [];
const readingTime = getReadingTime(post.content || ''); const readingTime = getReadingTime(post.content || '');

View File

@@ -22,7 +22,7 @@ const global = await directus.request(readSingleton('site_global'));
const posts = await directus.request( const posts = await directus.request(
readItems('posts', { readItems('posts', {
filter: { published: { _eq: true } }, filter: { published: { _eq: true } },
fields: ['*'], fields: ['*', 'category.slug'],
sort: ['-published_date'], sort: ['-published_date'],
}) })
); );
@@ -30,7 +30,7 @@ const posts = await directus.request(
const categoriesPosts = posts const categoriesPosts = posts
.sort((a: Post, b: Post) => b.published_date.valueOf() - a.published_date.valueOf()) .sort((a: Post, b: Post) => b.published_date.valueOf() - a.published_date.valueOf())
.filter((b) => { .filter((b) => {
return b.category === category.slug; return b.category?.slug === category.slug;
}); });
--- ---