-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (72 loc) · 3.4 KB
/
index.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// index.js
const { bbox, list_tables, transform_point, list_columns, query_table, nearest, mvt, intersect_point, intersect_feature, geojson, centroid, geobuf } = require("./lib");
module.exports = class Postgis {
constructor(client) {
if (!client || typeof client.query !== 'function') {
throw new Error('A valid pg.Client instance is required.');
}
this.client = client;
}
async list_tables({ filter } = {}) {
const query = list_tables(filter);
return this._executeQuery(query);
}
async list_columns(table) {
const query = list_columns(table);
return this._executeQuery(query);
}
async query_table(table, { columns = '*', filter, group, sort, limit = 100 } = {}) {
const query = query_table(table, columns, filter, group, sort, limit);
return this._executeQuery(query);
}
async bbox(table, { geom_column = 'geom', srid = 4326, filter } = {}) {
const query = bbox(table, geom_column, srid, filter);
return this._executeQuery(query);
}
async centroid(table, { force_on_surface = false, geom_column = 'geom', srid = '4326', filter } = {}) {
const query = centroid(table, force_on_surface, geom_column, srid, filter);
return this._executeQuery(query);
}
async intersect_feature(table_from, table_to, { columns = '*', distance = '0', geom_column_from = 'geom', geom_column_to = 'geom', filter, sort, limit } = {}) {
const query = intersect_feature(table_from, table_to, columns, distance, geom_column_from, geom_column_to, filter, sort, limit);
return this._executeQuery(query);
}
async intersect_point(table, point, { columns = '*', distance = '0', geom_column = 'geom', filter, sort, limit = 10 } = {}) {
const query = intersect_point(table, point, columns, distance, geom_column, filter, sort, limit);
return this._executeQuery(query);
}
async geojson(table, { bounds, id_column, precision = 9, geom_column = 'geom', columns, filter } = {}) {
const query = geojson(table, bounds, id_column, precision, geom_column, columns, filter);
const rows = await this._executeQuery(query);
const json = {
type: 'FeatureCollection',
features: rows.map((el) => el.geojson)
}
return json;
}
async geobuf(table, { bounds, geom_column = 'geom', columns, filter } = {}) {
const query = geobuf(table, bounds, geom_column, columns, filter);
const rows = await this._executeQuery(query);
return rows[0].st_asgeobuf;
}
async mvt(table, x, y, z, { columns, id_column, geom_column = 'geom', filter } = {}) {
const query = mvt(table, x, y, z, columns, id_column, geom_column, filter);
return this._executeQuery(query);
}
async nearest(table, point, { columns = '*', geom_column = 'geom', filter, limit = 10 } = {}) {
const query = nearest(table, point, columns, geom_column, filter, limit);
return this._executeQuery(query);
}
async transform_point(point, { srid = 4326 } = {}) {
const query = transform_point(point, srid);
return this._executeQuery(query);
}
async _executeQuery(query) {
try {
const res = await this.client.query(query);
return res.rows;
} catch (err) {
throw new Error(`Query execution failed: ${err.message}`);
}
}
};