-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsetup.ts
58 lines (50 loc) · 1.31 KB
/
setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require('dotenv').config();
const path = require('path');
import { Pool } from 'pg';
(async function() {
const pool = new Pool();
const exists = await pool.query(
`SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'points');`
);
if (!!exists && !!exists.rows && !!exists.rows[0] && !exists.rows[0].exists) {
try {
await pool.query('CREATE EXTENSION postgis;');
} catch (e) {
console.error(e.message);
}
try {
await pool.query(`CREATE TABLE public.points
(
id TEXT,
wkb_geometry geometry(Point,4326),
speed TEXT,
status TEXT,
PRIMARY KEY(id)
)
TABLESPACE pg_default;`);
} catch (e) {
console.error(e);
}
}
const points = require(path.join(__dirname, './points.json')),
sql = `INSERT INTO public.points (
id,
wkb_geometry,
speed,
status
)
VALUES ($1, ST_SetSRID(ST_GeomFromGeoJSON($2),4326),$3,$4);`;
for (const point of points) {
const params = [
point.id,
JSON.stringify(point.geometry),
point.properties.speed,
point.properties.status,
];
try {
await pool.query(sql, params);
} catch (e) {
console.error(e.message);
}
}
})().catch(console.error);