Created
February 24, 2021 20:43
-
-
Save guyroyse/b94a26d5f10770b3a44aebb84da8844f to your computer and use it in GitHub Desktop.
Wrapping module calls the ioredis way.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Redis from 'ioredis' | |
import * as ioredisearch from './ioredisearch' | |
let r = new Redis() | |
ioredisearch.use(r) | |
let results = yield r.list() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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