From f41258cdd8c8e54b912f47650f3a1aeff799204e Mon Sep 17 00:00:00 2001 From: Nozimzhon Yunusov Date: Fri, 1 Jan 2021 22:35:41 +0300 Subject: [PATCH] feat(): add array attributes support --- .gitignore | 1 + .npmrc | 1 + .nvmrc | 1 + example/express.ts | 8 ++++++++ lib/queries/attributes.ts | 10 ++++++++++ lib/queries/builder.ts | 5 ++++- lib/tiler.ts | 2 ++ lib/types/TileServerConfig.ts | 5 +++++ package.json | 2 +- 9 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 .npmrc create mode 100644 .nvmrc diff --git a/.gitignore b/.gitignore index 0707e89..a94e6f0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules /coverage /dist /sql/Generated.sql +.idea \ No newline at end of file diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..5660f81 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +registry=https://registry.npmjs.org/ \ No newline at end of file diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..9a03714 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +10 \ No newline at end of file diff --git a/example/express.ts b/example/express.ts index 77e8e27..8f72f31 100644 --- a/example/express.ts +++ b/example/express.ts @@ -12,6 +12,8 @@ const { TileServer } = require('../dist'); TileServer({ maxZoomLevel, attributes: ['status', 'speed'], + // you can use array attributes, e.g. ?statuses=busy,free + // arrayAttributes: ['statuses'], filtersToWhere: (filters = { status: undefined, speed: undefined }) => { // You are responsible for protecting against SQL injection in this function. Because there are many ways to filter, it depends on the filter type on how to approach this. @@ -23,6 +25,12 @@ TileServer({ if (filters.speed && ['slow', 'fast'].includes(filters.speed)) { whereStatements.push(`speed = '${filters.speed}'`); } + + // just example of array attributes: + // if (filters.statuses) { + // whereStatements.push(`statuses @> ARRAY[${filters.statuses}]`); + // } + return whereStatements; }, }).then(server => { diff --git a/lib/queries/attributes.ts b/lib/queries/attributes.ts index 5ce2bd4..d3972fa 100644 --- a/lib/queries/attributes.ts +++ b/lib/queries/attributes.ts @@ -22,3 +22,13 @@ export const attributesToArray = (attributes: string[]) => ? ', ' + attributes.map((attribute) => `'${attribute}', ${attribute}`).join(', ') : ''; + +export const customAttributesToArray = (attributes: string[], arrayAttrs) => + attributes.length > 0 + ? ', ' + + attributes.map((attribute) => { + const value = arrayAttrs.includes(attribute) ? `array_to_string(${attribute}, ',' , '*')` : attribute; + + return `'${attribute}', ${value}` + }).join(', ') + : ''; \ No newline at end of file diff --git a/lib/queries/builder.ts b/lib/queries/builder.ts index a189c05..6a7ef9c 100644 --- a/lib/queries/builder.ts +++ b/lib/queries/builder.ts @@ -10,6 +10,7 @@ import { attributesFirstToSelect, attributesToArray, attributesToSelect, + customAttributesToArray, } from './attributes'; import { defaultGetBaseQuery } from './base'; import { @@ -28,6 +29,7 @@ export interface ITileQueryInput extends TileRequest { extent: number; bufferSize: number; attributes: string[]; + arrayAttributes: string[]; query: string[]; debug: boolean; zoomToDistance?: ZoomToDistance; @@ -49,6 +51,7 @@ export function createQueryForTile({ extent, bufferSize, attributes, + arrayAttributes, query, debug, zoomToDistance = defaultZoomToDistance, @@ -104,7 +107,7 @@ export function createQueryForTile({ geometry: 'center', extent, bufferSize, - attributes: attributesToArray(attributes), + attributes: customAttributesToArray(attributes, arrayAttributes), })})` ); diff --git a/lib/tiler.ts b/lib/tiler.ts index 519c422..17a2a82 100644 --- a/lib/tiler.ts +++ b/lib/tiler.ts @@ -14,6 +14,7 @@ export async function TileServer({ pgPoolOptions = {}, filtersToWhere = null, attributes = [], + arrayAttributes = [], debug = false, }: TileServerConfig): Promise> { const { Pool } = require('pg'); @@ -87,6 +88,7 @@ export async function TileServer({ extent, bufferSize, attributes, + arrayAttributes, query: filtersQuery, debug, zoomToDistance, diff --git a/lib/types/TileServerConfig.ts b/lib/types/TileServerConfig.ts index b151d07..d6d3fce 100644 --- a/lib/types/TileServerConfig.ts +++ b/lib/types/TileServerConfig.ts @@ -40,6 +40,11 @@ export interface TileServerConfig { */ attributes: string[]; + /** + * Array type attributes + */ + arrayAttributes: string[]; + /** * @description Show debug logging, default false */ diff --git a/package.json b/package.json index 4238204..af6128d 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "ioredis": "^4.16.2", "jest": "^24.9.0", "lru-cache": "^5.1.1", - "pg": "^7.18.2", + "pg": "^8.4.1", "rollup": "^2.6.1", "rollup-plugin-babel": "^4.4.0", "rollup-plugin-commonjs": "^10.1.0",