2020-06-30 15:24:30 +02:00
|
|
|
import {
|
2020-07-01 18:13:32 +02:00
|
|
|
SearchQuery,
|
|
|
|
QueryResult
|
2020-10-13 17:05:57 +02:00
|
|
|
} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
|
|
|
|
import { MetadataCache, Logger } from '@oceanprotocol/lib'
|
2020-05-07 08:03:30 +02:00
|
|
|
|
2020-06-30 15:24:30 +02:00
|
|
|
export function getSearchQuery(
|
|
|
|
page?: string | string[],
|
|
|
|
offset?: string | string[],
|
|
|
|
text?: string | string[],
|
2020-10-22 14:46:11 +02:00
|
|
|
tags?: string | string[]
|
2020-06-30 15:24:30 +02:00
|
|
|
): SearchQuery {
|
|
|
|
return {
|
|
|
|
page: Number(page) || 1,
|
|
|
|
offset: Number(offset) || 20,
|
|
|
|
query: {
|
|
|
|
text,
|
2020-10-22 14:46:11 +02:00
|
|
|
tags: tags ? [tags] : undefined
|
2020-06-30 15:24:30 +02:00
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
created: -1
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:00:16 +02:00
|
|
|
// Something in squid-js is weird when using 'tags: [tag]'
|
2020-06-30 15:24:30 +02:00
|
|
|
// which is the only way the query actually returns desired results.
|
|
|
|
// But it doesn't follow 'SearchQuery' interface so we have to assign
|
|
|
|
// it here.
|
|
|
|
} as SearchQuery
|
|
|
|
}
|
|
|
|
|
2020-08-11 15:56:13 +02:00
|
|
|
export async function getResults(
|
2020-10-22 14:46:11 +02:00
|
|
|
params: { text?: string; tags?: string; page?: string; offset?: string },
|
2020-10-13 17:05:57 +02:00
|
|
|
metadataCacheUri: string
|
2020-08-11 15:56:13 +02:00
|
|
|
): Promise<QueryResult> {
|
2020-10-22 14:46:11 +02:00
|
|
|
const { text, tags, page, offset } = params
|
2020-06-30 15:24:30 +02:00
|
|
|
|
2020-10-13 17:05:57 +02:00
|
|
|
const metadataCache = new MetadataCache(metadataCacheUri, Logger)
|
2020-10-22 14:46:11 +02:00
|
|
|
const sQuery = getSearchQuery(page, offset, text, tags)
|
|
|
|
console.log(sQuery)
|
2020-10-13 17:05:57 +02:00
|
|
|
const queryResult = await metadataCache.queryMetadata(
|
2020-10-22 14:46:11 +02:00
|
|
|
getSearchQuery(page, offset, text, tags)
|
2020-05-07 08:03:30 +02:00
|
|
|
)
|
2020-06-30 15:24:30 +02:00
|
|
|
|
|
|
|
return queryResult
|
2020-05-07 08:03:30 +02:00
|
|
|
}
|