Skip to content

Commit a2400ad

Browse files
committed
Merge pull request #31 from CfABrigadePhiladelphia/import_stream
import data using createImportStream
2 parents 6f73359 + 1f2e0ea commit a2400ad

File tree

7 files changed

+125
-9
lines changed

7 files changed

+125
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
tmp
22
node_modules
3+
data.jawn

index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
module.exports = Jawn
2-
function Jawn () {}
3-
Jawn.prototype.import = function (data) {
4-
return 'Hello World'
5-
}
1+
var level = require('level')
2+
var hypercore = require('hypercore')
3+
var createImportStream = require('./lib/import.js')
4+
5+
module.exports = Jawn
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
12+
}
13+
14+
Jawn.prototype.createImportStream = function (opts) {
15+
return createImportStream(this, opts)
16+
}

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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@
2121
},
2222
"homepage": "https://github.com/CfABrigadePhiladelphia/jawn",
2323
"dependencies": {
24-
"hypercore": "^1.6.2"
24+
"fs": "0.0.2",
25+
"hypercore": "~1.8.0",
26+
"level": "~1.4.0",
27+
"parse-input-stream": "^1.0.1",
28+
"path": "~0.12.7",
29+
"pump": "^1.0.1",
30+
"tape": "~4.5.1",
31+
"through2": "^2.0.1"
2532
},
2633
"devDependencies": {
2734
"standard": "^6.0.5",
28-
"tape": "^4.4.0"
35+
"tape": "^4.4.0",
36+
"memdb": "^1.3.1"
2937
}
3038
}

test/data/sample.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Type of Experience,Little/No Experience,Some Experience,Very Familiar
2+
Writing software in any programming language,1,5,4
3+
Frontend Web Development,4,3,3
4+
Server-side (“backend”) Web Development,4,4,2
5+
"Using Git to track changes and share code (add, commit, push, pull)",2,5,3

test/import.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var test = require('tape')
2+
var Jawn = require('../')
3+
var memdb = require('memdb')
4+
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"}')
17+
18+
// This should be expecting JSON objects, not strings.
19+
// temporarily expecting strings in order to hand off the code as-is
20+
var expected = [
21+
'{foo: "bar", name: "josie"}',
22+
'{foo: "baz", name: "eloise"}',
23+
'{foo: "baz", name: "francoise"}'
24+
]
25+
26+
importStream.end(function () {
27+
var feedId = importStream.id.toString('hex')
28+
var rs = jawn.core.createReadStream(feedId)
29+
rs.on('data', function (block) {
30+
t.same(block.toString(), expected.shift(), 'block matches imported line')
31+
})
32+
t.same(jawn.core.get(feedId).blocks, 3, 'correct number of blocks returned')
33+
t.end()
34+
})
35+
})
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)