From 3cd520bd5abcb75aca270822afd15c7fda972f92 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 4 Jul 2022 16:10:54 -0600 Subject: [PATCH] Need to return buffers from search as well --- lib/client.ts | 2 +- lib/search/results-converter.ts | 13 +++++++------ spec/unit/client/client-search.spec.ts | 26 +++++++++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/client.ts b/lib/client.ts index 50407c7b..a84ec1b7 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -174,7 +174,7 @@ export class Client { if (keysOnly) command.push('RETURN', '0'); - return this.redis.sendCommand(command); + return this.redis.sendCommand(command, commandOptions({ returnBuffers: true })); } /** @internal */ diff --git a/lib/search/results-converter.ts b/lib/search/results-converter.ts index 14832d32..c7f4fb38 100644 --- a/lib/search/results-converter.ts +++ b/lib/search/results-converter.ts @@ -18,7 +18,7 @@ export abstract class SearchResultsConverter { } get ids(): Array { - return this.keys.map(key => (key as string).replace(/^.*:/, "")); + return this.keys.map(key => key.toString().replace(/^.*:/, "")); } get keys(): Array { @@ -42,12 +42,12 @@ export abstract class SearchResultsConverter { } export class HashSearchResultsConverter extends SearchResultsConverter { - protected arrayToEntity(id: string, array: Array): TEntity { + protected arrayToEntity(id: string, array: Array): TEntity { const keys = array.filter((_entry, index) => index % 2 === 0); const values = array.filter((_entry, index) => index % 2 !== 0); const hashData: RedisHashData = keys.reduce((object: any, key, index) => { - object[key] = values[index] + object[key.toString()] = values[index] return object }, {}); @@ -58,9 +58,10 @@ export class HashSearchResultsConverter extends SearchRe } export class JsonSearchResultsConverter extends SearchResultsConverter { - protected arrayToEntity(id: string, array: Array): TEntity { - const index = array.findIndex(value => value === '$') + 1; - const jsonString = array[index]; + protected arrayToEntity(id: string, array: Array): TEntity { + const items = array.map(item => item.toString()) + const index = items.findIndex(value => value === '$') + 1; + const jsonString = items[index]; const jsonData: RedisJsonData = JSON.parse(jsonString); const entity = new this.schema.entityCtor(this.schema, id); entity.fromRedisJson(jsonData); diff --git a/spec/unit/client/client-search.spec.ts b/spec/unit/client/client-search.spec.ts index da20aa6a..0b401d00 100644 --- a/spec/unit/client/client-search.spec.ts +++ b/spec/unit/client/client-search.spec.ts @@ -1,4 +1,4 @@ -import { redis } from '../helpers/mock-redis' +import { redis, commandOptions } from '../helpers/mock-redis' import { Client } from '$lib/client'; @@ -22,7 +22,9 @@ describe("Client", () => { query: 'query' }); expect(redis.sendCommand).toHaveBeenCalledWith([ - 'FT.SEARCH', 'index', 'query']); + 'FT.SEARCH', 'index', 'query'], + commandOptions({ returnBuffers: true }) + ); }); it("sends the expect command when given a limit", async () => { @@ -32,7 +34,9 @@ describe("Client", () => { limit: { offset: 0, count: 5 } }); expect(redis.sendCommand).toHaveBeenCalledWith([ - 'FT.SEARCH', 'index', 'query', 'LIMIT', '0', '5']); + 'FT.SEARCH', 'index', 'query', 'LIMIT', '0', '5'], + commandOptions({ returnBuffers: true }) + ); }); it("sends the expected command when given a sort", async () => { @@ -42,7 +46,9 @@ describe("Client", () => { sort: { field: 'sortField', order: 'ASC' } }); expect(redis.sendCommand).toHaveBeenCalledWith([ - 'FT.SEARCH', 'index', 'query', 'SORTBY', 'sortField', 'ASC']); + 'FT.SEARCH', 'index', 'query', 'SORTBY', 'sortField', 'ASC'], + commandOptions({ returnBuffers: true }) + ); }); it("sends the expected command when keysOnly is set to false", async () => { @@ -52,7 +58,9 @@ describe("Client", () => { keysOnly: false }); expect(redis.sendCommand).toHaveBeenCalledWith([ - 'FT.SEARCH', 'index', 'query']); + 'FT.SEARCH', 'index', 'query'], + commandOptions({ returnBuffers: true }) + ); }); it("sends the expected command when keysOnly is set to true", async () => { @@ -62,7 +70,9 @@ describe("Client", () => { keysOnly: true }); expect(redis.sendCommand).toHaveBeenCalledWith([ - 'FT.SEARCH', 'index', 'query', 'RETURN', '0']); + 'FT.SEARCH', 'index', 'query', 'RETURN', '0'], + commandOptions({ returnBuffers: true }) + ); }); it("sends the expected command with all options", async () => { @@ -75,7 +85,9 @@ describe("Client", () => { }); expect(redis.sendCommand).toHaveBeenCalledWith([ 'FT.SEARCH', 'index', 'query', 'LIMIT', '0', '5', - 'SORTBY', 'sortField', 'ASC', 'RETURN', '0']); + 'SORTBY', 'sortField', 'ASC', 'RETURN', '0'], + commandOptions({ returnBuffers: true }) + ); }); });