[GH-ISSUE #2775] lastContactByCharacter throws "groupBy is not a function" on the file-storage backend #1413

Closed
opened 2026-10-21 05:33:24 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kolacheee on GitHub (Jun 22, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2775

Originally assigned to: @kolacheee on GitHub.

Summary

chats.lastContactByCharacter() (packages/server/src/services/storage/chats.storage.ts) builds a Drizzle query using .groupBy() plus a sql MAX() aggregate. The default file-storage backend's query-builder shim (packages/server/src/db/file-backed-store.ts) implements select / from / where / orderBy but not groupBy, so the call throws at runtime:

TypeError: db.select(...).from(...).where(...).groupBy is not a function
    at Object.lastContactByCharacter (.../dist/services/storage/chats.storage.js)
    at Object.<anonymous> (.../dist/routes/conversation.routes.js)

Impact

conversation.routes.ts calls this when the conversation view loads, so on the default file-storage backend that route 500s repeatedly. It works on the libsql backend (real Drizzle supports .groupBy()), which is why it can pass unnoticed in libsql setups.

Root cause

.groupBy() is used in exactly one place in the server (this function), and the file-backed store never implemented it. It's also the only call relying on a sql MAX() aggregate, which the file store doesn't evaluate.

Reproduce

  1. Run on the default (file-storage) backend.
  2. Open a chat / load the conversation view.
  3. Server logs the TypeError above (HTTP 500).

Suggested fix

Aggregate in JS instead of via SQL GROUP BY / MAX(): select the plain characterId + createdAt columns (both supported by the file store) and reduce to latest-per-character in code. Backend-agnostic; no schema/API change. PR to follow.

Branch: staging.

Originally created by @kolacheee on GitHub (Jun 22, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2775 Originally assigned to: @kolacheee on GitHub. ### Summary `chats.lastContactByCharacter()` (`packages/server/src/services/storage/chats.storage.ts`) builds a Drizzle query using `.groupBy()` plus a `sql` `MAX()` aggregate. The default **file-storage** backend's query-builder shim (`packages/server/src/db/file-backed-store.ts`) implements `select` / `from` / `where` / `orderBy` but **not** `groupBy`, so the call throws at runtime: ``` TypeError: db.select(...).from(...).where(...).groupBy is not a function at Object.lastContactByCharacter (.../dist/services/storage/chats.storage.js) at Object.<anonymous> (.../dist/routes/conversation.routes.js) ``` ### Impact `conversation.routes.ts` calls this when the conversation view loads, so on the **default file-storage backend** that route 500s repeatedly. It works on the libsql backend (real Drizzle supports `.groupBy()`), which is why it can pass unnoticed in libsql setups. ### Root cause `.groupBy()` is used in exactly one place in the server (this function), and the file-backed store never implemented it. It's also the only call relying on a `sql` `MAX()` aggregate, which the file store doesn't evaluate. ### Reproduce 1. Run on the default (file-storage) backend. 2. Open a chat / load the conversation view. 3. Server logs the `TypeError` above (HTTP 500). ### Suggested fix Aggregate in JS instead of via SQL `GROUP BY` / `MAX()`: select the plain `characterId` + `createdAt` columns (both supported by the file store) and reduce to latest-per-character in code. Backend-agnostic; no schema/API change. PR to follow. Branch: `staging`.
bsakaguchi 2026-10-21 05:33:24 +07:00
  • closed this issue
  • added the
    in-pr
    label
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1413
No description provided.