Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): add array attributes support #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
/coverage
/dist
/sql/Generated.sql
.idea
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmjs.org/
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
8 changes: 8 additions & 0 deletions example/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 => {
Expand Down
10 changes: 10 additions & 0 deletions lib/queries/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ export const attributesToArray = (attributes: string[]) =>
? ', ' +
attributes.map((attribute) => `'${attribute}', ${attribute}`).join(', ')
: '';

export const customAttributesToArray = (attributes: string[], arrayAttrs) =>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some unit tests for this in order to prove it works?

attributes.length > 0
? ', ' +
attributes.map((attribute) => {
const value = arrayAttrs.includes(attribute) ? `array_to_string(${attribute}, ',' , '*')` : attribute;

return `'${attribute}', ${value}`
}).join(', ')
: '';
5 changes: 4 additions & 1 deletion lib/queries/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
attributesFirstToSelect,
attributesToArray,
attributesToSelect,
customAttributesToArray,
} from './attributes';
import { defaultGetBaseQuery } from './base';
import {
Expand All @@ -28,6 +29,7 @@ export interface ITileQueryInput extends TileRequest {
extent: number;
bufferSize: number;
attributes: string[];
arrayAttributes: string[];
query: string[];
debug: boolean;
zoomToDistance?: ZoomToDistance;
Expand All @@ -49,6 +51,7 @@ export function createQueryForTile({
extent,
bufferSize,
attributes,
arrayAttributes,
query,
debug,
zoomToDistance = defaultZoomToDistance,
Expand Down Expand Up @@ -104,7 +107,7 @@ export function createQueryForTile({
geometry: 'center',
extent,
bufferSize,
attributes: attributesToArray(attributes),
attributes: customAttributesToArray(attributes, arrayAttributes),
})})`
);

Expand Down
2 changes: 2 additions & 0 deletions lib/tiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function TileServer<T>({
pgPoolOptions = {},
filtersToWhere = null,
attributes = [],
arrayAttributes = [],
debug = false,
}: TileServerConfig<T>): Promise<TileRenderer<T>> {
const { Pool } = require('pg');
Expand Down Expand Up @@ -87,6 +88,7 @@ export async function TileServer<T>({
extent,
bufferSize,
attributes,
arrayAttributes,
query: filtersQuery,
debug,
zoomToDistance,
Expand Down
5 changes: 5 additions & 0 deletions lib/types/TileServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export interface TileServerConfig<T> {
*/
attributes: string[];

/**
* Array type attributes
*/
arrayAttributes: string[];

/**
* @description Show debug logging, default false
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down