Skip to content

feat: add opt-in query logging - #79

Merged
Artmann merged 2 commits into
mainfrom
feat/query-logging-28
Jul 17, 2026
Merged

feat: add opt-in query logging#79
Artmann merged 2 commits into
mainfrom
feat/query-logging-28

Conversation

@Artmann

@Artmann Artmann commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Summary

Adds opt-in logging of every MongoDB operation esix runs — operation name, collection, arguments, duration, and error (if any). Two ways to enable it:

// Programmatic — plug in any logger
import { setQueryLogger } from 'esix'

setQueryLogger((entry) => {
  myLogger.debug(entry)
})
# Or via env var — built-in console.debug output:
# esix invoices.insertOne([{...}]) took 1.2ms
DB_LOG_QUERIES=true

How it works

  • All DB access already funnels through useCollection() in the QueryBuilder, so the collection is wrapped there in a logging Proxy — one choke point, no changes to any query method.
  • Promise-returning ops log when they settle; cursor ops (find, aggregate) log when toArray() settles, so duration covers the full operation and survives sort().skip().limit() chaining.
  • Errors are logged with the error field set and the original rejection propagates unchanged.
  • A throwing user logger can never affect a query: logger calls are guarded, so a faulty logger neither rejects a successful operation nor masks a real DB error (regression-tested).
  • Zero overhead when disabled: no Proxy is allocated and the raw collection is used.

Changes

  • packages/esix/src/query-logger.ts (new) — QueryLogEntry/QueryLogger types, setQueryLogger(), logger resolution, and the collection/cursor proxies.
  • packages/esix/src/query-builder.ts — wraps the collection in useCollection() only when a logger is active.
  • packages/esix/src/index.ts — exports setQueryLogger, QueryLogEntry, QueryLogger.
  • packages/esix/src/query-logger.spec.ts (new) — 13 tests: integration against the mock adapter (entries for create/find/save/aggregate, env-var console path, disabled-mode silence, custom-logger precedence) and unit tests for the proxy (error propagation, chained-cursor single log, property passthrough, throwing-logger guards).
  • packages/website/docs/configuration.mdDB_LOG_QUERIES env entry and a Query Logging section.

Testing

  • yarn lint, yarn typecheck, yarn test all green (248 tests, +13 new).
  • Reviewed by a fresh-context review pass before opening this PR; its one finding (an unguarded user logger could break queries) is fixed and regression-tested.

Closes #28

🤖 Generated with Claude Code

@cursor

cursor Bot commented Jul 17, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@Artmann
Artmann merged commit 7722735 into main Jul 17, 2026
13 checks passed
@Artmann
Artmann deleted the feat/query-logging-28 branch July 17, 2026 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for logging queries

1 participant