UNPKG

2.15 kBMarkdownView Raw
1cartodb tools
2===
3
4```bash
5npm install cartodb-tools --save
6```
7
8some tools for working with cartodb, for now works only with api keys.
9
10API is shamelessly copied from [KNEX](http://knexjs.org/) as is much of the code,
11see the documentation over their for details, currently does not support table creation.
12
13One difference is that geojson geometries are treated as such and converted to
14geometries appropriate to the `the_geom` field in cartodb.
15
16
17```js
18var cartodb = require('cartodb-tools')('username', 'api-key');
19
20cartodb('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
31Write Stream
32
33
34```js
35var cartodb = require('cartodb-tools')('username', 'api-key')
36cartodb.createWriteStream('table_name', opts);
37// available options are `create` to create a new table
38```
39
40the query object has a few cartodb specific methods
41
42
43# batch
44
45the 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
48cartodb('myTable')
49 .update({
50 foo: 'bar'
51 })
52 .where('bar', 'baz')
53 .batch()
54 .then(function (resp) {
55 //use resp
56 })
57```
58
59you can also use the .onSuccess or .onError method to run those queries if the first one failed or succeeded
60
61```js
62cartodb('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
78By 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
81cartodb.raw('VACUUM ANALYZE').noTransaction().batch().then(function () {
82 console.log('yay!');
83}).catch(function () {
84 console.log('no!');
85});
86```