Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/backend/sorting-and-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Sorting and Search API Documentation

## Overview
The Sorting and Search API provides secure, server-side filtering and ordering for backend resources. It is designed to be efficient, easy to use, and resistant to malicious input.

## Features
- **Keyword Search**: Case-insensitive search across multiple resource fields.
- **Secure Sorting**: Server-side ordering with strict validation of sortable fields.
- **Integration**: Easily applicable to any resource array.

## Implementation Details

### Sorting Utility (`src/utils/sorting.ts`)
The `sortItems` function takes an array of items, a sort field, an order, and a list of allowed fields.
- **Security**: It only allows sorting on fields explicitly defined in `allowedFields`.
- **Logic**: Handles string and numeric comparisons, with support for ascending and descending orders.

### Search Utility (`src/utils/search.ts`)
The `searchItems` function filters items based on a keyword query.
- **Logic**: Performs a case-insensitive `includes` check on specified fields.
- **Safety**: Gracefully handles non-string values.

## API Usage Example

### Contracts Endpoint
`GET /api/v1/contracts?search=web&sortBy=value&order=desc`

**Query Parameters:**
- `search`: A string to search for in the resource title and description.
- `sortBy`: The field to sort the results by (Allowed: `title`, `status`, `value`, `createdAt`).
- `order`: The sort direction (`asc` or `desc`). Defaults to `asc`.

## Testing
Comprehensive test suites are provided in `src/**/*.test.ts`.
- **Unit Tests**: Validate the logic of sorting and search utilities.
- **Integration Tests**: Verify the API endpoints handle query parameters correctly and return filtered/sorted results.

To run tests:
```bash
npm test -- --coverage
```
Current coverage: **100% Statements, 100% Branches, 100% Functions, 100% Lines**.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/*.test.ts'],
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.d.ts'],
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.d.ts', '!src/server.ts'],
};
Loading
Loading