Skip to content

Conversation

Brendonovich
Copy link
Member

@Brendonovich Brendonovich commented Oct 10, 2025

Summary by CodeRabbit

  • Refactor
    • Standardized database access across backend services to a single pattern, enabling improved tracing and consistent resource handling.
    • Updated repository and service flows for folders, spaces, organizations, videos, and storage buckets with no change to returned data.
  • Chores
    • Migrated internal DB calls across multiple modules to the new access approach.
  • Notes
    • No user-facing behavior changes; reliability and observability improved.

Copy link
Contributor

coderabbitai bot commented Oct 10, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Database access was migrated from execute(...) to a new use(...) API across the backend. Database.use accepts a callback returning a Promise that may expose toSQL() for tracing; call sites were updated accordingly. Query shapes, error mappings, and public signatures remain unchanged.

Changes

Cohort / File(s) Summary of Changes
DB API update
packages/web-backend/src/Database.ts
Replaced execute with use(cb). use accepts a callback that returns Promise<T> optionally exposing toSQL() (typed via import type { Query }). Derives sql from `promise.toSQL?.().sql
Auth callsites
packages/web-backend/src/Auth.ts
Switched two db.execute(...) usages to db.use(...). Queries and control flow unchanged.
Folders feature (repo & flow)
packages/web-backend/src/Folders/FoldersRepo.ts, packages/web-backend/src/Folders/index.ts
Replaced db.execute(...) with db.use(...) across getById, create, update, delete_ and related folder operations (child fetch, relation updates, delete). Logic and queries unchanged.
Spaces feature (repo & flow)
packages/web-backend/src/Spaces/SpacesRepo.ts, packages/web-backend/src/Spaces/index.ts
Updated membershipForVideo, membership, getById, and getSpaceOrOrg call sites to db.use(...). Query shapes and result handling unchanged.
Videos feature (repo & flow)
packages/web-backend/src/Videos/VideosRepo.ts, packages/web-backend/src/Videos/index.ts
Switched getById, create, delete_, and getUploadProgress from db.execute(...) to db.use(...). No other behavioral changes.
Organisations repo
packages/web-backend/src/Organisations/OrganisationsRepo.ts
Replaced db.execute(...) with db.use(...) in membershipForVideo and membership. Queries unchanged.
S3 Buckets repo
packages/web-backend/src/S3Buckets/S3BucketsRepo.ts
Migrated getForVideo, getById, and getForUser to db.use(...). Same selects/joins and decoding preserved.
Frontend app folder queries
apps/web/lib/folder.ts
Replaced db.execute(...) with db.use(...) for getFolderById, getSharedSpacesForVideos, getVideosByFolderId, and getChildFolders. Query logic unchanged.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant Database as Database.use
  participant CB as Callback (cb)
  participant ORM as Drizzle Query
  participant Effect as Effect.tryPromise + withSpan

  Caller->>Database: use(cb)
  activate Database
  Database->>CB: invoke with db handle
  activate CB
  CB->>ORM: build/execute query
  note right of CB: cb returns Promise<T>\noptional toSQL() -> Query
  CB-->>Database: Promise<T> (& optional toSQL)
  deactivate CB

  Database->>Database: sql = promise.toSQL?.().sql || "Transaction"
  Database->>Effect: tryPromise(promise) with DatabaseError
  Effect-->>Caller: Effect<T> (traced span "Database.execute" with { sql })
  deactivate Database
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

I hop through rows with nimble paws,
Swapping execute for use because:
A traced small hop, SQL in tow,
My burrow's tidy, logs all glow.
Carrots counted, queries neat—🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly encapsulates the core change of replacing execute with use and highlights the addition of SQL query logging for tracing, directly reflecting the changes made across the database API in this pull request. It is concise, specific, and clearly conveys the primary intent of the updates without unnecessary detail.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between db72678 and 5dfbb8e.

📒 Files selected for processing (1)
  • apps/web/lib/folder.ts (5 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/web-backend/src/Folders/index.ts (1)

25-72: Implement transactional folder deletion
The current deleteFolder issues separate db.use calls for selecting children, updating videos/spaceVideos, and deleting folders, without a surrounding transaction. If any step fails, you’ll end up with partial deletes and stale references. Wrap the entire recursive deletion logic in a single transaction (e.g. via db.transaction inside one db.use callback) to guarantee atomicity.

🧹 Nitpick comments (2)
packages/web-backend/src/Folders/FoldersRepo.ts (1)

29-29: Consider renaming the callback parameter to avoid shadowing.

The callback parameter db shadows the outer Database service variable from line 19. While this may be intentional, it could reduce clarity. Consider using a distinct name like drizzle, conn, or client for the callback parameter to make the distinction explicit.

Example for line 29:

-				.use((db) =>
-					db
+				.use((drizzle) =>
+					drizzle

Also applies to: 44-44, 50-50, 66-66

packages/web-backend/src/Folders/index.ts (1)

31-31: Consider renaming the callback parameter to avoid shadowing.

Similar to the changes in FoldersRepo.ts, the callback parameter db shadows the outer Database service variable from line 21. Consider using a distinct name like drizzle, conn, or client for clarity.

Example for line 31:

-				const children = yield* db.use((db) =>
-					db
+				const children = yield* db.use((drizzle) =>
+					drizzle

Also applies to: 49-49, 61-61, 69-69, 121-121, 185-185

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d994632 and db72678.

📒 Files selected for processing (10)
  • packages/web-backend/src/Auth.ts (2 hunks)
  • packages/web-backend/src/Database.ts (1 hunks)
  • packages/web-backend/src/Folders/FoldersRepo.ts (3 hunks)
  • packages/web-backend/src/Folders/index.ts (5 hunks)
  • packages/web-backend/src/Organisations/OrganisationsRepo.ts (2 hunks)
  • packages/web-backend/src/S3Buckets/S3BucketsRepo.ts (3 hunks)
  • packages/web-backend/src/Spaces/SpacesRepo.ts (3 hunks)
  • packages/web-backend/src/Spaces/index.ts (2 hunks)
  • packages/web-backend/src/Videos/VideosRepo.ts (3 hunks)
  • packages/web-backend/src/Videos/index.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{ts,tsx,js,jsx,rs}

📄 CodeRabbit inference engine (CLAUDE.md)

Do not add inline, block, or docstring comments in any language; code must be self-explanatory

Files:

  • packages/web-backend/src/Spaces/SpacesRepo.ts
  • packages/web-backend/src/Videos/VideosRepo.ts
  • packages/web-backend/src/Videos/index.ts
  • packages/web-backend/src/S3Buckets/S3BucketsRepo.ts
  • packages/web-backend/src/Database.ts
  • packages/web-backend/src/Folders/index.ts
  • packages/web-backend/src/Spaces/index.ts
  • packages/web-backend/src/Folders/FoldersRepo.ts
  • packages/web-backend/src/Auth.ts
  • packages/web-backend/src/Organisations/OrganisationsRepo.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

Use strict TypeScript and avoid any; leverage shared types from packages

**/*.{ts,tsx}: Use a 2-space indent for TypeScript code.
Use Biome for formatting and linting TypeScript/JavaScript files by running pnpm format.

Files:

  • packages/web-backend/src/Spaces/SpacesRepo.ts
  • packages/web-backend/src/Videos/VideosRepo.ts
  • packages/web-backend/src/Videos/index.ts
  • packages/web-backend/src/S3Buckets/S3BucketsRepo.ts
  • packages/web-backend/src/Database.ts
  • packages/web-backend/src/Folders/index.ts
  • packages/web-backend/src/Spaces/index.ts
  • packages/web-backend/src/Folders/FoldersRepo.ts
  • packages/web-backend/src/Auth.ts
  • packages/web-backend/src/Organisations/OrganisationsRepo.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{ts,tsx,js,jsx}: Use kebab-case for filenames for TypeScript/JavaScript modules (e.g., user-menu.tsx).
Use PascalCase for React/Solid components.

Files:

  • packages/web-backend/src/Spaces/SpacesRepo.ts
  • packages/web-backend/src/Videos/VideosRepo.ts
  • packages/web-backend/src/Videos/index.ts
  • packages/web-backend/src/S3Buckets/S3BucketsRepo.ts
  • packages/web-backend/src/Database.ts
  • packages/web-backend/src/Folders/index.ts
  • packages/web-backend/src/Spaces/index.ts
  • packages/web-backend/src/Folders/FoldersRepo.ts
  • packages/web-backend/src/Auth.ts
  • packages/web-backend/src/Organisations/OrganisationsRepo.ts
🧬 Code graph analysis (8)
packages/web-backend/src/Spaces/SpacesRepo.ts (1)
packages/database/index.ts (1)
  • db (29-34)
packages/web-backend/src/Videos/index.ts (1)
packages/database/index.ts (1)
  • db (29-34)
packages/web-backend/src/S3Buckets/S3BucketsRepo.ts (1)
packages/database/index.ts (1)
  • db (29-34)
packages/web-backend/src/Database.ts (2)
packages/database/index.ts (1)
  • db (29-34)
packages/web-domain/src/Database.ts (1)
  • DatabaseError (3-6)
packages/web-backend/src/Folders/index.ts (1)
packages/database/index.ts (1)
  • db (29-34)
packages/web-backend/src/Spaces/index.ts (1)
packages/database/index.ts (1)
  • db (29-34)
packages/web-backend/src/Folders/FoldersRepo.ts (2)
packages/database/index.ts (1)
  • db (29-34)
packages/database/helpers.ts (1)
  • nanoId (6-9)
packages/web-backend/src/Organisations/OrganisationsRepo.ts (1)
packages/database/index.ts (1)
  • db (29-34)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build Desktop (x86_64-pc-windows-msvc, windows-latest)
  • GitHub Check: Build Desktop (aarch64-apple-darwin, macos-latest)
  • GitHub Check: Analyze (rust)
🔇 Additional comments (18)
packages/web-backend/src/Videos/index.ts (1)

111-121: LGTM!

The migration from execute to use is correct, and the query structure remains intact.

packages/web-backend/src/Spaces/index.ts (2)

22-33: LGTM!

The migration from execute to use is correct for the space query.


34-49: LGTM!

The migration from execute to use is correct for the organization query.

packages/web-backend/src/S3Buckets/S3BucketsRepo.ts (3)

17-23: LGTM!

The migration from execute to use is correct in getForVideo.


36-41: LGTM!

The migration from execute to use is correct in getById.


54-59: LGTM!

The migration from execute to use is correct in getForUser.

packages/web-backend/src/Database.ts (1)

9-19: Nice addition of SQL tracing!

The toSQL() extraction enables query logging while gracefully handling transactions (which lack toSQL()) by defaulting to "Transaction". The type signature correctly uses an intersection type to support both query builders and transactions.

packages/web-backend/src/Auth.ts (2)

22-27: LGTM!

The migration from execute to use is correct in getCurrentUser.


53-62: LGTM!

The migration from execute to use is correct in HttpAuthMiddlewareLive.

packages/web-backend/src/Organisations/OrganisationsRepo.ts (2)

16-33: LGTM!

The migration from execute to use is correct in membershipForVideo.


36-50: LGTM!

The migration from execute to use is correct in membership.

packages/web-backend/src/Videos/VideosRepo.ts (3)

26-28: LGTM!

The migration from execute to use is correct in getById.


48-57: LGTM!

The migration from execute to use is correct in delete_. The transaction handling is preserved.


63-98: LGTM!

The migration from execute to use is correct in create. The transaction handling is preserved.

packages/web-backend/src/Spaces/SpacesRepo.ts (3)

15-30: LGTM!

The migration from execute to use is correct in membershipForVideo.


37-51: LGTM!

The migration from execute to use is correct in membership.


55-58: LGTM!

The migration from execute to use is correct in getById.

packages/web-backend/src/Folders/FoldersRepo.ts (1)

29-80: Verify that Database.use maintains identical error propagation, transaction boundaries, and instrumentation as the removed execute API. Ensure callbacks run inside a transaction scope, errors map to DatabaseError, and spans still reflect the intended operation.

try: () => promise,
catch: (cause) => new DatabaseError({ cause }),
}),
}).pipe(Effect.withSpan("Database.execute", { attributes: { sql } }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Update the span name to match the new method name.

The span is still named "Database.execute" but the method has been renamed to use. This inconsistency could cause confusion when reviewing traces.

Apply this diff:

-				}).pipe(Effect.withSpan("Database.execute", { attributes: { sql } }));
+				}).pipe(Effect.withSpan("Database.use", { attributes: { sql } }));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
}).pipe(Effect.withSpan("Database.execute", { attributes: { sql } }));
}).pipe(Effect.withSpan("Database.use", { attributes: { sql } }));
🤖 Prompt for AI Agents
In packages/web-backend/src/Database.ts around line 18, the tracing span is
named "Database.execute" but the method was renamed to use; update the span name
to "Database.use" so traces match the current method name by changing the string
passed to Effect.withSpan from "Database.execute" to "Database.use".

@Brendonovich Brendonovich merged commit be66a20 into main Oct 10, 2025
13 of 15 checks passed
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.

1 participant