-
Notifications
You must be signed in to change notification settings - Fork 1
/
synchronizer_client.js
131 lines (105 loc) · 3.52 KB
/
synchronizer_client.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const _synchronizerClientInstances = {};
const axios = require("axios");
const _triggerTypes = ["insert", "replace", "delete", "update"];
const _validateDependency = function ({dependentCollection, refCollection, localField, fieldsToSync = {}, foreignField = "_id"}) {
if (!dependentCollection) {
throw new Error("dependentCollection is required");
}
if (!refCollection) {
throw new Error("refCollection is required");
}
if (!localField) {
throw new Error("localField is required");
}
if (!fieldsToSync || Object.keys(fieldsToSync).length === 0) {
throw new Error("fieldsToSync needs to has at least 1 field needed to be synced");
}
return {
dependentCollection,
refCollection,
localField,
foreignField,
fieldsToSync
};
};
const _validateTrigger = function ({dependentCollection, triggerType, url, triggerFields = [], knowledge = false}) {
if (!dependentCollection) {
throw new Error("dependentCollection is required");
}
if (!url) {
throw new Error("url is required");
}
if (!triggerType || !_triggerTypes.includes(triggerType)) {
throw new Error("triggerType is required and can be only " + _triggerTypes.join(" or "));
}
if (triggerType === "update" && triggerFields.length === 0) {
throw new Error("for update trigger you must set the fields that change in order to set the trigger ");
}
return {
dependentCollection,
triggerType,
triggerFields,
knowledge,
url
};
};
class SynchronizerClient {
constructor(dbName, engineUrl, apiKey) {
if (!dbName || !engineUrl || !apiKey) {
throw new Error("dbName,engineUrl and apiKey are required");
}
this.dbName = dbName;
this.engineUrl = engineUrl;
this.apiKey = apiKey;
this.active = true;
}
setActive(active) {
this.active = !!active;
}
addTrigger({dependentCollection, triggerType, triggerFields = [], url, knowledge = false}) {
if (this.active === false) {
return;
}
const trigger = _validateTrigger({dependentCollection, triggerType, triggerFields, url, knowledge});
trigger.dbName = this.dbName;
return axios.post(this.engineUrl + "/triggers?api_key=" + this.apiKey, trigger).then(response => response.data);
}
removeTrigger(id) {
return axios.delete(this.engineUrl + "/triggers/" + id + "?api_key=" + this.apiKey);
}
async addDependency({dependentCollection, refCollection, localField, fieldsToSync = {}, foreignField = "_id", refCollectionLastUpdateField}) {
if (this.active === false) {
return;
}
const dependency = _validateDependency({
dependentCollection,
refCollection,
localField,
fieldsToSync,
foreignField,
refCollectionLastUpdateField
});
dependency.dbName = this.dbName;
return axios.post(this.engineUrl + "/dependencies?api_key=" + this.apiKey, dependency).then(response => response.data);
}
removeDependency(id) {
return axios.delete(this.engineUrl + "/dependencies/" + id + "?api_key=" + this.apiKey);
}
getDependencies() {
return axios.get(this.engineUrl + "/dependencies?api_key=" + this.apiKey).then(response => response.data);
}
syncAll() {
return axios.post(this.engineUrl + "/sync?api_key=" + this.apiKey, {dbs: [this.dbName]}).then(response => response.data);
}
}
const init = function ({dbName, engineUrl, apiKey}) {
if (_synchronizerClientInstances[dbName]) {
return getInstance(dbName);
}
_synchronizerClientInstances[dbName] = new SynchronizerClient(dbName, engineUrl, apiKey);
return getInstance(dbName);
};
const getInstance = function ({dbName}) {
return _synchronizerClientInstances[dbName];
};
module.exports = {init, getInstance};