beermanB
Convex Community16mo ago
1 reply
beerman

a little confused about getManyVia

Hey all, I have a table features which stores different feature options and joiner table property_features which connects the features to properties and has an additional details field for property specific information per feature.

I'm trying to get all the property features (name, as well as the details), so I thought that getManyVia would be the best option to use, but I'm only getting the names but not the details. Have I misunderstood how this works?

extract from schema
properties: defineTable({
  ...
})

features: defineTable({
        name: v.string()
})
.index('by_name', ['name']),

property_features: defineTable({
    feature_id: v.id('features'),
    property_id: v.id('properties'),
    details: v.optional(v.string())
})
.index('by_feature_id', ['feature_id'])
.index('by_property_id', ['property_id']),

query

export const get = query({
    args: {},
    handler: async (ctx) => {
        // Fetch all properties
        const properties = await ctx.db.query('properties').collect();

        // Fetch related data for each property
        return await Promise.all(
            properties.map(async (property) => {
                const propertyFeatures = await getManyVia(
                    ctx.db,
                    'property_features',
                    'feature_id',
                    'by_property_id',
                    property._id
                );

                return {
                    propertyFeatures
                };
            })
        );
    }
});
Was this page helpful?