-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdateSourceRecords.js
More file actions
56 lines (46 loc) · 1.57 KB
/
updateSourceRecords.js
File metadata and controls
56 lines (46 loc) · 1.57 KB
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
/*
This script will take a collection in JSONL format of source records and make PUT requests based on the matchedId
*/
const fs = require('fs');
const superagent = require('superagent');
const { getAuthToken } = require('./lib/login');
const readline = require('readline');
let inFile = process.argv[2];
let match = process.argv[3] || 'id';
(async () => {
try {
let inData;
if (!inFile) {
throw new Error('Usage: node updateSourceRecords.js <source_record_collection.jsonl> [ <match field> ]');
} else if (!fs.existsSync(inFile)) {
throw new Error('Can\'t find input file');
}
const config = (fs.existsSync('./config.js')) ? require('./config.js') : require('./config.default.js');
const authToken = await getAuthToken(superagent, config.okapi, config.tenant, config.authpath, config.username, config.password);
const fileStream = fs.createReadStream(inFile);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
let x = 0;
for await (const line of rl) {
x++;
let rec = JSON.parse(line);
rec.generation++;
let url = `${config.okapi}/source-storage/records/${rec[match]}`;
console.log(`# ${x} PUT to ${url}`);
try {
let res = await superagent
.put(url)
.send(rec)
.set('x-okapi-token', authToken)
.set('content-type', 'application/json')
.set('accept', 'application/json');
} catch (e) {
console.log(e.response || e);
}
}
} catch (e) {
console.log(e.message);
}
})();