Giter Site home page Giter Site logo

Comments (3)

dbauszus-glx avatar dbauszus-glx commented on May 7, 2024

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.

ForbesLindesay avatar ForbesLindesay commented on May 7, 2024

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:

  1. 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.
  2. you've set the type of store to json, but what you're actually storing is a string (i.e. the result of calling JSON.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.
  3. 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.

dbauszus-glx avatar dbauszus-glx commented on May 7, 2024

Thanks Forbes that works perfectly fine.

from atdatabases.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.