Skip to content

Question: Custom filter operators #702

@joeybenenati

Description

@joeybenenati

I've been exploring nestjs-query for a couple days now and first off, thank you. This is an incredibly well thought out solution. My project requires the use of PostGIS methods like ST_Intersects, for which I've created a custom TypeORM operator. I'd like to extend the functionality of these custom operators to the built in query interface filter but I'm having a hard time understanding how I would go about that.

import { FindOperator, FindOperatorType } from 'typeorm';
type SqlGeneratorType = (aliasPath: string) => string;

class FindOperatorWithExtras<T> extends FindOperator<T> {
  constructor(
    type: FindOperatorType | 'intersects',
    value: FindOperator<T> | T,
    useParameter?: boolean,
    multipleParameters?: boolean,
    getSql?: SqlGeneratorType,
  ) {
    // @ts-ignore
    super(type, value, useParameter, multipleParameters, getSql);
  }
}
/**
 * Find Options Operator.
 * Example: { geometry: Intersects({ type: 'Point', coordinates: [1,2]}) }
 */
export function Intersects<T>(
  value: T | FindOperator<T>,
): FindOperatorWithExtras<T> {
  return new FindOperatorWithExtras(
    'intersects',
    value,
    true,
    false,
    (aliasPath: string) => `ST_Intersects(${aliasPath}, ST_GeomFromGeoJSON(:${JSON.stringify(value)}))`
  );
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions