You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
graph
This commit is contained in:
@@ -1,55 +1,103 @@
|
||||
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 {
|
||||
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: 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;
|
||||
|
Reference in New Issue
Block a user