Skip to content

Commit 1f2e0ea

Browse files
author
Matt Zumwalt
committed
import data using a stream from createImportStream
1 parent 6c3b64c commit 1f2e0ea

File tree

7 files changed

+96
-40
lines changed

7 files changed

+96
-40
lines changed

index.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
var level = require('level')
22
var hypercore = require('hypercore')
3+
var createImportStream = require('./lib/import.js')
34

45
module.exports = Jawn
5-
function Jawn () {
6-
var self = this
7-
self.db = level('data.jawn')
8-
self.core = hypercore(self.db)
6+
7+
function Jawn (opts) {
8+
if (!opts) opts = {}
9+
var db = opts.db || level('data.jawn')
10+
this.core = opts.core || hypercore(db)
11+
this.db = this.core.db
912
}
1013

11-
Jawn.prototype.import = function (stream) {
12-
var ws = this.core.createWriteStream()
13-
stream.on('data', function (line) {
14-
ws.write(line)
15-
})
16-
stream.on('end', function () {
17-
ws.end(function () {
18-
console.log(ws.id.toString('hex'))
19-
})
20-
})
21-
return ws
14+
Jawn.prototype.createImportStream = function (opts) {
15+
return createImportStream(this, opts)
2216
}

lib/import.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var pump = require('pump')
2+
var through = require('through2')
3+
4+
module.exports = importStream
5+
6+
function importStream (jawn, opts) {
7+
if (!opts) opts = {}
8+
var writeStream = jawn.core.createWriteStream(opts)
9+
var stream = pump(parseInputStream(opts), writeStream, function done (err) {
10+
stream.id = writeStream.id
11+
console.log(err)
12+
})
13+
return stream
14+
}
15+
16+
// Transform input CSV, JSON, etc to json objects that will be written as blocks in our hypercore feed
17+
// TODO: Implement this! See https://github.com/CfABrigadePhiladelphia/jawn/issues/32
18+
// This is the functionality that parse-input-stream aims to support.
19+
// If you can get that module to work, feel free to use it!
20+
function parseInputStream (opts) {
21+
var transformStream = through(write, end)
22+
return transformStream
23+
24+
function write (buffer, encoding, next) {
25+
// transform input CSV, JSON, etc. here ...
26+
next()
27+
}
28+
29+
function end (done) {
30+
done()
31+
}
32+
}

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@
2121
},
2222
"homepage": "https://github.com/CfABrigadePhiladelphia/jawn",
2323
"dependencies": {
24-
"hypercore": "~1.8.0",
2524
"fs": "0.0.2",
25+
"hypercore": "~1.8.0",
26+
"level": "~1.4.0",
27+
"parse-input-stream": "^1.0.1",
2628
"path": "~0.12.7",
29+
"pump": "^1.0.1",
2730
"tape": "~4.5.1",
28-
"level": "~1.4.0"
31+
"through2": "^2.0.1"
2932
},
3033
"devDependencies": {
3134
"standard": "^6.0.5",
32-
"tape": "^4.4.0"
35+
"tape": "^4.4.0",
36+
"memdb": "^1.3.1"
3337
}
3438
}

test.csv

Lines changed: 0 additions & 4 deletions
This file was deleted.
File renamed without changes.

test/import.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
var test = require('tape')
22
var Jawn = require('../')
3-
var fs = require('fs')
3+
var memdb = require('memdb')
44

5-
var j = new Jawn()
5+
test('import json to jawn', function (t) {
6+
var jawn = freshJawn()
7+
var importStream = jawn.createImportStream({'format': 'json'})
8+
// Imitate the stream that would come from reading sample.csv
9+
// importStream should parse the CSV correctly, identifying the first row as headers
10+
// This is the same as doing
11+
// var data = fs.createReadStream('./test/data/sample.csv')
12+
// data.pipe(importStream)
13+
// except the writes are being performed synchronously/inline so we can call importStream.end() after writing the contents into it.
14+
importStream.write('{foo: "bar", name: "josie"}')
15+
importStream.write('{foo: "baz", name: "eloise"}')
16+
importStream.write('{foo: "baz", name: "francoise"}')
617

7-
test('import csv to jawn', function (t) {
8-
var data = fs.createReadStream('./test/sample/sample.csv')
9-
var ws = j.import(data)
18+
// This should be expecting JSON objects, not strings.
19+
// temporarily expecting strings in order to hand off the code as-is
1020
var expected = [
11-
{'Type of Experience': 'Writing software in any programming language', 'Little/No Experience': 1, 'Some Experience': 5, 'Very Familiar': 4},
12-
{'Type of Experience': 'Frontend Web Development', 'Little/No Experience': 4, 'Some Experience': 3, 'Very Familiar': 3},
13-
{'Type of Experience': 'Server-side (backend) Web Development', 'Little/No Experience': 4, 'Some Experience': 4, 'Very Familiar': 2},
14-
{'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}
21+
'{foo: "bar", name: "josie"}',
22+
'{foo: "baz", name: "eloise"}',
23+
'{foo: "baz", name: "francoise"}'
1524
]
16-
ws.on('end', function () {
17-
var feedId = ws.id.toString('hex')
18-
var rs = j.core.createReadStream(feedId)
1925

26+
importStream.end(function () {
27+
var feedId = importStream.id.toString('hex')
28+
var rs = jawn.core.createReadStream(feedId)
2029
rs.on('data', function (block) {
2130
t.same(block.toString(), expected.shift(), 'block matches imported line')
2231
})
23-
24-
var blocks = j.core.get(feedId).blocks
25-
26-
t.same(blocks, 4, 'correct number of blocks returned')
32+
t.same(jawn.core.get(feedId).blocks, 3, 'correct number of blocks returned')
2733
t.end()
2834
})
2935
})
36+
37+
function freshJawn () {
38+
return new Jawn({db: memdb()})
39+
}

test/jawn.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
var test = require('tape')
22
var Jawn = require('../index')
3-
test('jawn import', function (t) {
4-
t.equal(new Jawn().import('some data'), 'Hello World')
3+
var memdb = require('memdb')
4+
var hypercore = require('hypercore')
5+
6+
test('jawn constructor defaults', function (t) {
7+
var jawn = new Jawn({db: memdb()})
8+
t.true(jawn.core instanceof hypercore, 'core is an instance of Hypercore')
9+
// t.true(jawn.db instanceof leveldb, 'db is an instance of leveldb')
10+
t.equal(jawn.db, jawn.core.db, 'jawn db matches core db')
11+
t.end()
12+
})
13+
14+
test('jawn constructor set db', function (t) {
15+
var sampledb = memdb()
16+
var jawn = new Jawn({db: sampledb})
17+
t.equal(jawn.core.db, sampledb, 'initializes hypercore with the provided db')
18+
t.end()
19+
})
20+
21+
test('jawn constructor set core', function (t) {
22+
var samplecore = hypercore(memdb())
23+
var jawn = new Jawn({core: samplecore})
24+
t.equal(jawn.core, samplecore, 'uses the provided instance of hypercore')
525
t.end()
626
})

0 commit comments

Comments
 (0)