diff --git a/lib/index.js b/lib/index.js index 1de7fd5..3dccb35 100644 --- a/lib/index.js +++ b/lib/index.js @@ -13,12 +13,22 @@ const adaptors = { const { SYNC } = core; const init = options => { - const { uri, deserialize, serialize } = options; + const { uri, deserialize, serialize, redisClient } = options; - if (!uri) { - throw new Error('A `uri` option with the database connection string has to be provided to feathers-sync'); + if (!uri && !redisClient) { + throw new Error('A `uri` option with the database connection string, or a `redisClient` object has to be provided to feathers-sync'); } + let adapter; + + if (redisClient) { + if (typeof redisClient !== "object") { + throw new Error('`redisClient` option provided to feathers-sync is not an object'); + } + + adapter = adaptors["redis"]; + } + if (deserialize && typeof deserialize !== 'function') { throw new Error('`deserialize` option provided to feathers-sync is not a function'); } @@ -27,14 +37,16 @@ const init = options => { throw new Error('`serialize` option provided to feathers-sync is not a function'); } - const { protocol } = new URL(uri); - const name = protocol.substring(0, protocol.length - 1); - const identifiedProtocolName = Object.keys(adaptors).filter((adaptor) => name.indexOf(adaptor) !== -1 ? adaptor : null); - const adapter = adaptors[identifiedProtocolName]; + if (typeof adapter !== "function") { + const { protocol } = new URL(uri); + const name = protocol.substring(0, protocol.length - 1); + const identifiedProtocolName = Object.keys(adaptors).filter((adaptor) => name.indexOf(adaptor) !== -1 ? adaptor : null); + adapter = adaptors[identifiedProtocolName]; - if (!adapter) { - throw new Error(`${name} is an invalid adapter (uri ${uri})`); - } + if (typeof adapter !== "function") { + throw new Error(`${name} is an invalid adapter (uri ${uri})`); + } + } return adapter({ serialize: JSON.stringify, @@ -49,4 +61,4 @@ module.exports = init; Object.assign(module.exports, adaptors, { default: init, SYNC -}); +}); \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 328c168..1ca3f2e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -12,7 +12,7 @@ describe('feathers-sync tests', () => { it('throws an error when uri is missing', () => { assert.throws(() => { feathers().configure(sync({})); - }, /A `uri` option with the database connection string has to be provided/); + }, /A `uri` option with the database connection string, or a `redisClient` object has to be provided to feathers-sync/); }); it('throws an error for invalid adapter', () => {