-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.js
94 lines (81 loc) · 3.73 KB
/
go.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
const DB_KEYS = require("./DB_KEYS/mongo.js")
const { MongoClient } = require("mongodb")
const { err, info } = require('./console.js')
const install = require('./DB/install.js')
const softExit = require('./softExit.js')
const CoinGecko = require('coingecko-api');
info(``)
info(``)
let rConnector = (onConnected, database)=>{
return (e, client)=>{
let db = client.db(database)
if(!err(e,`[:!:] Cannot work with unconnected database`)) {
info(`[:x:] Connected & Authenticated`)
onConnected(db)
softExit(db)
}
}
}
let connect = (config, onConnected)=>{
(new MongoClient(
config[0],
{
auth: {
user: config[1],
password: config[2]
},
useUnifiedTopology: true,
} ,
))
.connect(
rConnector(onConnected, config[3])
)
}
((keepUp)=>{
connect(DB_KEYS, (db)=>{
install(db).then(
(res)=>{
if(res) {info(`[:>>] Installed collections!`)}
else {info(`[:^:] Collections already installed`)}
((db)=>{
db.collections((e, collections)=>{
if(!err(e, `[:!:] Couldn't list collections...'`)) {
info(``)
info(`[:=:] We have ${collections.length} collections in database.`)
info(``)
}
})
})(db)
},
(rej)=>{
info(`[:!:] One collection failed, install cannot continue!`)
}
).finally(()=>{
keepUp(db)
})
})
})((db)=>{
let coins = []
let API = new CoinGecko();
API.coins.list().then(
(res)=>{
res.forEach((x)=>{
coins.push(x.symbol)
db.collection("coins").insertOne(x, (e, res)=>{
if(!err(e, `[:x:] Couldn't insert into "coins collection"'`)) {
// void
}
})
((x)=>{db.createCollection( `24hHistoryGecko_${x.symbol}` , (e, result)=>{
if(!err(e,`[:!>] Collection for "${x.symbol}" couldn't been created'`)) {
info(`[: >] Collection "${x.symbol}" created`)
}
})})(x)
})
},
(rej)=>{err(rej, `[:!:] Rejected promise`)}
).finally(()=>{
})
})
info(``)
info(``)