You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
geo
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { RedisArgument, BlobStringReply, NullReply, Command } from '../RESP/types';
|
||||
import { GeoUnits } from './generic-transformers';
|
||||
import { GeoUnits } from './GEOSEARCH';
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
|
@@ -178,195 +178,6 @@ export function transformLMPopArguments(
|
||||
return args;
|
||||
}
|
||||
|
||||
type GeoCountArgument = number | {
|
||||
value: number;
|
||||
ANY?: true
|
||||
};
|
||||
|
||||
export function pushGeoCountArgument(
|
||||
args: CommandArguments,
|
||||
count: GeoCountArgument | undefined
|
||||
): CommandArguments {
|
||||
if (typeof count === 'number') {
|
||||
args.push('COUNT', count.toString());
|
||||
} else if (count) {
|
||||
args.push('COUNT', count.value.toString());
|
||||
|
||||
if (count.ANY) {
|
||||
args.push('ANY');
|
||||
}
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
export type GeoUnits = 'm' | 'km' | 'mi' | 'ft';
|
||||
|
||||
export interface GeoCoordinates {
|
||||
longitude: RedisArgument | number;
|
||||
latitude: RedisArgument | number;
|
||||
}
|
||||
|
||||
type GeoSearchFromMember = string;
|
||||
|
||||
export type GeoSearchFrom = GeoSearchFromMember | GeoCoordinates;
|
||||
|
||||
interface GeoSearchByRadius {
|
||||
radius: number;
|
||||
unit: GeoUnits;
|
||||
}
|
||||
|
||||
interface GeoSearchByBox {
|
||||
width: number;
|
||||
height: number;
|
||||
unit: GeoUnits;
|
||||
}
|
||||
|
||||
export type GeoSearchBy = GeoSearchByRadius | GeoSearchByBox;
|
||||
|
||||
export interface GeoSearchOptions {
|
||||
SORT?: 'ASC' | 'DESC';
|
||||
COUNT?: GeoCountArgument;
|
||||
}
|
||||
|
||||
export function pushGeoSearchArguments(
|
||||
args: CommandArguments,
|
||||
key: RedisArgument,
|
||||
from: GeoSearchFrom,
|
||||
by: GeoSearchBy,
|
||||
options?: GeoSearchOptions
|
||||
): CommandArguments {
|
||||
args.push(key);
|
||||
|
||||
if (typeof from === 'string') {
|
||||
args.push('FROMMEMBER', from);
|
||||
} else {
|
||||
args.push('FROMLONLAT', from.longitude.toString(), from.latitude.toString());
|
||||
}
|
||||
|
||||
if ('radius' in by) {
|
||||
args.push('BYRADIUS', by.radius.toString());
|
||||
} else {
|
||||
args.push('BYBOX', by.width.toString(), by.height.toString());
|
||||
}
|
||||
|
||||
args.push(by.unit);
|
||||
|
||||
if (options?.SORT) {
|
||||
args.push(options.SORT);
|
||||
}
|
||||
|
||||
pushGeoCountArgument(args, options?.COUNT);
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
export function pushGeoRadiusArguments(
|
||||
args: CommandArguments,
|
||||
key: RedisArgument,
|
||||
from: GeoSearchFrom,
|
||||
radius: number,
|
||||
unit: GeoUnits,
|
||||
options?: GeoSearchOptions
|
||||
): CommandArguments {
|
||||
args.push(key);
|
||||
|
||||
if (typeof from === 'string') {
|
||||
args.push(from);
|
||||
} else {
|
||||
args.push(
|
||||
from.longitude.toString(),
|
||||
from.latitude.toString()
|
||||
);
|
||||
}
|
||||
|
||||
args.push(
|
||||
radius.toString(),
|
||||
unit
|
||||
);
|
||||
|
||||
if (options?.SORT) {
|
||||
args.push(options.SORT);
|
||||
}
|
||||
|
||||
pushGeoCountArgument(args, options?.COUNT);
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
export interface GeoRadiusStoreOptions extends GeoSearchOptions {
|
||||
STOREDIST?: boolean;
|
||||
}
|
||||
|
||||
export function pushGeoRadiusStoreArguments(
|
||||
args: CommandArguments,
|
||||
key: RedisArgument,
|
||||
from: GeoSearchFrom,
|
||||
radius: number,
|
||||
unit: GeoUnits,
|
||||
destination: RedisArgument,
|
||||
options?: GeoRadiusStoreOptions
|
||||
): CommandArguments {
|
||||
pushGeoRadiusArguments(args, key, from, radius, unit, options);
|
||||
|
||||
if (options?.STOREDIST) {
|
||||
args.push('STOREDIST', destination);
|
||||
} else {
|
||||
args.push('STORE', destination);
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
export enum GeoReplyWith {
|
||||
DISTANCE = 'WITHDIST',
|
||||
HASH = 'WITHHASH',
|
||||
COORDINATES = 'WITHCOORD'
|
||||
}
|
||||
|
||||
export interface GeoReplyWithMember {
|
||||
member: string;
|
||||
distance?: number;
|
||||
hash?: string;
|
||||
coordinates?: {
|
||||
longitude: string;
|
||||
latitude: string;
|
||||
};
|
||||
}
|
||||
|
||||
export function transformGeoMembersWithReply(reply: Array<Array<any>>, replyWith: Array<GeoReplyWith>): Array<GeoReplyWithMember> {
|
||||
const replyWithSet = new Set(replyWith);
|
||||
|
||||
let index = 0;
|
||||
const distanceIndex = replyWithSet.has(GeoReplyWith.DISTANCE) && ++index,
|
||||
hashIndex = replyWithSet.has(GeoReplyWith.HASH) && ++index,
|
||||
coordinatesIndex = replyWithSet.has(GeoReplyWith.COORDINATES) && ++index;
|
||||
|
||||
return reply.map(member => {
|
||||
const transformedMember: GeoReplyWithMember = {
|
||||
member: member[0]
|
||||
};
|
||||
|
||||
if (distanceIndex) {
|
||||
transformedMember.distance = member[distanceIndex];
|
||||
}
|
||||
|
||||
if (hashIndex) {
|
||||
transformedMember.hash = member[hashIndex];
|
||||
}
|
||||
|
||||
if (coordinatesIndex) {
|
||||
const [longitude, latitude] = member[coordinatesIndex];
|
||||
transformedMember.coordinates = {
|
||||
longitude,
|
||||
latitude
|
||||
};
|
||||
}
|
||||
|
||||
return transformedMember;
|
||||
});
|
||||
}
|
||||
|
||||
export function transformEXAT(EXAT: number | Date): string {
|
||||
return (typeof EXAT === 'number' ? EXAT : Math.floor(EXAT.getTime() / 1000)).toString();
|
||||
}
|
||||
|
Reference in New Issue
Block a user