get-convex / get-convex/better-auth

findIndex in src/client/adapter-utils.ts never matches composite indexes

Open Beginner friendly
#296 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
764
Forks
125
PR merge metrics
No merged PRs in 30d

Description

**Bug: Underscore prefix on range/sort fields breaks composite index lookup**

Lines 190 and 195 prepend `_` to the range and sort field names when building the lookup array:

```js
// L190 (range)
`${indexEqFields.length ? "_" : ""}${boundField}`

// L195 (sort)
`${indexEqFields.length || boundField ? "_" : ""}${sortField}`
```

This produces `["organizationId", "_expiresAt"]`, which is then compared field-by-field against the index definition `["organizationId", "expiresAt"]` at L211:

```js
indexFields.every((field, idx) => field === fields[idx])
```

`"_expiresAt" !== "expiresAt"` — the index is never found, falling back to a full table scan.

**Why it wasn't caught:** Auto-generated Better Auth indexes are single-field (`indexEqFields.length === 0`), so no underscore is added. The bug only surfaces with custom composite indexes (2+ fields).

**Fix — remove the underscore prefixing (L190, L195):**

```diff
- .concat(boundField && boundField !== "createdAt"
- ? `${indexEqFields.length ? "_" : ""}${boundField}`
+ .concat(boundField && boundField !== "createdAt"
+ ? boundField
: "")
- .concat(sortField && sortField !== "createdAt" && boundField !== sortField
- ? `${indexEqFields.length || boundField ? "_" : ""}${sortField}`
+ .concat(sortField && sortField !== "createdAt" && boundField !== sortField
+ ? sortField
: "")
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open src/client/adapter-utils.ts and inspect the lookup-array construction around lines 190 and 195, then compare it with the field-by-field match at line 211. Remove the underscore prefixes from range and sort fields, and verify that a custom composite index is selected instead of falling back to a full table scan.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
databases
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.