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

fix GEO* commands

This commit is contained in:
Leibale
2023-05-08 11:04:22 +03:00
parent d2e244a77d
commit 6a0b4db4f7
37 changed files with 1115 additions and 990 deletions

View File

@@ -1,17 +1,98 @@
// import { RedisCommandArgument, RedisCommandArguments } from '.';
// import { GeoSearchFrom, GeoSearchBy, GeoSearchOptions, pushGeoSearchArguments } from './generic-transformers';
import { RedisArgument, CommandArguments, ArrayReply, BlobStringReply, Command } from '../RESP/types';
// export const FIRST_KEY_INDEX = 1;
export type GeoUnits = 'm' | 'km' | 'mi' | 'ft';
// export const IS_READ_ONLY = true;
export interface GeoCoordinates {
longitude: RedisArgument | number;
latitude: RedisArgument | number;
}
// export function transformArguments(
// key: RedisCommandArgument,
// from: GeoSearchFrom,
// by: GeoSearchBy,
// options?: GeoSearchOptions
// ): RedisCommandArguments {
// return pushGeoSearchArguments(['GEOSEARCH'], key, from, by, options);
// }
export type GeoSearchFrom = RedisArgument | GeoCoordinates;
// export declare function transformReply(): Array<RedisCommandArgument>;
export interface GeoSearchByRadius {
radius: number;
unit: GeoUnits;
}
export interface GeoSearchByBox {
width: number;
height: number;
unit: GeoUnits;
}
export type GeoSearchBy = GeoSearchByRadius | GeoSearchByBox;
export function pushGeoSearchArguments(
args: CommandArguments,
key: RedisArgument,
from: GeoSearchFrom,
by: GeoSearchBy,
options?: GeoSearchOptions
) {
args.push(key);
if (typeof from === 'string' || from instanceof Buffer) {
args.push('FROMMEMBER', from);
} else {
args.push('FROMLONLAT', from.longitude.toString(), from.latitude.toString());
}
if ('radius' in by) {
args.push('BYRADIUS', by.radius.toString(), by.unit);
} else {
args.push('BYBOX', by.width.toString(), by.height.toString(), by.unit);
}
if (options?.SORT) {
args.push(options.SORT);
}
pushGeoSearchOptions(args, options);
return args;
}
export type GeoCountArgument = number | {
value: number;
ANY?: boolean;
};
export interface GeoSearchOptions {
SORT?: 'ASC' | 'DESC';
COUNT?: GeoCountArgument;
}
export function pushGeoSearchOptions(
args: CommandArguments,
options?: GeoSearchOptions
) {
if (options?.SORT) {
args.push(options.SORT);
}
if (options?.COUNT) {
if (typeof options.COUNT === 'number') {
args.push('COUNT', options.COUNT.toString());
} else {
args.push('COUNT', options.COUNT.value.toString());
if (options.COUNT.ANY) {
args.push('ANY');
}
}
}
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(
key: RedisArgument,
from: GeoSearchFrom,
by: GeoSearchBy,
options?: GeoSearchOptions
) {
return pushGeoSearchArguments(['GEOSEARCH'], key, from, by, options);
},
transformReply: undefined as unknown as () => ArrayReply<BlobStringReply>
} as const satisfies Command;