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

Commit b51d819

Browse files
authored
update to use promises (#4)
* update to use promises * update docs * update toiletdb
1 parent 0dc61ec commit b51d819

File tree

4 files changed

+170
-148
lines changed

4 files changed

+170
-148
lines changed

README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ 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-
})
22+
await datjson.create({title: 'a dat', description: 'exciting'})
2523

26-
datjson.read(function (err, data) {
27-
console.log(data)
28-
})
24+
console.log(await datjson.read())
2925
```
3026

3127
Write to a `dat.json` on the file system also:
@@ -35,9 +31,7 @@ var DatJSON = require('dat-json')
3531

3632
var datjson = DatJSON(archive, {file: path.join(dat.path, 'dat.json')})
3733

38-
datjson.create({title: 'a dat', description: 'exciting'}, function (err) {
39-
if (err) throw err
40-
})
34+
await datjson.create({title: 'a dat', description: 'exciting'})
4135
```
4236

4337
**TODO: replace file option with hyperdrive indexing**
@@ -52,19 +46,19 @@ Options:
5246

5347
* `opts.file` - dat.json file path, updates will be written to file system and archive
5448

55-
#### `datjson.create([data], cb)`
49+
#### `await datjson.create([data])`
5650

5751
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.
5852

59-
#### `datjson.write(key, val, cb)` or `datjson.write(data, cb)`
53+
#### `await datjson.write(key, val)` or `await datjson.write(data)`
6054

6155
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.
6256

63-
#### `datjson.delete(key, cb)`
57+
#### `await datjson.delete(key)`
6458

6559
Delete a `key` from the `dat.json` file.
6660

67-
#### `datjson.read(cb)`
61+
#### `await datjson.read()`
6862

6963
Read the current `dat.json`.
7064

index.js

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,108 @@
1-
var stringKey = require('dat-encoding').toStr
2-
// var path = require('path')
3-
var xtend = Object.assign
4-
var toiletdb = require('toiletdb')
1+
const stringKey = require('dat-encoding').toStr
2+
const xtend = Object.assign
3+
const toiletdb = require('toiletdb')
54

6-
module.exports = function (archive, opts) {
7-
if (!opts) opts = {}
5+
module.exports = (...args) => new DatJsonDb(...args)
86

9-
var db = toiletdb({ name: '/dat.json', fs: archive })
10-
var fileDb = opts.file ? toiletdb(opts.file) : null
7+
class DatJsonDb {
8+
constructor (archive, opts) {
9+
if (!opts) opts = {}
10+
this.archive = archive
11+
this.db = toiletdb({ name: '/dat.json', fs: archive })
12+
this.fileDb = opts.file ? toiletdb(opts.file) : null
13+
}
1114

12-
var that = {
13-
read: function (cb) {
14-
archive.stat('/dat.json', function (err, stat) {
15-
if (err) return cb(err)
16-
db.read(cb)
15+
read () {
16+
const self = this
17+
return new Promise((resolve, reject) => {
18+
this.archive.stat('/dat.json', async function (err, stat) {
19+
if (err) return reject(err)
20+
resolve(await self.db.read())
1721
})
18-
},
19-
write: function (key, val, cb) {
20-
if (typeof val === 'function') cb = val
21-
if (!archive.writable) {
22-
return process.nextTick(cb, new Error('Archive not writable'))
22+
})
23+
}
24+
25+
write (key, val) {
26+
return new Promise(async (resolve, reject) => {
27+
if (!this.archive.writable) {
28+
return reject(new Error('Archive not writable'))
2329
}
24-
if (typeof key === 'object') return writeAll(key, cb)
30+
31+
if (typeof key === 'object') {
32+
try {
33+
await this._writeAll(key)
34+
resolve()
35+
} catch (e) {
36+
reject(e)
37+
}
38+
return
39+
}
40+
2541
// TODO: validate things
26-
if (!fileDb) return db.write(key, val, cb)
42+
if (!this.fileDb) {
43+
try {
44+
await this.db.write(key, val)
45+
resolve()
46+
} catch (e) {
47+
reject(e)
48+
}
49+
return
50+
}
2751

2852
// write to file then archive
2953
// TODO: use hyperdrive indexing false option, need to talk to mafintosh about
3054
// https://botbot.me/freenode/dat/2017-05-12/?msg=85554242&page=3
31-
fileDb.write(key, val, function (err) {
32-
if (err) return cb(err)
33-
db.write(key, val, cb)
34-
})
35-
},
36-
delete: db.delete,
37-
create: function (data, cb) {
38-
if (typeof data === 'function') return that.create(null, data)
39-
if (!archive.writable) {
40-
return process.nextTick(cb, new Error('Archive not writable'))
55+
try {
56+
await this.fileDb.write(key, val)
57+
await this.db.write(key, val)
58+
resolve()
59+
} catch (e) {
60+
reject(e)
4161
}
42-
data = xtend(getdefaults(), data)
43-
that.write(data, cb)
44-
}
62+
})
4563
}
4664

47-
return that
65+
async delete (key) {
66+
await this.db.delete(key)
67+
}
4868

49-
function getdefaults () {
50-
return {
51-
title: '',
52-
description: '',
53-
url: 'dat://' + stringKey(archive.key)
54-
}
69+
create (data) {
70+
return new Promise(async (resolve, reject) => {
71+
if (!this.archive.writable) {
72+
return reject(new Error('Archive not writable'))
73+
}
74+
75+
data = xtend(this.getdefaults(), data)
76+
try {
77+
await this.write(data)
78+
resolve()
79+
} catch (e) {
80+
reject(e)
81+
}
82+
})
5583
}
5684

57-
function writeAll (data, cb) {
58-
var keys = Object.keys(data)
59-
var pending = keys.length
60-
keys.map(function (key) {
61-
that.write(key, data[key], function (err) {
62-
if (err) return cb(err)
63-
if (!--pending) return cb()
85+
_writeAll (data, cb) {
86+
const self = this
87+
return new Promise((resolve, reject) => {
88+
const keys = Object.keys(data)
89+
let pending = keys.length
90+
keys.map(async function (key) {
91+
try {
92+
await self.write(key, data[key])
93+
} catch (e) {
94+
reject(e)
95+
}
96+
if (!--pending) return resolve()
6497
})
6598
})
6699
}
100+
101+
getdefaults () {
102+
return {
103+
title: '',
104+
description: '',
105+
url: 'dat://' + stringKey(this.archive.key)
106+
}
107+
}
67108
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/joehand/dat-json/issues"
88
},
99
"devDependencies": {
10-
"hyperdrive": "^9.12.3",
10+
"hyperdrive": "^9.14.3",
1111
"random-access-memory": "^3.1.1",
1212
"standard": "^12.0.1",
1313
"tape": "^4.10.1"
@@ -25,6 +25,6 @@
2525
},
2626
"dependencies": {
2727
"dat-encoding": "^5.0.1",
28-
"toiletdb": "^1.4.1"
28+
"toiletdb": "^2.0.0"
2929
}
3030
}

0 commit comments

Comments
 (0)