Coffee11C
Convex Community2y ago
7 replies
Coffee11

Dynamic query

Is this how we create dynamic query in convex?

  let baseQuery = ctx.db
    .query("property_listings")
    .filter((q) => q.eq(q.field("listingType"), args.listingTypeId))

  if (args?.minPrice !== undefined && args.minPrice >= 0) {
    baseQuery = baseQuery.filter((q) =>
      q.gte(q.field("price"), Number(args.minPrice))
    );
  }

  if (args?.maxPrice !== undefined && args.maxPrice >= 0) {
    baseQuery = baseQuery.filter((q) =>
      q.lte(q.field("price"), Number(args.maxPrice))
    );
  }

  const results = await baseQuery.order("desc").paginate(args.paginationOpts);

  const updatedPage = await Promise.all(
    results.page.map(async (propertyListing) => ({
      ...propertyListing,
    }))
  );


it's working as expected but getting a typescript warning.

  const { isLoading, results, loadMore } = usePaginatedQuery(
    api.propertyListings.getPropertyListings, // <- on this part
    {
      listingTypeId: filters.listingTypeId as Id<"listing_types">,
      properTypeId: filters.propertyTypeId as Id<"property_types">,
      minPrice: filters.minPrice,
      maxPrice: filters.maxPrice,
    },
    {
      initialNumItems: 10,
    }
  );


Argument of type 'FunctionReference<"query", "public", { minPrice?: number | undefined; maxPrice?: number | undefined; listingTypeId: Id<"listing_types">; properTypeId: Id<"property_types">; paginationOpts: { ...; }; }, { ...; }>' is not assignable to parameter of type 'PaginatedQueryReference'.
  Types of property '_returnType' are incompatible.
    Type '{ page: { listingType: string | undefined; propertyType: string | undefined; favorite: boolean; _id: Id<"property_listings">; _creationTime: number; pricePerSqm?: number | undefined; ... 22 more ...; description: string; }[]; ... 10 more ...; [Symbol.asyncIterator](): AsyncIterator<...>; }' is missing the following properties from type 'PaginationResult<any>': isDone, continueCursorts(2345)
Was this page helpful?