-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
274 lines (266 loc) · 8.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
'use strict';
var once = require('once');
var cartodb = require('cartodb-tools');
var uploader = require('cartodb-uploader');
var Bluebird = require('bluebird');
var FirstN = require('first-n-stream');
var uuid = require('uuid');
var debug = require('debug')('into-cartodb');
var sanatize = require('./sanatize');
var validate = require('./validations');
var cartoCopyStream = require('carto-copy-stream');
var validator = require('./validator');
var escape = require('pg-escape');
var createOutput = require('./create-output');
module.exports = intoCartoDB;
function append(db, table, toUser, options, cb) {
if (options.copy) {
const cartoOpts = {
domain: options.domain,
subdomainless: options.subdomainless
};
return cartoCopyStream(options.user, options.key, table, options.fields, cartoOpts, function (err, resp) {
if (err) {
return cb(err);
}
toUser.emit('inserted', resp.total_rows);
cb();
});
}
return db.createWriteStream(table, {
batchSize: options.batchSize
})
.once('error', cb)
.once('uploaded', cb)
.on('inserted', function (num) {
toUser.emit('inserted', num);
});
}
var createTemptTable = Bluebird.coroutine(function * createTemptTable(table, db){
var id = `${table.slice(0, 21)}_temp_${uuid().replace(/-/g, '_')}`;
yield db.raw('create table ?? as table ?? with no data;', [id, table]);
return id;
});
var cleanUpTempTables = Bluebird.coroutine(function * cleanUp(user, key, opts) {
var db = cartodb(user, key, opts);
var done = 0;
var tables = yield db(db.raw('INFORMATION_SCHEMA.tables')).select('table_name')
.where('table_name', 'like', '%\_temp\_________\_____\_____\_____\_____________')
.groupBy('table_name');
if (!tables.length) {
return 0;
}
tables = tables.map(function (item) {
return item.table_name;
});
for (let name of tables) {
yield db.schema.dropTableIfExists(name);
++done;
}
return done;
});
module.exports.cleanUpTempTables = cleanUpTempTables;
var swap = Bluebird.coroutine(function * swap(table, tempTable, remove, db, config) {
var newFields, out;
const fields = config.fields;
var group = new Set();
try {
newFields = yield validate(tempTable, fields, config, db, group);
} catch(e) {
debug(e);
if (config.method === 'create') {
out = db.raw(escape('DROP TABLE %I, %I;', table, tempTable));
} else {
out = db.raw(escape('DROP TABLE %I;', tempTable));
}
return out.then(function () {
return Promise.reject(e || new Error('validation failed'));
});
}
var fromFields = [];
var toFields = [];
newFields.forEach(function (value, key) {
fromFields.push(value);
toFields.push(key);
});
var groupFields = [];
group.forEach(function (field) {
groupFields.push(field);
});
return db.raw(`
${remove ? escape('DELETE from %I', table) : ''};
INSERT into ?? ("${toFields.join('","')}")
SELECT "${fromFields.join('","')}" from ??
${groupFields.length ? `group by ${groupFields.join(',')}` : ''};
`,[table, tempTable]).batch().onSuccess(db.raw('DROP TABLE ??;', [tempTable])).onError(db.raw('DROP TABLE ??;', [tempTable]));
});
/*
{
style: create|replace|append
}
*/
function _exists(name, db) {
return db(db.raw('information_schema.tables')).count('table_name').where('table_name', name).then(function (resp) {
if (resp.length !== 1) {
throw new Error('invalid response');
}
if (typeof resp[0].count !== 'number') {
throw new Error('invalid response');
}
return resp[0].count;
});
}
function makeSchema(data) {
var out = new Map();
data.forEach(function (item) {
out.set(item.column_name, item.data_type);
});
return out;
}
var getFields = Bluebird.coroutine(function * (db, tempTable) {
let fields = yield db(db.raw('INFORMATION_SCHEMA.COLUMNS')).select('column_name', 'data_type')
.where({
table_name: tempTable // eslint-disable-line camelcase
})
.whereNotIn('column_name', ['cartodb_id', 'the_geom_webmercator', 'created_at', 'updated_at', 'the_geom']);
return {
fields: fields.map(function (item) {
return item.column_name;
}),
schema: makeSchema(fields)
}
})
const exists = Bluebird.coroutine(function * (name, db) {
let count = yield _exists(name, db);
if (count === 0) {
return {count};
}
let {fields, schema} = yield getFields(db, name);
return {count, fields, schema};
})
function part2(db, table, origTable, remove, toUser, config, done) {
return append(db, table, toUser, config, function (err) {
if (err) {
return done(err);
}
if (!origTable) {
return done();
}
swap(origTable, table, remove, db, config).then(function () {
done();
}).catch(done);
});
}
function intoCartoDB(user, key, table, options, done) {
table = sanatize(table);
if (table.match(/^[^a-z_]/)) {
table = 'table_' + table
} else if (table[0] === '_') {
table = 'table' + table;
}
table = table.slice(0, 63);
if (typeof options === 'function') {
done = options;
options = {};
}
if (typeof options === 'string') {
options = {
method: options
};
}
options = options || {};
options = Object.assign({user, key}, options)
options.method = options.method || 'create';
options.batchSize = options.batchSize || 200;
var direct = options.direct;
var method = options.method;
var toUser = createOutput(options.copy);
function warning(msg) {
toUser.emit('warning', msg);
}
var cb = once(function (err, resp) {
if (err) {
if (done) {
return done(err);
}
return toUser.emit('error', err);
}
if (done) {
done(null, resp);
}
toUser.emit('uploaded');
});
var cartoOpts = {
domain: options.domain,
subdomainless: options.subdomainless
};
var db = cartodb(user, key, cartoOpts);
exists(table, db).then(function (res) {
let count = res.count;
if (method === 'create') {
if (count !== 0) {
throw new Error('table already exists');
}
let out = new FirstN(options.batchSize, function (err, resp) {
if (err) {
return cb(err);
}
var uploadStream = uploader.geojson({
user: user,
key: key,
domain: options.domain,
subdomainless: options.subdomainless
}, table, Bluebird.coroutine(function * (err, r) {
if (err) {
return cb(err);
}
if (r.table_name !== table) {
return cb(new Error(`exptexted "${table}" but got "${r.table_name}"`));
}
try {
let {fields, schema} = yield getFields(db, table)
options.fields = fields;
var nextPart;
if (direct) {
nextPart = part2(db, table, false, true, toUser, options, cb);
return out.pipe(validator(warning, schema)).pipe(nextPart);
} else {
const id = yield createTemptTable(table, db)
nextPart = part2(db, id, table, true, toUser, options, cb);
resp.forEach(function (item) {
nextPart.write(item);
});
out.pipe(validator(warning, schema)).pipe(nextPart);
}
} catch (e) {
cb(e);
}
}));
resp.forEach(function (item) {
uploadStream.write(item);
});
uploadStream.end();
});
toUser.pipe(out);
return;
}
if (count !== 1) {
throw new Error('table must exist');
}
options.fields = res.fields;
if (method === 'append') {
if (direct) {
return toUser.pipe(validator(warning, res.schema)).pipe(part2(db, table, false, false, toUser, options, cb));
} else {
return createTemptTable(table, db).then(function (id) {
toUser.pipe(validator(warning, res.schema)).pipe(part2(db, id, table, false, toUser, options, cb));
});
}
} else if (method === 'replace') {
return createTemptTable(table, db).then(function (id) {
toUser.pipe(validator(warning, res.schema)).pipe(part2(db, id, table, true, toUser, options, cb));
});
}
}).catch(cb);
return toUser;
}