Skip to content

feat: add snippet search API#80

Merged
SudiptaPaul-31 merged 1 commit into
SudiptaPaul-31:mainfrom
isaacCodes1:fix/issue-69-snippet-search-api
May 29, 2026
Merged

feat: add snippet search API#80
SudiptaPaul-31 merged 1 commit into
SudiptaPaul-31:mainfrom
isaacCodes1:fix/issue-69-snippet-search-api

Conversation

@isaacCodes1
Copy link
Copy Markdown

@isaacCodes1 isaacCodes1 commented May 26, 2026

Overview

This PR adds a snippet search API to the backend so the frontend can search snippets by title, language, tags, and free-form keywords. It keeps the response format consistent with the existing snippets listing API, adds pagination and filtering support, and introduces PostgreSQL indexes to keep search queries fast and scalable.

Related Issue

Closes #69

Changes

⚙️ Snippet Search API

  • [MODIFY] app/api/snippets/route.ts

  • Extended GET /api/snippets to support title, language, tags, keyword, limit, and offset query parameters.

  • Fixed the existing pagination parsing bug in the snippets route.

  • Standardized the response to always include paginated data plus a filters object for frontend integration.

  • [MODIFY] app/api/snippets/snippet.service.ts

  • Added a dedicated searchSnippets() service method.

  • Preserved the existing service/repository separation so search behavior stays testable and maintainable.

  • [MODIFY] app/api/snippets/snippet.repository.ts

  • Added filtered snippet search by:

  • Title

  • Language

  • Tags

  • Keywords

  • Implemented pagination and hasMore metadata in the search flow.

  • Added weighted PostgreSQL full-text keyword search across title, description, code, language, and tags.

  • Used JSONB containment for tag filtering and case-insensitive language matching.

🗄️ Database Indexing

  • [MODIFY] scripts/init-db.sql

  • Added pg_trgm extension setup.

  • Added indexes for lowercase language filtering, trigram title search, JSONB tags lookup, and a GIN full-text search vector.

  • [ADD] scripts/add-snippet-search-indexes.sql

  • Added a standalone SQL script to apply the new snippet search indexes on existing databases.

🧪 Tests

  • [MODIFY] lib/snippet.service.test.ts
  • Added service coverage for successful snippet search.
  • Added failure-path coverage for search errors.

Verification Results

Acceptance Criteria Status
Search by title
Search by language
Search by tags
Search by keywords
Support for pagination and filtering
Consistent response format for frontend integration
PostgreSQL indexing added for search scalability
Snippet service tests pass

How to Test

# 1. Confirm you're on the branch
git branch --show-current

# 2. Run the snippet service tests
npm test -- --runInBand lib/snippet.service.test.ts

# 3. Optional: inspect the changed files
git diff -- app/api/snippets/route.ts app/api/snippets/snippet.service.ts app/api/snippets/snippet.repository.ts lib/snippet.service.test.ts scripts/init-db.sql scripts/add-snippet-search-indexes.sql

# 4. Optional: apply the new indexes to an existing database
psql "$DATABASE_URL" -f scripts/add-snippet-search-indexes.sql

# 5. Optional: exercise the search API locally
curl "http://localhost:3000/api/snippets?title=react&language=typescript&tags=frontend,hooks&keyword=state&limit=10&offset=0"

Screenshots

✅ Snippet service tests pass

A screenshot of:

npm test -- --runInBand lib/snippet.service.test.ts
image

[npm test -- --runInBand lib/snippet.service.test.ts]

@vercel
Copy link
Copy Markdown

vercel Bot commented May 26, 2026

@David-patrick-chuks is attempting to deploy a commit to the Sudipta 's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 26, 2026

@isaacCodes1 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@SudiptaPaul-31 SudiptaPaul-31 merged commit be6f11a into SudiptaPaul-31:main May 29, 2026
0 of 2 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.

Snippet Search API

3 participants