You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
Merge branch 'v5' of https://github.com/leibale/node-redis into v5
This commit is contained in:
@@ -18,12 +18,8 @@ describe('MEMORY STATS', () => {
|
||||
assert.equal(typeof memoryStats['replication.backlog'], 'number');
|
||||
assert.equal(typeof memoryStats['clients.slaves'], 'number');
|
||||
assert.equal(typeof memoryStats['clients.normal'], 'number');
|
||||
if (testUtils.isVersionGreaterThan([7])) {
|
||||
assert.equal(typeof memoryStats['cluster.links'], 'number');
|
||||
}
|
||||
assert.equal(typeof memoryStats['aof.buffer'], 'number');
|
||||
assert.equal(typeof memoryStats['lua.caches'], 'number');
|
||||
assert.equal(typeof memoryStats['functions.caches'], 'number');
|
||||
assert.equal(typeof memoryStats['overhead.total'], 'number');
|
||||
assert.equal(typeof memoryStats['keys.count'], 'number');
|
||||
assert.equal(typeof memoryStats['keys.bytes-per-key'], 'number');
|
||||
@@ -41,5 +37,10 @@ describe('MEMORY STATS', () => {
|
||||
assert.equal(typeof memoryStats['rss-overhead.bytes'], 'number');
|
||||
assert.equal(typeof memoryStats['fragmentation'], 'string');
|
||||
assert.equal(typeof memoryStats['fragmentation.bytes'], 'number');
|
||||
|
||||
if (testUtils.isVersionGreaterThan([7])) {
|
||||
assert.equal(typeof memoryStats['cluster.links'], 'number');
|
||||
assert.equal(typeof memoryStats['functions.caches'], 'number');
|
||||
}
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
|
@@ -11,6 +11,7 @@ export type MemoryStatsReply = TuplesToMapReply<[
|
||||
[BlobStringReply<'cluster.links'>, NumberReply],
|
||||
[BlobStringReply<'aof.buffer'>, NumberReply],
|
||||
[BlobStringReply<'lua.caches'>, NumberReply],
|
||||
/** added in 7.0 */
|
||||
[BlobStringReply<'functions.caches'>, NumberReply],
|
||||
[BlobStringReply<'overhead.total'>, NumberReply],
|
||||
[BlobStringReply<'keys.count'>, NumberReply],
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { RedisArgument, SetReply, BlobStringReply, Command } from '../RESP/types';
|
||||
import { RedisArgument, ArrayReply, BlobStringReply, SetReply, Command } from '../RESP/types';
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
@@ -6,5 +6,8 @@ export default {
|
||||
transformArguments(key: RedisArgument) {
|
||||
return ['SMEMBERS', key];
|
||||
},
|
||||
transformReply: undefined as unknown as () => SetReply<BlobStringReply>
|
||||
transformReply: {
|
||||
2: undefined as unknown as () => ArrayReply<BlobStringReply>,
|
||||
3: undefined as unknown as () => SetReply<BlobStringReply>
|
||||
}
|
||||
} as const satisfies Command;
|
||||
|
@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import CONFIG_GET from './CONFIG_GET';
|
||||
|
||||
describe('CONFIG GET', () => {
|
||||
describe('GRAPH.CONFIG GET', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
CONFIG_GET.transformArguments('TIMEOUT'),
|
||||
|
@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import CONFIG_SET from './CONFIG_SET';
|
||||
|
||||
describe('CONFIG SET', () => {
|
||||
describe('GRAPH.CONFIG SET', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
CONFIG_SET.transformArguments('TIMEOUT', 0),
|
||||
|
@@ -11,5 +11,5 @@ export default {
|
||||
value.toString()
|
||||
];
|
||||
},
|
||||
transformReply: undefined as unknown as () => SimpleStringReply
|
||||
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
|
||||
} as const satisfies Command;
|
||||
|
@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import DELETE from './DELETE';
|
||||
|
||||
describe('', () => {
|
||||
describe('GRAPH.DELETE', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
DELETE.transformArguments('key'),
|
||||
@@ -11,11 +11,11 @@ describe('', () => {
|
||||
});
|
||||
|
||||
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(
|
||||
typeof await client.graph.delete('key'),
|
||||
'string'
|
||||
);
|
||||
assert.equal(reply, 'OK');
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
|
@@ -6,5 +6,5 @@ export default {
|
||||
transformArguments(key: RedisArgument) {
|
||||
return ['GRAPH.DELETE', key];
|
||||
},
|
||||
transformReply: undefined as unknown as () => SimpleStringReply
|
||||
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
|
||||
} as const satisfies Command;
|
||||
|
@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import EXPLAIN from './EXPLAIN';
|
||||
|
||||
describe('EXPLAIN', () => {
|
||||
describe('GRAPH.EXPLAIN', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
EXPLAIN.transformArguments('key', 'RETURN 0'),
|
||||
@@ -16,6 +16,8 @@ describe('EXPLAIN', () => {
|
||||
client.graph.explain('key', 'RETURN 0')
|
||||
]);
|
||||
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);
|
||||
});
|
||||
|
@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import LIST from './LIST';
|
||||
|
||||
describe('LIST', () => {
|
||||
describe('GRAPH.LIST', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
LIST.transformArguments(),
|
||||
|
@@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import PROFILE from './PROFILE';
|
||||
|
||||
describe('PROFILE', () => {
|
||||
describe('GRAPH.PROFILE', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
PROFILE.transformArguments('key', 'RETURN 0'),
|
||||
@@ -13,6 +13,8 @@ describe('PROFILE', () => {
|
||||
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'));
|
||||
for (const item of reply) {
|
||||
assert.equal(typeof item, 'string');
|
||||
}
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
|
@@ -2,14 +2,60 @@ import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import QUERY from './QUERY';
|
||||
|
||||
describe('QUERY', () => {
|
||||
it('transformArguments', () => {
|
||||
describe('GRAPH.QUERY', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('simple', () => {
|
||||
assert.deepEqual(
|
||||
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 => {
|
||||
const { data } = await client.graph.query('key', 'RETURN 0');
|
||||
assert.deepEqual(data, [[0]]);
|
||||
|
@@ -1,48 +1,95 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands/index';
|
||||
import { pushQueryArguments, QueryOptionsBackwardCompatible } from '.';
|
||||
import { RedisArgument, Command, ArrayReply, BlobStringReply, NumberReply, NullReply, TuplesReply } from '@redis/client/dist/lib/RESP/types';
|
||||
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
type Headers = ArrayReply<BlobStringReply>;
|
||||
|
||||
export function transformArguments(
|
||||
graph: RedisCommandArgument,
|
||||
query: RedisCommandArgument,
|
||||
options?: QueryOptionsBackwardCompatible,
|
||||
compact?: boolean
|
||||
): RedisCommandArguments {
|
||||
return pushQueryArguments(
|
||||
['GRAPH.QUERY'],
|
||||
graph,
|
||||
query,
|
||||
options,
|
||||
compact
|
||||
);
|
||||
}
|
||||
// TODO: cannot use `ArrayReply` due to circular reference
|
||||
type Data = Array<BlobStringReply | NumberReply | NullReply | Data>;
|
||||
|
||||
type Headers = Array<string>;
|
||||
type Metadata = ArrayReply<BlobStringReply>;
|
||||
|
||||
type Data = Array<string | number | null | Data>;
|
||||
|
||||
type Metadata = Array<string>;
|
||||
|
||||
type QueryRawReply = [
|
||||
type QueryRawReply = TuplesReply<[
|
||||
headers: Headers,
|
||||
data: Data,
|
||||
metadata: Metadata
|
||||
] | [
|
||||
metadata: Metadata
|
||||
];
|
||||
]>;
|
||||
|
||||
export type QueryReply = {
|
||||
headers: undefined;
|
||||
data: undefined;
|
||||
metadata: Metadata;
|
||||
} | {
|
||||
headers: Headers;
|
||||
data: Data;
|
||||
metadata: Metadata;
|
||||
type QueryParam = null | string | number | boolean | QueryParams | Array<QueryParam>;
|
||||
|
||||
type QueryParams = {
|
||||
[key: string]: QueryParam;
|
||||
};
|
||||
|
||||
export function transformReply(reply: QueryRawReply): QueryReply {
|
||||
export interface QueryOptions {
|
||||
params?: QueryParams;
|
||||
TIMEOUT?: number;
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -53,3 +100,4 @@ export function transformReply(reply: QueryRawReply): QueryReply {
|
||||
metadata: reply[2]
|
||||
};
|
||||
}
|
||||
} as const satisfies Command;
|
||||
|
@@ -1,11 +1,11 @@
|
||||
import { strict as assert } from 'assert';
|
||||
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', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key', 'query'),
|
||||
RO_QUERY.transformArguments('key', 'query'),
|
||||
['GRAPH.RO_QUERY', 'key', 'query']
|
||||
);
|
||||
});
|
||||
|
@@ -1,23 +1,9 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
|
||||
import { pushQueryArguments, QueryOptionsBackwardCompatible } from '.';
|
||||
import { Command } from '@redis/client/dist/lib/RESP/types';
|
||||
import QUERY, { transformQueryArguments } from './QUERY';
|
||||
|
||||
export { FIRST_KEY_INDEX } from './QUERY';
|
||||
|
||||
export const IS_READ_ONLY = true;
|
||||
|
||||
export function transformArguments(
|
||||
graph: RedisCommandArgument,
|
||||
query: RedisCommandArgument,
|
||||
options?: QueryOptionsBackwardCompatible,
|
||||
compact?: boolean
|
||||
): RedisCommandArguments {
|
||||
return pushQueryArguments(
|
||||
['GRAPH.RO_QUERY'],
|
||||
graph,
|
||||
query,
|
||||
options,
|
||||
compact
|
||||
);
|
||||
}
|
||||
|
||||
export { transformReply } from './QUERY';
|
||||
export default {
|
||||
FIRST_KEY_INDEX: QUERY.FIRST_KEY_INDEX,
|
||||
IS_READ_ONLY: true,
|
||||
transformArguments: transformQueryArguments.bind(undefined, 'GRAPH.RO_QUERY'),
|
||||
transformReply: QUERY.transformReply
|
||||
} as const satisfies Command;
|
||||
|
@@ -1,8 +1,8 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL } from '../test-utils';
|
||||
import { transformArguments } from './SLOWLOG';
|
||||
import SLOWLOG from './SLOWLOG';
|
||||
|
||||
describe('SLOWLOG', () => {
|
||||
describe('GRAPH.SLOWLOG', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('key'),
|
||||
@@ -11,8 +11,10 @@ describe('SLOWLOG', () => {
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.graph.slowLog', async client => {
|
||||
await client.graph.query('key', 'RETURN 1');
|
||||
const reply = await client.graph.slowLog('key');
|
||||
const [, reply] = await Promise.all([
|
||||
client.graph.query('key', 'RETURN 1'),
|
||||
client.graph.slowLog('key')
|
||||
]);
|
||||
assert.equal(reply.length, 1);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
|
@@ -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];
|
||||
}
|
||||
|
||||
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),
|
||||
},
|
||||
transformReply(reply: SlowLogRawReply) {
|
||||
return reply.map(([timestamp, command, query, took]) => ({
|
||||
timestamp: Number(timestamp),
|
||||
command,
|
||||
query,
|
||||
took: Number(took)
|
||||
}));
|
||||
}
|
||||
} as const satisfies Command;
|
||||
|
@@ -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']
|
||||
);
|
||||
});
|
||||
});
|
@@ -1,13 +1,13 @@
|
||||
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 from './QUERY';
|
||||
import * as RO_QUERY from './RO_QUERY';
|
||||
import * as SLOWLOG from './SLOWLOG';
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
|
||||
import type { RedisCommands } from '@redis/client/dist/lib/RESP/types';
|
||||
import CONFIG_GET from './CONFIG_GET';
|
||||
import CONFIG_SET from './CONFIG_SET';;
|
||||
import DELETE from './DELETE';
|
||||
import EXPLAIN from './EXPLAIN';
|
||||
import LIST from './LIST';
|
||||
import PROFILE from './PROFILE';
|
||||
import QUERY from './QUERY';
|
||||
import RO_QUERY from './RO_QUERY';
|
||||
// import SLOWLOG from './SLOWLOG';
|
||||
|
||||
export default {
|
||||
CONFIG_GET,
|
||||
@@ -26,89 +26,6 @@ export default {
|
||||
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,
|
||||
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}`)
|
||||
}
|
||||
}
|
||||
// SLOWLOG,
|
||||
// slowLog: SLOWLOG
|
||||
} as const satisfies RedisCommands;
|
||||
|
@@ -1,7 +1,6 @@
|
||||
import { RedisClientType } from '@redis/client/dist/lib/client/index';
|
||||
import { RedisCommandArgument, RedisFunctions, RedisScripts } from '@redis/client/dist/lib/commands';
|
||||
import { QueryOptions } from './commands';
|
||||
import { QueryReply } from './commands/QUERY';
|
||||
import { RedisArgument, RedisFunctions, RedisScripts } from '@redis/client/dist/lib/RESP/types';
|
||||
import QUERY, { QueryOptions } from './commands/QUERY';
|
||||
|
||||
interface GraphMetadata {
|
||||
labels: Array<string>;
|
||||
@@ -126,14 +125,14 @@ type GraphValue = null | string | number | boolean | Array<GraphValue> | {
|
||||
longitude: string;
|
||||
};
|
||||
|
||||
type GraphReply<T> = Omit<QueryReply, 'headers' | 'data'> & {
|
||||
type GraphReply<T> = {
|
||||
data?: Array<T>;
|
||||
};
|
||||
|
||||
type GraphClientType = RedisClientType<{
|
||||
graph: {
|
||||
query: typeof import('./commands/QUERY'),
|
||||
roQuery: typeof import('./commands/RO_QUERY')
|
||||
query: typeof QUERY,
|
||||
roQuery: typeof import('./commands/RO_QUERY').default
|
||||
}
|
||||
}, RedisFunctions, RedisScripts>;
|
||||
|
||||
@@ -151,7 +150,7 @@ export default class Graph {
|
||||
}
|
||||
|
||||
async query<T>(
|
||||
query: RedisCommandArgument,
|
||||
query: RedisArgument,
|
||||
options?: QueryOptions
|
||||
) {
|
||||
return this.#parseReply<T>(
|
||||
@@ -165,7 +164,7 @@ export default class Graph {
|
||||
}
|
||||
|
||||
async roQuery<T>(
|
||||
query: RedisCommandArgument,
|
||||
query: RedisArgument,
|
||||
options?: QueryOptions
|
||||
) {
|
||||
return this.#parseReply<T>(
|
||||
@@ -224,7 +223,8 @@ export default class Graph {
|
||||
return value;
|
||||
}
|
||||
|
||||
async #parseReply<T>(reply: QueryReply): Promise<GraphReply<T>> {
|
||||
// TODO: reply type
|
||||
async #parseReply<T>(reply: any): Promise<GraphReply<T>> {
|
||||
if (!reply.data) return reply;
|
||||
|
||||
const promises: Array<Promise<unknown>> = [],
|
||||
|
@@ -5,10 +5,12 @@
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/"
|
||||
"dist/",
|
||||
"!dist/tsconfig.tsbuildinfo"
|
||||
],
|
||||
"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": {
|
||||
"@redis/client": "*"
|
||||
},
|
||||
|
Reference in New Issue
Block a user