Skip to content

Commit b2da350

Browse files
committed
Merge pull request #36 from benjaminettori/master
Return stream from import.js
2 parents c60b777 + 3ab64dc commit b2da350

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ function Jawn (opts) {
1111
this.db = this.core.db
1212
}
1313

14-
Jawn.prototype.createImportPipeline = function (opts, callback) {
15-
return createImportPipeline(this, opts, callback)
14+
Jawn.prototype.createImportPipeline = function (opts) {
15+
return createImportPipeline(this, opts)
1616
}

lib/import.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ module.exports = importPipeline
77
// Returns the parser at the beginning of the pipeline
88
// When the pipeline finishes, it calls `callback(feedId, err)`.
99
// If there were no errors, err will be undefined.
10-
function importPipeline (jawn, opts, callback) {
10+
function importPipeline (jawn, opts) {
1111
if (!opts) opts = {}
1212
var writeStream = jawn.core.createWriteStream(opts)
1313
var parser = parseInputStream(opts)
1414
var transform = through.obj(stringifyData, end)
1515

16-
miss.pipe(parser, transform, writeStream, function done (err) {
17-
callback(err, writeStream.id)
18-
})
16+
var importStream = miss.pipeline(parser, transform, writeStream)
1917

2018
function stringifyData (data, enc, next) {
2119
this.push(JSON.stringify(data))
@@ -26,5 +24,7 @@ function importPipeline (jawn, opts, callback) {
2624
done()
2725
}
2826

29-
return parser
27+
importStream.writeStream = writeStream
28+
29+
return importStream
3030
}

test/import.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ var memdb = require('memdb')
66

77
test('import json to jawn', function (t) {
88
var jawn = freshJawn()
9-
importFromFile(jawn, 'dummy.json', {'format': 'json'}, verify)
9+
var importStream = importFromFile(jawn, 'dummy.json', {'format': 'json'})
1010
var expected = [
1111
'{"foo":"bar","name":"josie","age":"35"}',
1212
'{"foo":"baz","name":"eloise","age":"71"}',
1313
'{"foo":"baz","name":"francoise","age":"5"}'
1414
]
15-
function verify (err, feedId) {
15+
16+
importStream.on('finish', verify)
17+
18+
function verify (err) {
1619
if (err) { console.log(err) }
20+
var feedId = importStream.writeStream.id
1721
var rs = jawn.core.createReadStream(feedId)
1822
rs.on('data', function (block) {
1923
t.same(block.toString(), expected.shift(), 'block matches imported line')
@@ -25,16 +29,20 @@ test('import json to jawn', function (t) {
2529

2630
test('import csv to jawn', function (t) {
2731
var jawn = freshJawn()
28-
importFromFile(jawn, 'sample.csv', {'format': 'csv'}, verify)
32+
importFromFile(jawn, 'sample.csv', {'format': 'csv'})
33+
var importStream = importFromFile(jawn, 'sample.csv', {'format': 'csv'}, verify)
2934
var expected = [
3035
'{"Type of Experience":"Writing software in any programming language","Little/No Experience":"1","Some Experience":"5","Very Familiar":"4"}',
3136
'{"Type of Experience":"Frontend Web Development","Little/No Experience":"4","Some Experience":"3","Very Familiar":"3"}',
3237
'{"Type of Experience":"Server-side (“backend”) Web Development","Little/No Experience":"4","Some Experience":"4","Very Familiar":"2"}',
3338
'{"Type of Experience":"Using Git to track changes and share code (add, commit, push, pull)","Little/No Experience":"2","Some Experience":"5","Very Familiar":"3"}'
3439
]
3540

36-
function verify (err, feedId) {
41+
importStream.on('finish', verify)
42+
43+
function verify (err) {
3744
if (err) { console.log(err) }
45+
var feedId = importStream.writeStream.id
3846
var rs = jawn.core.createReadStream(feedId)
3947
rs.on('data', function (block) {
4048
t.same(block.toString(), expected.shift(), 'block matches imported line')
@@ -54,8 +62,9 @@ function freshJawn () {
5462
return new Jawn({db: memdb()})
5563
}
5664

57-
function importFromFile (jawn, file, opts, callback) {
58-
var importPipeline = jawn.createImportPipeline(opts, callback)
65+
function importFromFile (jawn, file, opts) {
66+
var importPipeline = jawn.createImportPipeline(opts)
5967
var data = fs.createReadStream(fixture(file))
6068
data.pipe(importPipeline)
69+
return importPipeline
6170
}

0 commit comments

Comments
 (0)