Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#RI-3063-change etries structure #810

Merged
merged 5 commits into from Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 12 additions & 13 deletions redisinsight/api/src/modules/browser/dto/stream.dto.ts
Expand Up @@ -11,15 +11,13 @@ import {
IsString,
Min,
ValidateNested,
isString,
NotEquals,
ValidateIf,
IsBoolean,
} from 'class-validator';
import { KeyDto, KeyWithExpireDto } from 'src/modules/browser/dto/keys.dto';
import { SortOrder } from 'src/constants';
import { Type } from 'class-transformer';
import { IsObjectWithValues } from 'src/validators/isObjectWithValues.validator';

export class StreamEntryDto {
@ApiProperty({
Expand All @@ -35,12 +33,13 @@ export class StreamEntryDto {
@ApiProperty({
type: Object,
description: 'Entry fields',
example: { field1: 'value1', field2: 'value2' },
example: [['field1', 'value1'], ['field2', 'value2']],
})
@IsDefined()
@IsNotEmpty()
@IsObjectWithValues([isString], { message: '$property must be an object with string values' })
fields: Record<string, string>;
@IsArray()
@ArrayNotEmpty()
fields: Array<Array<string>>;
}

export class GetStreamEntriesDto extends KeyDto {
Expand Down Expand Up @@ -131,17 +130,17 @@ export class AddStreamEntriesDto extends KeyDto {
example: [
{
id: '*',
fields: {
field1: 'value1',
field2: 'value2',
},
fields: [
['field1', 'value1'],
['field2', 'value2'],
],
},
{
id: '*',
fields: {
field1: 'value1',
field2: 'value2',
},
fields: [
['field1', 'value1'],
['field2', 'value2'],
],
},
],
})
Expand Down
Expand Up @@ -23,9 +23,9 @@ const mockKeyDto = {

const mockStreamEntry: StreamEntryDto = {
id: '*',
fields: {
field1: 'value1',
},
fields: [
['field1', 'value1']
]
};
const mockAddStreamEntriesDto: AddStreamEntriesDto = {
keyName: 'testList',
Expand Down
Expand Up @@ -18,9 +18,9 @@ const mockClientOptions: IFindRedisClientInstanceByOptions = {

const mockStreamEntry: StreamEntryDto = {
id: '*',
fields: {
field1: 'value1',
},
fields: [
['field1', 'value1'],
],
};
const mockAddStreamEntriesDto: AddStreamEntriesDto = {
keyName: 'testList',
Expand Down Expand Up @@ -74,11 +74,11 @@ const mockStreamInfo = {
lastGeneratedId: '1651130346487-1',
firstEntry: {
id: '1651130346487-0',
fields: { field1: 'value1', field2: 'value2' },
fields: [ ['field1', 'value1'], ['field2', 'value2'] ],
},
lastEntry: {
id: '1651130346487-1',
fields: { field1: 'value1', field2: 'value2' },
fields: [ ['field1', 'value1'], ['field2', 'value2'] ],
},
};
const mockStreamEntriesReply = [
Expand All @@ -87,8 +87,8 @@ const mockStreamEntriesReply = [
];
const mockEmptyStreamEntriesReply = [];
const mockStreamEntries = [
{ id: '1651130346487-1', fields: { field1: 'value1', field2: 'value2' } },
{ id: '1651130346487-0', fields: { field1: 'value1', field2: 'value2' } },
{ id: '1651130346487-1', fields: [ ['field1', 'value1'], ['field2', 'value2'] ] },
{ id: '1651130346487-0', fields: [ ['field1', 'value1'], ['field2', 'value2'] ] },
];

const mockGetStreamEntriesDto: GetStreamEntriesDto = {
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('StreamService', () => {
).resolves.not.toThrow();
expect(browserTool.execMulti).toHaveBeenCalledWith(mockClientOptions, [
[BrowserToolStreamCommands.XAdd, mockAddStreamEntriesDto.keyName, mockStreamEntry.id,
...Object.keys(mockStreamEntry.fields), ...Object.values(mockStreamEntry.fields)],
...mockStreamEntry.fields[0]],
[BrowserToolKeysCommands.Expire, mockAddStreamEntriesDto.keyName, 1000],
]);
});
Expand All @@ -145,7 +145,7 @@ describe('StreamService', () => {
).resolves.not.toThrow();
expect(browserTool.execMulti).toHaveBeenCalledWith(mockClientOptions, [
[BrowserToolStreamCommands.XAdd, mockAddStreamEntriesDto.keyName, mockStreamEntry.id,
...Object.keys(mockStreamEntry.fields), ...Object.values(mockStreamEntry.fields)],
...mockStreamEntry.fields[0]],
]);
});
it('should throw error key exists', async () => {
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('StreamService', () => {
).resolves.not.toThrow();
expect(browserTool.execMulti).toHaveBeenCalledWith(mockClientOptions, [
[BrowserToolStreamCommands.XAdd, mockAddStreamEntriesDto.keyName, mockStreamEntry.id,
...Object.keys(mockStreamEntry.fields), ...Object.values(mockStreamEntry.fields)],
...mockStreamEntry.fields[0]],
]);
});
it('should throw Not Found when key does not exists', async () => {
Expand Down
Expand Up @@ -172,7 +172,7 @@ export class StreamService {

const entriesArray = entries.map((entry) => [
entry.id,
...flatMap(map(entry.fields, (value, field) => [field, value])),
...flatMap(map(entry.fields, (field) => [field[0], field[1]])),
]);

const toolCommands: Array<[
Expand Down Expand Up @@ -243,7 +243,7 @@ export class StreamService {

const entriesArray = entries.map((entry) => [
entry.id,
...flatMap(map(entry.fields, (value, field) => [field, value])),
...flatMap(map(entry.fields, (field) => [field[0], field[1]])),
]);

const toolCommands: Array<[
Expand Down Expand Up @@ -340,8 +340,8 @@ export class StreamService {
* to DTO
*
* [
* { id: '1650985323741-0', fields: { field: 'value' } },
* { id: '1650985351882-0', fields: { field: 'value2' } },
* { id: '1650985323741-0', fields: [ ['field', 'value'] ] },
* { id: '1650985351882-0', fields: [ ['field', 'value2 ] },
* ...
* ]
* @param reply
Expand All @@ -359,13 +359,6 @@ export class StreamService {
return null;
}

const dto = { id: entry[0], fields: {} };

chunk(entry[1] || [], 2).forEach((keyFieldPair) => {
// eslint-disable-next-line prefer-destructuring
dto.fields[keyFieldPair[0]] = keyFieldPair[1];
});

return dto;
return { id: entry[0], fields: chunk(entry[1] || [], 2) };
}
}
Expand Up @@ -34,7 +34,7 @@ const validInputData = {

const entrySchema = Joi.object().keys({
id: Joi.string().required(),
fields: Joi.object().required(),
fields: Joi.array().required(),
});

const responseSchema = Joi.object().keys({
Expand Down Expand Up @@ -91,9 +91,9 @@ describe('POST /instance/:instanceId/streams/entries/get', () => {
expect(body.entries.length).to.eql(500);
body.entries.forEach((entry, i) => {
expect(entry.id).to.be.a('string');
expect(entry.fields).to.eql({
[`f_${9999 - i}`]: `v_${9999 - i}`,
});
expect(entry.fields).to.eql([
[`f_${9999 - i}`, `v_${9999 - i}`],
]);
});
},
},
Expand All @@ -112,9 +112,9 @@ describe('POST /instance/:instanceId/streams/entries/get', () => {
expect(body.entries.length).to.eql(10);
body.entries.forEach((entry, i) => {
expect(entry.id).to.be.a('string');
expect(entry.fields).to.eql({
[`f_${9900 - i}`]: `v_${9900 - i}`,
});
expect(entry.fields).to.eql([
[`f_${9900 - i}`, `v_${9900 - i}`],
]);
});
},
},
Expand All @@ -132,9 +132,9 @@ describe('POST /instance/:instanceId/streams/entries/get', () => {
expect(body.entries.length).to.eql(500);
body.entries.forEach((entry, i) => {
expect(entry.id).to.be.a('string');
expect(entry.fields).to.eql({
[`f_${i}`]: `v_${i}`,
});
expect(entry.fields).to.eql([
[`f_${i}`, `v_${i}`],
]);
});
},
},
Expand All @@ -154,9 +154,9 @@ describe('POST /instance/:instanceId/streams/entries/get', () => {
expect(body.entries.length).to.eql(10);
body.entries.forEach((entry, i) => {
expect(entry.id).to.be.a('string');
expect(entry.fields).to.eql({
[`f_${99 + i}`]: `v_${99 + i}`,
});
expect(entry.fields).to.eql([
[`f_${99 + i}`, `v_${99 + i}`]
]);
});
},
},
Expand Down