csv-to-pg 0.6.2 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/cli.d.ts +2 -0
- package/cli.js +60 -0
- package/esm/cli.js +58 -0
- package/esm/index.js +3 -0
- package/esm/parse.js +257 -0
- package/esm/parser.js +59 -0
- package/esm/utils.js +267 -0
- package/index.d.ts +2 -0
- package/index.js +19 -0
- package/package.json +25 -55
- package/parse.d.ts +3 -0
- package/parse.js +289 -0
- package/parser.d.ts +4 -0
- package/parser.js +63 -0
- package/utils.d.ts +135 -0
- package/utils.js +300 -0
- package/main/cli.js +0 -96
- package/main/index.js +0 -29
- package/main/parse.js +0 -352
- package/main/parser.js +0 -139
- package/main/utils.js +0 -413
- package/module/cli.js +0 -59
- package/module/index.js +0 -2
- package/module/parse.js +0 -308
- package/module/parser.js +0 -67
- package/module/utils.js +0 -334
package/main/parse.js
DELETED
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
|
4
|
-
|
|
5
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "__esModule", {
|
|
8
|
-
value: true
|
|
9
|
-
});
|
|
10
|
-
exports.parseTypes = exports.readConfig = exports.parse = void 0;
|
|
11
|
-
|
|
12
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
-
|
|
14
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
15
|
-
|
|
16
|
-
var _csvParser = _interopRequireDefault(require("csv-parser"));
|
|
17
|
-
|
|
18
|
-
var _fs = require("fs");
|
|
19
|
-
|
|
20
|
-
var _jsYaml = require("js-yaml");
|
|
21
|
-
|
|
22
|
-
var ast = _interopRequireWildcard(require("pg-ast"));
|
|
23
|
-
|
|
24
|
-
var _utils = require("./utils");
|
|
25
|
-
|
|
26
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
27
|
-
|
|
28
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
29
|
-
|
|
30
|
-
function isNumeric(str) {
|
|
31
|
-
if (typeof str != 'string') return false; // we only process strings!
|
|
32
|
-
|
|
33
|
-
return !isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
|
|
34
|
-
!isNaN(parseFloat(str)); // ...and ensure strings of whitespace fail
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
var parseJson = function parseJson(value) {
|
|
38
|
-
if (typeof value === 'string') return value;
|
|
39
|
-
return value && JSON.stringify(value);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
var psqlArray = function psqlArray(value) {
|
|
43
|
-
if (value && value.length) {
|
|
44
|
-
return "{".concat(value.map(function (v) {
|
|
45
|
-
return v;
|
|
46
|
-
}), "}");
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
var parse = function parse(path, opts) {
|
|
51
|
-
return new Promise(function (resolve, reject) {
|
|
52
|
-
var results = [];
|
|
53
|
-
(0, _fs.createReadStream)(path).pipe((0, _csvParser["default"])(opts)) // TODO check if 'data' is guaranteed to have a full row,
|
|
54
|
-
// if so, make a hook to use the stream properly
|
|
55
|
-
.on('data', function (data) {
|
|
56
|
-
return results.push(data);
|
|
57
|
-
}).on('error', function (er) {
|
|
58
|
-
reject(er);
|
|
59
|
-
}).on('end', function () {
|
|
60
|
-
resolve(results);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
exports.parse = parse;
|
|
66
|
-
|
|
67
|
-
var readConfig = function readConfig(config) {
|
|
68
|
-
var configValue;
|
|
69
|
-
|
|
70
|
-
if (config.endsWith('.js')) {
|
|
71
|
-
configValue = require(config);
|
|
72
|
-
} else if (config.endsWith('json')) {
|
|
73
|
-
configValue = JSON.parse((0, _fs.readFileSync)(config, 'utf-8'));
|
|
74
|
-
} else if (config.endsWith('yaml') || config.endsWith('yml')) {
|
|
75
|
-
configValue = (0, _jsYaml.safeLoad)((0, _fs.readFileSync)(config, 'utf-8'));
|
|
76
|
-
} else {
|
|
77
|
-
throw new Error('unsupported config!');
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return configValue;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
exports.readConfig = readConfig;
|
|
84
|
-
|
|
85
|
-
var getFromValue = function getFromValue(from) {
|
|
86
|
-
if (Array.isArray(from)) return from;
|
|
87
|
-
return [from];
|
|
88
|
-
}; // looks like the CSV library gives us empty strings?
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
var cleanseEmptyStrings = function cleanseEmptyStrings(str) {
|
|
92
|
-
if (typeof str === 'string') {
|
|
93
|
-
if (str.trim() === '') return null;
|
|
94
|
-
return str;
|
|
95
|
-
} else {
|
|
96
|
-
return str;
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
var parseBoolean = function parseBoolean(str) {
|
|
101
|
-
if (typeof str === 'boolean') {
|
|
102
|
-
return str;
|
|
103
|
-
} else if (typeof str === 'string') {
|
|
104
|
-
var s = str.toLowerCase();
|
|
105
|
-
|
|
106
|
-
if (s === 'true') {
|
|
107
|
-
return true;
|
|
108
|
-
} else if (s === 't') {
|
|
109
|
-
return true;
|
|
110
|
-
} else if (s === 'f') {
|
|
111
|
-
return false;
|
|
112
|
-
} else if (s === 'false') {
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return null;
|
|
117
|
-
} else {
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
var getValuesFromKeys = function getValuesFromKeys(object, keys) {
|
|
123
|
-
return keys.map(function (key) {
|
|
124
|
-
return object[key];
|
|
125
|
-
});
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
var identity = function identity(a) {
|
|
129
|
-
return a;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
var isEmpty = function isEmpty(value) {
|
|
133
|
-
return value === null || typeof value === 'undefined';
|
|
134
|
-
}; // type (int, text, etc)
|
|
135
|
-
// from Array of keys that map to records found (e.g., ['lon', 'lat'])
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
var getCoercionFunc = function getCoercionFunc(type, from, opts) {
|
|
139
|
-
var parse = opts.parse = opts.parse || identity;
|
|
140
|
-
|
|
141
|
-
switch (type) {
|
|
142
|
-
case 'int':
|
|
143
|
-
return function (record) {
|
|
144
|
-
var value = parse(record[from[0]]);
|
|
145
|
-
|
|
146
|
-
if (isEmpty(value)) {
|
|
147
|
-
return ast.Null({});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (!isNumeric(value)) {
|
|
151
|
-
return ast.Null({});
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
var val = ast.A_Const({
|
|
155
|
-
val: ast.Integer({
|
|
156
|
-
ival: value
|
|
157
|
-
})
|
|
158
|
-
});
|
|
159
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
case 'float':
|
|
163
|
-
return function (record) {
|
|
164
|
-
var value = parse(record[from[0]]);
|
|
165
|
-
|
|
166
|
-
if (isEmpty(value)) {
|
|
167
|
-
return ast.Null({});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (!isNumeric(value)) {
|
|
171
|
-
return ast.Null({});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
var val = ast.A_Const({
|
|
175
|
-
val: ast.Float({
|
|
176
|
-
str: value
|
|
177
|
-
})
|
|
178
|
-
});
|
|
179
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
case 'boolean':
|
|
183
|
-
case 'bool':
|
|
184
|
-
return function (record) {
|
|
185
|
-
var value = parse(parseBoolean(record[from[0]]));
|
|
186
|
-
|
|
187
|
-
if (isEmpty(value)) {
|
|
188
|
-
return ast.Null({});
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
var val = ast.String({
|
|
192
|
-
str: value ? 'TRUE' : 'FALSE'
|
|
193
|
-
});
|
|
194
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
case 'bbox':
|
|
198
|
-
// do bbox magic with args from the fields
|
|
199
|
-
return function (record) {
|
|
200
|
-
var val = (0, _utils.makeBoundingBox)(parse(record[from[0]]));
|
|
201
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
case 'location':
|
|
205
|
-
return function (record) {
|
|
206
|
-
var _getValuesFromKeys = getValuesFromKeys(record, from),
|
|
207
|
-
_getValuesFromKeys2 = (0, _slicedToArray2["default"])(_getValuesFromKeys, 2),
|
|
208
|
-
lon = _getValuesFromKeys2[0],
|
|
209
|
-
lat = _getValuesFromKeys2[1];
|
|
210
|
-
|
|
211
|
-
if (typeof lon === 'undefined') {
|
|
212
|
-
return ast.Null({});
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (typeof lat === 'undefined') {
|
|
216
|
-
return ast.Null({});
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (!isNumeric(lon) || !isNumeric(lat)) {
|
|
220
|
-
return ast.Null({});
|
|
221
|
-
} // NO parse here...
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
var val = (0, _utils.makeLocation)(lon, lat);
|
|
225
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
case 'related':
|
|
229
|
-
return function (record) {
|
|
230
|
-
return (0, _utils.getRelatedField)(_objectSpread(_objectSpread({}, opts), {}, {
|
|
231
|
-
record: record,
|
|
232
|
-
from: from
|
|
233
|
-
}));
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
case 'uuid':
|
|
237
|
-
return function (record) {
|
|
238
|
-
var value = parse(record[from[0]]);
|
|
239
|
-
|
|
240
|
-
if (isEmpty(value) || !/^([0-9a-fA-F]{8})-(([0-9a-fA-F]{4}-){3})([0-9a-fA-F]{12})$/i.test(value)) {
|
|
241
|
-
return ast.Null({});
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
var val = ast.A_Const({
|
|
245
|
-
val: ast.String({
|
|
246
|
-
str: value
|
|
247
|
-
})
|
|
248
|
-
});
|
|
249
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
case 'text':
|
|
253
|
-
return function (record) {
|
|
254
|
-
var value = parse(cleanseEmptyStrings(record[from[0]]));
|
|
255
|
-
|
|
256
|
-
if (isEmpty(value)) {
|
|
257
|
-
return ast.Null({});
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
var val = ast.A_Const({
|
|
261
|
-
val: ast.String({
|
|
262
|
-
str: value
|
|
263
|
-
})
|
|
264
|
-
});
|
|
265
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
case 'text[]':
|
|
269
|
-
return function (record) {
|
|
270
|
-
var value = parse(psqlArray(cleanseEmptyStrings(record[from[0]])));
|
|
271
|
-
|
|
272
|
-
if (isEmpty(value)) {
|
|
273
|
-
return ast.Null({});
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
var val = ast.A_Const({
|
|
277
|
-
val: ast.String({
|
|
278
|
-
str: value
|
|
279
|
-
})
|
|
280
|
-
});
|
|
281
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
case 'image':
|
|
285
|
-
case 'attachment':
|
|
286
|
-
case 'json':
|
|
287
|
-
case 'jsonb':
|
|
288
|
-
return function (record) {
|
|
289
|
-
var value = parse(parseJson(cleanseEmptyStrings(record[from[0]])));
|
|
290
|
-
|
|
291
|
-
if (isEmpty(value)) {
|
|
292
|
-
return ast.Null({});
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
var val = ast.A_Const({
|
|
296
|
-
val: ast.String({
|
|
297
|
-
str: value
|
|
298
|
-
})
|
|
299
|
-
});
|
|
300
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
default:
|
|
304
|
-
return function (record) {
|
|
305
|
-
var value = parse(cleanseEmptyStrings(record[from[0]]));
|
|
306
|
-
|
|
307
|
-
if (isEmpty(value)) {
|
|
308
|
-
return ast.Null({});
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
var val = ast.A_Const({
|
|
312
|
-
val: ast.String({
|
|
313
|
-
str: value
|
|
314
|
-
})
|
|
315
|
-
});
|
|
316
|
-
return (0, _utils.wrapValue)(val, opts);
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
var parseTypes = function parseTypes(config) {
|
|
322
|
-
return Object.entries(config.fields).reduce(function (m, v) {
|
|
323
|
-
var _v = (0, _slicedToArray2["default"])(v, 2),
|
|
324
|
-
key = _v[0],
|
|
325
|
-
value = _v[1];
|
|
326
|
-
|
|
327
|
-
var type;
|
|
328
|
-
var from;
|
|
329
|
-
|
|
330
|
-
if (typeof value === 'string') {
|
|
331
|
-
type = value;
|
|
332
|
-
from = [key];
|
|
333
|
-
|
|
334
|
-
if (['related', 'location'].includes(type)) {
|
|
335
|
-
throw new Error('must use object for ' + type + ' type');
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
value = {
|
|
339
|
-
type: type,
|
|
340
|
-
from: from
|
|
341
|
-
};
|
|
342
|
-
} else {
|
|
343
|
-
type = value.type;
|
|
344
|
-
from = getFromValue(value.from || key);
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
m[key] = getCoercionFunc(type, from, value);
|
|
348
|
-
return m;
|
|
349
|
-
}, {});
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
exports.parseTypes = parseTypes;
|
package/main/parser.js
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.Parser = void 0;
|
|
9
|
-
|
|
10
|
-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
11
|
-
|
|
12
|
-
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
13
|
-
|
|
14
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
15
|
-
|
|
16
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
17
|
-
|
|
18
|
-
var _fs = require("fs");
|
|
19
|
-
|
|
20
|
-
var _index = require("./index");
|
|
21
|
-
|
|
22
|
-
var _pgsqlDeparser = require("pgsql-deparser");
|
|
23
|
-
|
|
24
|
-
var _utils = require("./utils");
|
|
25
|
-
|
|
26
|
-
var Parser = /*#__PURE__*/function () {
|
|
27
|
-
function Parser(config) {
|
|
28
|
-
(0, _classCallCheck2["default"])(this, Parser);
|
|
29
|
-
this.config = config;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
(0, _createClass2["default"])(Parser, [{
|
|
33
|
-
key: "parse",
|
|
34
|
-
value: function () {
|
|
35
|
-
var _parse2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(data) {
|
|
36
|
-
var config, schema, table, singleStmts, conflict, headers, delimeter, opts, records, types, stmts, stmt;
|
|
37
|
-
return _regenerator["default"].wrap(function _callee$(_context) {
|
|
38
|
-
while (1) {
|
|
39
|
-
switch (_context.prev = _context.next) {
|
|
40
|
-
case 0:
|
|
41
|
-
config = this.config;
|
|
42
|
-
schema = config.schema, table = config.table, singleStmts = config.singleStmts, conflict = config.conflict, headers = config.headers, delimeter = config.delimeter;
|
|
43
|
-
opts = {};
|
|
44
|
-
if (headers) opts.headers = headers;
|
|
45
|
-
if (delimeter) opts.separator = delimeter;
|
|
46
|
-
|
|
47
|
-
if (!(typeof data === 'undefined')) {
|
|
48
|
-
_context.next = 15;
|
|
49
|
-
break;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (!(config.json || config.input.endsWith('.json'))) {
|
|
53
|
-
_context.next = 10;
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
records = JSON.parse((0, _fs.readFileSync)(config.input, 'utf-8'));
|
|
58
|
-
_context.next = 13;
|
|
59
|
-
break;
|
|
60
|
-
|
|
61
|
-
case 10:
|
|
62
|
-
_context.next = 12;
|
|
63
|
-
return (0, _index.parse)(config.input, opts);
|
|
64
|
-
|
|
65
|
-
case 12:
|
|
66
|
-
records = _context.sent;
|
|
67
|
-
|
|
68
|
-
case 13:
|
|
69
|
-
_context.next = 18;
|
|
70
|
-
break;
|
|
71
|
-
|
|
72
|
-
case 15:
|
|
73
|
-
if (Array.isArray(data)) {
|
|
74
|
-
_context.next = 17;
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
throw new Error('data is not an array');
|
|
79
|
-
|
|
80
|
-
case 17:
|
|
81
|
-
records = data;
|
|
82
|
-
|
|
83
|
-
case 18:
|
|
84
|
-
if (!config.debug) {
|
|
85
|
-
_context.next = 21;
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
console.log(records);
|
|
90
|
-
return _context.abrupt("return");
|
|
91
|
-
|
|
92
|
-
case 21:
|
|
93
|
-
types = (0, _index.parseTypes)(config);
|
|
94
|
-
|
|
95
|
-
if (!singleStmts) {
|
|
96
|
-
_context.next = 27;
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
stmts = records.map(function (record) {
|
|
101
|
-
return (0, _utils.InsertOne)({
|
|
102
|
-
schema: schema,
|
|
103
|
-
table: table,
|
|
104
|
-
types: types,
|
|
105
|
-
record: record,
|
|
106
|
-
conflict: conflict
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
return _context.abrupt("return", (0, _pgsqlDeparser.deparse)(stmts));
|
|
110
|
-
|
|
111
|
-
case 27:
|
|
112
|
-
stmt = (0, _utils.InsertMany)({
|
|
113
|
-
schema: schema,
|
|
114
|
-
table: table,
|
|
115
|
-
types: types,
|
|
116
|
-
records: records,
|
|
117
|
-
conflict: conflict
|
|
118
|
-
});
|
|
119
|
-
return _context.abrupt("return", (0, _pgsqlDeparser.deparse)([stmt]));
|
|
120
|
-
|
|
121
|
-
case 29:
|
|
122
|
-
case "end":
|
|
123
|
-
return _context.stop();
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}, _callee, this);
|
|
127
|
-
}));
|
|
128
|
-
|
|
129
|
-
function parse(_x) {
|
|
130
|
-
return _parse2.apply(this, arguments);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return parse;
|
|
134
|
-
}()
|
|
135
|
-
}]);
|
|
136
|
-
return Parser;
|
|
137
|
-
}();
|
|
138
|
-
|
|
139
|
-
exports.Parser = Parser;
|