1 | cartodb tools
|
2 | ===
|
3 |
|
4 | ```bash
|
5 | npm install cartodb-tools --save
|
6 | ```
|
7 |
|
8 | some tools for working with cartodb, for now works only with api keys.
|
9 |
|
10 | API is shamelessly copied from [KNEX](http://knexjs.org/) as is much of the code,
|
11 | see the documentation over their for details, currently does not support table creation.
|
12 |
|
13 | One difference is that geojson geometries are treated as such and converted to
|
14 | geometries appropriate to the `the_geom` field in cartodb.
|
15 |
|
16 |
|
17 | ```js
|
18 | var cartodb = require('cartodb-tools')('username', 'api-key');
|
19 |
|
20 | cartodb('myTable')
|
21 | .select('foo')
|
22 | .where('bar', 'baz')
|
23 | .then(function (resp) {
|
24 | //use resp
|
25 | })
|
26 | .catch(function (err) {
|
27 | // something bad happened
|
28 | });
|
29 | ```
|
30 |
|
31 | Write Stream
|
32 |
|
33 |
|
34 | ```js
|
35 | var cartodb = require('cartodb-tools')('username', 'api-key')
|
36 | cartodb.createWriteStream('table_name', opts);
|
37 | // available options are `create` to create a new table
|
38 | ```
|
39 |
|
40 | the query object has a few cartodb specific methods
|
41 |
|
42 |
|
43 | # batch
|
44 |
|
45 | the batch method will use the [carto batch api](https://carto.com/docs/carto-engine/sql-api/batch-queries/) 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
|
46 |
|
47 | ```js
|
48 | cartodb('myTable')
|
49 | .update({
|
50 | foo: 'bar'
|
51 | })
|
52 | .where('bar', 'baz')
|
53 | .batch()
|
54 | .then(function (resp) {
|
55 | //use resp
|
56 | })
|
57 | ```
|
58 |
|
59 | you can also use the .onSuccess or .onError method to run those queries if the first one failed or succeeded
|
60 |
|
61 | ```js
|
62 | cartodb('myTable')
|
63 | .update({
|
64 | foo: 'bar'
|
65 | })
|
66 | .where('fake collumn', 'baz')
|
67 | .batch()
|
68 | .onSuccess(cartodb('errors_log').insert({
|
69 | error_message: 'NONE!',
|
70 | date: cartodb.raw('CURRENT_TIMESTAMP')
|
71 | }))
|
72 | .onError('INSERT INTO errors_log (job_id, error_message, date) VALUES (\'<%= job_id %>\', \'<%= error_message %>\', NOW())')
|
73 | .then(function (resp) {
|
74 | //use resp
|
75 | })
|
76 | ```
|
77 |
|
78 | By default raw queries are wrapped in a transaction, use `.noTransaction()` to avoid this, useful for queries that can't be in transactions
|
79 |
|
80 | ```js
|
81 | cartodb.raw('VACUUM ANALYZE').noTransaction().batch().then(function () {
|
82 | console.log('yay!');
|
83 | }).catch(function () {
|
84 | console.log('no!');
|
85 | });
|
86 | ```
|