1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

V5 bringing RESP3, Sentinel and TypeMapping to node-redis

RESP3 Support
   - Some commands responses in RESP3 aren't stable yet and therefore return an "untyped" ReplyUnion.
 
Sentinel

TypeMapping

Correctly types Multi commands

Note: some API changes to be further documented in v4-to-v5.md
This commit is contained in:
Shaya Potter
2024-10-15 17:46:52 +03:00
committed by GitHub
parent 2fc79bdfb3
commit b2d35c5286
1174 changed files with 45931 additions and 36274 deletions

View File

@@ -1,22 +1,22 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './CONFIG_GET';
import CONFIG_GET from './CONFIG_GET';
describe('CONFIG GET', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('TIMEOUT'),
['GRAPH.CONFIG', 'GET', 'TIMEOUT']
);
});
describe('GRAPH.CONFIG GET', () => {
it('transformArguments', () => {
assert.deepEqual(
CONFIG_GET.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);
testUtils.testWithClient('client.graph.configGet', async client => {
assert.deepEqual(
await client.graph.configGet('TIMEOUT'),
[
'TIMEOUT',
0
]
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,12 +1,15 @@
export const IS_READ_ONLY = true;
import { RedisArgument, TuplesReply, ArrayReply, BlobStringReply, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(configKey: string): Array<string> {
type ConfigItemReply = TuplesReply<[
configKey: BlobStringReply,
value: NumberReply
]>;
export default {
FIRST_KEY_INDEX: undefined,
IS_READ_ONLY: true,
transformArguments(configKey: RedisArgument) {
return ['GRAPH.CONFIG', 'GET', configKey];
}
type ConfigItem = [
configKey: string,
value: number
];
export declare function transformReply(): ConfigItem | Array<ConfigItem>;
},
transformReply: undefined as unknown as () => ConfigItemReply | ArrayReply<ConfigItemReply>
} as const satisfies Command;

View File

@@ -1,19 +1,19 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './CONFIG_SET';
import CONFIG_SET from './CONFIG_SET';
describe('CONFIG SET', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('TIMEOUT', 0),
['GRAPH.CONFIG', 'SET', 'TIMEOUT', '0']
);
});
describe('GRAPH.CONFIG SET', () => {
it('transformArguments', () => {
assert.deepEqual(
CONFIG_SET.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);
testUtils.testWithClient('client.graph.configSet', async client => {
assert.equal(
await client.graph.configSet('TIMEOUT', 0),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,10 +1,15 @@
export function transformArguments(configKey: string, value: number): Array<string> {
return [
'GRAPH.CONFIG',
'SET',
configKey,
value.toString()
];
}
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export declare function transformReply(): 'OK';
export default {
FIRST_KEY_INDEX: undefined,
IS_READ_ONLY: false,
transformArguments(configKey: RedisArgument, value: number) {
return [
'GRAPH.CONFIG',
'SET',
configKey,
value.toString()
];
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

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

View File

@@ -1,7 +1,10 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, BlobStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument) {
return ['GRAPH.DELETE', key];
}
export declare function transformReply(): string;
},
transformReply: undefined as unknown as () => BlobStringReply
} as const satisfies Command;

View File

@@ -1,21 +1,23 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './EXPLAIN';
import EXPLAIN from './EXPLAIN';
describe('EXPLAIN', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'RETURN 0'),
['GRAPH.EXPLAIN', 'key', 'RETURN 0']
);
});
describe('GRAPH.EXPLAIN', () => {
it('transformArguments', () => {
assert.deepEqual(
EXPLAIN.transformArguments('key', 'RETURN 0'),
['GRAPH.EXPLAIN', 'key', 'RETURN 0']
);
});
testUtils.testWithClient('client.graph.explain', async client => {
const [, reply] = await Promise.all([
client.graph.query('key', 'RETURN 0'), // make sure to create a graph first
client.graph.explain('key', 'RETURN 0')
]);
assert.ok(Array.isArray(reply));
assert.ok(!reply.find(x => typeof x !== 'string'));
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.graph.explain', async client => {
const [, reply] = await Promise.all([
client.graph.query('key', 'RETURN 0'), // make sure to create a graph first
client.graph.explain('key', 'RETURN 0')
]);
assert.ok(Array.isArray(reply));
for (const item of reply) {
assert.equal(typeof item, 'string');
}
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,9 +1,10 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, ArrayReply, BlobStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export const IS_READ_ONLY = true;
export function transformArguments(key: string, query: string): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, query: RedisArgument) {
return ['GRAPH.EXPLAIN', key, query];
}
export declare function transformReply(): Array<string>;
},
transformReply: undefined as unknown as () => ArrayReply<BlobStringReply>
} as const satisfies Command;

View File

@@ -1,19 +1,19 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './LIST';
import LIST from './LIST';
describe('LIST', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments(),
['GRAPH.LIST']
);
});
describe('GRAPH.LIST', () => {
it('transformArguments', () => {
assert.deepEqual(
LIST.transformArguments(),
['GRAPH.LIST']
);
});
testUtils.testWithClient('client.graph.list', async client => {
assert.deepEqual(
await client.graph.list(),
[]
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.graph.list', async client => {
assert.deepEqual(
await client.graph.list(),
[]
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,7 +1,10 @@
export const IS_READ_ONLY = true;
import { ArrayReply, BlobStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(): Array<string> {
export default {
FIRST_KEY_INDEX: undefined,
IS_READ_ONLY: true,
transformArguments() {
return ['GRAPH.LIST'];
}
export declare function transformReply(): Array<string>;
},
transformReply: undefined as unknown as () => ArrayReply<BlobStringReply>
} as const satisfies Command;

View File

@@ -1,18 +1,20 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './PROFILE';
import PROFILE from './PROFILE';
describe('PROFILE', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'RETURN 0'),
['GRAPH.PROFILE', 'key', 'RETURN 0']
);
});
describe('GRAPH.PROFILE', () => {
it('transformArguments', () => {
assert.deepEqual(
PROFILE.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);
testUtils.testWithClient('client.graph.profile', async client => {
const reply = await client.graph.profile('key', 'RETURN 0');
assert.ok(Array.isArray(reply));
for (const item of reply) {
assert.equal(typeof item, 'string');
}
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,9 +1,10 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, ArrayReply, BlobStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export const IS_READ_ONLY = true;
export function transformArguments(key: string, query: string): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, query: RedisArgument) {
return ['GRAPH.PROFILE', key, query];
}
export declare function transformReply(): Array<string>;
},
transformReply: undefined as unknown as () => ArrayReply<BlobStringReply>
} as const satisfies Command;

View File

@@ -1,17 +1,63 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './QUERY';
import QUERY from './QUERY';
describe('QUERY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'query'),
['GRAPH.QUERY', 'key', 'query']
);
describe('GRAPH.QUERY', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
QUERY.transformArguments('key', 'query'),
['GRAPH.QUERY', 'key', 'query']
);
});
testUtils.testWithClient('client.graph.query', async client => {
const { data } = await client.graph.query('key', 'RETURN 0');
assert.deepEqual(data, [[0]]);
}, GLOBAL.SERVERS.OPEN);
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: Symbol()
}
})
}, 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]]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,55 +1,102 @@
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands/index';
import { pushQueryArguments, QueryOptionsBackwardCompatible } from '.';
import { RedisArgument, ArrayReply, BlobStringReply, NumberReply, NullReply, TuplesReply, UnwrapReply, Command } 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
);
}
type Data = ArrayReply<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 = [
headers: Headers,
data: Data,
metadata: Metadata
type QueryRawReply = TuplesReply<[
headers: Headers,
data: Data,
metadata: Metadata
] | [
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 {
return reply.length === 1 ? {
headers: undefined,
data: undefined,
metadata: reply[0]
} : {
headers: reply[0],
data: reply[1],
metadata: reply[2]
};
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: UnwrapReply<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 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './RO_QUERY';
import RO_QUERY from './RO_QUERY';
describe('RO_QUERY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'query'),
['GRAPH.RO_QUERY', 'key', 'query']
);
});
describe('GRAPH.RO_QUERY', () => {
it('transformArguments', () => {
assert.deepEqual(
RO_QUERY.transformArguments('key', 'query'),
['GRAPH.RO_QUERY', 'key', 'query']
);
});
testUtils.testWithClient('client.graph.roQuery', async client => {
const [, { data }] = await Promise.all([
client.graph.query('key', 'RETURN 0'), // make sure to create a graph first
client.graph.roQuery('key', 'RETURN 0')
]);
assert.deepEqual(data, [[0]]);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.graph.roQuery', async client => {
const [, { data }] = await Promise.all([
client.graph.query('key', 'RETURN 0'), // make sure to create a graph first
client.graph.roQuery('key', 'RETURN 0')
]);
assert.deepEqual(data, [[0]]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -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;

View File

@@ -1,18 +1,20 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './SLOWLOG';
import SLOWLOG from './SLOWLOG';
describe('SLOWLOG', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key'),
['GRAPH.SLOWLOG', 'key']
);
});
describe('GRAPH.SLOWLOG', () => {
it('transformArguments', () => {
assert.deepEqual(
SLOWLOG.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);
testUtils.testWithClient('client.graph.slowLog', async client => {
const [, reply] = await Promise.all([
client.graph.query('key', 'RETURN 1'),
client.graph.slowLog('key')
]);
assert.equal(reply.length, 1);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,30 +1,27 @@
export const IS_READ_ONLY = true;
import { RedisArgument, ArrayReply, TuplesReply, BlobStringReply, UnwrapReply, 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: UnwrapReply<SlowLogRawReply>) {
return reply.map(log => {
const [timestamp, command, query, took] = log as unknown as UnwrapReply<typeof log>;
return {
timestamp: Number(timestamp),
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 * 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,
configGet: CONFIG_GET,
CONFIG_SET,
configSet: CONFIG_SET,
DELETE,
delete: DELETE,
EXPLAIN,
explain: EXPLAIN,
LIST,
list: LIST,
PROFILE,
profile: PROFILE,
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,
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}`)
}
}
CONFIG_GET,
configGet: CONFIG_GET,
CONFIG_SET,
configSet: CONFIG_SET,
DELETE,
delete: DELETE,
EXPLAIN,
explain: EXPLAIN,
LIST,
list: LIST,
PROFILE,
profile: PROFILE,
QUERY,
query: QUERY,
RO_QUERY,
roQuery: RO_QUERY,
SLOWLOG,
slowLog: SLOWLOG
} as const satisfies RedisCommands;