Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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,
Expand All @@ -49,4 +61,4 @@ module.exports = init;
Object.assign(module.exports, adaptors, {
default: init,
SYNC
});
});
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down