Browse Source

pass in the collection

Kieran Gibb 2 years ago
parent
commit
6c5c944448
No known key found for this signature in database
2 changed files with 56 additions and 82 deletions
  1. 24
    61
      index.js
  2. 32
    21
      test/index.test.js

+ 24
- 61
index.js View File

@@ -8,20 +8,18 @@ const maybe = require('call-me-maybe')
8 8
 
9 9
 const { uniq, isString } = require('./util')
10 10
 
11
-module.exports = (createGroup, storage, opts) => new GroupStore(createGroup, storage, opts)
11
+module.exports = (storage, factory, collection, opts) => new GroupStore(storage, factory, collection, opts)
12 12
 
13 13
 class GroupStore {
14
-  constructor (createGroup, storage, opts = {}) {
14
+  constructor (storage, factory, collection, opts = {}) {
15 15
     this._id = crypto.randomBytes(16).toString('hex')
16
-    this.root = storage || constants.storage
17
-    this.storage = path.join(this.root, 'groups')
18
-    this.config = opts.config
16
+    this.storage = storage
19 17
     this.opts = opts
20
-    this.createGroup = createGroup
18
+    this.factory = factory
21 19
 
22
-    this.groups = {}
23
-    this.config.groups.list().forEach((groupOpts, i) => {
24
-      this._cacheGroup(this._buildGroup(groupOpts))
20
+    this.collection = {}
21
+    collection.forEach((_opts, i) => {
22
+      this._cache(this._build(_opts))
25 23
     })
26 24
 
27 25
     this._readyCallback = thunky(this._ready.bind(this))
@@ -38,7 +36,7 @@ class GroupStore {
38 36
 
39 37
   async all () {
40 38
     await this.ready()
41
-    return uniq(Object.values(this.groups))
39
+    return uniq(Object.values(this.collection))
42 40
   }
43 41
 
44 42
   async where (params = {}) {
@@ -63,34 +61,15 @@ class GroupStore {
63 61
   }
64 62
 
65 63
   async create (params = {}) {
66
-    var group = await this.findBy(params)
67
-    if (group) throw new Error('group already exists')
64
+    var entry = await this.findBy(params)
65
+    if (entry) throw new Error('entry already exists')
66
+    var entry = this._build(params)
68 67
 
69
-    var group = this._buildGroup(params)
68
+    await entry.ready()
70 69
 
71
-    await group.ready()
72
-
73
-    this._cacheGroup(group)
74
-    this._saveGroup(group)
75
-
76
-    return group
77
-  }
78
-
79
-  // TODO: this doesn't work currently, throws a level db error
80
-  async destroy (params = {}) {
81
-    var group = await this.findBy(params)
82
-    if (!group) throw new Error('no group exists')
83
-
84
-    this.config.groups.delete(group.address)
85
-    // TODO: delete group data
86
-
87
-    if (this.config.save()) {
88
-      delete this.groups[group.address.toString('hex')]
89
-      return true
90
-    } else {
91
-      this.config.groups.set(group.address, group)
92
-      throw new Error('failed to save config file')
93
-    }
70
+    this._cache(entry)
71
+    entry.save()
72
+    return entry
94 73
   }
95 74
 
96 75
   // ------------------------------------------------------------------
@@ -98,14 +77,14 @@ class GroupStore {
98 77
   _ready (callback) {
99 78
     var pending = 1
100 79
     var self = this
101
-    var groups = Object.values(self.groups)
80
+    var collection = Object.values(self.collection)
102 81
 
103 82
     function next (n) {
104
-      var group = groups[n]
105
-      if (!group) return done()
83
+      var entry = collection[n]
84
+      if (!entry) return done()
106 85
       ++pending
107 86
       process.nextTick(next, n + 1)
108
-      group.ready(done)
87
+      entry.ready(done)
109 88
     }
110 89
 
111 90
     function done (err) {
@@ -121,33 +100,17 @@ class GroupStore {
121 100
     next(0)
122 101
   }
123 102
 
124
-  _buildGroup (params = {}) {
125
-    return this.createGroup(
103
+  _build (params = {}) {
104
+    return this.factory(
126 105
       this.storage,
127 106
       params.address,
128 107
       Object.assign(this.opts, params)
129 108
     )
130 109
   }
131 110
 
132
-  _cacheGroup (group) {
133
-    this.groups[group.address.toString('hex')] = group
134
-    if (group.name) this.groups[group.name] = group
135
-    return true
136
-  }
137
-
138
-  _saveGroup (group) {
139
-    this.config.groups.set(group.address, group.toConfig())
140
-    var success = this.config.save()
141
-    if (!success) return false
111
+  _cache (entry) {
112
+    this.collection[entry.address.toString('hex')] = entry
113
+    if (entry.name) this.collection[entry.name] = entry
142 114
     return true
143 115
   }
144 116
 }
145
-
146
-// _validate (id) {
147
-//   if (crypto.isKey(id)) key = id
148
-//   if (!crypto.isKey(id) && isString(id)) name = id
149
-//   // Ensure we either have no key, or the key is a valid key
150
-//   if (!(!key || crypto.isKey(key))) throw new Error('invalid: key format')
151
-//   return { key, name }
152
-// }
153
-

+ 32
- 21
test/index.test.js View File

@@ -4,7 +4,7 @@ const crypto = require('cobox-crypto')
4 4
 const Store = require('../')
5 5
 
6 6
 const { tmp, cleanup } = require('./util')
7
-const { createGroup } = require('./mocks')
7
+const createGroup = require('cobox-group/test/mock')
8 8
 
9 9
 describe('group store: basic', (context) => {
10 10
   context('ready()', async function (assert, next) {
@@ -14,12 +14,13 @@ describe('group store: basic', (context) => {
14 14
 
15 15
     config.groups.set(params.address, params)
16 16
 
17
-    const store = Store(createGroup, storage, { config })
17
+    const collection = config.groups.list()
18
+    const store = Store(storage, createGroup, collection, { config })
18 19
 
19 20
     try {
20 21
       await store.ready()
21
-      assert.same(Object.values(store.groups).length, 1, 'builds then caches groups from the config')
22
-      assert.ok(store.groups[params.address.toString('hex')], 'accessible using address hex string')
22
+      assert.same(Object.values(store.collection).length, 1, 'builds then caches groups from the config')
23
+      assert.ok(store.collection[params.address.toString('hex')], 'accessible using address hex string')
23 24
     } catch (err) {
24 25
       assert.error(err, 'no error')
25 26
     }
@@ -34,7 +35,8 @@ describe('group store: basic', (context) => {
34 35
 
35 36
     config.groups.set(params.address, params)
36 37
 
37
-    const store = Store(createGroup, storage, { config })
38
+    const collection = config.groups.list()
39
+    const store = Store(storage, createGroup, collection, { config })
38 40
 
39 41
     try {
40 42
       await store.ready()
@@ -56,7 +58,8 @@ describe('group store: basic', (context) => {
56 58
     seedConfig()
57 59
     config.groups.set(params.address, params)
58 60
 
59
-    const store = Store(createGroup, storage, { config })
61
+    const collection = config.groups.list()
62
+    const store = Store(storage, createGroup, collection, { config })
60 63
 
61 64
     try {
62 65
       await store.ready()
@@ -66,9 +69,9 @@ describe('group store: basic', (context) => {
66 69
       })
67 70
 
68 71
       assert.ok(Array.isArray(response), 'returns an array')
69
-      var group = response[0]
70
-      assert.ok(group, 'finds a group')
71
-      assert.same(group && group.address, params.address, 'finds the correct group')
72
+      var entry = response[0]
73
+      assert.ok(entry, 'finds a entry')
74
+      assert.same(entry && entry.address, params.address, 'finds the correct entry')
72 75
     } catch (err) {
73 76
       assert.error(err, 'no error')
74 77
     }
@@ -93,7 +96,8 @@ describe('group store: basic', (context) => {
93 96
 
94 97
     config.groups.set(params.address, params)
95 98
 
96
-    const store = Store(createGroup, storage, { config })
99
+    const collection = config.groups.list()
100
+    const store = Store(storage, createGroup, collection, { config })
97 101
 
98 102
     try {
99 103
       await store.ready()
@@ -103,7 +107,7 @@ describe('group store: basic', (context) => {
103 107
         address: null
104 108
       })
105 109
 
106
-      assert.same(response.length, 0, 'fails to find a group')
110
+      assert.same(response.length, 0, 'fails to find an entry')
107 111
     } catch (err) {
108 112
       assert.error(err, 'no error')
109 113
     }
@@ -118,7 +122,8 @@ describe('group store: basic', (context) => {
118 122
 
119 123
     config.groups.set(params.address, params)
120 124
 
121
-    const store = Store(createGroup, storage, { config })
125
+    const collection = config.groups.list()
126
+    const store = Store(storage, createGroup, collection, { config })
122 127
 
123 128
     try {
124 129
       await store.ready()
@@ -128,7 +133,7 @@ describe('group store: basic', (context) => {
128 133
         address: params.address
129 134
       })
130 135
 
131
-      assert.same(response.length, 0, 'fails to find a group')
136
+      assert.same(response.length, 0, 'fails to find an entry')
132 137
     } catch (err) {
133 138
       assert.error(err, 'no error')
134 139
     }
@@ -143,7 +148,8 @@ describe('group store: basic', (context) => {
143 148
 
144 149
     config.groups.set(params.address, params)
145 150
 
146
-    const store = Store(createGroup, storage, { config })
151
+    const collection = config.groups.list()
152
+    const store = Store(storage, createGroup, collection, { config })
147 153
 
148 154
     try {
149 155
       await store.ready()
@@ -153,7 +159,7 @@ describe('group store: basic', (context) => {
153 159
         address: crypto.randomBytes(32).toString('hex')
154 160
       })
155 161
 
156
-      assert.same(response.length, 0, 'fails to find a group')
162
+      assert.same(response.length, 0, 'fails to find an entry')
157 163
     } catch (err) {
158 164
       assert.error(err, 'no error')
159 165
     }
@@ -168,7 +174,8 @@ describe('group store: basic', (context) => {
168 174
 
169 175
     config.groups.set(params.address, params)
170 176
 
171
-    const store = Store(createGroup, storage, { config })
177
+    const collection = config.groups.list()
178
+    const store = Store(storage, createGroup, collection, { config })
172 179
 
173 180
     try {
174 181
       await store.ready()
@@ -178,7 +185,7 @@ describe('group store: basic', (context) => {
178 185
         address: params.address
179 186
       })
180 187
 
181
-      assert.same(response.length, 0, 'fails to find a group')
188
+      assert.same(response.length, 0, 'fails to find an entry')
182 189
     } catch (err) {
183 190
       assert.error(err, 'no error')
184 191
     }
@@ -193,7 +200,8 @@ describe('group store: basic', (context) => {
193 200
 
194 201
     config.groups.set(params.address, params)
195 202
 
196
-    const store = Store(createGroup, storage, { config })
203
+    const collection = config.groups.list()
204
+    const store = Store(storage, createGroup, collection, { config })
197 205
 
198 206
     try {
199 207
       await store.ready()
@@ -217,7 +225,8 @@ describe('group store: basic', (context) => {
217 225
 
218 226
     config.groups.set(params.address, params)
219 227
 
220
-    const store = Store(createGroup, storage, { config })
228
+    const collection = config.groups.list()
229
+    const store = Store(storage, createGroup, collection, { config })
221 230
 
222 231
     try {
223 232
       await store.ready()
@@ -241,7 +250,8 @@ describe('group store: basic', (context) => {
241 250
 
242 251
     config.groups.set(params.address, params)
243 252
 
244
-    const store = Store(createGroup, storage, { config })
253
+    const collection = config.groups.list()
254
+    const store = Store(storage, createGroup, collection, { config })
245 255
 
246 256
     try {
247 257
       await store.ready()
@@ -261,7 +271,8 @@ describe('group store: basic', (context) => {
261 271
       config = Config(storage),
262 272
       params = Object.assign(crypto.keySet(), { name: 'magma' })
263 273
 
264
-    const store = Store(createGroup, storage, { config })
274
+    const collection = config.groups.list()
275
+    const store = Store(storage, createGroup, collection, { config })
265 276
 
266 277
     try {
267 278
       var group = await store.create(params)