Convex CommunityCCConvex Community
Powered by
pwuexecP
Convex Community•15mo ago•
13 replies
pwuexec

Dynamic query builder in convex

I'm trying to do a dynamic query based on conditions

import { v } from "convex/values";
import { BookingStatus } from "./lib/bookings";
import { query } from "./_generated/server";

export const getBookings = query({
  args: {
    studentId: v.optional(v.id("users")),
    tutorId: v.optional(v.id("users")),
    status: v.optional(
      v.union(
        v.literal(BookingStatus.AWAITING_TUTOR_CONFIRMATION),
        v.literal(BookingStatus.AWAITING_STUDENT_CONFIRMATION),
        v.literal(BookingStatus.AWAITING_PAYMENT),
        v.literal(BookingStatus.PAYMENT_FAILED),
        v.literal(BookingStatus.SCHEDULED),
        v.literal(BookingStatus.CANCELED),
        v.literal(BookingStatus.COMPLETED)
      )
    ),
    startDate: v.optional(v.number()), // Unix timestamp in milliseconds
    endDate: v.optional(v.number()), // Unix timestamp in milliseconds
  },
  handler: async (ctx, args) => {
    let query = ctx.db.query("bookings");

    if (args.studentId) {
      query = query.withIndex("by_student", (q) =>
        q.eq("student", args.studentId!)
      );
    }

    if (args.tutorId) {
      query = query.withIndex("by_tutor", (q) => q.eq("tutor", args.tutorId));
    }

    if (args.status) {
      query = query.withIndex("by_status", (q) => q.eq("status", args.status));
    }

    if (args.startDate !== undefined) {
      query = query.filter((q) => q.gte(q.field("startTime"), args.startDate));
    }

    if (args.endDate !== undefined) {
      query = query.filter((q) => q.lte(q.field("startTime"), args.endDate));
    }

    return await query.collect();
  },
});
import { v } from "convex/values";
import { BookingStatus } from "./lib/bookings";
import { query } from "./_generated/server";

export const getBookings = query({
  args: {
    studentId: v.optional(v.id("users")),
    tutorId: v.optional(v.id("users")),
    status: v.optional(
      v.union(
        v.literal(BookingStatus.AWAITING_TUTOR_CONFIRMATION),
        v.literal(BookingStatus.AWAITING_STUDENT_CONFIRMATION),
        v.literal(BookingStatus.AWAITING_PAYMENT),
        v.literal(BookingStatus.PAYMENT_FAILED),
        v.literal(BookingStatus.SCHEDULED),
        v.literal(BookingStatus.CANCELED),
        v.literal(BookingStatus.COMPLETED)
      )
    ),
    startDate: v.optional(v.number()), // Unix timestamp in milliseconds
    endDate: v.optional(v.number()), // Unix timestamp in milliseconds
  },
  handler: async (ctx, args) => {
    let query = ctx.db.query("bookings");

    if (args.studentId) {
      query = query.withIndex("by_student", (q) =>
        q.eq("student", args.studentId!)
      );
    }

    if (args.tutorId) {
      query = query.withIndex("by_tutor", (q) => q.eq("tutor", args.tutorId));
    }

    if (args.status) {
      query = query.withIndex("by_status", (q) => q.eq("status", args.status));
    }

    if (args.startDate !== undefined) {
      query = query.filter((q) => q.gte(q.field("startTime"), args.startDate));
    }

    if (args.endDate !== undefined) {
      query = query.filter((q) => q.lte(q.field("startTime"), args.endDate));
    }

    return await query.collect();
  },
});


schema.ts

  bookings: defineTable({
    student: v.id("users"), // Reference to the student (user with role STUDENT)
    tutor: v.id("users"), // Reference to the tutor (user with role TUTOR)
    service: v.id("services"), // Reference to the service being booked
    type: v.union(
      v.literal(BookingType.FREE_MEETING),
      v.literal(BookingType.LESSON)
    ), // Booking type
    status: v.union(
      v.literal(BookingStatus.AWAITING_TUTOR_CONFIRMATION),
      v.literal(BookingStatus.AWAITING_STUDENT_CONFIRMATION),
      v.literal(BookingStatus.AWAITING_PAYMENT),
      v.literal(BookingStatus.PAYMENT_FAILED),
      v.literal(BookingStatus.SCHEDULED),
      v.literal(BookingStatus.CANCELED),
      v.literal(BookingStatus.COMPLETED)
    ), // Booking status
    startTime: v.number(), // Start time of the booking (timestamp)
    endTime: v.number(), // End time of the booking (timestamp)
    createdAt: v.number(), // Creation time of the booking (timestamp)
    updatedAt: v.number(), // Last updated time of the booking (timestamp)
  })
    .index("by_student", ["student", "startTime"])
    .index("by_tutor", ["tutor", "startTime"])
    .index("by_status", ["status"]),
  bookings: defineTable({
    student: v.id("users"), // Reference to the student (user with role STUDENT)
    tutor: v.id("users"), // Reference to the tutor (user with role TUTOR)
    service: v.id("services"), // Reference to the service being booked
    type: v.union(
      v.literal(BookingType.FREE_MEETING),
      v.literal(BookingType.LESSON)
    ), // Booking type
    status: v.union(
      v.literal(BookingStatus.AWAITING_TUTOR_CONFIRMATION),
      v.literal(BookingStatus.AWAITING_STUDENT_CONFIRMATION),
      v.literal(BookingStatus.AWAITING_PAYMENT),
      v.literal(BookingStatus.PAYMENT_FAILED),
      v.literal(BookingStatus.SCHEDULED),
      v.literal(BookingStatus.CANCELED),
      v.literal(BookingStatus.COMPLETED)
    ), // Booking status
    startTime: v.number(), // Start time of the booking (timestamp)
    endTime: v.number(), // End time of the booking (timestamp)
    createdAt: v.number(), // Creation time of the booking (timestamp)
    updatedAt: v.number(), // Last updated time of the booking (timestamp)
  })
    .index("by_student", ["student", "startTime"])
    .index("by_tutor", ["tutor", "startTime"])
    .index("by_status", ["status"]),

I get a huge error, sorry for the spam

Type 'Query<{ document: { _id: Id<"bookings">; _creationTime: number; type: BookingType; tutor: Id<"users">; student: Id<"users">; service: Id<"services">; status: BookingStatus; startTime: number; endTime: number; createdAt: number; updatedAt: number; }; fieldPaths: ExtractFieldPaths<...> | "_id"; indexes: { ...; }; sear...' is missing the following properties from type 'QueryInitializer<{ document: { _id: Id<"bookings">; _creationTime: number; type: BookingType; tutor: Id<"users">; student: Id<"users">; service: Id<"services">; status: BookingStatus; startTime: number; endTime: number; createdAt: number; updatedAt: number; }; fieldPaths: ExtractFieldPaths<...> | "_id"; indexes: { ....': fullTableScan, withIndex, withSearchIndexts(2739)
Type 'Query<{ document: { _id: Id<"bookings">; _creationTime: number; type: BookingType; tutor: Id<"users">; student: Id<"users">; service: Id<"services">; status: BookingStatus; startTime: number; endTime: number; createdAt: number; updatedAt: number; }; fieldPaths: ExtractFieldPaths<...> | "_id"; indexes: { ...; }; sear...' is missing the following properties from type 'QueryInitializer<{ document: { _id: Id<"bookings">; _creationTime: number; type: BookingType; tutor: Id<"users">; student: Id<"users">; service: Id<"services">; status: BookingStatus; startTime: number; endTime: number; createdAt: number; updatedAt: number; }; fieldPaths: ExtractFieldPaths<...> | "_id"; indexes: { ....': fullTableScan, withIndex, withSearchIndexts(2739)


and all the args need to be marked with
!
!
Convex Community banner
Convex CommunityJoin
Join the Convex Discord! Explore Convex, the backend platform that keeps your app in sync.
15,010Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Recent Announcements
james

Hi @everyone with everything going on today I forgot to post on discord about our EU launch! We have a region in Dublin now! You can start using it immediately, even for folks on the free plan. https://news.convex.dev/we-finally-got-our-eu-visa/ Stay tuned for more regions but also stay tuned for more infra improvements where we improve latency for teams no matter where their servers are located. Just at the tip of the iceberg re all the optimizations we have lined up.

james · this hour

Wayne

Hi @everyone happy Monday. Components Authoring [Challenge](https://www.convex.dev/components/challenge) updates! Meet the second batch of community-approved components **Firecrawl Scrape **- Scrape any URL and get clean markdown, HTML, screenshots, or structured JSON - with durable caching and reactive queries. https://www.convex.dev/components/firecrawl-scrape Built by: Gitmaxd **Durable Agents **- A Convex component for building durable AI agents with an async tool loop. https://www.convex.dev/components/durable-agents Built by: Siegfried **Convex Debouncer** - A server-side debouncing component for debouncing expensive operations like LLM calls, metrics computation, or any heavy processing that should only run after a period of inactivity. https://www.convex.dev/components/debouncer Built by: Ilya **DatabaseChat **- A Convex component for adding natural language database queries to your app. https://www.convex.dev/components/database-chat Built by: Nick **Transloadit** - A Convex component for creating Transloadit Assemblies, handling resumable uploads with status, and persisting status/results in Convex. https://www.convex.dev/components/transloadit Built by: Kvz **Loops** - A Convex component for integrating with Loops.so email marketing platform. https://www.convex.dev/components/loops Built by: Bobby The [challenge](https://www.convex.dev/components/challenge) is now ongoing, so keep building, and we'll keep rewarding. Thanks, everyone!

Wayne · 2w ago

Liz C

Hi everyone! Have you ever wanted to get your hands on some convex swag? Well you're in luck! We just launched our Convex swag store. Check it out here ---> https://store.convex.dev/

Liz C · 3w ago

Similar Threads

convexAction in @convex-dev/react-query ?
anmot.Aanmot. / support-community
2y ago
Dynamic funtion in convex
oznfcOoznfc / support-community
2y ago
Dynamic query
Coffee11CCoffee11 / support-community
2y ago
Idiomatic "in" query in Convex
isaac_wayIisaac_way / support-community
2mo ago