From 04e1bbffa0fcff851185e0ffa2df6749ee90a087 Mon Sep 17 00:00:00 2001 From: Zijian Zhang Date: Sat, 2 Nov 2024 22:48:52 +0800 Subject: [PATCH] feat: support must query condition --- CHANGELOG.md | 121 --------------------------------------- apps/api/redis-client.ts | 32 ++++++++++- 2 files changed, 29 insertions(+), 124 deletions(-) delete mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 511a99a..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,121 +0,0 @@ -# Changelog - -All notable changes to this project will be documented in this file. - -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - -## [0.11.0] - 2024-10-02 - -### Added - -- Support for broader index for Endstone plugins. - -## [0.10.0] - 2024-09-23 - -### Changed - -- Rewrite everything and upgrade to API v2. - -## [0.9.0] - 2024-03-03 - -### Added - -- `tag:` search pattern. - -## [0.8.2] - 2024-02-23 - -### Fixed - -- Crash when duplicated latest version found. - -## [0.8.1] - 2024-02-17 - -### Fixed - -- Incorret tag pattern parsing. - -## [0.8.0] - 2024-02-03 - -### Changed - -- Remove `source` field from `tooth.json`. - -## [0.7.0] - 2024-01-19 - -### Changed - -- Rewrite all code in TypeScript. -- Change Web APIs. - -## [0.6.0] - 2023-12-30 - -### Added - -- Source field support for `tooth.json`. - -## [0.5.0] - 2023-12-28 - -### Added - -- Content returned by `/teeth` API now contains tooth information. - -### Fixed - -- Clearing stale data before updating. - -### Security - -- Upgrade vulnerable dependency `Octokit`. - -## [0.4.0] - 2023-12-25 - -### Changed - -- Only query repositories with `format_version` 2 for `tooth.json`. - -## [0.3.0] - 2023-10-04 - -### Changed - -- Change API definitions. - -### Fixed - -- Incorrect definition of stable version. - -## [0.2.0] - 2023-09-09 - -### Changed - -- Change the exposed port from `11400` to `80`. -- Optimize data model. -- Change APIs. - -## [0.1.1] - 2023-09-08 - -### Fixed - -- Problems caused by URI encoding. -- Wrong debug messages. - -## [0.1.0] - 2023-09-07 - -### Added - -- Basic functionality. - -[0.11.0]: https://github.com/futrime/bedrinth-api/compare/v0.10.0...v0.11.0 -[0.10.0]: https://github.com/futrime/bedrinth-api/compare/v0.9.0...v0.10.0 -[0.9.0]: https://github.com/futrime/bedrinth-api/compare/v0.8.2...v0.9.0 -[0.8.2]: https://github.com/futrime/bedrinth-api/compare/v0.8.1...v0.8.2 -[0.8.1]: https://github.com/futrime/bedrinth-api/compare/v0.8.0...v0.8.1 -[0.8.0]: https://github.com/futrime/bedrinth-api/compare/v0.7.0...v0.8.0 -[0.7.0]: https://github.com/futrime/bedrinth-api/compare/v0.6.0...v0.7.0 -[0.6.0]: https://github.com/futrime/bedrinth-api/compare/v0.5.0...v0.6.0 -[0.5.0]: https://github.com/futrime/bedrinth-api/compare/v0.4.0...v0.5.0 -[0.4.0]: https://github.com/futrime/bedrinth-api/compare/v0.3.0...v0.4.0 -[0.3.0]: https://github.com/futrime/bedrinth-api/compare/v0.2.0...v0.3.0 -[0.2.0]: https://github.com/futrime/bedrinth-api/compare/v0.1.1...v0.2.0 -[0.1.1]: https://github.com/futrime/bedrinth-api/compare/v0.1.0...v0.1.1 -[0.1.0]: https://github.com/futrime/bedrinth-api/releases/tag/v0.1.0 diff --git a/apps/api/redis-client.ts b/apps/api/redis-client.ts index aee610d..9253400 100644 --- a/apps/api/redis-client.ts +++ b/apps/api/redis-client.ts @@ -89,14 +89,18 @@ export class RedisClient implements DatabaseClient { if (q.length > 1) { const qList = q.replaceAll('*', ' ').split(' ').filter(item => item.length > 0) - for (const qItem of qList) { + // If the query starts with a plus, it is an exact match + const optionalQList = qList.filter(item => !item.startsWith('+')) + const exactQList = qList.filter(item => item.startsWith('+')).map(item => item.slice(1)) + + for (const qItem of exactQList) { if (/^[a-z0-9-]+:[a-z0-9-]+$/.test(qItem)) { - query = query.and(search => search + query = query.and(subQuery => subQuery .or('tags').contains(qItem) ) } else { const pattern = `*${qItem}*` - query = query.and(search => search + query = query.and(subQuery => subQuery .or('name').matches(pattern) .or('description').matches(pattern) .or('author').matches(pattern) @@ -104,6 +108,28 @@ export class RedisClient implements DatabaseClient { ) } } + + if (optionalQList.length > 0) { + query = query.and(subQuery => { + for (const qItem of optionalQList) { + if (/^[a-z0-9-]+:[a-z0-9-]+$/.test(qItem)) { + subQuery = subQuery.or(sub2Query => sub2Query + .or('tags').contains(qItem) + ) + } else { + const pattern = `*${qItem}*` + subQuery = subQuery.or(sub2Query => sub2Query + .or('name').matches(pattern) + .or('description').matches(pattern) + .or('author').matches(pattern) + .or('tags').contains(pattern) + ) + } + } + + return subQuery + }) + } } const pageCount = Math.ceil(await query.count() / perPage)