Browse Source

use async level setup

Kieran Gibb 2 years ago
parent
commit
7939256e98
No known key found for this signature in database
2 changed files with 33 additions and 9 deletions
  1. 12
    9
      index.js
  2. 21
    0
      lib/level.js

+ 12
- 9
index.js View File

@@ -49,17 +49,20 @@ class Admin extends Replicator {
49 49
       }, this._opts))
50 50
     })
51 51
 
52
-    this.db.log = setupLevel(path.join(this.path, 'views', 'log'))
53 52
     this.core = kappa(this.storage, { multifeed: this.multifeed })
54
-    this.core.use('logs', Query(this.db.log, query.log))
55
-    this.logs = this.core.api.logs
56 53
 
57
-    this.multifeed.writer('log', {
58
-      keypair: this.deriveKeyPair(LOG_ID, this.address)
59
-    }, (err, feed) => {
60
-      if (err) return callback(err)
61
-      this.log = feed
62
-      this.core.ready(callback)
54
+    setupLevel(path.join(this.path, 'views', 'log'), (err, db) => {
55
+      this.db.log = db
56
+      this.core.use('logs', Query(this.db.log, query.log))
57
+      this.logs = this.core.api.logs
58
+
59
+      this.multifeed.writer('log', {
60
+        keypair: this.deriveKeyPair(LOG_ID, this.address)
61
+      }, (err, feed) => {
62
+        if (err) return callback(err)
63
+        this.log = feed
64
+        this.core.ready(callback)
65
+      })
63 66
     })
64 67
   }
65 68
 }

+ 21
- 0
lib/level.js View File

@@ -0,0 +1,21 @@
1
+const mkdirp = require('mkdirp')
2
+const level = require('level')
3
+const liveStream = require('level-live-stream')
4
+
5
+function setupLevel (levelPath, callback) {
6
+  mkdirp(levelPath, (err) => {
7
+    if (err) return callback(err)
8
+    level(levelPath, { createIfMissing: true }, callback)
9
+  })
10
+}
11
+
12
+function setupLiveStream (db) {
13
+  if (!db) return
14
+  liveStream.install(db)
15
+  return db
16
+}
17
+
18
+module.exports = {
19
+  setupLevel,
20
+  setupLiveStream
21
+}