diff --git a/src/sharding.js b/src/sharding.js index b0a722a..15b5715 100644 --- a/src/sharding.js +++ b/src/sharding.js @@ -1,6 +1,6 @@ 'use strict' -const { Adapter, Key, utils: { utf8Encoder } } = require('interface-datastore') +const { Adapter, Key, utils: { utf8Encoder }, Errors } = require('interface-datastore') const sh = require('./shard') const KeytransformStore = require('./keytransform') @@ -100,6 +100,9 @@ class ShardingDatastore extends Adapter { */ static async create (store, shard) { const hasShard = await store.has(shardKey) + if (!hasShard && !shard) { + throw Errors.dbOpenFailedError(Error('Shard is required when datastore doesn\'t have a shard key already.')) + } 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) diff --git a/test/sharding.spec.js b/test/sharding.spec.js index bc2758f..f037bc1 100644 --- a/test/sharding.spec.js +++ b/test/sharding.spec.js @@ -21,21 +21,21 @@ describe('ShardingStore', () => { expect(utf8Decoder.decode(res[1])).to.eql(sh.readme) }) - it('open - empty', async () => { + it('open - empty', () => { const ms = new MemoryDatastore() // @ts-expect-error const store = new ShardingStore(ms) - expect(store.open()) + return expect(store.open()) .to.eventually.be.rejected() - .with.property('code', 'ERR_NOT_FOUND') + .with.property('code', 'ERR_DB_OPEN_FAILED') }) - it('open - existing', async () => { + it('open - existing', () => { const ms = new MemoryDatastore() const shard = new sh.NextToLast(2) const store = new ShardingStore(ms, shard) - expect(store.open()).to.eventually.be.fulfilled() + return expect(store.open()).to.eventually.be.fulfilled() }) it('basics', async () => {