Skip to content

Commit 643044f

Browse files
committed
Current work; move to cloud
1 parent 86e6cd9 commit 643044f

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
var level = require('level')
2+
var hypercore = require('hypercore')
23

34
var hyperkv = require('hyperkv')
45
var hyperlog = require('hyperlog')
56
var sub = require('subleveldown')
67

7-
// var createImportPipeline = require('./lib/import.js')
8+
var createImportPipeline = require('./lib/import.js')
9+
var importRowKv = require('./lib/importkv.js')
810
var addRow = require('./lib/add.js')
911
var deleteRow = require('./lib/delete.js')
1012

1113
module.exports = Jawn
1214

1315
function Jawn (opts) {
1416
if (!opts) opts = {}
17+
this.core = initializeHypercore(opts)
1518
this.kv = initializeHyperkv(opts)
1619
this.db = this.core.db
20+
this.db_kv = this.kv.db
1721
}
1822

19-
/*
2023
Jawn.prototype.createImportPipeline = function (opts) {
2124
return createImportPipeline(this, opts)
2225
}
23-
*/
26+
27+
Jawn.prototype.importRowKv = function (file) {
28+
return importRowKv(this, file)
29+
}
2430

2531
Jawn.prototype.addRow = function (key, value) {
2632
return addRow(this, key, value)
@@ -34,6 +40,17 @@ Jawn.prototype.deleteRow = function (key) {
3440
// @default Creates a leveldb database called `data.jawn` and initializes hypercore using that db
3541
// @option 'core' the hypercore instance to use
3642
// @option 'db' the db instace (leveldb) to initialize hypercore with. This is ignored if you use the `core` option
43+
function initializeHypercore (opts) {
44+
var core
45+
if (opts.hasOwnProperty('core')) {
46+
core = opts.core
47+
} else {
48+
var db = opts.db || level('data.jawn')
49+
core = hypercore(db)
50+
}
51+
return core
52+
}
53+
3754
function initializeHyperkv (opts) {
3855
var kv
3956
if (opts.hasOwnProperty('core')) {

lib/add.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module.exports = addRow
33
function addRow (jawn, key, value) {
44
jawn.kv.put(key, JSON.stringify(value), function (err, node) {
55
if (err) console.error(err)
6-
else console.log(node.key)
6+
else console.log(node.key + ': ' + node.value)
77
})
88
}

test/add.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var test = require('tape')
2+
var Jawn = require('../')
3+
var memdb = require('memdb')
4+
5+
test('add 3 rows', function (t) {
6+
var jawn = freshJawn()
7+
var testValues = [
8+
'{"foo":"bar","name":"josie","age":"35"}',
9+
'{"foo":"baz","name":"eloise","age":"71"}',
10+
'{"foo":"baz","name":"francoise","age":"5"}'
11+
]
12+
/*
13+
var expected = [
14+
'0: {"foo":"bar","name":"josie","age":"35"}',
15+
'1: {"foo":"baz","name":"eloise","age":"71"}',
16+
'2: {"foo":"baz","name":"francoise","age":"5"}'
17+
]
18+
*/
19+
20+
for (var i = 0; testValues.length; i++) {
21+
jawn.addRow(i, testValues[i])
22+
}
23+
24+
jawn.kv.on('put', function (key, value, node) {
25+
console.log(key + ': ' + value)
26+
})
27+
})
28+
29+
function freshJawn () {
30+
return new Jawn({db: memdb()})
31+
}

0 commit comments

Comments
 (0)