@@ -3,30 +3,38 @@ require("dotenv").config()
3
3
4
4
const sql = ( params , query ) => {
5
5
return `
6
- WITH mvtgeom as (
6
+ WITH mvtgeom2 as (
7
+ SELECT
8
+ id,
9
+ json,
10
+ geom
11
+ FROM
12
+ ${ process . env . TABLE_NAME }
13
+ WHERE
14
+ ${ `xform_id=${ query . form_id } AND geom is not null AND deleted_at is null` }
15
+ ), mvtgeom as (
7
16
SELECT
8
17
ST_AsMVTGeom (
9
18
ST_Transform(${ process . env . TABLE_COLUMN } , 3857),
10
- ST_TileEnvelope(${ params . z } , ${ params . x } , ${ params . y } )
11
- ) as geom, id, json
12
- ${ query . columns ? `, ${ query . columns } ` : '' }
13
- ${ query . id_column ? `, ${ query . id_column } ` : '' }
19
+ ST_TileEnvelope(${ params . z } , ${ params . x } , ${ params . y } )) as geom,
20
+ id,
21
+ json
22
+ ${ query . columns ? `, ${ query . columns } ` : '' }
23
+ ${ query . id_column ? `, ${ query . id_column } ` : '' }
14
24
FROM
15
- ${ process . env . TABLE_NAME } ,
16
- (SELECT ST_SRID(${ process . env . TABLE_COLUMN } ) AS srid FROM ${ process . env . TABLE_NAME } WHERE ${ process . env . TABLE_COLUMN } IS NOT NULL LIMIT 1) a
25
+ mvtgeom2 ,
26
+ (SELECT ST_SRID(${ process . env . TABLE_COLUMN } ) AS srid FROM mvtgeom2 WHERE ${ process . env . TABLE_COLUMN } LIMIT 1) a
17
27
WHERE
18
28
ST_Intersects(
19
29
${ process . env . TABLE_COLUMN } ,
20
30
ST_Transform(
21
31
ST_TileEnvelope(${ params . z } , ${ params . x } , ${ params . y } ),
22
32
srid
33
+ )
23
34
)
24
- )
25
-
26
- -- Optional Filter
27
- ${ `AND xform_id=${ query . form_id } AND geom is not null AND deleted_at is null` }
28
35
29
- ${ query . field_name ? `AND json->>'${ query . field_name } '='${ query . field_value } '` : `` }
36
+ -- Optional Filter
37
+ ${ query . field_name ? `AND json->>'${ query . field_name } '='${ query . field_value } '` : `` }
30
38
)
31
39
SELECT ST_AsMVT(mvtgeom.*, '${ process . env . TABLE_NAME } ', 4096, 'geom' ${ query . id_column ? `, '${ query . id_column } '` : ''
32
40
} ) AS mvt from mvtgeom;
0 commit comments