ThallesComHT
Convex Community5mo ago
2 replies
ThallesComH

How would you architect this in Convex?

Hey, I'm trying out Convex in a learning project, a simple streaming service, and I'm wondering how would you deal with fetching external movie metadata (tmdb in this case), is syncing the best option or should I go with the action route? And for the action, do I really need to create a empty useEffect to call the action?

Here's what I thought so far:
import { ActionCache } from "@convex-dev/action-cache";
import { Movie, TMDB } from "tmdb-ts";
import { components, internal } from "./_generated/api";
import { action, internalAction } from "./_generated/server";

const cache = new ActionCache(components.actionCache, {
    action: internal.movies.getPopularMoviesFromTMDB,
})

export const getPopularMovies = action({
  args: {},
  handler: async (ctx): Promise<Movie[]> => {
    const result = await cache.fetch(ctx, {});

    return result;
  },
});

export const getPopularMoviesFromTMDB = internalAction({
  args: {},
  handler: async (ctx) => {
    const tmdb = new TMDB(process.env.TMDB_ACCESS_TOKEN!);

    const popularMovies = await tmdb.movies.popular();

    return popularMovies.results;
  },
});
Was this page helpful?