-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDB.js
More file actions
115 lines (107 loc) · 2.53 KB
/
createDB.js
File metadata and controls
115 lines (107 loc) · 2.53 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
var DbUrl = process.env.DB_URL || "http://localhost:5984";
const PouchDB = require("pouchdb-node");
const designDocs = require("./designDocs.json");
const maxTries = 10;
var tries = {};
const retryMs = 500;
const now = Date.now();
// demoData, make the timestamps look like they are recent
const demoData = {
actions: [
{
_id: "/myactions/sum",
code:
'function main(params){ return { "sum": Number(params.a) + Number(params.b)};}',
},
{
_id: "/myactions/mult",
code:
'function main(params){ return { "mult":Number(params.a) * Number(params.b)};}',
},
],
requests: [
{
path: "/myactions/sum",
timestamp: new Date(now - 8000000).toISOString(),
params: {
a: 1,
b: 2,
},
result: {
sum: "3",
},
status: "success",
},
{
path: "/myactions/sum",
timestamp: new Date(now - 320000).toISOString(),
params: {
a: 1,
b: 2,
},
status: "new",
},
{
path: "/myactions/mult",
timestamp: new Date(now - 140000).toISOString(),
params: {
a: 1,
b: 2,
},
status: "new",
},
],
};
// try to empty the DB, if the DB is not present try again after some time
function destroyDB(dbname, callback) {
const db = new PouchDB(`${DbUrl}/${dbname}`);
db.destroy((err, _) => {
if (err) {
if (typeof tries[dbname] === "undefined") {
tries[dbname] = 0;
}
if (tries[dbname]++ < maxTries) {
console.log(
"Going to retry to connect to",
dbname,
"in",
retryMs,
"ms"
);
setTimeout(() => {
destroyDB(dbname, callback);
}, retryMs);
} else {
console.log("Failed to connect to database at", DbUrl);
}
} else {
callback();
}
});
}
function doInsert(dbname, data) {
// clean up the database we migh have created previously
destroyDB(dbname, () => {
const db = new PouchDB(`${DbUrl}/${dbname}`);
db.bulkDocs(data, (err, response) => {
if (err) {
console.log(err, response);
} else {
console.log(dbname, "loaded ok");
}
});
});
}
// start the show
// merge the examples with the design docs
for (let dbname in designDocs) {
if (typeof demoData[dbname] === "undefined") {
demoData[dbname] = [];
}
for (let item in designDocs[dbname]) {
demoData[dbname].push(designDocs[dbname][item]);
}
}
for (let dbname in demoData) {
doInsert(dbname, demoData[dbname]);
}