Skip to content

Commit

Permalink
Need to return buffers from search as well
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainCodeman committed Jul 6, 2022
1 parent aebbcef commit 3cd520b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class Client {

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

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

/** @internal */
Expand Down
13 changes: 7 additions & 6 deletions lib/search/results-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export abstract class SearchResultsConverter<TEntity extends Entity> {
}

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

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

export class HashSearchResultsConverter<TEntity extends Entity> extends SearchResultsConverter<TEntity> {
protected arrayToEntity(id: string, array: Array<string>): TEntity {
protected arrayToEntity(id: string, array: Array<string | Buffer>): 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
}, {});

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

export class JsonSearchResultsConverter<TEntity extends Entity> extends SearchResultsConverter<TEntity> {
protected arrayToEntity(id: string, array: Array<string>): TEntity {
const index = array.findIndex(value => value === '$') + 1;
const jsonString = array[index];
protected arrayToEntity(id: string, array: Array<string | Buffer>): 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);
Expand Down
26 changes: 19 additions & 7 deletions spec/unit/client/client-search.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { redis } from '../helpers/mock-redis'
import { redis, commandOptions } from '../helpers/mock-redis'
import { Client } from '$lib/client';


Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 })
);
});
});

Expand Down

0 comments on commit 3cd520b

Please sign in to comment.