beermanB
Convex Community16mo ago
3 replies
beerman

unable to query relation with getOneFrom helper

import { query } from './_generated/server';
import { getOneFrom, getAll, getManyFrom, getManyVia } from 'convex-helpers/server/relationships';

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) => {
                // Fetch related data here. For example:
                const propertyType = await getOneFrom(
                    ctx.db,
                    'property_type_options',
                    'id',
                    property.property_type_id
                );
                const propertyLocation = await getOneFrom(
                    ctx.db,
                    'property_locations',
                    'id',
                    property.property_location_id
                );

                // Return the property with its related data
                return {
                    ...property,
                    propertyType,
                    propertyLocation
                };
            })
        );
    }
});
Was this page helpful?