From bd7a17a4754dbde974bd398ba6c9fe05efa53297 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 21 Jan 2021 19:22:12 +0000 Subject: [PATCH] fix: fix ShardingDatastore creation This makes the ShardingDatastore work like the other datastore, where we can just do `const store = new Datastore()` and `await store.open()` without the need of extra static methods. The old static methods should work as expected they are deprecated now we can make a breaking change and remove them all together. What do you think ? --- src/sharding.js | 27 +++++++++++++++++++-------- test/sharding.spec.js | 32 +++++++++++++++++--------------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/sharding.js b/src/sharding.js index 1b80ff6..b0a722a 100644 --- a/src/sharding.js +++ b/src/sharding.js @@ -41,8 +41,10 @@ class ShardingDatastore extends Adapter { this.shard = shard } - open () { - return this.child.open() + async open () { + await this.child.open() + + this.shard = await ShardingDatastore.create(this.child, this.shard) } /** @@ -70,6 +72,7 @@ class ShardingDatastore extends Adapter { } /** + * @deprecated * @param {Datastore} store * @param {Shard} shard */ @@ -83,6 +86,7 @@ class ShardingDatastore extends Adapter { } /** + * @deprecated * @param {Datastore} store */ static async open (store) { @@ -95,19 +99,26 @@ class ShardingDatastore extends Adapter { * @param {Shard} shard */ static async create (store, shard) { - const exists = await store.has(shardKey) - if (!exists) { + const hasShard = await store.has(shardKey) + if (!hasShard) { // @ts-ignore i have no idea what putRaw is or saw any implementation const put = typeof store.putRaw === 'function' ? store.putRaw.bind(store) : store.put.bind(store) - return Promise.all([put(shardKey, utf8Encoder.encode(shard.toString() + '\n')), - put(shardReadmeKey, utf8Encoder.encode(sh.readme))]) + await Promise.all([ + put(shardKey, utf8Encoder.encode(shard.toString() + '\n')), + put(shardReadmeKey, utf8Encoder.encode(sh.readme)) + ]) + + return shard } + // test shards const diskShard = await sh.readShardFun('/', store) const a = (diskShard || '').toString() const b = shard.toString() - if (a !== b) throw new Error(`specified fun ${b} does not match repo shard fun ${a}`) - throw new Error('datastore exists') + if (a !== b) { + throw new Error(`specified fun ${b} does not match repo shard fun ${a}`) + } + return diskShard } /** diff --git a/test/sharding.spec.js b/test/sharding.spec.js index 94a8f80..bc2758f 100644 --- a/test/sharding.spec.js +++ b/test/sharding.spec.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ 'use strict' -const { expect, assert } = require('aegir/utils/chai') +const { expect } = require('aegir/utils/chai') const { Key, MemoryDatastore, utils: { utf8Decoder, utf8Encoder } } = require('interface-datastore') const ShardingStore = require('../src').ShardingDatastore @@ -11,36 +11,38 @@ describe('ShardingStore', () => { it('create', async () => { const ms = new MemoryDatastore() const shard = new sh.NextToLast(2) - await ShardingStore.create(ms, shard) - const res = await Promise.all([ms.get(new Key(sh.SHARDING_FN)), ms.get(new Key(sh.README_FN))]) + const store = new ShardingStore(ms, shard) + await store.open() + const res = await Promise.all([ + ms.get(new Key(sh.SHARDING_FN)), + ms.get(new Key(sh.README_FN)) + ]) expect(utf8Decoder.decode(res[0])).to.eql(shard.toString() + '\n') expect(utf8Decoder.decode(res[1])).to.eql(sh.readme) }) it('open - empty', async () => { const ms = new MemoryDatastore() - try { - await ShardingStore.open(ms) - assert(false, 'Failed to throw error on ShardStore.open') - } catch (err) { - expect(err.code).to.equal('ERR_NOT_FOUND') - } + // @ts-expect-error + const store = new ShardingStore(ms) + expect(store.open()) + .to.eventually.be.rejected() + .with.property('code', 'ERR_NOT_FOUND') }) it('open - existing', async () => { const ms = new MemoryDatastore() const shard = new sh.NextToLast(2) + const store = new ShardingStore(ms, shard) - await ShardingStore.create(ms, shard) - await ShardingStore.open(ms) + expect(store.open()).to.eventually.be.fulfilled() }) it('basics', async () => { const ms = new MemoryDatastore() const shard = new sh.NextToLast(2) - const store = await ShardingStore.createOrOpen(ms, shard) - expect(store).to.exist() - await ShardingStore.createOrOpen(ms, shard) + const store = new ShardingStore(ms, shard) + await store.open() await store.put(new Key('hello'), utf8Encoder.encode('test')) const res = await ms.get(new Key('ll').child(new Key('hello'))) expect(res).to.eql(utf8Encoder.encode('test')) @@ -51,7 +53,7 @@ describe('ShardingStore', () => { require('interface-datastore/src/tests')({ setup () { const shard = new sh.NextToLast(2) - return ShardingStore.createOrOpen(new MemoryDatastore(), shard) + return new ShardingStore(new MemoryDatastore(), shard) }, teardown () { } })