Skip to content

Commit 7060ab7

Browse files
steve-chavezsoedirgo
authored andcommitted
feat: add geojson transform
Also change the docker image to include postgis
1 parent fea31ff commit 7060ab7

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

src/lib/PostgrestTransformBuilder.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,12 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
120120
this.headers['Accept'] = 'text/csv'
121121
return this as PromiseLike<PostgrestSingleResponse<string>>
122122
}
123+
124+
/**
125+
* Set the response type to GeoJSON.
126+
*/
127+
geojson(): PromiseLike<PostgrestSingleResponse<Record<string, unknown>>> {
128+
this.headers['Accept'] = 'application/geo+json'
129+
return this as PromiseLike<PostgrestSingleResponse<Record<string, unknown>>>
130+
}
123131
}

test/db/00-schema.sql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CREATE TABLE public.users (
1313
status user_status DEFAULT 'ONLINE'::public.user_status,
1414
catchphrase tsvector DEFAULT null
1515
);
16-
ALTER TABLE public.users REPLICA IDENTITY FULL; -- Send "previous data" to supabase
16+
ALTER TABLE public.users REPLICA IDENTITY FULL; -- Send "previous data" to supabase
1717
COMMENT ON COLUMN public.users.data IS 'For unstructured data and prototyping.';
1818

1919
-- CHANNELS
@@ -47,10 +47,18 @@ RETURNS TABLE(username text, status user_status) AS $$
4747
SELECT username, status from users WHERE username=name_param;
4848
$$ LANGUAGE SQL IMMUTABLE;
4949

50-
CREATE FUNCTION public.void_func()
50+
CREATE FUNCTION public.void_func()
5151
RETURNS void AS $$
5252
$$ LANGUAGE SQL;
5353

54+
create extension postgis;
55+
56+
create table public.shops (
57+
id int primary key
58+
, address text
59+
, shop_geom geometry(POINT, 4326)
60+
);
61+
5462
-- SECOND SCHEMA USERS
5563
CREATE TYPE personal.user_status AS ENUM ('ONLINE', 'OFFLINE');
5664
CREATE TABLE personal.users(

test/db/01-dummy-data.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ VALUES
2525
('kiwicopple', 'OFFLINE', '[25,35)'::int4range),
2626
('awailas', 'ONLINE', '[25,35)'::int4range),
2727
('dragarcia', 'ONLINE', '[20,30)'::int4range),
28-
('leroyjenkins', 'ONLINE', '[20,40)'::int4range);
28+
('leroyjenkins', 'ONLINE', '[20,40)'::int4range);
29+
30+
INSERT INTO shops(id, address, shop_geom)
31+
VALUES
32+
(1, '1369 Cambridge St', 'SRID=4326;POINT(-71.10044 42.373695)');

test/db/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
depends_on:
1414
- db
1515
db:
16-
image: postgres:12
16+
image: postgis/postgis:12-3.2
1717
ports:
1818
- '5432:5432'
1919
volumes:

test/transforms.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,32 @@ test('abort signal', async () => {
101101
}
102102
`)
103103
})
104+
105+
test('geojson', async () => {
106+
const res = await postgrest
107+
.from('shops')
108+
.select()
109+
.geojson()
110+
.then((res) => res.data)
111+
expect(res).toMatchInlineSnapshot(`
112+
Object {
113+
"features": Array [
114+
Object {
115+
"geometry": Object {
116+
"coordinates": Array [
117+
-71.10044,
118+
42.373695,
119+
],
120+
"type": "Point",
121+
},
122+
"properties": Object {
123+
"address": "1369 Cambridge St",
124+
"id": 1,
125+
},
126+
"type": "Feature",
127+
},
128+
],
129+
"type": "FeatureCollection",
130+
}
131+
`)
132+
})

0 commit comments

Comments
 (0)