1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00
This commit is contained in:
Leibale
2023-07-10 12:20:40 -04:00
parent a4aa903987
commit 3fa7717a7d
20 changed files with 648 additions and 711 deletions

View File

@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import CONFIG_GET from './CONFIG_GET'; import CONFIG_GET from './CONFIG_GET';
describe('CONFIG GET', () => { describe('GRAPH.CONFIG GET', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
CONFIG_GET.transformArguments('TIMEOUT'), CONFIG_GET.transformArguments('TIMEOUT'),

View File

@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import CONFIG_SET from './CONFIG_SET'; import CONFIG_SET from './CONFIG_SET';
describe('CONFIG SET', () => { describe('GRAPH.CONFIG SET', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
CONFIG_SET.transformArguments('TIMEOUT', 0), CONFIG_SET.transformArguments('TIMEOUT', 0),

View File

@@ -11,5 +11,5 @@ export default {
value.toString() value.toString()
]; ];
}, },
transformReply: undefined as unknown as () => SimpleStringReply transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command; } as const satisfies Command;

View File

@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import DELETE from './DELETE'; import DELETE from './DELETE';
describe('', () => { describe('GRAPH.DELETE', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
DELETE.transformArguments('key'), DELETE.transformArguments('key'),
@@ -11,11 +11,11 @@ describe('', () => {
}); });
testUtils.testWithClient('client.graph.delete', async client => { testUtils.testWithClient('client.graph.delete', async client => {
await client.graph.query('key', 'RETURN 1'); const [, reply] = await Promise.all([
client.graph.query('key', 'RETURN 1'),
client.graph.delete('key')
]);
assert.equal( assert.equal(reply, 'OK');
typeof await client.graph.delete('key'),
'string'
);
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -6,5 +6,5 @@ export default {
transformArguments(key: RedisArgument) { transformArguments(key: RedisArgument) {
return ['GRAPH.DELETE', key]; return ['GRAPH.DELETE', key];
}, },
transformReply: undefined as unknown as () => SimpleStringReply transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command; } as const satisfies Command;

View File

@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import EXPLAIN from './EXPLAIN'; import EXPLAIN from './EXPLAIN';
describe('EXPLAIN', () => { describe('GRAPH.EXPLAIN', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
EXPLAIN.transformArguments('key', 'RETURN 0'), EXPLAIN.transformArguments('key', 'RETURN 0'),
@@ -16,6 +16,8 @@ describe('EXPLAIN', () => {
client.graph.explain('key', 'RETURN 0') client.graph.explain('key', 'RETURN 0')
]); ]);
assert.ok(Array.isArray(reply)); assert.ok(Array.isArray(reply));
assert.ok(!reply.find(x => typeof x !== 'string')); for (const item of reply) {
assert.equal(typeof item, 'string');
}
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import LIST from './LIST'; import LIST from './LIST';
describe('LIST', () => { describe('GRAPH.LIST', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
LIST.transformArguments(), LIST.transformArguments(),

View File

@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import PROFILE from './PROFILE'; import PROFILE from './PROFILE';
describe('PROFILE', () => { describe('GRAPH.PROFILE', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
PROFILE.transformArguments('key', 'RETURN 0'), PROFILE.transformArguments('key', 'RETURN 0'),
@@ -13,6 +13,8 @@ describe('PROFILE', () => {
testUtils.testWithClient('client.graph.profile', async client => { testUtils.testWithClient('client.graph.profile', async client => {
const reply = await client.graph.profile('key', 'RETURN 0'); const reply = await client.graph.profile('key', 'RETURN 0');
assert.ok(Array.isArray(reply)); assert.ok(Array.isArray(reply));
assert.ok(!reply.find(x => typeof x !== 'string')); for (const item of reply) {
assert.equal(typeof item, 'string');
}
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -2,14 +2,60 @@ import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import QUERY from './QUERY'; import QUERY from './QUERY';
describe('QUERY', () => { describe('GRAPH.QUERY', () => {
it('transformArguments', () => { describe('transformArguments', () => {
assert.deepEqual( it('simple', () => {
QUERY.transformArguments('key', 'query'), assert.deepEqual(
['GRAPH.QUERY', 'key', 'query'] QUERY.transformArguments('key', 'query'),
); ['GRAPH.QUERY', 'key', 'query']
);
});
describe('params', () => {
it('all types', () => {
assert.deepEqual(
QUERY.transformArguments('key', 'query', {
params: {
null: null,
string: '"\\',
number: 0,
boolean: false,
array: [0],
object: {a: 0}
}
}),
['GRAPH.QUERY', 'key', 'CYPHER null=null string="\\"\\\\" number=0 boolean=false array=[0] object={a:0} query']
);
});
it('TypeError', () => {
assert.throws(() => {
QUERY.transformArguments('key', 'query', {
params: {
a: Buffer.from('a')
}
})
}, TypeError);
});
});
it('TIMEOUT', () => {
assert.deepEqual(
QUERY.transformArguments('key', 'query', {
TIMEOUT: 1
}),
['GRAPH.QUERY', 'key', 'query', 'TIMEOUT', '1']
);
});
it('compact', () => {
assert.deepEqual(
QUERY.transformArguments('key', 'query', undefined, true),
['GRAPH.QUERY', 'key', 'query', '--compact']
);
});
}); });
testUtils.testWithClient('client.graph.query', async client => { testUtils.testWithClient('client.graph.query', async client => {
const { data } = await client.graph.query('key', 'RETURN 0'); const { data } = await client.graph.query('key', 'RETURN 0');
assert.deepEqual(data, [[0]]); assert.deepEqual(data, [[0]]);

View File

@@ -1,55 +1,103 @@
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands/index'; import { RedisArgument, Command, ArrayReply, BlobStringReply, NumberReply, NullReply, TuplesReply } from '@redis/client/dist/lib/RESP/types';
import { pushQueryArguments, QueryOptionsBackwardCompatible } from '.';
export const FIRST_KEY_INDEX = 1; type Headers = ArrayReply<BlobStringReply>;
export function transformArguments( // TODO: cannot use `ArrayReply` due to circular reference
graph: RedisCommandArgument, type Data = Array<BlobStringReply | NumberReply | NullReply | Data>;
query: RedisCommandArgument,
options?: QueryOptionsBackwardCompatible,
compact?: boolean
): RedisCommandArguments {
return pushQueryArguments(
['GRAPH.QUERY'],
graph,
query,
options,
compact
);
}
type Headers = Array<string>; type Metadata = ArrayReply<BlobStringReply>;
type Data = Array<string | number | null | Data>; type QueryRawReply = TuplesReply<[
type Metadata = Array<string>;
type QueryRawReply = [
headers: Headers, headers: Headers,
data: Data, data: Data,
metadata: Metadata metadata: Metadata
] | [ ] | [
metadata: Metadata metadata: Metadata
]; ]>;
export type QueryReply = { type QueryParam = null | string | number | boolean | QueryParams | Array<QueryParam>;
headers: undefined;
data: undefined; type QueryParams = {
metadata: Metadata; [key: string]: QueryParam;
} | {
headers: Headers;
data: Data;
metadata: Metadata;
}; };
export function transformReply(reply: QueryRawReply): QueryReply { export interface QueryOptions {
return reply.length === 1 ? { params?: QueryParams;
headers: undefined, TIMEOUT?: number;
data: undefined,
metadata: reply[0]
} : {
headers: reply[0],
data: reply[1],
metadata: reply[2]
};
} }
export function transformQueryArguments(
command: RedisArgument,
graph: RedisArgument,
query: RedisArgument,
options?: QueryOptions,
compact?: boolean
) {
const args = [
command,
graph,
options?.params ?
`CYPHER ${queryParamsToString(options.params)} ${query}` :
query
];
if (options?.TIMEOUT !== undefined) {
args.push('TIMEOUT', options.TIMEOUT.toString());
}
if (compact) {
args.push('--compact');
}
return args;
}
function queryParamsToString(params: QueryParams) {
return Object.entries(params)
.map(([key, value]) => `${key}=${queryParamToString(value)}`)
.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}`)
}
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments: transformQueryArguments.bind(undefined, 'GRAPH.QUERY'),
transformReply(reply: QueryRawReply) {
return reply.length === 1 ? {
headers: undefined,
data: undefined,
metadata: reply[0]
} : {
headers: reply[0],
data: reply[1],
metadata: reply[2]
};
}
} as const satisfies Command;

View File

@@ -1,20 +1,20 @@
import { strict as assert } from 'assert'; import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './RO_QUERY'; import RO_QUERY from './RO_QUERY';
describe('RO_QUERY', () => { describe('GRAPH.RO_QUERY', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
transformArguments('key', 'query'), RO_QUERY.transformArguments('key', 'query'),
['GRAPH.RO_QUERY', 'key', 'query'] ['GRAPH.RO_QUERY', 'key', 'query']
); );
}); });
testUtils.testWithClient('client.graph.roQuery', async client => { testUtils.testWithClient('client.graph.roQuery', async client => {
const [, { data }] = await Promise.all([ const [, { data }] = await Promise.all([
client.graph.query('key', 'RETURN 0'), // make sure to create a graph first client.graph.query('key', 'RETURN 0'), // make sure to create a graph first
client.graph.roQuery('key', 'RETURN 0') client.graph.roQuery('key', 'RETURN 0')
]); ]);
assert.deepEqual(data, [[0]]); assert.deepEqual(data, [[0]]);
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -1,23 +1,9 @@
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands'; import { Command } from '@redis/client/dist/lib/RESP/types';
import { pushQueryArguments, QueryOptionsBackwardCompatible } from '.'; import QUERY, { transformQueryArguments } from './QUERY';
export { FIRST_KEY_INDEX } from './QUERY'; export default {
FIRST_KEY_INDEX: QUERY.FIRST_KEY_INDEX,
export const IS_READ_ONLY = true; IS_READ_ONLY: true,
transformArguments: transformQueryArguments.bind(undefined, 'GRAPH.RO_QUERY'),
export function transformArguments( transformReply: QUERY.transformReply
graph: RedisCommandArgument, } as const satisfies Command;
query: RedisCommandArgument,
options?: QueryOptionsBackwardCompatible,
compact?: boolean
): RedisCommandArguments {
return pushQueryArguments(
['GRAPH.RO_QUERY'],
graph,
query,
options,
compact
);
}
export { transformReply } from './QUERY';

View File

@@ -1,8 +1,8 @@
import { strict as assert } from 'assert'; import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './SLOWLOG'; import SLOWLOG from './SLOWLOG';
describe('SLOWLOG', () => { describe('GRAPH.SLOWLOG', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
transformArguments('key'), transformArguments('key'),
@@ -11,8 +11,10 @@ describe('SLOWLOG', () => {
}); });
testUtils.testWithClient('client.graph.slowLog', async client => { testUtils.testWithClient('client.graph.slowLog', async client => {
await client.graph.query('key', 'RETURN 1'); const [, reply] = await Promise.all([
const reply = await client.graph.slowLog('key'); client.graph.query('key', 'RETURN 1'),
client.graph.slowLog('key')
]);
assert.equal(reply.length, 1); assert.equal(reply.length, 1);
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -1,30 +1,24 @@
export const IS_READ_ONLY = true; import { RedisArgument, ArrayReply, TuplesReply, BlobStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export const FIRST_KEY_INDEX = 1; type SlowLogRawReply = ArrayReply<TuplesReply<[
timestamp: BlobStringReply,
command: BlobStringReply,
query: BlobStringReply,
took: BlobStringReply
]>>;
export function transformArguments(key: string) { export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument) {
return ['GRAPH.SLOWLOG', key]; return ['GRAPH.SLOWLOG', key];
} },
transformReply(reply: SlowLogRawReply) {
type SlowLogRawReply = Array<[ return reply.map(([timestamp, command, query, took]) => ({
timestamp: string, timestamp: Number(timestamp),
command: string, command,
query: string, query,
took: string took: Number(took)
]>;
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)
})); }));
} }
} as const satisfies Command;

View File

@@ -1,62 +0,0 @@
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']
);
});
});

View File

@@ -1,114 +1,31 @@
import * as CONFIG_GET from './CONFIG_GET'; import type { RedisCommands } from '@redis/client/dist/lib/RESP/types';
import * as CONFIG_SET from './CONFIG_SET';; import CONFIG_GET from './CONFIG_GET';
import * as DELETE from './DELETE'; import CONFIG_SET from './CONFIG_SET';;
import * as EXPLAIN from './EXPLAIN'; import DELETE from './DELETE';
import * as LIST from './LIST'; import EXPLAIN from './EXPLAIN';
import * as PROFILE from './PROFILE'; import LIST from './LIST';
import * as QUERY from './QUERY'; import PROFILE from './PROFILE';
import * as RO_QUERY from './RO_QUERY'; import QUERY from './QUERY';
import * as SLOWLOG from './SLOWLOG'; import RO_QUERY from './RO_QUERY';
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands'; // import SLOWLOG from './SLOWLOG';
export default { export default {
CONFIG_GET, CONFIG_GET,
configGet: CONFIG_GET, configGet: CONFIG_GET,
CONFIG_SET, CONFIG_SET,
configSet: CONFIG_SET, configSet: CONFIG_SET,
DELETE, DELETE,
delete: DELETE, delete: DELETE,
EXPLAIN, EXPLAIN,
explain: EXPLAIN, explain: EXPLAIN,
LIST, LIST,
list: LIST, list: LIST,
PROFILE, PROFILE,
profile: PROFILE, profile: PROFILE,
QUERY, QUERY,
query: QUERY, query: QUERY,
RO_QUERY, RO_QUERY,
roQuery: RO_QUERY, roQuery: RO_QUERY,
SLOWLOG, // SLOWLOG,
slowLog: SLOWLOG // slowLog: SLOWLOG
}; } as const satisfies RedisCommands;
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,
options?: QueryOptionsBackwardCompatible,
compact?: boolean
): RedisCommandArguments {
args.push(graph);
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}`)
}
}

View File

@@ -3,146 +3,146 @@ import testUtils, { GLOBAL } from './test-utils';
import Graph from './graph'; import Graph from './graph';
describe('Graph', () => { describe('Graph', () => {
testUtils.testWithClient('null', async client => { testUtils.testWithClient('null', async client => {
const graph = new Graph(client as any, 'graph'), const graph = new Graph(client as any, 'graph'),
{ data } = await graph.query('RETURN null AS key'); { data } = await graph.query('RETURN null AS key');
assert.deepEqual( assert.deepEqual(
data, data,
[{ key: null }] [{ key: null }]
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('string', async client => { testUtils.testWithClient('string', async client => {
const graph = new Graph(client as any, 'graph'), const graph = new Graph(client as any, 'graph'),
{ data } = await graph.query('RETURN "string" AS key'); { data } = await graph.query('RETURN "string" AS key');
assert.deepEqual( assert.deepEqual(
data, data,
[{ key: 'string' }] [{ key: 'string' }]
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('integer', async client => { testUtils.testWithClient('integer', async client => {
const graph = new Graph(client as any, 'graph'), const graph = new Graph(client as any, 'graph'),
{ data } = await graph.query('RETURN 0 AS key'); { data } = await graph.query('RETURN 0 AS key');
assert.deepEqual( assert.deepEqual(
data, data,
[{ key: 0 }] [{ key: 0 }]
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('boolean', async client => { testUtils.testWithClient('boolean', async client => {
const graph = new Graph(client as any, 'graph'), const graph = new Graph(client as any, 'graph'),
{ data } = await graph.query('RETURN false AS key'); { data } = await graph.query('RETURN false AS key');
assert.deepEqual( assert.deepEqual(
data, data,
[{ key: false }] [{ key: false }]
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('double', async client => { testUtils.testWithClient('double', async client => {
const graph = new Graph(client as any, 'graph'), const graph = new Graph(client as any, 'graph'),
{ data } = await graph.query('RETURN 0.1 AS key'); { data } = await graph.query('RETURN 0.1 AS key');
assert.deepEqual( assert.deepEqual(
data, data,
[{ key: 0.1 }] [{ key: 0.1 }]
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('array', async client => { testUtils.testWithClient('array', async client => {
const graph = new Graph(client as any, 'graph'), const graph = new Graph(client as any, 'graph'),
{ data } = await graph.query('RETURN [null] AS key'); { data } = await graph.query('RETURN [null] AS key');
assert.deepEqual( assert.deepEqual(
data, data,
[{ key: [null] }] [{ key: [null] }]
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('edge', async client => { testUtils.testWithClient('edge', async client => {
const graph = new Graph(client as any, 'graph'); const graph = new Graph(client as any, 'graph');
// check with and without metadata cache // check with and without metadata cache
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
const { data } = await graph.query<any>('CREATE ()-[edge :edge]->() RETURN edge'); const { data } = await graph.query<any>('CREATE ()-[edge :edge]->() RETURN edge');
assert.ok(Array.isArray(data)); assert.ok(Array.isArray(data));
assert.equal(data.length, 1); assert.equal(data.length, 1);
assert.equal(typeof data[0].edge.id, 'number'); assert.equal(typeof data[0].edge.id, 'number');
assert.equal(data[0].edge.relationshipType, 'edge'); assert.equal(data[0].edge.relationshipType, 'edge');
assert.equal(typeof data[0].edge.sourceId, 'number'); assert.equal(typeof data[0].edge.sourceId, 'number');
assert.equal(typeof data[0].edge.destinationId, 'number'); assert.equal(typeof data[0].edge.destinationId, 'number');
assert.deepEqual(data[0].edge.properties, {}); assert.deepEqual(data[0].edge.properties, {});
} }
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('node', async client => { testUtils.testWithClient('node', async client => {
const graph = new Graph(client as any, 'graph'); const graph = new Graph(client as any, 'graph');
// check with and without metadata cache // check with and without metadata cache
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
const { data } = await graph.query<any>('CREATE (node :node { p: 0 }) RETURN node'); const { data } = await graph.query<any>('CREATE (node :node { p: 0 }) RETURN node');
assert.ok(Array.isArray(data)); assert.ok(Array.isArray(data));
assert.equal(data.length, 1); assert.equal(data.length, 1);
assert.equal(typeof data[0].node.id, 'number'); assert.equal(typeof data[0].node.id, 'number');
assert.deepEqual(data[0].node.labels, ['node']); assert.deepEqual(data[0].node.labels, ['node']);
assert.deepEqual(data[0].node.properties, { p: 0 }); assert.deepEqual(data[0].node.properties, { p: 0 });
} }
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('path', async client => { testUtils.testWithClient('path', async client => {
const graph = new Graph(client as any, 'graph'), const graph = new Graph(client as any, 'graph'),
[, { data }] = await Promise.all([ [, { data }] = await Promise.all([
await graph.query('CREATE ()-[:edge]->()'), await graph.query('CREATE ()-[:edge]->()'),
await graph.roQuery<any>('MATCH path = ()-[:edge]->() RETURN path') await graph.roQuery<any>('MATCH path = ()-[:edge]->() RETURN path')
]); ]);
assert.ok(Array.isArray(data)); assert.ok(Array.isArray(data));
assert.equal(data.length, 1); assert.equal(data.length, 1);
assert.ok(Array.isArray(data[0].path.nodes)); assert.ok(Array.isArray(data[0].path.nodes));
assert.equal(data[0].path.nodes.length, 2); assert.equal(data[0].path.nodes.length, 2);
for (const node of data[0].path.nodes) { for (const node of data[0].path.nodes) {
assert.equal(typeof node.id, 'number'); assert.equal(typeof node.id, 'number');
assert.deepEqual(node.labels, []); assert.deepEqual(node.labels, []);
assert.deepEqual(node.properties, {}); assert.deepEqual(node.properties, {});
} }
assert.ok(Array.isArray(data[0].path.edges)); assert.ok(Array.isArray(data[0].path.edges));
assert.equal(data[0].path.edges.length, 1); assert.equal(data[0].path.edges.length, 1);
for (const edge of data[0].path.edges) { for (const edge of data[0].path.edges) {
assert.equal(typeof edge.id, 'number'); assert.equal(typeof edge.id, 'number');
assert.equal(edge.relationshipType, 'edge'); assert.equal(edge.relationshipType, 'edge');
assert.equal(typeof edge.sourceId, 'number'); assert.equal(typeof edge.sourceId, 'number');
assert.equal(typeof edge.destinationId, 'number'); assert.equal(typeof edge.destinationId, 'number');
assert.deepEqual(edge.properties, {}); assert.deepEqual(edge.properties, {});
} }
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('map', async client => { testUtils.testWithClient('map', async client => {
const graph = new Graph(client as any, 'graph'), const graph = new Graph(client as any, 'graph'),
{ data } = await graph.query('RETURN { key: "value" } AS map'); { data } = await graph.query('RETURN { key: "value" } AS map');
assert.deepEqual(data, [{ assert.deepEqual(data, [{
map: { map: {
key: 'value' key: 'value'
} }
}]); }]);
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('point', async client => { testUtils.testWithClient('point', async client => {
const graph = new Graph(client as any, 'graph'), const graph = new Graph(client as any, 'graph'),
{ data } = await graph.query('RETURN point({ latitude: 1, longitude: 2 }) AS point'); { data } = await graph.query('RETURN point({ latitude: 1, longitude: 2 }) AS point');
assert.deepEqual(data, [{ assert.deepEqual(data, [{
point: { point: {
latitude: 1, latitude: 1,
longitude: 2 longitude: 2
} }
}]); }]);
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -1,359 +1,359 @@
import { RedisClientType } from '@redis/client/dist/lib/client/index'; import { RedisClientType } from '@redis/client/dist/lib/client/index';
import { RedisCommandArgument, RedisFunctions, RedisScripts } from '@redis/client/dist/lib/commands'; import { RedisArgument, RedisFunctions, RedisScripts } from '@redis/client/dist/lib/RESP/types';
import { QueryOptions } from './commands'; import QUERY, { QueryOptions } from './commands/QUERY';
import { QueryReply } from './commands/QUERY';
interface GraphMetadata { interface GraphMetadata {
labels: Array<string>; labels: Array<string>;
relationshipTypes: Array<string>; relationshipTypes: Array<string>;
propertyKeys: Array<string>; propertyKeys: Array<string>;
} }
// https://github.com/RedisGraph/RedisGraph/blob/master/src/resultset/formatters/resultset_formatter.h#L20 // https://github.com/RedisGraph/RedisGraph/blob/master/src/resultset/formatters/resultset_formatter.h#L20
enum GraphValueTypes { enum GraphValueTypes {
UNKNOWN = 0, UNKNOWN = 0,
NULL = 1, NULL = 1,
STRING = 2, STRING = 2,
INTEGER = 3, INTEGER = 3,
BOOLEAN = 4, BOOLEAN = 4,
DOUBLE = 5, DOUBLE = 5,
ARRAY = 6, ARRAY = 6,
EDGE = 7, EDGE = 7,
NODE = 8, NODE = 8,
PATH = 9, PATH = 9,
MAP = 10, MAP = 10,
POINT = 11 POINT = 11
} }
type GraphEntityRawProperties = Array<[ type GraphEntityRawProperties = Array<[
id: number, id: number,
...value: GraphRawValue ...value: GraphRawValue
]>; ]>;
type GraphEdgeRawValue = [ type GraphEdgeRawValue = [
GraphValueTypes.EDGE, GraphValueTypes.EDGE,
[ [
id: number, id: number,
relationshipTypeId: number, relationshipTypeId: number,
sourceId: number, sourceId: number,
destinationId: number, destinationId: number,
properties: GraphEntityRawProperties properties: GraphEntityRawProperties
] ]
]; ];
type GraphNodeRawValue = [ type GraphNodeRawValue = [
GraphValueTypes.NODE, GraphValueTypes.NODE,
[ [
id: number, id: number,
labelIds: Array<number>, labelIds: Array<number>,
properties: GraphEntityRawProperties properties: GraphEntityRawProperties
] ]
]; ];
type GraphPathRawValue = [ type GraphPathRawValue = [
GraphValueTypes.PATH, GraphValueTypes.PATH,
[ [
nodes: [ nodes: [
GraphValueTypes.ARRAY, GraphValueTypes.ARRAY,
Array<GraphNodeRawValue> Array<GraphNodeRawValue>
], ],
edges: [ edges: [
GraphValueTypes.ARRAY, GraphValueTypes.ARRAY,
Array<GraphEdgeRawValue> Array<GraphEdgeRawValue>
]
] ]
]
]; ];
type GraphMapRawValue = [ type GraphMapRawValue = [
GraphValueTypes.MAP, GraphValueTypes.MAP,
Array<string | GraphRawValue> Array<string | GraphRawValue>
]; ];
type GraphRawValue = [ type GraphRawValue = [
GraphValueTypes.NULL, GraphValueTypes.NULL,
null null
] | [ ] | [
GraphValueTypes.STRING, GraphValueTypes.STRING,
string string
] | [ ] | [
GraphValueTypes.INTEGER, GraphValueTypes.INTEGER,
number number
] | [ ] | [
GraphValueTypes.BOOLEAN, GraphValueTypes.BOOLEAN,
string string
] | [ ] | [
GraphValueTypes.DOUBLE, GraphValueTypes.DOUBLE,
string string
] | [ ] | [
GraphValueTypes.ARRAY, GraphValueTypes.ARRAY,
Array<GraphRawValue> Array<GraphRawValue>
] | GraphEdgeRawValue | GraphNodeRawValue | GraphPathRawValue | GraphMapRawValue | [ ] | GraphEdgeRawValue | GraphNodeRawValue | GraphPathRawValue | GraphMapRawValue | [
GraphValueTypes.POINT, GraphValueTypes.POINT,
[ [
latitude: string, latitude: string,
longitude: string longitude: string
] ]
]; ];
type GraphEntityProperties = Record<string, GraphValue>; type GraphEntityProperties = Record<string, GraphValue>;
interface GraphEdge { interface GraphEdge {
id: number; id: number;
relationshipType: string; relationshipType: string;
sourceId: number; sourceId: number;
destinationId: number; destinationId: number;
properties: GraphEntityProperties; properties: GraphEntityProperties;
} }
interface GraphNode { interface GraphNode {
id: number; id: number;
labels: Array<string>; labels: Array<string>;
properties: GraphEntityProperties; properties: GraphEntityProperties;
} }
interface GraphPath { interface GraphPath {
nodes: Array<GraphNode>; nodes: Array<GraphNode>;
edges: Array<GraphEdge>; edges: Array<GraphEdge>;
} }
type GraphMap = { type GraphMap = {
[key: string]: GraphValue; [key: string]: GraphValue;
}; };
type GraphValue = null | string | number | boolean | Array<GraphValue> | { type GraphValue = null | string | number | boolean | Array<GraphValue> | {
} | GraphEdge | GraphNode | GraphPath | GraphMap | { } | GraphEdge | GraphNode | GraphPath | GraphMap | {
latitude: string; latitude: string;
longitude: string; longitude: string;
}; };
type GraphReply<T> = Omit<QueryReply, 'headers' | 'data'> & { type GraphReply<T> = {
data?: Array<T>; data?: Array<T>;
}; };
type GraphClientType = RedisClientType<{ type GraphClientType = RedisClientType<{
graph: { graph: {
query: typeof import('./commands/QUERY'), query: typeof QUERY,
roQuery: typeof import('./commands/RO_QUERY') roQuery: typeof import('./commands/RO_QUERY').default
} }
}, RedisFunctions, RedisScripts>; }, RedisFunctions, RedisScripts>;
export default class Graph { export default class Graph {
#client: GraphClientType; #client: GraphClientType;
#name: string; #name: string;
#metadata?: GraphMetadata; #metadata?: GraphMetadata;
constructor( constructor(
client: GraphClientType, client: GraphClientType,
name: string name: string
) { ) {
this.#client = client; this.#client = client;
this.#name = name; this.#name = name;
} }
async query<T>( async query<T>(
query: RedisCommandArgument, query: RedisArgument,
options?: QueryOptions options?: QueryOptions
) { ) {
return this.#parseReply<T>( return this.#parseReply<T>(
await this.#client.graph.query( await this.#client.graph.query(
this.#name, this.#name,
query, query,
options, options,
true true
) )
); );
} }
async roQuery<T>( async roQuery<T>(
query: RedisCommandArgument, query: RedisArgument,
options?: QueryOptions options?: QueryOptions
) { ) {
return this.#parseReply<T>( return this.#parseReply<T>(
await this.#client.graph.roQuery( await this.#client.graph.roQuery(
this.#name, this.#name,
query, query,
options, options,
true true
) )
); );
} }
#setMetadataPromise?: Promise<GraphMetadata>; #setMetadataPromise?: Promise<GraphMetadata>;
#updateMetadata(): Promise<GraphMetadata> { #updateMetadata(): Promise<GraphMetadata> {
this.#setMetadataPromise ??= this.#setMetadata() this.#setMetadataPromise ??= this.#setMetadata()
.finally(() => this.#setMetadataPromise = undefined); .finally(() => this.#setMetadataPromise = undefined);
return this.#setMetadataPromise; return this.#setMetadataPromise;
} }
// DO NOT use directly, use #updateMetadata instead // DO NOT use directly, use #updateMetadata instead
async #setMetadata(): Promise<GraphMetadata> { async #setMetadata(): Promise<GraphMetadata> {
const [labels, relationshipTypes, propertyKeys] = await Promise.all([ const [labels, relationshipTypes, propertyKeys] = await Promise.all([
this.#client.graph.roQuery(this.#name, 'CALL db.labels()'), this.#client.graph.roQuery(this.#name, 'CALL db.labels()'),
this.#client.graph.roQuery(this.#name, 'CALL db.relationshipTypes()'), this.#client.graph.roQuery(this.#name, 'CALL db.relationshipTypes()'),
this.#client.graph.roQuery(this.#name, 'CALL db.propertyKeys()') this.#client.graph.roQuery(this.#name, 'CALL db.propertyKeys()')
]); ]);
this.#metadata = { this.#metadata = {
labels: this.#cleanMetadataArray(labels.data as Array<[string]>), labels: this.#cleanMetadataArray(labels.data as Array<[string]>),
relationshipTypes: this.#cleanMetadataArray(relationshipTypes.data as Array<[string]>), relationshipTypes: this.#cleanMetadataArray(relationshipTypes.data as Array<[string]>),
propertyKeys: this.#cleanMetadataArray(propertyKeys.data as Array<[string]>) propertyKeys: this.#cleanMetadataArray(propertyKeys.data as Array<[string]>)
}; };
return this.#metadata; return this.#metadata;
} }
#cleanMetadataArray(arr: Array<[string]>): Array<string> { #cleanMetadataArray(arr: Array<[string]>): Array<string> {
return arr.map(([value]) => value); return arr.map(([value]) => value);
} }
#getMetadata<T extends keyof GraphMetadata>( #getMetadata<T extends keyof GraphMetadata>(
key: T, key: T,
id: number id: number
): GraphMetadata[T][number] | Promise<GraphMetadata[T][number]> { ): GraphMetadata[T][number] | Promise<GraphMetadata[T][number]> {
return this.#metadata?.[key][id] ?? this.#getMetadataAsync(key, id); return this.#metadata?.[key][id] ?? this.#getMetadataAsync(key, id);
} }
// DO NOT use directly, use #getMetadata instead // DO NOT use directly, use #getMetadata instead
async #getMetadataAsync<T extends keyof GraphMetadata>( async #getMetadataAsync<T extends keyof GraphMetadata>(
key: T, key: T,
id: number id: number
): Promise<GraphMetadata[T][number]> { ): Promise<GraphMetadata[T][number]> {
const value = (await this.#updateMetadata())[key][id]; const value = (await this.#updateMetadata())[key][id];
if (value === undefined) throw new Error(`Cannot find value from ${key}[${id}]`); if (value === undefined) throw new Error(`Cannot find value from ${key}[${id}]`);
return value;
}
// TODO: reply type
async #parseReply<T>(reply: any): Promise<GraphReply<T>> {
if (!reply.data) return reply;
const promises: Array<Promise<unknown>> = [],
parsed = {
metadata: reply.metadata,
data: reply.data!.map((row: any) => {
const data: Record<string, GraphValue> = {};
for (let i = 0; i < row.length; i++) {
data[reply.headers[i][1]] = this.#parseValue(row[i], promises);
}
return data as unknown as T;
})
};
if (promises.length) await Promise.all(promises);
return parsed;
}
#parseValue([valueType, value]: GraphRawValue, promises: Array<Promise<unknown>>): GraphValue {
switch (valueType) {
case GraphValueTypes.NULL:
return null;
case GraphValueTypes.STRING:
case GraphValueTypes.INTEGER:
return value; return value;
}
async #parseReply<T>(reply: QueryReply): Promise<GraphReply<T>> { case GraphValueTypes.BOOLEAN:
if (!reply.data) return reply; return value === 'true';
const promises: Array<Promise<unknown>> = [], case GraphValueTypes.DOUBLE:
parsed = { return parseFloat(value);
metadata: reply.metadata,
data: reply.data!.map((row: any) => {
const data: Record<string, GraphValue> = {};
for (let i = 0; i < row.length; i++) {
data[reply.headers[i][1]] = this.#parseValue(row[i], promises);
}
return data as unknown as T; case GraphValueTypes.ARRAY:
}) return value.map(x => this.#parseValue(x, promises));
};
if (promises.length) await Promise.all(promises); case GraphValueTypes.EDGE:
return this.#parseEdge(value, promises);
return parsed; case GraphValueTypes.NODE:
} return this.#parseNode(value, promises);
#parseValue([valueType, value]: GraphRawValue, promises: Array<Promise<unknown>>): GraphValue {
switch (valueType) {
case GraphValueTypes.NULL:
return null;
case GraphValueTypes.STRING:
case GraphValueTypes.INTEGER:
return value;
case GraphValueTypes.BOOLEAN:
return value === 'true';
case GraphValueTypes.DOUBLE:
return parseFloat(value);
case GraphValueTypes.ARRAY:
return value.map(x => this.#parseValue(x, promises));
case GraphValueTypes.EDGE:
return this.#parseEdge(value, promises);
case GraphValueTypes.NODE:
return this.#parseNode(value, promises);
case GraphValueTypes.PATH:
return {
nodes: value[0][1].map(([, node]) => this.#parseNode(node, promises)),
edges: value[1][1].map(([, edge]) => this.#parseEdge(edge, promises))
};
case GraphValueTypes.MAP:
const map: GraphMap = {};
for (let i = 0; i < value.length; i++) {
map[value[i++] as string] = this.#parseValue(value[i] as GraphRawValue, promises);
}
return map;
case GraphValueTypes.POINT:
return {
latitude: parseFloat(value[0]),
longitude: parseFloat(value[1])
};
default:
throw new Error(`unknown scalar type: ${valueType}`);
}
}
#parseEdge([
id,
relationshipTypeId,
sourceId,
destinationId,
properties
]: GraphEdgeRawValue[1], promises: Array<Promise<unknown>>): GraphEdge {
const edge = {
id,
sourceId,
destinationId,
properties: this.#parseProperties(properties, promises)
} as GraphEdge;
const relationshipType = this.#getMetadata('relationshipTypes', relationshipTypeId);
if (relationshipType instanceof Promise) {
promises.push(
relationshipType.then(value => edge.relationshipType = value)
);
} else {
edge.relationshipType = relationshipType;
}
return edge;
}
#parseNode([
id,
labelIds,
properties
]: GraphNodeRawValue[1], promises: Array<Promise<unknown>>): GraphNode {
const labels = new Array<string>(labelIds.length);
for (let i = 0; i < labelIds.length; i++) {
const value = this.#getMetadata('labels', labelIds[i]);
if (value instanceof Promise) {
promises.push(value.then(value => labels[i] = value));
} else {
labels[i] = value;
}
}
case GraphValueTypes.PATH:
return { return {
id, nodes: value[0][1].map(([, node]) => this.#parseNode(node, promises)),
labels, edges: value[1][1].map(([, edge]) => this.#parseEdge(edge, promises))
properties: this.#parseProperties(properties, promises)
}; };
}
#parseProperties(raw: GraphEntityRawProperties, promises: Array<Promise<unknown>>): GraphEntityProperties { case GraphValueTypes.MAP:
const parsed: GraphEntityProperties = {}; const map: GraphMap = {};
for (const [id, type, value] of raw) { for (let i = 0; i < value.length; i++) {
const parsedValue = this.#parseValue([type, value] as GraphRawValue, promises), map[value[i++] as string] = this.#parseValue(value[i] as GraphRawValue, promises);
key = this.#getMetadata('propertyKeys', id);
if (key instanceof Promise) {
promises.push(key.then(key => parsed[key] = parsedValue));
} else {
parsed[key] = parsedValue;
}
} }
return parsed; return map;
case GraphValueTypes.POINT:
return {
latitude: parseFloat(value[0]),
longitude: parseFloat(value[1])
};
default:
throw new Error(`unknown scalar type: ${valueType}`);
} }
}
#parseEdge([
id,
relationshipTypeId,
sourceId,
destinationId,
properties
]: GraphEdgeRawValue[1], promises: Array<Promise<unknown>>): GraphEdge {
const edge = {
id,
sourceId,
destinationId,
properties: this.#parseProperties(properties, promises)
} as GraphEdge;
const relationshipType = this.#getMetadata('relationshipTypes', relationshipTypeId);
if (relationshipType instanceof Promise) {
promises.push(
relationshipType.then(value => edge.relationshipType = value)
);
} else {
edge.relationshipType = relationshipType;
}
return edge;
}
#parseNode([
id,
labelIds,
properties
]: GraphNodeRawValue[1], promises: Array<Promise<unknown>>): GraphNode {
const labels = new Array<string>(labelIds.length);
for (let i = 0; i < labelIds.length; i++) {
const value = this.#getMetadata('labels', labelIds[i]);
if (value instanceof Promise) {
promises.push(value.then(value => labels[i] = value));
} else {
labels[i] = value;
}
}
return {
id,
labels,
properties: this.#parseProperties(properties, promises)
};
}
#parseProperties(raw: GraphEntityRawProperties, promises: Array<Promise<unknown>>): GraphEntityProperties {
const parsed: GraphEntityProperties = {};
for (const [id, type, value] of raw) {
const parsedValue = this.#parseValue([type, value] as GraphRawValue, promises),
key = this.#getMetadata('propertyKeys', id);
if (key instanceof Promise) {
promises.push(key.then(key => parsed[key] = parsedValue));
} else {
parsed[key] = parsedValue;
}
}
return parsed;
}
} }

View File

@@ -2,19 +2,19 @@ import TestUtils from '@redis/test-utils';
import RedisGraph from '.'; import RedisGraph from '.';
export default new TestUtils({ export default new TestUtils({
dockerImageName: 'redislabs/redisgraph', dockerImageName: 'redislabs/redisgraph',
dockerImageVersionArgument: 'redisgraph-version' dockerImageVersionArgument: 'redisgraph-version'
}); });
export const GLOBAL = { export const GLOBAL = {
SERVERS: { SERVERS: {
OPEN: { OPEN: {
serverArguments: ['--loadmodule /usr/lib/redis/modules/redisgraph.so'], serverArguments: ['--loadmodule /usr/lib/redis/modules/redisgraph.so'],
clientOptions: { clientOptions: {
modules: { modules: {
graph: RedisGraph graph: RedisGraph
}
}
} }
}
} }
}
}; };

View File

@@ -5,10 +5,12 @@
"main": "./dist/index.js", "main": "./dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
"files": [ "files": [
"dist/" "dist/",
"!dist/tsconfig.tsbuildinfo"
], ],
"scripts": { "scripts": {
"test": "nyc -r text-summary -r lcov mocha -r source-map-support/register -r ts-node/register './lib/**/*.spec.ts'" }, "test": "nyc -r text-summary -r lcov mocha -r source-map-support/register -r ts-node/register './lib/**/*.spec.ts'"
},
"peerDependencies": { "peerDependencies": {
"@redis/client": "*" "@redis/client": "*"
}, },