From 0184147852b92a62cf650b2150499a633927813d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orchestrator=20Zeta=20=E2=80=94=20VantageOS=20Team?= Date: Mon, 6 Apr 2026 09:32:29 +0000 Subject: [PATCH 1/2] fix: add compound [token, expiresAt] index on session table When using the multi-session plugin, queries on the session table filter by token eq + expiresAt range. Without a compound index, this falls back to a full table scan, triggering a "Querying without an index" warning. Fixes #305 --- src/client/create-schema.ts | 2 +- src/component/schema.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/create-schema.ts b/src/client/create-schema.ts index d8163a71..3503e382 100644 --- a/src/client/create-schema.ts +++ b/src/client/create-schema.ts @@ -5,7 +5,7 @@ import type { BetterAuthDBSchema, DBFieldAttribute } from "better-auth/db"; export const indexFields = { account: ["accountId", ["accountId", "providerId"], ["providerId", "userId"]], rateLimit: ["key"], - session: ["expiresAt", ["expiresAt", "userId"]], + session: ["expiresAt", ["expiresAt", "userId"], ["token", "expiresAt"]], verification: ["expiresAt", "identifier"], user: [["email", "name"], "name", "userId"], oauthConsent: [["clientId", "userId"]], diff --git a/src/component/schema.ts b/src/component/schema.ts index 82bd97ab..aee6a965 100644 --- a/src/component/schema.ts +++ b/src/component/schema.ts @@ -44,6 +44,7 @@ export const tables = { .index("expiresAt", ["expiresAt"]) .index("expiresAt_userId", ["expiresAt","userId"]) .index("token", ["token"]) + .index("token_expiresAt", ["token","expiresAt"]) .index("userId", ["userId"]), account: defineTable({ accountId: v.string(), From a881995d2dd6e9c353dad111e9ba708be0d736c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orchestrator=20Zeta=20=E2=80=94=20VantageOS=20Team?= Date: Mon, 6 Apr 2026 09:47:10 +0000 Subject: [PATCH 2/2] fix: preserve compound index field order in schema generator The .sort() on index arrays was reordering compound index fields alphabetically, breaking order-dependent queries. Sort only the copy used for the index name, keep original order for the definition. --- src/client/create-schema.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/create-schema.ts b/src/client/create-schema.ts index 3503e382..341854ce 100644 --- a/src/client/create-schema.ts +++ b/src/client/create-schema.ts @@ -129,8 +129,8 @@ export const tables = { mergedIndexFields(tables)[ tableKey as keyof typeof mergedIndexFields ]?.map((index) => { - const indexArray = Array.isArray(index) ? index.sort() : [index]; - const indexName = indexArray.join("_"); + const indexArray = Array.isArray(index) ? index : [index]; + const indexName = [...indexArray].sort().join("_"); return `.index("${indexName}", ${JSON.stringify(indexArray)})`; }) || [];