Giter VIP home page Giter VIP logo

cartodb's Introduction

cartodb tools

npm install cartodb-tools --save

some tools for working with cartodb, for now works only with api keys.

API is shamelessly copied from KNEX as is much of the code, see the documentation over their for details, currently does not support table creation.

One difference is that geojson geometries are treated as such and converted to geometries appropriate to the the_geom field in cartodb.

var cartodb = require('cartodb-tools')('username', 'api-key');

cartodb('myTable')
  .select('foo')
  .where('bar', 'baz')
  .then(function (resp) {
    //use resp
  })
  .catch(function (err) {
    // something bad happened
  });

Write Stream

var cartodb = require('cartodb-tools')('username', 'api-key')
cartodb.createWriteStream('table_name', opts);
// available options are `create` to create a new table

the query object has a few cartodb specific methods

batch

the batch method will use the carto batch api method for doing the query, since this will never return results don't use it for selects, though you can if you want it's just kinda pointless

cartodb('myTable')
  .update({
    foo: 'bar'
  })
  .where('bar', 'baz')
  .batch()
  .then(function (resp) {
    //use resp
  })

you can also use the .onSuccess or .onError method to run those queries if the first one failed or succeeded

cartodb('myTable')
  .update({
    foo: 'bar'
  })
  .where('fake collumn', 'baz')
  .batch()
  .onSuccess(cartodb('errors_log').insert({
    error_message: 'NONE!',
    date: cartodb.raw('CURRENT_TIMESTAMP')
  }))
  .onError('INSERT INTO errors_log (job_id, error_message, date) VALUES (\'<%= job_id %>\', \'<%= error_message %>\', NOW())')
  .then(function (resp) {
    //use resp
  })

By default raw queries are wrapped in a transaction, use .noTransaction() to avoid this, useful for queries that can't be in transactions

cartodb.raw('VACUUM ANALYZE').noTransaction().batch().then(function () {
  console.log('yay!');
}).catch(function () {
  console.log('no!');
});

cartodb's People

Contributors

calvinmetcalf avatar dependabot[bot] avatar knownasilya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cartodb's Issues

insert error with geojson

The following code:

   if (normalized.paths) {
    let geojson = {
      type: 'MultiLineString',
      coordinates: normalized.paths
    };
    normalized.the_geom = db.raw(`ST_GeomFromGeoJSON('${JSON.stringify(geojson)}')`);
    delete normalized.paths;
  }

  return db('appgeo_surveys')
    .insert(normalized)
    .then(() => {
      return data;
    });

Returns the following sql (via query.toString()):

BEGIN;
PREPARE _2c97d7a66be43fe4 AS
insert into "appgeo_surveys" ("is_correct", "issue", "comments", "confidence", "neighborhood_id", "city_id", "user_id", "the_geom") values ($1, $2, $3, $4, $5, $6, $7, $8);
EXECUTE _2c97d7a66be43fe4(false,$cartodb$boundaries little off$cartodb$,$cartodb$ewrwer$cartodb$,$cartodb$very$cartodb$,$cartodb$prt023$cartodb$,$cartodb$portland$cartodb$,$cartodb$1$cartodb$,$cartodb${"client":{"url":"urlobfuscated","key":"keyobfuscated","isBatch":false},"sql":"ST_GeomFromGeoJSON('{\\"type\\":\\"MultiLineString\\",\\"coordinates\\":[[[-122.66329765319824,45.60052733338059],[-122.66029357910158,45.60160825936073],[-122.65771865844728,45.60220876479576],[-122.65583038330078,45.60226881498576],[-122.65437126159667,45.60190851288167],[-122.65308380126955,45.60184846230603],[-122.65222549438475,45.60190851288167],[-122.65188217163086,45.602028613840126]],[[-122.68106460571289,45.60407029079811],[-122.67926216125487,45.60389014581998],[-122.67797470092772,45.60364995161597],[-122.67788887023924,45.60340975638357],[-122.67754554748535,45.60310951089689]]]}')","_noTransaction":false}$cartodb$);;
COMMIT;

notice the escaped double-quotes in the sql's geojson string, not sure if this is an issue

the error is

Error: {"error":["parse error - invalid geometry"],"hint":"\"{\"\" <-- parse error at position 2 within geometry"}

I've tried a variety of ways to represent the value, like db.raw('?', [...]) and db.raw(`ST_GeomFromGeoJSON('?')`, [..]), etc..

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.