Skip to content

Instantly share code, notes, and snippets.

@guyroyse
Created February 24, 2021 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guyroyse/b94a26d5f10770b3a44aebb84da8844f to your computer and use it in GitHub Desktop.
Save guyroyse/b94a26d5f10770b3a44aebb84da8844f to your computer and use it in GitHub Desktop.
Wrapping module calls the ioredis way.
import Redis from 'ioredis'
import * as ioredisearch from './ioredisearch'
let r = new Redis()
ioredisearch.use(r)
let results = yield r.list()
import Redis from 'ioredis'
function use(connection) {
let list = connection.createBuiltinCommand('FT._LIST')
let create = connection.createBuiltinCommand('FT.CREATE')
let search = connection.createBuiltinCommand('FT.SEARCH')
Redis.Command.setReplyTransformer('FT.SEARCH', (response) => {
let [count, ...keysAndHashes] = response
let hashes = keysAndHashes
.filter((_, index) => index % 2 !== 0)
.map(hash => arrayToObject(hash))
let items = keysAndHashes
.filter((_, index) => index % 2 === 0)
.map((key, index) => ({ key, value: hashes[index] }))
return { count, items }
})
function arrayToObject(array) {
let keys = array.filter((_, index) => index % 2 === 0)
let values = array.filter((_, index) => index % 2 !== 0)
return keys.reduce((object, key, index) => {
object[key] = values[index]
return object
}, {})
}
connection.ftList = list.string
connection.ftCreate = create.string
connection.ftSearch = search.string
}
export default { use }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment