This repository was archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
158 lines (142 loc) · 3.31 KB
/
server.js
File metadata and controls
158 lines (142 loc) · 3.31 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const sleep = require('sleep');
const process = require('process');
const fetch = require('node-fetch');
const PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-find'));
const {
mainDb,
eventsDb,
notificationsDb,
archiveDb,
sharedDb,
publicDb,
authDb,
anonymousDb
} = require('experimental-server');
(async() => {
try {
const COUCHDB_USER = process.env.COUCHDB_USER;
const COUCHDB_PASSWORD = process.env.COUCHDB_PASSWORD;
const mainReplicatorUrl = "http://main-db:5984/_replicator";
const eventsReplicatorUrl = "http://events-db:5984/_replicator";
const notificationsReplicatorUrl = "http://notifications-db:5984/_replicator";
const archiveReplicatorUrl = "http://archive-db:5984/_replicator";
const createDB = dbUrl => {
return new PouchDB(dbUrl, {
ajax: {
cache: false,
timeout: 60000
},
auth: {
username: COUCHDB_USER,
password: COUCHDB_PASSWORD
}
});
}
const mainReplicator = await createDB(mainReplicatorUrl);
const eventsReplicator = await createDB(eventsReplicatorUrl);
const notificationsReplicator = await createDB(notificationsReplicatorUrl);
const archiveReplicator = await createDB(archiveReplicatorUrl);
const mainDbUrl = `http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@main-db:5984/db`;
const eventsDbUrl = `http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@events-db:5984/events`;
const notificationsDbUrl = `http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@notifications-db:5984/notifications`;
const archiveDbUrl = `http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@archive-db:5984/archive`;
await fetch(`http://${COUCHDB_USER}:${COUCHDB_PASSWORD}@auth-db:5984/_users`, {method: 'PUT'});
for (db of[mainDb,
eventsDb,
notificationsDb,
archiveDb,
sharedDb,
publicDb,
anonymousDb]) {
await db.createIndex({
index: {
fields: [
{
createdAt: "desc"
}
]
}
});
}
try {
await mainReplicator.put({
_id: "main_to_events",
source: mainDbUrl,
target: eventsDbUrl,
continuous: true,
selector: {
type: "EVENT",
_deleted: {
$exists: false
}
}
});
} catch (e) {
if (e.status != 409) {
throw e;
}
}
try {
await eventsReplicator.put({
_id: "events_to_main",
source: eventsDbUrl,
target: mainDbUrl,
continuous: true,
selector: {
status: {
"$in": ["done", "archived", "deleted"]
}
}
});
} catch (e) {
if (e.status != 409) {
throw e;
}
}
try {
await mainReplicator.put({
_id: "main_to_notifications",
source: mainDbUrl,
target: notificationsDbUrl,
continuous: true,
selector: {
type: "notification",
_deleted: {
$exists: false
}
}
});
} catch (e) {
if (e.status != 409) {
throw e;
}
}
try {
await notificationsReplicator.put({_id: "notifications_to_main", source: notificationsDbUrl, target: mainDbUrl, continuous: true});
} catch (e) {
if (e.status != 409) {
throw e;
}
}
try {
await mainReplicator.put({
_id: "main_to_archive",
source: mainDbUrl,
target: archiveDbUrl,
continuous: true,
selector: {
type: "EVENT",
status: "archived"
}
});
} catch (e) {
if (e.status != 409) {
throw e;
}
}
} catch (e) {
console.log(e);
process.exit(1);
}
})();