Skip to content

Commit

Permalink
Merge pull request lifeomic#1 in PHI/phi_cli from feature/PHD-417-jme…
Browse files Browse the repository at this point in the history
…spath-csv to master

* commit 'f82c2c4e5c0f71ea3d1c2e87d40eae74bff2477b':
  PHD-417 restored build.sh
  PHD-417 Added --jsonLine to make line per resource output and --csv to support a json to CSV conversion via a specification with the mappings of fhir jmesPath to column mapping.
  PHD-417 Updated list to dump output per bundle rather than collect them all.
  • Loading branch information
taylordeatri committed May 14, 2019
2 parents 8ceed45 + f82c2c4 commit 09a0b25
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
66 changes: 59 additions & 7 deletions lib/cmds/fhir_cmds/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const querystring = require('querystring');
const { post, getAccount } = require('../../fhir');
const print = require('../../print');
const url = require('url');
const jmespath = require('jmespath');
const fs = require('fs');

exports.command = 'list <type>';
exports.desc = 'List FHIR resources by type <type>.';
Expand All @@ -22,13 +24,29 @@ exports.builder = yargs => {
describe: 'Number of resources to return',
type: 'integer',
default: 1000
}).option('csv', {
describe: 'CSV Format Configuration file in json',
type: 'string'
});
};

async function search (type, query, options) {
const account = getAccount(options);
const result = [];
const limit = options.limit;
const csvConfigFile = options.csv;

const csvConfig = csvConfigFile != null ? JSON.parse(fs.readFileSync(csvConfigFile, 'UTF-8')) : null;

if (csvConfig && csvConfig.header) {
let header = '';
csvConfig.fieldMaps.forEach(fieldMap => {
if (header.length > 0) {
header = header + ',';
}
header = header + fieldMap.columnName;
});
console.log(header);
}

if (query) {
query.pageSize = limit;
Expand All @@ -46,22 +64,57 @@ async function search (type, query, options) {
);

const data = response.data;
result.push(...data.entry.map(x => x.resource));

if (result.length < limit && data.link) {
const results = data.entry.map(x => x.resource);

if (results.length > 0) {
if (csvConfig) {
const linesResult = formatResults(results, csvConfig, options);
linesResult.forEach(line => {
console.log(line);
});
} else {
results.forEach(result => print(result, options));
}
}

if (results.length < limit && data.link) {
const next = data.link.find(x => x.relation === 'next');
if (next) {
const parsedUrl = url.parse(next.url, true);
query = Object.assign(query, parsedUrl.query);
} else {
return result;
return;
}
} else {
return result;
return;
}
}
}

function formatResults (results, csvConfig, options) {
const lines = [];
results.forEach(result => {
const fldD = csvConfig.fieldDelimiter;
const valueD = csvConfig.valueDelimiter;
const fields = [];
csvConfig.fieldMaps.forEach(fieldMap => {
const value = jmespath.search(result, fieldMap.jpath)
if ( value && value.length >0 ) {
fields.push(valueD + value + valueD);
} else {
fields.push('');
}
});
let line = '';
fields.forEach(field => {
line = line + field + fldD;
});
lines.push(line.substring(0, line.length - 1));
});
return lines;
}

exports.handler = async argv => {
if (argv.query) {
argv.query = querystring.parse(argv.query);
Expand All @@ -77,6 +130,5 @@ exports.handler = async argv => {
}
}

const result = await search(argv.type, argv.query, argv);
print(result, argv);
await search(argv.type, argv.query, argv);
};
3 changes: 3 additions & 0 deletions lib/common-yargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports = (yargs) => {
.option('json', {
describe: 'List output as JSON',
type: 'boolean'
}).option('jsonLine', {
describe: 'List output as JSON LINE Format',
type: 'boolean'
}).option('account', {
describe: 'Override the account',
type: 'string'
Expand Down
2 changes: 2 additions & 0 deletions lib/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const yaml = require('js-yaml');
module.exports = function (data, options) {
if (options.json) {
console.log(JSON.stringify(data, null, 2));
} else if (options.jsonLine) {
console.log(JSON.stringify(data, null, 0));
} else {
console.log(yaml.safeDump(data));
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"debug": "^3.1.0",
"get-stdin": "^6.0.0",
"inquirer": "^5.2.0",
"jmespath": "^0.15.0",
"js-yaml": "^3.13.1",
"jsonwebtoken": "^8.2.0",
"lodash": "^4.17.5",
Expand Down

0 comments on commit 09a0b25

Please sign in to comment.