Skip to content

Commit

Permalink
feat: support must query condition
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Nov 2, 2024
1 parent d7d8821 commit 04e1bbf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 124 deletions.
121 changes: 0 additions & 121 deletions CHANGELOG.md

This file was deleted.

32 changes: 29 additions & 3 deletions apps/api/redis-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,47 @@ 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)
.or('tags').contains(pattern)
)
}
}

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)
Expand Down

0 comments on commit 04e1bbf

Please sign in to comment.