relation select query with where condition

I want to select all user's tasks where parent id is null.
Can someone help me fix this query please:
const result = await db.query.usersTasks.findMany({
    with: {
      tasks: {
        with: {
          subtasks: true,
        },
        where: isNull(tasks.parentId),
      },
    },
    where: eq(usersTasks.userId, id),
  });

these are the tables in question :
export const users = mysqlTable("users", {
  userId: varchar("userId", { length: 256 }).primaryKey().notNull().unique(),
  theme: mysqlEnum("theme", ["DARK", "LIGHT"]).default("DARK"),
  createdAt: timestamp("created_at").default(sql`CURRENT_TIMESTAMP`),
  updatedAt: timestamp("updated_at").default(sql`CURRENT_TIMESTAMP`),
});
export const usersTasks = mysqlTable(
  "users_tasks",
  {
    id: int("id").primaryKey().autoincrement(),
    userId: varchar("user_id", { length: 256 }).references(() => users.userId),
    taskId: int("tas_id").references(() => tasks.id),
    createdAt: timestamp("created_at").default(sql`CURRENT_TIMESTAMP`),
    updatedAt: timestamp("updated_at").default(sql`CURRENT_TIMESTAMP`),
  }
);
export const tasks = mysqlTable(
  "tasks",
  {
    id: int("id").primaryKey().autoincrement(),
    parentId: int("parent_id").references((): AnyMySqlColumn => tasks.id),
    title: varchar("title", { length: 256 }).notNull(),
    description: varchar("description", { length: 2000 }),
    dueDate: datetime("due_date"),
    createdAt: timestamp("created_at").default(sql`CURRENT_TIMESTAMP`),
    updatedAt: timestamp("updated_at").default(sql`CURRENT_TIMESTAMP`),
  },
  (table) => ({
    parentIdx: index("parent_idx").on(table.parentId)
  })
);

export const usersRelations = relations(users, ({ many }) => ({
  userTasks: many(usersTasks)
}));
export const tasksRelations = relations(tasks, ({ many, one }) => ({
  userTasks: many(usersTasks),
  subtasks: many(tasks, { relationName: "subtasks" }),
  parent: one(tasks)
}));
Was this page helpful?