flightF
Convex Communityโ€ข2y agoโ€ข
76 replies
flight

Search & filter

I have the code below, but i seem to be getting an error on the args.name
Property 'withSearchIndex' does not exist on type
export const getParts = query({
  args: {
    locationId: v.id("locations"),
    factoryId: v.id("factories"),
    machineClassId: v.id("machineClasses"),
    name: v.string(),
  },
  async handler(ctx, args) {
    // Required filter by locationId
    let partsQuery = ctx.db
      .query("parts")
      .withIndex("by_locationId", (q) => q.eq("locationId", args.locationId));

    // Optional filter by machineClassId
    if (args.machineClassId) {
      partsQuery = partsQuery.filter((q) =>
        q.eq(q.field("machineClassId"), args.machineClassId)
      );
    }

    // Optional filter by factoryId
    if (args.factoryId) {
      partsQuery = partsQuery.filter((q) =>
        q.eq(q.field("factoryId"), args.factoryId)
      );
    }

    // Optional search by name
    if (args.name) {
      partsQuery = partsQuery
        .withSearchIndex("by_name", (q) => q.match("name", args.name), {
          searchField: "name",
        });
    }

    const parts = await partsQuery.collect();

    return parts;
  },
});
Was this page helpful?