Verified Commit fed9758c authored by Michael Auer's avatar Michael Auer
Browse files

refactored code to use pg-promise instead of pg-async to communicate with PostgreSQL

parent 1ab6657c
......@@ -18,7 +18,7 @@
"moment": "^2.24.0",
"normalize-url": "^4.3.0",
"papaparse": "^5.0.0",
"pg-async": "^3.1.0",
"pg-promise": "^10.9.5",
"proj4": "^2.5.0",
"querystring": "^0.2.0",
"reproject": "^1.2.4",
......@@ -142,6 +142,14 @@
"node": ">=8"
}
},
"node_modules/assert-options": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/assert-options/-/assert-options-0.7.0.tgz",
"integrity": "sha512-7q9uNH/Dh8gFgpIIb9ja8PJEWA5AQy3xnBC8jtKs8K/gNVCr1K6kIvlm59HUyYgvM7oEDoLzGgPcGd9FqhtXEQ==",
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
......@@ -150,11 +158,6 @@
"follow-redirects": "^1.10.0"
}
},
"node_modules/bluebird": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.5.tgz",
"integrity": "sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w=="
},
"node_modules/buffer-from": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
......@@ -231,14 +234,6 @@
"typedarray": "^0.0.6"
}
},
"node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"dependencies": {
"ms": "2.0.0"
}
},
"node_modules/duplexer": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz",
......@@ -462,11 +457,6 @@
"node": "*"
}
},
"node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"node_modules/mute-stream": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz",
......@@ -517,55 +507,78 @@
"through": "~2.3"
}
},
"node_modules/pg": {
"version": "7.11.0",
"resolved": "https://registry.npmjs.org/pg/-/pg-7.11.0.tgz",
"integrity": "sha512-YO4V7vCmEMGoF390LJaFaohWNKaA2ayoQOEZmiHVcAUF+YsRThpf/TaKCgSvsSE7cDm37Q/Cy3Gz41xiX/XjTw==",
"node_modules/pg-int8": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/pg-minify": {
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/pg-minify/-/pg-minify-1.6.2.tgz",
"integrity": "sha512-1KdmFGGTP6jplJoI8MfvRlfvMiyBivMRP7/ffh4a11RUFJ7kC2J0ZHlipoKiH/1hz+DVgceon9U2qbaHpPeyPg==",
"engines": {
"node": ">=8.0"
}
},
"node_modules/pg-promise": {
"version": "10.9.5",
"resolved": "https://registry.npmjs.org/pg-promise/-/pg-promise-10.9.5.tgz",
"integrity": "sha512-llJfdNQVTixjpsTuKTGStybkTWN5PYHWCTb0iqA9GFsvf1khJaH2dexljkMuyeQ41FG8ZkuqkmxrtRVauuS+0A==",
"dependencies": {
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "0.1.3",
"pg-pool": "^2.0.4",
"pg-types": "~2.0.0",
"pgpass": "1.x",
"semver": "4.3.2"
"assert-options": "0.7.0",
"pg": "8.5.1",
"pg-minify": "1.6.2",
"spex": "3.2.0"
},
"engines": {
"node": ">= 4.5.0"
"node": ">=8.0"
}
},
"node_modules/pg-async": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/pg-async/-/pg-async-3.1.0.tgz",
"integrity": "sha512-GwxE/NkOCAgM6K0r1VoMEO61x+ze1KxdzFQitmOLZe6MmqKmP7iQLdXV7ZBHji10tz52sl+64EN7NqqtVJJaIA==",
"node_modules/pg-promise/node_modules/pg": {
"version": "8.5.1",
"resolved": "https://registry.npmjs.org/pg/-/pg-8.5.1.tgz",
"integrity": "sha512-9wm3yX9lCfjvA98ybCyw2pADUivyNWT/yIP4ZcDVpMN0og70BUWYEGXPCTAQdGTAqnytfRADb7NERrY1qxhIqw==",
"dependencies": {
"bluebird": "^3.5.0",
"debug": "^2.6.3",
"pg": "^7.0.2"
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "^2.4.0",
"pg-pool": "^3.2.2",
"pg-protocol": "^1.4.0",
"pg-types": "^2.1.0",
"pgpass": "1.x"
},
"engines": {
"node": ">= 8.0.0"
},
"peerDependencies": {
"pg-native": ">=2.0.0"
},
"peerDependenciesMeta": {
"pg-native": {
"optional": true
}
}
},
"node_modules/pg-connection-string": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz",
"integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc="
"node_modules/pg-promise/node_modules/pg-connection-string": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.4.0.tgz",
"integrity": "sha512-3iBXuv7XKvxeMrIgym7njT+HlZkwZqqGX4Bu9cci8xHZNT+Um1gWKqCsAzcC0d95rcKMU5WBg6YRUcHyV0HZKQ=="
},
"node_modules/pg-int8": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
"engines": {
"node": ">=4.0.0"
"node_modules/pg-promise/node_modules/pg-pool": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.2.2.tgz",
"integrity": "sha512-ORJoFxAlmmros8igi608iVEbQNNZlp89diFVx6yV5v+ehmpMY9sK6QgpmgoXbmkNaBAx8cOOZh9g80kJv1ooyA==",
"peerDependencies": {
"pg": ">=8.0"
}
},
"node_modules/pg-pool": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.6.tgz",
"integrity": "sha512-hod2zYQxM8Gt482q+qONGTYcg/qVcV32VHVPtktbBJs0us3Dj7xibISw0BAAXVMCzt8A/jhfJvpZaxUlqtqs0g=="
},
"node_modules/pg-types": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.0.1.tgz",
"integrity": "sha512-b7y6QM1VF5nOeX9ukMQ0h8a9z89mojrBHXfJeSug4mhL0YpxNBm83ot2TROyoAmX/ZOX3UbwVO4EbH7i1ZZNiw==",
"node_modules/pg-promise/node_modules/pg-types": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
"dependencies": {
"pg-int8": "1.0.1",
"postgres-array": "~2.0.0",
......@@ -577,6 +590,11 @@
"node": ">=4"
}
},
"node_modules/pg-protocol": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.4.0.tgz",
"integrity": "sha512-El+aXWcwG/8wuFICMQjM5ZSAm6OWiJicFdNYo+VY3QP+8vI4SvLIWVe51PppTzMhikUJR+PsyIFKqfdXPz/yxA=="
},
"node_modules/pgpass": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.2.tgz",
......@@ -709,19 +727,19 @@
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"node_modules/semver": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz",
"integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=",
"bin": {
"semver": "bin/semver"
}
},
"node_modules/signal-exit": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.3.tgz",
"integrity": "sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA=="
},
"node_modules/spex": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/spex/-/spex-3.2.0.tgz",
"integrity": "sha512-9srjJM7NaymrpwMHvSmpDeIK5GoRMX/Tq0E8aOlDPS54dDnDUIp30DrP9SphMPEETDLzEM9+4qo+KipmbtPecg==",
"engines": {
"node": ">=4.5"
}
},
"node_modules/split": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz",
......@@ -965,6 +983,11 @@
"color-convert": "^2.0.1"
}
},
"assert-options": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/assert-options/-/assert-options-0.7.0.tgz",
"integrity": "sha512-7q9uNH/Dh8gFgpIIb9ja8PJEWA5AQy3xnBC8jtKs8K/gNVCr1K6kIvlm59HUyYgvM7oEDoLzGgPcGd9FqhtXEQ=="
},
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
......@@ -973,11 +996,6 @@
"follow-redirects": "^1.10.0"
}
},
"bluebird": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.5.tgz",
"integrity": "sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w=="
},
"buffer-from": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
......@@ -1039,14 +1057,6 @@
"typedarray": "^0.0.6"
}
},
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"requires": {
"ms": "2.0.0"
}
},
"duplexer": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz",
......@@ -1220,11 +1230,6 @@
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"mute-stream": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz",
......@@ -1266,57 +1271,71 @@
"through": "~2.3"
}
},
"pg": {
"version": "7.11.0",
"resolved": "https://registry.npmjs.org/pg/-/pg-7.11.0.tgz",
"integrity": "sha512-YO4V7vCmEMGoF390LJaFaohWNKaA2ayoQOEZmiHVcAUF+YsRThpf/TaKCgSvsSE7cDm37Q/Cy3Gz41xiX/XjTw==",
"requires": {
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "0.1.3",
"pg-pool": "^2.0.4",
"pg-types": "~2.0.0",
"pgpass": "1.x",
"semver": "4.3.2"
}
},
"pg-async": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/pg-async/-/pg-async-3.1.0.tgz",
"integrity": "sha512-GwxE/NkOCAgM6K0r1VoMEO61x+ze1KxdzFQitmOLZe6MmqKmP7iQLdXV7ZBHji10tz52sl+64EN7NqqtVJJaIA==",
"requires": {
"bluebird": "^3.5.0",
"debug": "^2.6.3",
"pg": "^7.0.2"
}
},
"pg-connection-string": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz",
"integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc="
},
"pg-int8": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw=="
},
"pg-pool": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.6.tgz",
"integrity": "sha512-hod2zYQxM8Gt482q+qONGTYcg/qVcV32VHVPtktbBJs0us3Dj7xibISw0BAAXVMCzt8A/jhfJvpZaxUlqtqs0g=="
"pg-minify": {
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/pg-minify/-/pg-minify-1.6.2.tgz",
"integrity": "sha512-1KdmFGGTP6jplJoI8MfvRlfvMiyBivMRP7/ffh4a11RUFJ7kC2J0ZHlipoKiH/1hz+DVgceon9U2qbaHpPeyPg=="
},
"pg-types": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.0.1.tgz",
"integrity": "sha512-b7y6QM1VF5nOeX9ukMQ0h8a9z89mojrBHXfJeSug4mhL0YpxNBm83ot2TROyoAmX/ZOX3UbwVO4EbH7i1ZZNiw==",
"pg-promise": {
"version": "10.9.5",
"resolved": "https://registry.npmjs.org/pg-promise/-/pg-promise-10.9.5.tgz",
"integrity": "sha512-llJfdNQVTixjpsTuKTGStybkTWN5PYHWCTb0iqA9GFsvf1khJaH2dexljkMuyeQ41FG8ZkuqkmxrtRVauuS+0A==",
"requires": {
"pg-int8": "1.0.1",
"postgres-array": "~2.0.0",
"postgres-bytea": "~1.0.0",
"postgres-date": "~1.0.4",
"postgres-interval": "^1.1.0"
"assert-options": "0.7.0",
"pg": "8.5.1",
"pg-minify": "1.6.2",
"spex": "3.2.0"
},
"dependencies": {
"pg": {
"version": "8.5.1",
"resolved": "https://registry.npmjs.org/pg/-/pg-8.5.1.tgz",
"integrity": "sha512-9wm3yX9lCfjvA98ybCyw2pADUivyNWT/yIP4ZcDVpMN0og70BUWYEGXPCTAQdGTAqnytfRADb7NERrY1qxhIqw==",
"requires": {
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "^2.4.0",
"pg-pool": "^3.2.2",
"pg-protocol": "^1.4.0",
"pg-types": "^2.1.0",
"pgpass": "1.x"
}
},
"pg-connection-string": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.4.0.tgz",
"integrity": "sha512-3iBXuv7XKvxeMrIgym7njT+HlZkwZqqGX4Bu9cci8xHZNT+Um1gWKqCsAzcC0d95rcKMU5WBg6YRUcHyV0HZKQ=="
},
"pg-pool": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.2.2.tgz",
"integrity": "sha512-ORJoFxAlmmros8igi608iVEbQNNZlp89diFVx6yV5v+ehmpMY9sK6QgpmgoXbmkNaBAx8cOOZh9g80kJv1ooyA==",
"requires": {}
},
"pg-types": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
"requires": {
"pg-int8": "1.0.1",
"postgres-array": "~2.0.0",
"postgres-bytea": "~1.0.0",
"postgres-date": "~1.0.4",
"postgres-interval": "^1.1.0"
}
}
}
},
"pg-protocol": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.4.0.tgz",
"integrity": "sha512-El+aXWcwG/8wuFICMQjM5ZSAm6OWiJicFdNYo+VY3QP+8vI4SvLIWVe51PppTzMhikUJR+PsyIFKqfdXPz/yxA=="
},
"pgpass": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.2.tgz",
......@@ -1419,16 +1438,16 @@
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"semver": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz",
"integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c="
},
"signal-exit": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.3.tgz",
"integrity": "sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA=="
},
"spex": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/spex/-/spex-3.2.0.tgz",
"integrity": "sha512-9srjJM7NaymrpwMHvSmpDeIK5GoRMX/Tq0E8aOlDPS54dDnDUIp30DrP9SphMPEETDLzEM9+4qo+KipmbtPecg=="
},
"split": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz",
......
......@@ -30,7 +30,7 @@ export abstract class FeatureType {
/**
* physically deletes the feature type from the store (e.g. file from disk or table from database)
**/
abstract async delete(): Promise<void>;
abstract async delete(): Promise<null|undefined|void>;
/**
* Do some additional work in the end, e.g. close connections to a database
......
import {PgFeatureType} from './PgFeatureType';
import {GeoJsonFeatureType} from './GeoJsonFeatureType';
import PgAsync from 'pg-async';
import {FeatureTypeConfig, GeoJSONFeatureTypeConfig, PostgisFeatureTypeConfig} from './config_types_interfaces';
// const PgFeatureType = require('./PgFeatureType.js');
// const GeoJsonFeatureType = require('./GeoJsonFeatureType.js');
// const PgAsync = require('pg-async').default;
export class FeatureTypeFactory {
//factory
static async create(featureTypeConfig: FeatureTypeConfig<any>){
......
......@@ -553,7 +553,8 @@ class Ohsome2X extends EventEmitter {
url: `${this.ohsomeApiUrl}/${queryType}`,
method: 'post',
header: {'content-type': 'application/x-www-form-urlencoded'},
maxContentLength: 1024 * 1024 * 1024 * 1024,
maxContentLength: Infinity,
maxBodyLength: Infinity,
data: dataString
});
//only for json requests with showMetadata=true, not csv
......
......@@ -7,13 +7,9 @@
// const moment = require('moment');
//extends FeatureType
import {
PostgisStore,
SourcePostgisFeatureTypeConfig,
TargetPostgisFeatureTypeConfig
} from './config_types_interfaces';
import {PostgisStore, SourcePostgisFeatureTypeConfig, TargetPostgisFeatureTypeConfig} from './config_types_interfaces';
import {FeatureType} from './FeatureType';
import PgAsync from 'pg-async';
import pgPromise, {IDatabase} from 'pg-promise'
import {types as pgTypes} from 'pg';
import * as turfHelpers from '@turf/helpers';
import {Feature, FeatureCollection} from '@turf/helpers';
......@@ -33,8 +29,9 @@ export class PgFeatureType extends FeatureType {
private geometryId: string | number | undefined;
private geometryColumn: string;
private fetchSize: number | undefined;
private db: PgAsync;
private db: IDatabase<any>;
private tableExists: any;
private pgp: pgPromise.IMain<any>;
constructor(config: SourcePostgisFeatureTypeConfig | TargetPostgisFeatureTypeConfig) {
super(config);
......@@ -53,23 +50,27 @@ export class PgFeatureType extends FeatureType {
pgTypes.setTypeParser(20, function (val) {
return parseInt(val)
});
this.db = new PgAsync(this.store);
// this.db = new PgAsync(this.store);
this.pgp = pgPromise({});
this.db = this.pgp(this.store);
this.checkTableExists();
}
async checkTableExists() {
const sql = `SELECT EXISTS (
const sql = `SELECT exists(
SELECT 1
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = '${this.schemaName}'
AND c.relname = '${this.tableName}'
WHERE n.nspname = $1
AND c.relname = $2
AND (c.relkind = 'r' OR c.relkind = 'v' ) -- only tables or views
);`;
let exists;
try {
exists = await this.db.value(sql);
exists = await this.db.one(sql,[this.schemaName, this.tableName], row => row.exists);//.value(sql);
console.log(`Checked if TABLE ${this.schemaName}.${this.tableName} exists: ${exists}`);
this.tableExists = exists;
return exists;
......@@ -81,14 +82,14 @@ export class PgFeatureType extends FeatureType {
async closeConnections() {
console.log(`${this.schemaName}.${this.tableName}: ` + 'Close all connections of database: ' + this.store.database + ' on ' + this.store.host);
await this.db.closeConnections();
await this.pgp.end(); //this.db.$pool.end() //.closeConnections();
}
async delete() {
const sql = `DROP TABLE IF EXISTS ${this.schemaName}."${this.tableName}" CASCADE;`;
try {
const res = await this.db.query(sql);
const res = await this.db.none(sql);
this.tableExists = false;
console.log(sql);
return res;
......@@ -105,9 +106,9 @@ export class PgFeatureType extends FeatureType {
if (this.tableExists || await this.checkTableExists()){
const sql = `SELECT "${this.geometryId}" as id
FROM ${this.schemaName}."${this.tableName}" LIMIT 1;`;
const result = await this.db.query(sql);
const id = await this.db.one(sql, null, row=>row.id);
return typeof result.rows[0].id;
return typeof id;
} else {
throw Error(`Can't getId data type from table that doesn't exist: ${this.schemaName}.${this.tableName}`);
}
......@@ -119,7 +120,7 @@ export class PgFeatureType extends FeatureType {
async getFeatureCount(): Promise<number> {
const sql = `SELECT count(*)
FROM ${this.schemaName}."${this.tableName}";`;
return await this.db.value(sql);
return await this.db.one(sql, null, row=>row.count);
}
/**
......@@ -132,11 +133,11 @@ export class PgFeatureType extends FeatureType {
FROM ${this.schemaName}."${this.tableName}";`;
console.log(`Get all cells from ${this.schemaName}."${this.tableName}"`);
const result = await this.db.query(sql);
const result = await this.db.any(sql);
// console.log(JSON.stringify(result,undefined,2));
// console.log(result.rows);
let features = result.rows.map((row: any) => turfHelpers.feature(row.geom, {id: row.id}));
let features = result.map((row: any) => turfHelpers.feature(row.geom, {id: row.id}));
let featureCollection = turfHelpers.featureCollection(features);
return featureCollection;
......@@ -152,31 +153,22 @@ export class PgFeatureType extends FeatureType {
throw Error('Please set params: cursor: int | string, fetchSize: int');
}
const cursorSql = (typeof cursor === 'number')? cursor : `'${cursor}'`;
// const cursorSql = (typeof cursor === 'number')? cursor : `'${cursor}'`;
const sql = `SELECT "${this.geometryId}" as id,
st_asgeojson(st_multi("${this.geometryColumn}"))::json as geom
FROM ${this.schemaName}."${this.tableName}"
WHERE "${this.geometryId}" > ${cursorSql}
WHERE "${this.geometryId}" > $1
ORDER BY "${this.geometryId}" ASC
LIMIT ${fetchSize};`;
console.log('Get cells from ' + this.tableName + ' with id > ' + cursorSql + ' with fetchSize: ' + fetchSize);
console.log('Get cells from ' + this.tableName + ' with id > ' + cursor + ' with fetchSize: ' + fetchSize);
const result = await this.db.query(sql);
let features = result.rows.map((row: any) => turfHelpers.feature(row.geom, {id: row.id}));
const result = await this.db.any(sql, cursor);
let features = result.map((row: any) => turfHelpers.feature(row.geom, {id: row.id}));
let featureCollection = turfHelpers.featureCollection(features);
return featureCollection;
}
async getPropertyAgg(propertyName: string) {
let sql = `SELECT
array_agg("${propertyName}") as values
FROM ${this.schemaName}."${this.tableName}"
WHERE "timestamp" = (SELECT max("timestamp") FROM ${this.schemaName}."${this.tableName}")
;`;
const result = await this.db.query(sql);
return result;
}
async createTableFromGeoJSONFeature(geoJSONFeature: Feature, epsg?: number | string) {
//check if geometries exists
......@@ -236,10 +228,11 @@ export class PgFeatureType extends FeatureType {
const sql = `CREATE TABLE IF NOT EXISTS ${this.schemaName}."${this.tableName}" (${columnDefsString});`;
// console.log(sql);
let res = await this.db.query(sql);
this.tableExists = true;
console.log( (sql.length > 70)? sql.substring(0,67) + '...' : sql );
let res = await this.db.none(sql);
this.tableExists = true;
console.log(`✔️ TABLE is ready.`)
return res;
}
......@@ -302,7 +295,7 @@ export class PgFeatureType extends FeatureType {
}