Skip to content

Commit acaaeff

Browse files
committed
added example to build schema from file
file needs to be newline-delimited json format (like mongoexport produces).
1 parent 8b63396 commit acaaeff

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

examples/parse-from-file.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var Schema = require('../lib/schema');
2+
var fs = require('fs');
3+
var es = require('event-stream');
4+
5+
var ts = new Date();
6+
7+
// create new schema object with fake namespace
8+
var schema = new Schema({
9+
ns: 'perf.testing'
10+
});
11+
12+
if (process.argv.length < 3) {
13+
console.log('Usage: node parse-from-file.js <jsonfile>');
14+
} else {
15+
fs.createReadStream(process.argv[2], {
16+
flags: 'r'
17+
})
18+
.pipe(es.split()) // split file into individual json docs (one per line)
19+
.pipe(es.parse()) // parse each doc
20+
.pipe(schema.stream()) // comment out this line to skip schema parsing
21+
.pipe(es.stringify()) // stringify result
22+
.pipe(es.wait(function(err, res) { // assemble everything back together
23+
if (err) {
24+
throw err;
25+
}
26+
var dur = new Date() - ts;
27+
console.log(res);
28+
console.log('took ' + dur + 'ms.'); // log time it took to parse
29+
}));
30+
}

0 commit comments

Comments
 (0)