You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +03:00
* fix #2189 - add graph --compact support * clean code * fix graph string param escaping * fix "is not assignable to parameter of type 'GraphClientType'" * fix README
This commit is contained in:
@ -5,18 +5,13 @@ import { transformArguments } from './QUERY';
|
||||
describe('QUERY', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key', '*', 100),
|
||||
['GRAPH.QUERY', 'key', '*', '100']
|
||||
transformArguments('key', 'query'),
|
||||
['GRAPH.QUERY', 'key', 'query']
|
||||
);
|
||||
});
|
||||
|
||||
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);
|
||||
const { data } = await client.graph.query('key', 'RETURN 0');
|
||||
assert.deepEqual(data, [[0]]);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
|
@ -1,24 +1,26 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands/index';
|
||||
import { pushQueryArguments } from '.';
|
||||
import { pushQueryArguments, QueryOptionsBackwardCompatible } from '.';
|
||||
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
|
||||
export function transformArguments(
|
||||
graph: RedisCommandArgument,
|
||||
query: RedisCommandArgument,
|
||||
timeout?: number
|
||||
options?: QueryOptionsBackwardCompatible,
|
||||
compact?: boolean
|
||||
): RedisCommandArguments {
|
||||
return pushQueryArguments(
|
||||
['GRAPH.QUERY'],
|
||||
graph,
|
||||
query,
|
||||
timeout
|
||||
options,
|
||||
compact
|
||||
);
|
||||
}
|
||||
|
||||
type Headers = Array<string>;
|
||||
|
||||
type Data = Array<Array<string | number | null>>;
|
||||
type Data = Array<string | number | null | Data>;
|
||||
|
||||
type Metadata = Array<string>;
|
||||
|
||||
@ -26,16 +28,26 @@ type QueryRawReply = [
|
||||
headers: Headers,
|
||||
data: Data,
|
||||
metadata: Metadata
|
||||
] | [
|
||||
metadata: Metadata
|
||||
];
|
||||
|
||||
interface QueryReply {
|
||||
headers: Headers,
|
||||
data: Data,
|
||||
metadata: Metadata
|
||||
export type QueryReply = {
|
||||
headers: undefined;
|
||||
data: undefined;
|
||||
metadata: Metadata;
|
||||
} | {
|
||||
headers: Headers;
|
||||
data: Data;
|
||||
metadata: Metadata;
|
||||
};
|
||||
|
||||
export function transformReply(reply: QueryRawReply): QueryReply {
|
||||
return {
|
||||
return reply.length === 1 ? {
|
||||
headers: undefined,
|
||||
data: undefined,
|
||||
metadata: reply[0]
|
||||
} : {
|
||||
headers: reply[0],
|
||||
data: reply[1],
|
||||
metadata: reply[2]
|
||||
|
@ -1,22 +0,0 @@
|
||||
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);
|
||||
});
|
17
packages/graph/lib/commands/RO_QUERY.spec.ts
Normal file
17
packages/graph/lib/commands/RO_QUERY.spec.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './RO_QUERY';
|
||||
|
||||
describe('RO_QUERY', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key', 'query'),
|
||||
['GRAPH.RO_QUERY', 'key', 'query']
|
||||
);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.roQuery', async client => {
|
||||
const { data } = await client.graph.roQuery('key', 'RETURN 0');
|
||||
assert.deepEqual(data, [[0]]);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
@ -1,5 +1,5 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
|
||||
import { pushQueryArguments } from '.';
|
||||
import { pushQueryArguments, QueryOptionsBackwardCompatible } from '.';
|
||||
|
||||
export { FIRST_KEY_INDEX } from './QUERY';
|
||||
|
||||
@ -8,13 +8,15 @@ export const IS_READ_ONLY = true;
|
||||
export function transformArguments(
|
||||
graph: RedisCommandArgument,
|
||||
query: RedisCommandArgument,
|
||||
timeout?: number
|
||||
options?: QueryOptionsBackwardCompatible,
|
||||
compact?: boolean
|
||||
): RedisCommandArguments {
|
||||
return pushQueryArguments(
|
||||
['GRAPH.RO_QUERY'],
|
||||
graph,
|
||||
query,
|
||||
timeout
|
||||
options,
|
||||
compact
|
||||
);
|
||||
}
|
||||
|
62
packages/graph/lib/commands/index.spec.ts
Normal file
62
packages/graph/lib/commands/index.spec.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import { pushQueryArguments } from '.';
|
||||
|
||||
describe('pushQueryArguments', () => {
|
||||
it('simple', () => {
|
||||
assert.deepEqual(
|
||||
pushQueryArguments(['GRAPH.QUERY'], 'graph', 'query'),
|
||||
['GRAPH.QUERY', 'graph', 'query']
|
||||
);
|
||||
});
|
||||
|
||||
describe('params', () => {
|
||||
it('all types', () => {
|
||||
assert.deepEqual(
|
||||
pushQueryArguments(['GRAPH.QUERY'], 'graph', 'query', {
|
||||
params: {
|
||||
null: null,
|
||||
string: '"\\',
|
||||
number: 0,
|
||||
boolean: false,
|
||||
array: [0],
|
||||
object: {a: 0}
|
||||
}
|
||||
}),
|
||||
['GRAPH.QUERY', 'graph', 'CYPHER null=null string="\\"\\\\" number=0 boolean=false array=[0] object={a:0} query']
|
||||
);
|
||||
});
|
||||
|
||||
it('TypeError', () => {
|
||||
assert.throws(() => {
|
||||
pushQueryArguments(['GRAPH.QUERY'], 'graph', 'query', {
|
||||
params: {
|
||||
a: undefined as any
|
||||
}
|
||||
})
|
||||
}, TypeError);
|
||||
});
|
||||
});
|
||||
|
||||
it('TIMEOUT backward compatible', () => {
|
||||
assert.deepEqual(
|
||||
pushQueryArguments(['GRAPH.QUERY'], 'graph', 'query', 1),
|
||||
['GRAPH.QUERY', 'graph', 'query', 'TIMEOUT', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('TIMEOUT', () => {
|
||||
assert.deepEqual(
|
||||
pushQueryArguments(['GRAPH.QUERY'], 'graph', 'query', {
|
||||
TIMEOUT: 1
|
||||
}),
|
||||
['GRAPH.QUERY', 'graph', 'query', 'TIMEOUT', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('compact', () => {
|
||||
assert.deepEqual(
|
||||
pushQueryArguments(['GRAPH.QUERY'], 'graph', 'query', undefined, true),
|
||||
['GRAPH.QUERY', 'graph', 'query', '--compact']
|
||||
);
|
||||
});
|
||||
});
|
@ -4,8 +4,8 @@ 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 RO_QUERY from './RO_QUERY';
|
||||
import * as SLOWLOG from './SLOWLOG';
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
|
||||
|
||||
@ -22,28 +22,93 @@ export default {
|
||||
list: LIST,
|
||||
PROFILE,
|
||||
profile: PROFILE,
|
||||
QUERY_RO,
|
||||
queryRo: QUERY_RO,
|
||||
QUERY,
|
||||
query: QUERY,
|
||||
RO_QUERY,
|
||||
roQuery: RO_QUERY,
|
||||
SLOWLOG,
|
||||
slowLog: SLOWLOG
|
||||
};
|
||||
|
||||
type QueryParam = null | string | number | boolean | QueryParams | Array<QueryParam>;
|
||||
|
||||
type QueryParams = {
|
||||
[key: string]: QueryParam;
|
||||
};
|
||||
|
||||
export interface QueryOptions {
|
||||
params?: QueryParams;
|
||||
TIMEOUT?: number;
|
||||
}
|
||||
|
||||
export type QueryOptionsBackwardCompatible = QueryOptions | number;
|
||||
|
||||
export function pushQueryArguments(
|
||||
args: RedisCommandArguments,
|
||||
graph: RedisCommandArgument,
|
||||
query: RedisCommandArgument,
|
||||
timeout?: number
|
||||
options?: QueryOptionsBackwardCompatible,
|
||||
compact?: boolean
|
||||
): RedisCommandArguments {
|
||||
args.push(
|
||||
graph,
|
||||
query
|
||||
);
|
||||
args.push(graph);
|
||||
|
||||
if (timeout !== undefined) {
|
||||
args.push(timeout.toString());
|
||||
if (typeof options === 'number') {
|
||||
args.push(query);
|
||||
pushTimeout(args, options);
|
||||
} else {
|
||||
args.push(
|
||||
options?.params ?
|
||||
`CYPHER ${queryParamsToString(options.params)} ${query}` :
|
||||
query
|
||||
);
|
||||
|
||||
if (options?.TIMEOUT !== undefined) {
|
||||
pushTimeout(args, options.TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
if (compact) {
|
||||
args.push('--compact');
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
||||
function pushTimeout(args: RedisCommandArguments, timeout: number): void {
|
||||
args.push('TIMEOUT', timeout.toString());
|
||||
}
|
||||
|
||||
function queryParamsToString(params: QueryParams): string {
|
||||
const parts = [];
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
parts.push(`${key}=${queryParamToString(value)}`);
|
||||
}
|
||||
return parts.join(' ');
|
||||
}
|
||||
|
||||
function queryParamToString(param: QueryParam): string {
|
||||
if (param === null) {
|
||||
return 'null';
|
||||
}
|
||||
|
||||
switch (typeof param) {
|
||||
case 'string':
|
||||
return `"${param.replace(/["\\]/g, '\\$&')}"`;
|
||||
|
||||
case 'number':
|
||||
case 'boolean':
|
||||
return param.toString();
|
||||
}
|
||||
|
||||
if (Array.isArray(param)) {
|
||||
return `[${param.map(queryParamToString).join(',')}]`;
|
||||
} else if (typeof param === 'object') {
|
||||
const body = [];
|
||||
for (const [key, value] of Object.entries(param)) {
|
||||
body.push(`${key}:${queryParamToString(value)}`);
|
||||
}
|
||||
return `{${body.join(',')}}`;
|
||||
} else {
|
||||
throw new TypeError(`Unexpected param type ${typeof param} ${param}`)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user