Comments (3)
Here is the full code with @database/pg
const connect = require('@databases/pg');
const sql = require('@databases/sql');
const db = connect(process.env.pg_connection);
db.query(sql`
SELECT
city_name,
st_x(geom_p_4326) as lng,
st_y(geom_p_4326) as lat
FROM dev.world_cities limit 10;
`)
.then(
results => processResults(results),
err => console.error(err)
);
function processResults(results) {
results.forEach(async row => {
const response = await require('node-fetch')(`https://www.jack-wolfskin.co.uk/on/demandware.store/Sites-JackWolfskin_UK-Site/en_GB/Store-FindStores?lat=${row.lat}&lng=${row.lng}`);
const json = await response.json();
if (!json.stores.length) return;
json.stores.forEach(store => processStore(store));
});
}
function processStore(store){
db.query(sql`
INSERT INTO dev.jack_wolfskin (id, city, store, lat, lng, geom)
VALUES (
${parseInt(store.id)},
'${store.city}',
'${JSON.stringify(store)}',
${parseFloat(store.lat)},
${parseFloat(store.lng)},
${'ST_SetSRID(ST_Point(' + parseFloat(store.lng) + ',' + parseFloat(store.lat) + '),4326)'}
)
`)
.then(
results => console.log(results),
err => console.error(err)
);
}
This fails on the insert statement with all kinds of errors.
And here is the same script using the pg module.
const db = new require('pg').Pool({
connectionString: process.env.pg_connection,
statement_timeout: 10000
});
db.query(`
SELECT
city_name,
st_x(geom_p_4326) as lng,
st_y(geom_p_4326) as lat
FROM dev.world_cities limit 10;
`)
.then(
results => processResults(results),
err => console.error(err)
);
function processResults(results) {
results.rows.forEach(async row => {
const response = await require('node-fetch')(`https://www.jack-wolfskin.co.uk/on/demandware.store/Sites-JackWolfskin_UK-Site/en_GB/Store-FindStores?lat=${row.lat}&lng=${row.lng}`);
const json = await response.json();
if (!json.stores.length) return;
json.stores.forEach(store => processStore(store));
});
}
function processStore(store){
db.query(`
INSERT INTO dev.jack_wolfskin (id, city, store, lat, lng, geom)
VALUES (
${parseInt(store.id)},
'${store.city}',
'${JSON.stringify(store)}',
${parseFloat(store.lat)},
${parseFloat(store.lng)},
${'ST_SetSRID(ST_Point(' + parseFloat(store.lng) + ',' + parseFloat(store.lat) + '),4326)'}
)
`)
.then(
results => console.log(results),
err => console.error(err)
);
}
This works A-OK.
from atdatabases.
I think you want something like:
db.query(sql`
INSERT INTO dev.jack_wolfskin (id, city, store, lat, lng, geom)
VALUES (
${parseInt(store.id)},
${store.city},
${store},
${parseFloat(store.lat)},
${parseFloat(store.lng)},
ST_SetSRID(ST_Point(${parseFloat(store.lng)}, ${parseFloat(store.lat)}), 4326)'
)
`)
There are three problems with your original code:
- you're wrapping your strings in quotes, which you would need to do if they were being added directly to the SQL string, but they're not. They are being put into the SQL as values, so you can just trust to the fact that they are strings.
- you've set the type of
store
tojson
, but what you're actually storing is a string (i.e. the result of callingJSON.stringify
. I think, if I remember correctly, this will result in postgres double JSON stringifying the value. Just pass the actual value and trust to pg to handle it. - The value isn't the string
ST_SetSRID(...
the values are the three numbers. The rest is SQL syntax, so it needs to be in template literal land, not JavaScript string land.
I hope this helps.
from atdatabases.
Thanks Forbes that works perfectly fine.
from atdatabases.
Related Issues (20)
- [Inquiry]: Supported SQLite connection protocols HOT 1
- documentation on insert ID HOT 2
- Dynamically choosing a schema in Postgres queries HOT 1
- [pg-migrations] CockroachDB SQL version parsing HOT 1
- Postgres: pg-typed: support for arrays
- pg - optional id for `generated always as identity`
- pg: Cannot interpolate values into Postgres 14-style JSON queries
- Mysql: where in expression?
- Unable to compile typescript targeting ESNext with ES Module HOT 1
- Pg queryNodeStream ignoring batchSize and highWaterMark HOT 2
- [Postgres] Support bigIntMode for pg-schema-cli, pg-schema-print-types
- sqlite and multiple processes HOT 1
- split-sql-query breaks a trigger into multiple queries HOT 4
- Event Handlers: does not support promises HOT 2
- Request for Generic Type Support in 'Queryable.query' HOT 1
- Add support for `stream` parameter for PostgreSQL and MySQL HOT 1
- Dates and Timestamps retrieved from Postgres are offset incorrectly based on the local time zone
- Support for PostgreSQL partitioned tables
- removed cuid dependency as it's deprecated HOT 2
- Question: Capability of browser and esm HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from atdatabases.