Skip to content

Commit e1bed89

Browse files
Need to return buffers from search as well
1 parent 3021834 commit e1bed89

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

lib/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class Client {
174174

175175
if (keysOnly) command.push('RETURN', '0');
176176

177-
return this.redis.sendCommand<any[]>(command);
177+
return this.redis.sendCommand<any[]>(command, commandOptions({ returnBuffers: true }));
178178
}
179179

180180
/** @internal */

lib/search/results-converter.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export abstract class SearchResultsConverter<TEntity extends Entity> {
1818
}
1919

2020
get ids(): Array<string> {
21-
return this.keys.map(key => (key as string).replace(/^.*:/, ""));
21+
return this.keys.map(key => key.toString().replace(/^.*:/, ""));
2222
}
2323

2424
get keys(): Array<string> {
@@ -42,12 +42,12 @@ export abstract class SearchResultsConverter<TEntity extends Entity> {
4242
}
4343

4444
export class HashSearchResultsConverter<TEntity extends Entity> extends SearchResultsConverter<TEntity> {
45-
protected arrayToEntity(id: string, array: Array<string>): TEntity {
45+
protected arrayToEntity(id: string, array: Array<string | Buffer>): TEntity {
4646
const keys = array.filter((_entry, index) => index % 2 === 0);
4747
const values = array.filter((_entry, index) => index % 2 !== 0);
4848

4949
const hashData: RedisHashData = keys.reduce((object: any, key, index) => {
50-
object[key] = values[index]
50+
object[key.toString()] = values[index]
5151
return object
5252
}, {});
5353

@@ -58,9 +58,10 @@ export class HashSearchResultsConverter<TEntity extends Entity> extends SearchRe
5858
}
5959

6060
export class JsonSearchResultsConverter<TEntity extends Entity> extends SearchResultsConverter<TEntity> {
61-
protected arrayToEntity(id: string, array: Array<string>): TEntity {
62-
const index = array.findIndex(value => value === '$') + 1;
63-
const jsonString = array[index];
61+
protected arrayToEntity(id: string, array: Array<string | Buffer>): TEntity {
62+
const items = array.map(item => item.toString())
63+
const index = items.findIndex(value => value === '$') + 1;
64+
const jsonString = items[index];
6465
const jsonData: RedisJsonData = JSON.parse(jsonString);
6566
const entity = new this.schema.entityCtor(this.schema, id);
6667
entity.fromRedisJson(jsonData);

spec/unit/client/client-search.spec.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { redis } from '../helpers/mock-redis'
1+
import { redis, commandOptions } from '../helpers/mock-redis'
22
import { Client } from '$lib/client';
33

44

@@ -22,7 +22,9 @@ describe("Client", () => {
2222
query: 'query'
2323
});
2424
expect(redis.sendCommand).toHaveBeenCalledWith([
25-
'FT.SEARCH', 'index', 'query']);
25+
'FT.SEARCH', 'index', 'query'],
26+
commandOptions({ returnBuffers: true })
27+
);
2628
});
2729

2830
it("sends the expect command when given a limit", async () => {
@@ -32,7 +34,9 @@ describe("Client", () => {
3234
limit: { offset: 0, count: 5 }
3335
});
3436
expect(redis.sendCommand).toHaveBeenCalledWith([
35-
'FT.SEARCH', 'index', 'query', 'LIMIT', '0', '5']);
37+
'FT.SEARCH', 'index', 'query', 'LIMIT', '0', '5'],
38+
commandOptions({ returnBuffers: true })
39+
);
3640
});
3741

3842
it("sends the expected command when given a sort", async () => {
@@ -42,7 +46,9 @@ describe("Client", () => {
4246
sort: { field: 'sortField', order: 'ASC' }
4347
});
4448
expect(redis.sendCommand).toHaveBeenCalledWith([
45-
'FT.SEARCH', 'index', 'query', 'SORTBY', 'sortField', 'ASC']);
49+
'FT.SEARCH', 'index', 'query', 'SORTBY', 'sortField', 'ASC'],
50+
commandOptions({ returnBuffers: true })
51+
);
4652
});
4753

4854
it("sends the expected command when keysOnly is set to false", async () => {
@@ -52,7 +58,9 @@ describe("Client", () => {
5258
keysOnly: false
5359
});
5460
expect(redis.sendCommand).toHaveBeenCalledWith([
55-
'FT.SEARCH', 'index', 'query']);
61+
'FT.SEARCH', 'index', 'query'],
62+
commandOptions({ returnBuffers: true })
63+
);
5664
});
5765

5866
it("sends the expected command when keysOnly is set to true", async () => {
@@ -62,7 +70,9 @@ describe("Client", () => {
6270
keysOnly: true
6371
});
6472
expect(redis.sendCommand).toHaveBeenCalledWith([
65-
'FT.SEARCH', 'index', 'query', 'RETURN', '0']);
73+
'FT.SEARCH', 'index', 'query', 'RETURN', '0'],
74+
commandOptions({ returnBuffers: true })
75+
);
6676
});
6777

6878
it("sends the expected command with all options", async () => {
@@ -75,7 +85,9 @@ describe("Client", () => {
7585
});
7686
expect(redis.sendCommand).toHaveBeenCalledWith([
7787
'FT.SEARCH', 'index', 'query', 'LIMIT', '0', '5',
78-
'SORTBY', 'sortField', 'ASC', 'RETURN', '0']);
88+
'SORTBY', 'sortField', 'ASC', 'RETURN', '0'],
89+
commandOptions({ returnBuffers: true })
90+
);
7991
});
8092
});
8193

0 commit comments

Comments
 (0)