hiHOH
Convex Community13mo ago
2 replies
hiHO

is there a method to populate the query

is there a method to populate the query with foriegn key data?
export const getAllStock = query({
  args: {},
  handler: async (ctx) => {
    return await ctx.db
      .query("stockPrices")
      .withIndex("by_stock")
      .collect()
      .then(async (stockPrices) => {
        // Fetch related stock records for each stock price
        const stocksWithDetails = await Promise.all(
          stockPrices.map(async (stockPrice) => {
            const stock = await ctx.db.get(stockPrice.stockId);
            const { stockId, ...rest } = stockPrice;
            return {
              ...rest,
              symbol: stock!.symbol,
              name: stock!.name,
            };
          })
        );
        return stocksWithDetails;
      });
  },
});
Was this page helpful?