typeorm-dynamic-search
v1.2.5
Published
Dynamic search service for NestJS and TypeORM
Maintainers
Readme
🚀 NestJS Dynamic Search
What is it?
nestjs-dynamic-search is a flexible and powerful search service package developed for NestJS and TypeORM. It allows you to dynamically create, filter, sort, and paginate your database queries.
✨ Features
- 🔍 Dynamic and flexible filtering
- 📄 Pagination support
- 🔗 Multiple relation management
- 🔢 Array and Enum type searching
- 🌳 Tree structure querying
- 🔄 Customizable sorting
- 🚀 High performance
- 💡 Easy to use
📦 Installation
npm install nestjs-dynamic-search
# or
yarn add nestjs-dynamic-search🛠️ Usage Examples
Basic Usage
import { BaseService } from 'nestjs-dynamic-search';
import { WhereType, OrderType } from 'nestjs-dynamic-search';
@Injectable()
export class UserService extends BaseService<User> {
constructor(
@InjectRepository(User)
private userRepository: Repository<User>
) {
super(userRepository);
}
async findUsers() {
const options = {
query: {
name: 'John', // Users with name John
age: [25, 30] // Users who are 25 or 30 years old
},
relations: ['role'], // Also include role information
whereType: WhereType.AND,
partialMatch: true,
page: 1,
take: 10,
sort: 'name', // Sort by name
order: OrderType.ASC // Ascending order
};
return this.searchPagination(options);
}
}Advanced Filtering
const complexOptions = {
query: {
name: 'John', // Names containing John
status: 'active', // Active status
age: [25, 30], // 25 or 30 years old
role: 1 // Specific role
},
relations: ['role', 'permissions'],
whereType: WhereType.AND,
order: OrderType.DESC,
sort: 'updatedAt', // Sort by update date
partialMatch: true
};
const results = await userService.search(complexOptions);Custom Sorting Usage
// Sort users by age in ascending order
const sortByAgeOptions = {
query: { status: 'active' },
sort: 'age', // Sort by age
order: OrderType.ASC, // Ascending order
take: 20
};
const youngToOldUsers = await userService.search(sortByAgeOptions);
// Sort users by creation date in descending order
const sortByCreatedAtOptions = {
query: { status: 'active' },
sort: 'createdAt', // Sort by creation date
order: OrderType.DESC, // Descending order (newest to oldest)
take: 20
};
const newestUsers = await userService.search(sortByCreatedAtOptions);🔧 Parameters
SearchOptions
| Parameter | Type | Description | Default | |-----------|------|-------------|---------| | relations | string[] | Joins related tables | - | | whereType | WhereType | Query conditions connection (AND/OR) | AND | | order | OrderType | Sort direction (ASC/DESC) | DESC | | sort | string | Field name to sort by | createdAt | | query | object | Search criteria | - | | single | boolean | Returns a single result | false | | withDeleted | boolean | Includes deleted records | false | | take | number | Number of records to retrieve | - | | partialMatch | boolean | Partial text matching | false | | tree | string | Retrieves related data in tree structure | - |
🌈 Supported Features
- Dynamic relationship management
- Flexible AND/OR filtering
- Partial text search
- Array and Enum type searching
- Customizable field sorting
- Pagination
- Include deleted records
⚠️ Important Considerations
- Use relationship names in the relations parameter
- Choose column names carefully in the query object
- Avoid unnecessary relationships for performance
- Ensure a valid column name is used in the sort parameter
🔒 License
MIT
🤝 Contributing
Bug reports and pull requests are always welcome. If you'd like to contribute, please open an issue first.
📞 Contact
For any issues or suggestions, please use GitHub issues.
Note: This package is designed to work with TypeORM and NestJS. Make sure you have completed the basic installation and configuration of these libraries before using it.
