redis: support for redis streams

This commit is contained in:
Jan Prochazka
2022-03-27 17:16:17 +02:00
parent 4bd7cd26d0
commit e25657bd43
6 changed files with 56 additions and 6 deletions

View File

@@ -297,6 +297,14 @@ const driver = {
switch (method) {
case 'mdel':
return await this.deleteBranch(pool, args[0]);
case 'xaddjson':
let json;
try {
json = JSON.parse(args[2]);
} catch (e) {
throw new Error('Value must be valid JSON. ' + e.message);
}
return await pool.xadd(args[0], args[1] || '*', ..._.flatten(_.toPairs(json)));
}
return await pool[method](...args);
},
@@ -332,6 +340,22 @@ const driver = {
items: _.chunk(res[1], 2).map((item) => ({ key: item[0], value: item[1] })),
};
}
case 'stream': {
const res = await pool.xrange(key, cursor == 0 ? '-' : cursor, '+', 'COUNT', count);
let newCursor = 0;
if (res.length > 0) {
const id = res[res.length - 1][0];
const idParts = id.split('-');
newCursor = `${idParts[0]}-${parseInt(idParts[1] + 1)}`;
}
return {
cursor: newCursor,
items: res.map(([id, vals]) => ({
id,
value: JSON.stringify(_.fromPairs(_.chunk(vals, 2)), undefined, 2),
})),
};
}
}
return null;
},

View File

@@ -64,6 +64,14 @@ const driver = {
addMethod: 'hset',
showItemList: true,
},
{
name: 'stream',
label: 'Stream',
dbKeyFields: [{ name: 'id' }, { name: 'value' }],
keyColumn: 'id',
addMethod: 'xaddjson',
showItemList: true,
},
],
showConnectionField: (field, values) => {