You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +03:00
Graph (#1887)
* init * implement graph commands * add graph to packages table * fix ts.infoDebug * fix redisearch tests * Update INFO_DEBUG.ts * fix INFO.spec.ts * test QUERY and SLOWLOG Co-authored-by: Avital-Fine <avital.fine@redis.com>
This commit is contained in:
22
packages/graph/lib/commands/CONFIG_GET.spec.ts
Normal file
22
packages/graph/lib/commands/CONFIG_GET.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './CONFIG_GET';
|
||||
|
||||
describe('CONFIG GET', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('TIMEOUT'),
|
||||
['GRAPH.CONFIG', 'GET', 'TIMEOUT']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.configGet', async client => {
|
||||
assert.deepEqual(
|
||||
await client.graph.configGet('TIMEOUT'),
|
||||
[
|
||||
'TIMEOUT',
|
||||
0
|
||||
]
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
12
packages/graph/lib/commands/CONFIG_GET.ts
Normal file
12
packages/graph/lib/commands/CONFIG_GET.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export const IS_READ_ONLY = true;
|
||||
|
||||
export function transformArguments(configKey: string): Array<string> {
|
||||
return ['GRAPH.CONFIG', 'GET', configKey];
|
||||
}
|
||||
|
||||
type ConfigItem = [
|
||||
configKey: string,
|
||||
value: number
|
||||
];
|
||||
|
||||
export declare function transformReply(): ConfigItem | Array<ConfigItem>;
|
19
packages/graph/lib/commands/CONFIG_SET.spec.ts
Normal file
19
packages/graph/lib/commands/CONFIG_SET.spec.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './CONFIG_SET';
|
||||
|
||||
describe('CONFIG SET', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('TIMEOUT', 0),
|
||||
['GRAPH.CONFIG', 'SET', 'TIMEOUT', '0']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.configSet', async client => {
|
||||
assert.equal(
|
||||
await client.graph.configSet('TIMEOUT', 0),
|
||||
'OK'
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
10
packages/graph/lib/commands/CONFIG_SET.ts
Normal file
10
packages/graph/lib/commands/CONFIG_SET.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export function transformArguments(configKey: string, value: number): Array<string> {
|
||||
return [
|
||||
'GRAPH.CONFIG',
|
||||
'SET',
|
||||
configKey,
|
||||
value.toString()
|
||||
];
|
||||
}
|
||||
|
||||
export declare function transformReply(): 'OK';
|
21
packages/graph/lib/commands/DELETE.spec.ts
Normal file
21
packages/graph/lib/commands/DELETE.spec.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './DELETE';
|
||||
|
||||
describe('', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key'),
|
||||
['GRAPH.DELETE', 'key']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.delete', async client => {
|
||||
await client.graph.query('key', 'RETURN 1');
|
||||
|
||||
assert.equal(
|
||||
typeof await client.graph.delete('key'),
|
||||
'string'
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
7
packages/graph/lib/commands/DELETE.ts
Normal file
7
packages/graph/lib/commands/DELETE.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
|
||||
export function transformArguments(key: string): Array<string> {
|
||||
return ['GRAPH.DELETE', key];
|
||||
}
|
||||
|
||||
export declare function transformReply(): string;
|
18
packages/graph/lib/commands/EXPLAIN.spec.ts
Normal file
18
packages/graph/lib/commands/EXPLAIN.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './EXPLAIN';
|
||||
|
||||
describe('EXPLAIN', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key', 'RETURN 0'),
|
||||
['GRAPH.EXPLAIN', 'key', 'RETURN 0']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.explain', async client => {
|
||||
const reply = await client.graph.explain('key', 'RETURN 0');
|
||||
assert.ok(Array.isArray(reply));
|
||||
assert.ok(!reply.find(x => typeof x !== 'string'));
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
9
packages/graph/lib/commands/EXPLAIN.ts
Normal file
9
packages/graph/lib/commands/EXPLAIN.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
|
||||
export const IS_READ_ONLY = true;
|
||||
|
||||
export function transformArguments(key: string, query: string): Array<string> {
|
||||
return ['GRAPH.EXPLAIN', key, query];
|
||||
}
|
||||
|
||||
export declare function transfromReply(): Array<string>;
|
19
packages/graph/lib/commands/LIST.spec.ts
Normal file
19
packages/graph/lib/commands/LIST.spec.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './LIST';
|
||||
|
||||
describe('LIST', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments(),
|
||||
['GRAPH.LIST']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.list', async client => {
|
||||
assert.deepEqual(
|
||||
await client.graph.list(),
|
||||
[]
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
7
packages/graph/lib/commands/LIST.ts
Normal file
7
packages/graph/lib/commands/LIST.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export const IS_READ_ONLY = true;
|
||||
|
||||
export function transformArguments(): Array<string> {
|
||||
return ['GRAPH.LIST'];
|
||||
}
|
||||
|
||||
export declare function transformReply(): Array<string>;
|
18
packages/graph/lib/commands/PROFILE.spec.ts
Normal file
18
packages/graph/lib/commands/PROFILE.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './PROFILE';
|
||||
|
||||
describe('PROFILE', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key', 'RETURN 0'),
|
||||
['GRAPH.PROFILE', 'key', 'RETURN 0']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.profile', async client => {
|
||||
const reply = await client.graph.profile('key', 'RETURN 0');
|
||||
assert.ok(Array.isArray(reply));
|
||||
assert.ok(!reply.find(x => typeof x !== 'string'));
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
9
packages/graph/lib/commands/PROFILE.ts
Normal file
9
packages/graph/lib/commands/PROFILE.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
|
||||
export const IS_READ_ONLY = true;
|
||||
|
||||
export function transformArguments(key: string, query: string): Array<string> {
|
||||
return ['GRAPH.PROFILE', key, query];
|
||||
}
|
||||
|
||||
export declare function transfromReply(): Array<string>;
|
22
packages/graph/lib/commands/QUERY.spec.ts
Normal file
22
packages/graph/lib/commands/QUERY.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './QUERY';
|
||||
|
||||
describe('QUERY', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key', '*', 100),
|
||||
['GRAPH.QUERY', 'key', '*', '100']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.query', async client => {
|
||||
await client.graph.query('key',
|
||||
"CREATE (r:human {name:'roi', age:34}), (a:human {name:'amit', age:32}), (r)-[:knows]->(a)"
|
||||
);
|
||||
const reply = await client.graph.query('key',
|
||||
"MATCH (r:human)-[:knows]->(a:human) RETURN r.age, r.name"
|
||||
);
|
||||
assert.equal(reply.data.length, 1);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
43
packages/graph/lib/commands/QUERY.ts
Normal file
43
packages/graph/lib/commands/QUERY.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@node-redis/client/dist/lib/commands/index';
|
||||
import { pushQueryArguments } from '.';
|
||||
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
|
||||
export function transformArguments(
|
||||
graph: RedisCommandArgument,
|
||||
query: RedisCommandArgument,
|
||||
timeout?: number
|
||||
): RedisCommandArguments {
|
||||
return pushQueryArguments(
|
||||
['GRAPH.QUERY'],
|
||||
graph,
|
||||
query,
|
||||
timeout
|
||||
);
|
||||
}
|
||||
|
||||
type Headers = Array<string>;
|
||||
|
||||
type Data = Array<Array<string | number | null>>;
|
||||
|
||||
type Metadata = Array<string>;
|
||||
|
||||
type QueryRawReply = [
|
||||
headers: Headers,
|
||||
data: Data,
|
||||
metadata: Metadata
|
||||
];
|
||||
|
||||
interface QueryReply {
|
||||
headers: Headers,
|
||||
data: Data,
|
||||
metadata: Metadata
|
||||
};
|
||||
|
||||
export function transformReply(reply: QueryRawReply): QueryReply {
|
||||
return {
|
||||
headers: reply[0],
|
||||
data: reply[1],
|
||||
metadata: reply[2]
|
||||
};
|
||||
}
|
22
packages/graph/lib/commands/QUERY_RO.spec.ts
Normal file
22
packages/graph/lib/commands/QUERY_RO.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './QUERY_RO';
|
||||
|
||||
describe('QUERY_RO', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key', '*', 100),
|
||||
['GRAPH.RO_QUERY', 'key', '*', '100']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.queryRo', async client => {
|
||||
await client.graph.query('key',
|
||||
"CREATE (r:human {name:'roi', age:34}), (a:human {name:'amit', age:32}), (r)-[:knows]->(a)"
|
||||
);
|
||||
const reply = await client.graph.queryRo('key',
|
||||
"MATCH (r:human)-[:knows]->(a:human) RETURN r.age, r.name"
|
||||
);
|
||||
assert.equal(reply.data.length, 1);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
21
packages/graph/lib/commands/QUERY_RO.ts
Normal file
21
packages/graph/lib/commands/QUERY_RO.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@node-redis/client/dist/lib/commands';
|
||||
import { pushQueryArguments } from '.';
|
||||
|
||||
export { FIRST_KEY_INDEX } from './QUERY';
|
||||
|
||||
export const IS_READ_ONLY = true;
|
||||
|
||||
export function transformArguments(
|
||||
graph: RedisCommandArgument,
|
||||
query: RedisCommandArgument,
|
||||
timeout?: number
|
||||
): RedisCommandArguments {
|
||||
return pushQueryArguments(
|
||||
['GRAPH.RO_QUERY'],
|
||||
graph,
|
||||
query,
|
||||
timeout
|
||||
);
|
||||
}
|
||||
|
||||
export { transformReply } from './QUERY';
|
18
packages/graph/lib/commands/SLOWLOG.spec.ts
Normal file
18
packages/graph/lib/commands/SLOWLOG.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './SLOWLOG';
|
||||
|
||||
describe('SLOWLOG', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key'),
|
||||
['GRAPH.SLOWLOG', 'key']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.slowLog', async client => {
|
||||
await client.graph.query('key', 'RETURN 1');
|
||||
const reply = await client.graph.slowLog('key');
|
||||
assert.equal(reply.length, 1);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
30
packages/graph/lib/commands/SLOWLOG.ts
Normal file
30
packages/graph/lib/commands/SLOWLOG.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export const IS_READ_ONLY = true;
|
||||
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
|
||||
export function transformArguments(key: string) {
|
||||
return ['GRAPH.SLOWLOG', key];
|
||||
}
|
||||
|
||||
type SlowLogRawReply = Array<[
|
||||
timestamp: string,
|
||||
command: string,
|
||||
query: string,
|
||||
took: string
|
||||
]>;
|
||||
|
||||
type SlowLogReply = Array<{
|
||||
timestamp: Date;
|
||||
command: string;
|
||||
query: string;
|
||||
took: number;
|
||||
}>;
|
||||
|
||||
export function transformReply(logs: SlowLogRawReply): SlowLogReply {
|
||||
return logs.map(([timestamp, command, query, took]) => ({
|
||||
timestamp: new Date(Number(timestamp) * 1000),
|
||||
command,
|
||||
query,
|
||||
took: Number(took)
|
||||
}));
|
||||
}
|
49
packages/graph/lib/commands/index.ts
Normal file
49
packages/graph/lib/commands/index.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import * as CONFIG_GET from './CONFIG_GET';
|
||||
import * as CONFIG_SET from './CONFIG_SET';;
|
||||
import * as DELETE from './DELETE';
|
||||
import * as EXPLAIN from './EXPLAIN';
|
||||
import * as LIST from './LIST';
|
||||
import * as PROFILE from './PROFILE';
|
||||
import * as QUERY_RO from './QUERY_RO';
|
||||
import * as QUERY from './QUERY';
|
||||
import * as SLOWLOG from './SLOWLOG';
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@node-redis/client/dist/lib/commands';
|
||||
|
||||
export default {
|
||||
CONFIG_GET,
|
||||
configGet: CONFIG_GET,
|
||||
CONFIG_SET,
|
||||
configSet: CONFIG_SET,
|
||||
DELETE,
|
||||
delete: DELETE,
|
||||
EXPLAIN,
|
||||
explain: EXPLAIN,
|
||||
LIST,
|
||||
list: LIST,
|
||||
PROFILE,
|
||||
profile: PROFILE,
|
||||
QUERY_RO,
|
||||
queryRo: QUERY_RO,
|
||||
QUERY,
|
||||
query: QUERY,
|
||||
SLOWLOG,
|
||||
slowLog: SLOWLOG
|
||||
};
|
||||
|
||||
export function pushQueryArguments(
|
||||
args: RedisCommandArguments,
|
||||
graph: RedisCommandArgument,
|
||||
query: RedisCommandArgument,
|
||||
timeout?: number
|
||||
): RedisCommandArguments {
|
||||
args.push(
|
||||
graph,
|
||||
query
|
||||
);
|
||||
|
||||
if (timeout !== undefined) {
|
||||
args.push(timeout.toString());
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
Reference in New Issue
Block a user