hiHO
hiHO3mo ago

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;
});
},
});
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;
});
},
});
3 Replies
erquhart
erquhart3mo ago
Hey there - this is exactly how you would do it. What are you trying to figure out?
hiHO
hiHOOP3mo ago
i was asking if there was a more direct ".populate()" methode to do this like in mongoose or include or annotate field like in ORM's like prisma and django i have worked in the past
erquhart
erquhart3mo ago
GitHub
convex-helpers/packages/convex-helpers/README.md at main · get-conv...
A collection of useful code to complement the official packages. - get-convex/convex-helpers

Did you find this page helpful?