Skip to content

feat(core): asset data layer — schema, repository, service, GET /api/v1/assets - #6

Merged
deckyfx merged 4 commits into
mainfrom
feat/core-domain
Jun 28, 2026
Merged

deckyfx merged 4 commits into
mainfrom
feat/core-domain

Conversation

@deckyfx

@deckyfx deckyfx commented Jun 28, 2026 •

Copy link
Copy Markdown
Owner

Data layer — first vertical slice

Proves Repository → Service → Route end-to-end with Postgres (Drizzle), one live endpoint.

@bunbooru/db

  • Core schema: assets, tags, asset_tags, users (enums, FKs, tag index). Inferred row types are canonical.
  • createDb(url) connection factory over Bun's native SQL driver — injected, not a singleton (no env coupling).
  • AssetRepository = interface + factory (matches the StorageProvider precedent; the only layer that runs SQL).
  • drizzle-kit config, generated migration, bun run migrate CLI.

@bunbooru/core

  • AssetService — pagination clamping, no SQL.
  • createCore({databaseUrl}) / assembleCore(db) wire db → repository → service so apps depend only on @bunbooru/core, never on @bunbooru/db.

@bunbooru/api

  • createApp(deps) factory replaces the module-level app singleton — a DI seam so tests inject a stub Core and need no live Postgres.
  • GET /api/v1/assets with integer-only, range-checked page/per_page and an ISO-timestamp AssetDto, so the Eden Treaty client infers exact wire types.

CI / tooling

  • Ephemeral Postgres service + migrate step. Repository integration tests skip when DATABASE_URL is unset (local bun test stays green; CI runs them).
  • docker compose: bind-mount service data under ./data (git-ignored) + fix the Postgres 18 data-dir mount path.

Validation

  • typecheck 0 errors · 64 tests pass (against real Postgres) · boundaries 0 violations.
  • Pre-reviewed locally with the CodeRabbit CLI; findings addressed (integer pagination, trimmed DATABASE_URL, $count instead of count(*)::int, migration error handling, tighter test assertions).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added GET /api/v1/assets with pagination and input validation.
    • Added explicit migration and database setup support for local development and CI.
  • Bug Fixes
    • Added stricter validation for missing/blank database connection settings.
    • Improved graceful shutdown behavior on termination signals.
  • Documentation
    • Updated getting-started instructions to include migrations, typechecking, and tests; clarified Redis setup.

…v1/assets

First vertical slice of the data layer (Postgres via Drizzle), proving
Repository → Service → Route end-to-end with one live endpoint.

db:
- Core schema (assets, tags, asset_tags, users) with enums, FKs and a tag index;
  inferred row types are the canonical shapes.
- createDb(url) connection factory over Bun's native SQL driver (injected, not a
  singleton — no env coupling).
- AssetRepository as interface + factory (matches the StorageProvider precedent;
  the only layer that executes SQL).
- drizzle-kit config, generated migration, and a `bun run migrate` CLI.

core:
- AssetService with pagination clamping (no SQL).
- createCore({databaseUrl}) / assembleCore(db) wire db → repository → service so
  apps depend only on @bunbooru/core, never on @bunbooru/db.

api:
- createApp(deps) factory (DI seam) replaces the module-level app singleton, so
  tests inject a stub Core and need no live Postgres.
- GET /api/v1/assets with coerced, range-checked page/per_page and an
  ISO-timestamp AssetDto, so the Eden Treaty client infers exact wire types.

ci / tooling:
- Ephemeral Postgres service + migrate step; repository integration tests skip
  when DATABASE_URL is unset.
- docker compose: bind-mount service data under ./data (git-ignored) and fix the
  Postgres 18 data-dir mount path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 28, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2e601217-c777-4d1b-9728-1b1902979f0f

📥 Commits

Reviewing files that changed from the base of the PR and between cc1318c and b12b772.

📒 Files selected for processing (1)
  • packages/db/test/asset-repository.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/db/test/asset-repository.test.ts

📝 Walkthrough

Walkthrough

Adds the Drizzle database schema, migration artifacts, repository, and asset pagination service, then wires the API to a dependency-injected core with a new assets endpoint and updates CI and local development commands for database setup.

Changes

DB, service, API, and workflow updates

Layer / File(s) Summary
Schema, migration, client, and repository
packages/db/src/schema.ts, packages/db/drizzle/0000_small_maximus.sql, packages/db/drizzle/meta/*, packages/db/drizzle.config.ts, packages/db/src/client.ts, packages/db/src/migrate.ts, packages/db/package.json, packages/db/src/index.ts, packages/db/src/repositories/asset-repository.ts
Defines PostgreSQL enums and tables, generated migration artifacts, Drizzle config, the Bun/Drizzle client, the migration runner, repository exports, and the asset repository implementation.
Asset pagination service and core wiring
packages/core/src/services/asset-service.ts, packages/core/src/core.ts, packages/core/src/index.ts, packages/core/test/asset-service.test.ts
Adds the asset pagination service, core composition root, entrypoint re-exports, and pagination tests for defaults, clamping, flooring, and empty results.
API factory, assets endpoint, and env access
apps/api/src/server.ts, apps/api/src/index.ts, apps/api/src/env-config.ts, apps/api/test/server.test.ts, apps/api/test/env-config.test.ts
Refactors the API into a createApp factory, adds asset serialization and GET /api/v1/assets, exposes DATABASE_URL, and updates API startup and tests.
CI and local development workflow
.github/workflows/ci.yml, docker-compose.yml, .gitignore, package.json, README.md
Updates CI to run migrations against PostgreSQL, switches compose persistence to bind mounts, ignores local data, adds database scripts/deps, and updates setup instructions.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant createApp as createApp
  participant AssetService
  participant AssetRepository
  participant Postgres

  Client->>createApp: GET /api/v1/assets?page=2&per_page=10
  createApp->>createApp: validate query params
  createApp->>AssetService: core.assetService.list({page:2, perPage:10})
  AssetService->>AssetRepository: findMany({limit:10, offset:10})
  AssetService->>AssetRepository: count()
  AssetRepository->>Postgres: query assets
  AssetRepository->>Postgres: count assets
  Postgres-->>AssetRepository: rows and total
  AssetService-->>createApp: AssetListPage
  createApp-->>Client: serialized assets and pagination
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • deckyfx/bunbooru#1: Shares the local PostgreSQL and compose-based development setup that this PR extends with migration and database workflow changes.
  • deckyfx/bunbooru#2: Shares the API server wiring patterns around request handling and graceful shutdown that this PR preserves while refactoring the server into createApp.

Poem

🐇 I hopped through tables, square and neat,
and found the assets list complete.
Migrations ran, the tests all sang,
with createApp and createCore they rang.
PostgreSQL purred, the burrow shone—
a tidy stack from root to stone.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change set: the asset data layer plus the new GET /api/v1/assets endpoint.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/core-domain

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 7

🧹 Nitpick comments (1)
.github/workflows/ci.yml (1)

26-26: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Pin the Postgres image more tightly.

postgres:18-alpine will drift underneath CI over time. An exact patch tag or digest keeps migration/test behavior reproducible and avoids surprise breakage from upstream image changes.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/ci.yml at line 26, The CI workflow’s Postgres container
image is using a floating tag, which can change behavior over time. Update the
image reference in the workflow definition to a fully pinned Postgres version,
using a specific patch tag or digest, so the CI job stays reproducible and
stable.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/api/src/server.ts`:
- Around line 124-129: The `page` query in `server.ts` only enforces a minimum,
so extremely large values can overflow the `offset` calculation in
`AssetService.list()`. Add a reasonable upper bound to the `t.Numeric` schema
for `page`, and mirror the same validation in
`packages/core/src/services/asset-service.ts` inside `list()` before computing
`offset = (page - 1) * perPage`. Use the existing `MAX_PER_PAGE`-style guard
pattern so both HTTP and non-HTTP callers are protected consistently.

In `@packages/core/src/services/asset-service.ts`:
- Around line 46-48: The pagination logic in asset-service still computes offset
from an unbounded page value, which can overflow and produce rounded or infinite
offsets. Update the page handling in the asset-service pagination flow by
clamping or validating page to a safe maximum before calculating offset,
alongside the existing perPage cap, so the offset used by the repository stays
within safe integer bounds.

In `@packages/db/src/index.ts`:
- Around line 4-5: Update the package-boundary comment in the module that
exports createDb, createAssetRepository, and AssetRepository so it matches the
current ownership. The comment currently says repositories live in
`@bunbooru/core`, but this file now exposes repository APIs from `@bunbooru/db`;
revise the wording to describe the actual boundary enforced by the db package
and keep the description aligned with the exported symbols.

In `@packages/db/src/schema.ts`:
- Around line 57-59: Add database-level CHECK constraints to the schema so
impossible numeric values are rejected at the source. Update the table
definition in schema.ts where width, height, sizeBytes, and postCount are
declared, and enforce non-negative (or otherwise valid) bounds directly on those
columns instead of relying on callers. Use the existing schema/table builder
around the width/height/sizeBytes fields and the postCount definition to attach
the constraints.
- Around line 48-60: The assets table currently uses md5 as the unique dedupe
key, which should be replaced with a stronger approach for identifying uploads.
Update the schema in assets to stop relying on the md5 column for uniqueness and
move deduplication to a safer hash or another collision-resistant identifier,
then adjust any upload/dedupe logic that references assets.md5 so it uses the
new key consistently.
- Around line 39-45: The persisted 64-bit columns in the schema are currently
using a lossy numeric mode, which can corrupt large IDs and byte counts. Update
the relevant column definitions in the schema (including users.id and the
related assets/tags primary and foreign key fields, plus assets.sizeBytes) to
use a lossless representation such as mode: "bigint" so the shared contract
preserves full 64-bit values; use the existing schema field names to locate and
adjust each affected column consistently.

In `@packages/db/test/asset-repository.test.ts`:
- Around line 11-13: The integration test gating in AssetRepository uses
DATABASE_URL directly, so whitespace-only values still count as enabled and the
suite reaches beforeAll before failing. Trim DATABASE_URL before passing it to
describe.skipIf, matching the env handling used in env-config.ts, and keep the
check centered around the DATABASE_URL constant and the AssetRepository
(integration) describe block.

---

Nitpick comments:
In @.github/workflows/ci.yml:
- Line 26: The CI workflow’s Postgres container image is using a floating tag,
which can change behavior over time. Update the image reference in the workflow
definition to a fully pinned Postgres version, using a specific patch tag or
digest, so the CI job stays reproducible and stable.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4f479bbf-20ec-4550-93a7-844a85632d3b

📥 Commits

Reviewing files that changed from the base of the PR and between 9e71084 and 50fc047.

⛔ Files ignored due to path filters (1)
  • bun.lock is excluded by !**/*.lock
📒 Files selected for processing (25)
  • .github/workflows/ci.yml
  • .gitignore
  • README.md
  • apps/api/src/env-config.ts
  • apps/api/src/index.ts
  • apps/api/src/server.ts
  • apps/api/test/env-config.test.ts
  • apps/api/test/server.test.ts
  • docker-compose.yml
  • package.json
  • packages/core/src/core.ts
  • packages/core/src/index.ts
  • packages/core/src/services/asset-service.ts
  • packages/core/test/asset-service.test.ts
  • packages/db/drizzle.config.ts
  • packages/db/drizzle/0000_slippery_rafael_vega.sql
  • packages/db/drizzle/meta/0000_snapshot.json
  • packages/db/drizzle/meta/_journal.json
  • packages/db/package.json
  • packages/db/src/client.ts
  • packages/db/src/index.ts
  • packages/db/src/migrate.ts
  • packages/db/src/repositories/asset-repository.ts
  • packages/db/src/schema.ts
  • packages/db/test/asset-repository.test.ts

Comment thread apps/api/src/server.ts Resolved
Comment thread packages/db/src/index.ts Resolved
Comment thread packages/db/src/schema.ts Resolved
Comment thread packages/db/src/schema.ts Resolved
Comment thread packages/db/src/schema.ts Resolved
deckyfx added a commit that referenced this pull request Jun 28, 2026
…traints

- api/core: cap `page` so (page-1)*perPage can't exceed MAX_SAFE_INTEGER and
  turn the offset into a rounded/Infinity value (5xx); guard mirrored in the
  service for non-HTTP callers.
- db: `sha256` is the unique content key (collision-resistant dedupe +
  integrity); `md5` kept, non-unique, for booru-ecosystem compatibility.
  Add CHECK constraints rejecting negative width/height/size_bytes/post_count
  at the source-of-truth layer. Document the `bigint mode:"number"` 2^53
  headroom (values stay far below it at booru scale).
- fix the stale `@bunbooru/db` boundary comment (repositories live here, not in
  core); trim `DATABASE_URL` before the integration-test skip check.
- tests: thread `sha256` through fixtures; add duplicate-sha256 (unique) and
  negative-dimension (CHECK) integration tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…traints

- api/core: cap `page` so (page-1)*perPage can't exceed MAX_SAFE_INTEGER and
  turn the offset into a rounded/Infinity value (5xx); guard mirrored in the
  service for non-HTTP callers.
- db: `sha256` is the unique content key (collision-resistant dedupe +
  integrity); `md5` kept, non-unique, for booru-ecosystem compatibility.
  Add CHECK constraints rejecting negative width/height/size_bytes/post_count
  at the source-of-truth layer. Document the `bigint mode:"number"` 2^53
  headroom (values stay far below it at booru scale).
- fix the stale `@bunbooru/db` boundary comment (repositories live here, not in
  core); trim `DATABASE_URL` before the integration-test skip check.
- tests: thread `sha256` through fixtures; add duplicate-sha256 (unique) and
  negative-dimension (CHECK) integration tests.
- ci/compose: digest-pin the Postgres (18.4-alpine) and Redis (8-alpine) images
  so CI and local services are reproducible (no floating tags).

Deliberately kept `bigint mode:"number"` (not "bigint"): values stay far below
2^53 at booru scale and JS BigInt isn't JSON-serializable; documented in schema.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@deckyfx
deckyfx force-pushed the feat/core-domain branch from 1a841e7 to 20c34c2 Compare June 28, 2026 15:29
@deckyfx

deckyfx commented Jun 28, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/db/src/schema.ts`:
- Around line 74-75: Add digest validation to the assets schema in schema.ts so
sha256 and md5 cannot store malformed or mixed-case values; update the table
definition for the assets model to include CHECK constraints that enforce
lowercase hexadecimal digests. Then update the asset fixtures and any migration
snapshot tied to this schema so they use real canonical digest values that
satisfy the new checks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a120f581-d485-49fe-b6ea-808f60e1265d

📥 Commits

Reviewing files that changed from the base of the PR and between 50fc047 and 20c34c2.

📒 Files selected for processing (12)
  • .github/workflows/ci.yml
  • apps/api/src/server.ts
  • apps/api/test/server.test.ts
  • docker-compose.yml
  • packages/core/src/services/asset-service.ts
  • packages/core/test/asset-service.test.ts
  • packages/db/drizzle/0000_dry_deathstrike.sql
  • packages/db/drizzle/meta/0000_snapshot.json
  • packages/db/drizzle/meta/_journal.json
  • packages/db/src/index.ts
  • packages/db/src/schema.ts
  • packages/db/test/asset-repository.test.ts
✅ Files skipped from review due to trivial changes (2)
  • packages/db/drizzle/meta/_journal.json
  • packages/db/drizzle/meta/0000_snapshot.json
🚧 Files skipped from review as they are similar to previous changes (7)
  • packages/core/src/services/asset-service.ts
  • .github/workflows/ci.yml
  • docker-compose.yml
  • packages/db/src/index.ts
  • packages/core/test/asset-service.test.ts
  • apps/api/test/server.test.ts
  • apps/api/src/server.ts

Comment thread packages/db/src/schema.ts Resolved
CHECK constraints so assets.sha256/md5 must be lowercase hex of the correct
length (64/32) — a malformed or mixed-case digest can't alias or break dedupe
lookups (extends the source-of-truth invariant stance). Fixtures now use real
canonical digests (Bun CryptoHasher); ordering asserts via storageKey; added a
non-hex/uppercase rejection test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@deckyfx

deckyfx commented Jun 28, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/db/test/asset-repository.test.ts`:
- Around line 65-76: The asset repository tests currently only assert that
create() throws, which can pass for unrelated failures and does not verify the
schema contract. Update the expectations in asset-repository.test.ts around
repo.create so they match the specific DB constraint names for the unique,
dimension CHECK, and digest CHECK violations, using the same create() call sites
and seed()/hexDigest helpers to keep the tests tied to the intended invariants.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a2d3cf7a-988b-476d-b2e2-7a2901ae02e5

📥 Commits

Reviewing files that changed from the base of the PR and between 20c34c2 and cc1318c.

📒 Files selected for processing (7)
  • apps/api/test/server.test.ts
  • packages/core/test/asset-service.test.ts
  • packages/db/drizzle/0000_small_maximus.sql
  • packages/db/drizzle/meta/0000_snapshot.json
  • packages/db/drizzle/meta/_journal.json
  • packages/db/src/schema.ts
  • packages/db/test/asset-repository.test.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/db/drizzle/meta/_journal.json
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/core/test/asset-service.test.ts
  • apps/api/test/server.test.ts
  • packages/db/drizzle/meta/0000_snapshot.json
  • packages/db/src/schema.ts

The rejection tests now read error.cause.constraint (the named DB constraint
Bun's Postgres driver exposes) — assets_sha256_unique / _width_nonneg /
_sha256_hex / _md5_hex — so they stay tied to the schema contract and can't pass
on an unrelated error. CodeRabbit's suggested toThrow(/name/) can't match here:
Drizzle's wrapped message omits the constraint name (it's only on the cause).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@deckyfx

deckyfx commented Jun 28, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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