Repository navigation
feat(core): asset data layer — schema, repository, service, GET /api/v1/assets - #6
Conversation
…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>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds 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. ChangesDB, service, API, and workflow updates
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
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 7
🧹 Nitpick comments (1)
.github/workflows/ci.yml (1)
26-26: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPin the Postgres image more tightly.
postgres:18-alpinewill 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
⛔ Files ignored due to path filters (1)
bun.lockis excluded by!**/*.lock
📒 Files selected for processing (25)
.github/workflows/ci.yml.gitignoreREADME.mdapps/api/src/env-config.tsapps/api/src/index.tsapps/api/src/server.tsapps/api/test/env-config.test.tsapps/api/test/server.test.tsdocker-compose.ymlpackage.jsonpackages/core/src/core.tspackages/core/src/index.tspackages/core/src/services/asset-service.tspackages/core/test/asset-service.test.tspackages/db/drizzle.config.tspackages/db/drizzle/0000_slippery_rafael_vega.sqlpackages/db/drizzle/meta/0000_snapshot.jsonpackages/db/drizzle/meta/_journal.jsonpackages/db/package.jsonpackages/db/src/client.tspackages/db/src/index.tspackages/db/src/migrate.tspackages/db/src/repositories/asset-repository.tspackages/db/src/schema.tspackages/db/test/asset-repository.test.ts
…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>
1a841e7 to
20c34c2
Compare
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
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
📒 Files selected for processing (12)
.github/workflows/ci.ymlapps/api/src/server.tsapps/api/test/server.test.tsdocker-compose.ymlpackages/core/src/services/asset-service.tspackages/core/test/asset-service.test.tspackages/db/drizzle/0000_dry_deathstrike.sqlpackages/db/drizzle/meta/0000_snapshot.jsonpackages/db/drizzle/meta/_journal.jsonpackages/db/src/index.tspackages/db/src/schema.tspackages/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
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>
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
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
📒 Files selected for processing (7)
apps/api/test/server.test.tspackages/core/test/asset-service.test.tspackages/db/drizzle/0000_small_maximus.sqlpackages/db/drizzle/meta/0000_snapshot.jsonpackages/db/drizzle/meta/_journal.jsonpackages/db/src/schema.tspackages/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>
|
@coderabbitai review |
✅ Action performedReview finished.
|
Data layer — first vertical slice
Proves Repository → Service → Route end-to-end with Postgres (Drizzle), one live endpoint.
@bunbooru/dbassets,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 theStorageProviderprecedent; the only layer that runs SQL).bun run migrateCLI.@bunbooru/coreAssetService— pagination clamping, no SQL.createCore({databaseUrl})/assembleCore(db)wire db → repository → service so apps depend only on@bunbooru/core, never on@bunbooru/db.@bunbooru/apicreateApp(deps)factory replaces the module-levelappsingleton — a DI seam so tests inject a stubCoreand need no live Postgres.GET /api/v1/assetswith integer-only, range-checkedpage/per_pageand an ISO-timestampAssetDto, so the Eden Treaty client infers exact wire types.CI / tooling
DATABASE_URLis unset (localbun teststays green; CI runs them).docker compose: bind-mount service data under./data(git-ignored) + fix the Postgres 18 data-dir mount path.Validation
typecheck0 errors · 64 tests pass (against real Postgres) · boundaries 0 violations.DATABASE_URL,$countinstead ofcount(*)::int, migration error handling, tighter test assertions).🤖 Generated with Claude Code
Summary by CodeRabbit
GET /api/v1/assetswith pagination and input validation.