chore: optimize post query
This commit is contained in:
parent
ce4c43ddf1
commit
d424082c95
1 changed files with 3 additions and 86 deletions
|
@ -5,58 +5,6 @@ const SUPABASE_KEY = process.env['SUPABASE_KEY']
|
|||
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
|
||||
const PAGE_SIZE = 1000
|
||||
|
||||
const fetchAllTags = async () => {
|
||||
const { data, error } = await supabase
|
||||
.from('posts_tags')
|
||||
.select('posts_id, tags(name)')
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching all tags from Supabase:', error)
|
||||
return {}
|
||||
}
|
||||
|
||||
return data.reduce((acc, { posts_id, tags }) => {
|
||||
if (!tags || !tags['name']) return acc
|
||||
if (!acc[posts_id]) acc[posts_id] = []
|
||||
acc[posts_id].push(tags['name'])
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
const fetchAllBlocks = async () => {
|
||||
const { data, error } = await supabase
|
||||
.from('posts_blocks')
|
||||
.select('posts_id, collection, item, sort')
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching all blocks from Supabase:', error)
|
||||
return {}
|
||||
}
|
||||
|
||||
return data.reduce((acc, block) => {
|
||||
if (!acc[block['posts_id']]) {
|
||||
acc[block['posts_id']] = []
|
||||
}
|
||||
acc[block['posts_id']].push(block)
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
const fetchBlockData = async (collection, itemId) => {
|
||||
const { data, error } = await supabase
|
||||
.from(collection)
|
||||
.select('*')
|
||||
.eq('id', itemId)
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
console.error(`Error fetching data from ${collection} for item ${itemId}:`, error)
|
||||
return null
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
const fetchAllPosts = async () => {
|
||||
let posts = []
|
||||
let page = 0
|
||||
|
@ -90,50 +38,24 @@ const fetchAllPosts = async () => {
|
|||
return posts
|
||||
}
|
||||
|
||||
const processPosts = async (posts, tagsByPostId, blocksByPostId) => {
|
||||
const processPosts = async (posts) => {
|
||||
return Promise.all(posts.map(async post => {
|
||||
// tags
|
||||
post['tags'] = tagsByPostId[post['id']] || []
|
||||
|
||||
// blocks
|
||||
const blocks = blocksByPostId[post['id']] || []
|
||||
post['blocks'] = await Promise.all(blocks.map(async block => {
|
||||
const blockData = await fetchBlockData(block['collection'], block['item'])
|
||||
if (!blockData) return null
|
||||
return {
|
||||
'type': block['collection'],
|
||||
'sort': block['sort'],
|
||||
...blockData
|
||||
}
|
||||
})).then(blocks => blocks.filter(block => block !== null))
|
||||
|
||||
// artists
|
||||
post['artists'] = post['artists'] ? post['artists'].sort((a, b) => a['name'].localeCompare(b['name'])) : null
|
||||
|
||||
// books
|
||||
post['books'] = post['books'] ? post['books'].map(book => ({
|
||||
title: book['title'],
|
||||
author: book['author'],
|
||||
description: book['description'],
|
||||
url: `/books/${book['isbn']}`,
|
||||
})).sort((a, b) => a['title'].localeCompare(b['title'])) : null
|
||||
|
||||
// movies
|
||||
post['movies'] = post['movies'] ? post['movies'].map(movie => {
|
||||
movie['url'] = `/watching/movies/${movie['tmdb_id']}`
|
||||
return movie
|
||||
}).sort((a, b) => b['year'] - a['year']) : null
|
||||
|
||||
// genres
|
||||
post['genres'] = post['genres'] ? post['genres'].sort((a, b) => a['name'].localeCompare(b['name'])) : null
|
||||
|
||||
// shows
|
||||
post['shows'] = post['shows'] ? post['shows'].map(show => {
|
||||
show['url'] = `/watching/shows/${show['tmdb_id']}`
|
||||
return show
|
||||
}).sort((a, b) => b['year'] - a['year']) : null
|
||||
|
||||
// image
|
||||
if (post['image']) post['image'] = post['image']['filename_disk']
|
||||
|
||||
return post
|
||||
|
@ -141,11 +63,6 @@ const processPosts = async (posts, tagsByPostId, blocksByPostId) => {
|
|||
}
|
||||
|
||||
export default async function () {
|
||||
const [posts, tagsByPostId, blocksByPostId] = await Promise.all([
|
||||
fetchAllPosts(),
|
||||
fetchAllTags(),
|
||||
fetchAllBlocks()
|
||||
])
|
||||
|
||||
return await processPosts(posts, tagsByPostId, blocksByPostId)
|
||||
const posts = await fetchAllPosts()
|
||||
return await processPosts(posts)
|
||||
}
|
Reference in a new issue