Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.

Commit 01008b3

Browse files
committed
add file option to write to fs
1 parent de75346 commit 01008b3

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,46 @@ var DatJSON = require('dat-json')
1919

2020
var datjson = DatJSON(archive)
2121

22+
datjson.create({title: 'a dat', description: 'exciting'}, function (err) {
23+
if (err) throw err
24+
})
25+
26+
datjson.read(function (err, data) {
27+
console.log(data)
28+
})
29+
```
30+
31+
Write to a `dat.json` on the file system also:
32+
33+
```js
34+
var DatJSON = require('dat-json')
35+
36+
var datjson = DatJSON(archive, {file: path.join(dat.path, 'dat.json')})
37+
2238
datjson.create({title: 'a dat', description: 'exciting'}, function (err) {
2339
if (err) throw err
2440
})
2541
```
2642

43+
**TODO: replace file option with hyperdrive indexing**
44+
2745
## API
2846

29-
### `var datjson = DatJSON(archive)`
47+
### `var datjson = DatJSON(archive, [opts])`
48+
49+
create a new datJson db
50+
51+
Options:
52+
53+
* `opts.file` - dat.json file path, updates will be written to file system and archive
3054

3155
#### `datjson.create([data], cb)`
3256

33-
Create a new `dat.json` file in the archive with the default keys (`name`, `url`, `title`, `description`). Pass in any additional data to add on initial create.
57+
Create a new `dat.json` file in the archive with the default keys (`url`, `title`, `description`). Pass in any additional data to add on initial create.
3458

3559
#### `datjson.write(key, val, cb)` or `datjson.write(data, cb)`
3660

37-
Write a single `key` and `value` or an object, `data`, to the `dat.json` file.
61+
Write a single `key` and `value` or an object, `data`, to the `dat.json` file. Use `file` option above to also update the file on the file system.
3862

3963
#### `datjson.delete(key, cb)`
4064

index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ var stringKey = require('dat-encoding').toStr
33
var xtend = require('xtend')
44
var toiletdb = require('toiletdb')
55

6-
module.exports = function (archive) {
6+
module.exports = function (archive, opts) {
7+
if (!opts) opts = {}
8+
79
var db = toiletdb({name: '/dat.json', fs: archive})
10+
var fileDb = opts.file ? toiletdb(opts.file) : null
11+
812
var that = {
913
read: function (cb) {
1014
archive.stat('/dat.json', function (err, stat) {
@@ -16,7 +20,15 @@ module.exports = function (archive) {
1620
if (!archive.writable) return cb(new Error('Archive not writable'))
1721
if (typeof key === 'object') return writeAll(key, val) // assume val = cb
1822
// TODO: validate things
19-
db.write(key, val, cb)
23+
if (!fileDb) return db.write(key, val, cb)
24+
25+
// write to file then archive
26+
// TODO: use hyperdrive indexing false option, need to talk to mafintosh about
27+
// https://botbot.me/freenode/dat/2017-05-12/?msg=85554242&page=3
28+
fileDb.write(key, val, function (err) {
29+
if (err) return cb(err)
30+
db.write(key, val, cb)
31+
})
2032
},
2133
delete: db.delete,
2234
create: function (data, cb) {

test/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var fs = require('fs')
2+
var path = require('path')
13
var test = require('tape')
24
var hyperdrive = require('hyperdrive')
35
var ram = require('random-access-memory')
@@ -45,3 +47,39 @@ test('Write dat.json to archive', function (t) {
4547
})
4648
})
4749
})
50+
51+
test('Write dat.json to file and archive', function (t) {
52+
var archive = hyperdrive(ram)
53+
var file = path.join(__dirname, 'dat.json')
54+
archive.ready(function () {
55+
var datjson = datJSON(archive, {file: file})
56+
datjson.create(function (err) {
57+
t.error(err, 'no error')
58+
datjson.write({specialVal: 'cat'}, checkFile)
59+
60+
function checkFile (err) {
61+
t.error(err, 'no error')
62+
fs.readFile(file, 'utf-8', function (err, data) {
63+
data = JSON.parse(data)
64+
t.error(err, 'fs no error')
65+
t.ok(data, 'fs has metadata')
66+
t.same(data.url, `dat://${archive.key.toString('hex')}`, 'fs url ok')
67+
t.same(data.specialVal, 'cat', 'fs has special value')
68+
fs.unlinkSync(file)
69+
checkRead()
70+
})
71+
}
72+
73+
function checkRead (err) {
74+
t.error(err, 'no error')
75+
datjson.read(function (err, data) {
76+
t.error(err, 'no error')
77+
t.ok(data, 'has metadata')
78+
t.same(data.url, `dat://${archive.key.toString('hex')}`, 'url ok')
79+
t.same(data.specialVal, 'cat', 'has special value')
80+
t.end()
81+
})
82+
}
83+
})
84+
})
85+
})

0 commit comments

Comments
 (0)