Skip to content

Commit

Permalink
Using JSONStream when writing JSON file for potentially giant JSON ob…
Browse files Browse the repository at this point in the history
…jects
  • Loading branch information
seanmcilvenna committed Jan 19, 2023
1 parent 1fa225d commit a75101d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1,184 deletions.
27 changes: 17 additions & 10 deletions export.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion export.js.map

Large diffs are not rendered by default.

26 changes: 16 additions & 10 deletions export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {IBundle} from "./fhir/bundle";
import {getFhirInstance} from "./helper";
import {Auth} from "./auth";
import {CoreOptions} from "request";
import {stringify, stringifyObject} from 'jsonstream';

export class ExportOptions {
public fhir_base: string;
Expand Down Expand Up @@ -326,18 +327,23 @@ export class Export {
console.log('Done exporting history for resources');
}

let outputContent: string;
if (shouldOutput) {
if (this.options.xml) {
let fhir = getFhirInstance(this.version);
const outputContent = fhir.objToXml(this.exportBundle);
fs.writeFileSync(this.options.out_file, outputContent);
} else {
if (fs.existsSync(this.options.out_file)) {
fs.unlinkSync(this.options.out_file);
}

if (this.options.xml) {
let fhir = getFhirInstance(this.version);
outputContent = fhir.objToXml(this.exportBundle);
} else {
outputContent = JSON.stringify(this.exportBundle);
}
const st = stringify();
st.pipe(fs.createWriteStream(this.options.out_file));
st.write(this.exportBundle as any);
st.end();
}

if (shouldOutput) {
fs.writeFileSync(this.options.out_file, outputContent);
console.log(`Created file ${this.options.out_file} with a Bundle of ${this.exportBundle.total} entries`);
console.log('done!');
}
}

Expand Down
Loading

0 comments on commit a75101d

Please sign in to comment.