forked from feathersjs-ecosystem/feathers-sync
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (43 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { URL } = require('url');
const core = require('./core');
const redis = require('./adapters/redis');
const amqp = require('./adapters/amqp');
const nats = require('./adapters/nats');
const adaptors = {
core,
nats,
redis,
amqp,
rabbitmq: amqp
};
const { SYNC } = core;
const init = options => {
const { uri, deserialize, serialize } = options;
if (!uri) {
throw new Error('A `uri` option with the database connection string has to be provided to feathers-sync');
}
if (deserialize && typeof deserialize !== 'function') {
throw new Error('`deserialize` option provided to feathers-sync is not a function');
}
if (serialize && typeof serialize !== 'function') {
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 (!adapter) {
throw new Error(`${name} is an invalid adapter (uri ${uri})`);
}
return adapter({
serialize: JSON.stringify,
deserialize: JSON.parse,
key: 'feathers-sync',
...options
});
};
module.exports = init;
Object.assign(module.exports, adaptors, {
default: init,
SYNC
});