Skip to content

Commit 6621f66

Browse files
author
tombertrand
committed
Mongo update to 4.x on Node 18
1 parent 784d0c5 commit 6621f66

18 files changed

+237
-269
lines changed

package-lock.json

Lines changed: 150 additions & 197 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
"cors": "^2.8.5",
4747
"dot-object": "^2.1.4",
4848
"dotenv": "^8.2.0",
49-
"express": "^4.17.1",
50-
"find-process": "^1.4.3",
49+
"express": "^4.18.2",
50+
"find-process": "^1.4.7",
5151
"fs-extra": "^10.0.0",
5252
"ga-4-react": "^0.1.281",
5353
"html5-qrcode": "github:mebjas/html5-qrcode#master",
@@ -57,7 +57,7 @@
5757
"leaflet-extra-markers": "^1.2.1",
5858
"lodash": "^4.17.15",
5959
"moment": "^2.29.2",
60-
"mongodb": "^3.5.5",
60+
"mongodb": "^5.6.0",
6161
"node-cache": "^5.1.0",
6262
"node-cron": "^2.0.3",
6363
"node-fetch": "^2.6.7",

server/api/2minersStats.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const get2MinersStats = async () => {
1010
return minersStats;
1111
}
1212

13-
return new Promise(resolve => {
13+
return new Promise(async resolve => {
1414
try {
15-
const database = db.getDatabase();
15+
const database = await db.getDatabase();
1616

1717
if (!database) {
1818
throw new Error("Mongo unavailable for get2MinersStats");
@@ -22,7 +22,8 @@ const get2MinersStats = async () => {
2222
.collection(MINERS_STATS_COLLECTION)
2323
.find()
2424
.sort({ date: -1 })
25-
.toArray((_err, data = []) => {
25+
.toArray()
26+
.then(data => {
2627
const filteredData = data.map(({ uniqueAccounts, ...rest }) => rest);
2728
nodeCache.set(MINERS_STATS, filteredData, EXPIRE_6H);
2829
resolve(data);

server/api/coingeckoStats.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ const getCoingeckoStats = async ({ fiat, cryptocurrency }) => {
3434

3535
const getMarketCapRank24h =
3636
marketCapRank24h ||
37-
new Promise((resolve, reject) => {
37+
new Promise(async (resolve, reject) => {
3838
try {
39-
const database = db.getDatabase();
39+
const database = await db.getDatabase();
4040

4141
if (!database) {
4242
throw new Error("Mongo unavailable for getCoingeckoStats");
@@ -45,15 +45,14 @@ const getCoingeckoStats = async ({ fiat, cryptocurrency }) => {
4545
database
4646
.collection(MARKET_CAP_RANK_COLLECTION)
4747
.find({
48-
$query: {
49-
createdAt: {
50-
$lte: new Date(Date.now() - EXPIRE_24H * 1000),
51-
$gte: new Date(Date.now() - EXPIRE_48H * 1000),
52-
},
48+
createdAt: {
49+
$lte: new Date(Date.now() - EXPIRE_24H * 1000),
50+
$gte: new Date(Date.now() - EXPIRE_48H * 1000),
5351
},
54-
$orderby: { value: 1 },
5552
})
56-
.toArray((_err, [{ value } = {}] = []) => {
53+
.sort({ value: 1 })
54+
.toArray()
55+
.then(([{ value } = {}]) => {
5756
nodeCache.set(MARKET_CAP_RANK_24H, value, EXPIRE_1h);
5857
resolve(value);
5958
});
@@ -82,9 +81,9 @@ const getCoingeckoMarketCapStats = async () => {
8281
return marketCapStats;
8382
}
8483

85-
return new Promise(resolve => {
84+
return new Promise(async resolve => {
8685
try {
87-
const database = db.getDatabase();
86+
const database = await db.getDatabase();
8887

8988
if (!database) {
9089
throw new Error("Mongo unavailable for getCoingeckoMarketCapStats");
@@ -93,7 +92,8 @@ const getCoingeckoMarketCapStats = async () => {
9392
database
9493
.collection(MARKET_CAP_STATS_COLLECTION)
9594
.find()
96-
.toArray((_err, value = []) => {
95+
.toArray()
96+
.then(value => {
9797
nodeCache.set(COINGECKO_MARKET_CAP_STATS, value);
9898
resolve(value);
9999
});

server/api/historyFilters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const getHistoryFilters = async ({ account, filters: rawFilters }) => {
3838
if (!(await getIsAccountFilterable(account))) {
3939
return data;
4040
}
41-
const database = db.getDatabase();
41+
const database = await db.getDatabase();
4242

4343
if (!database) {
4444
throw new Error("Mongo unavailable for getHistoryFilters");

server/api/largeTransactions.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@ const { LARGE_TRANSACTIONS } = require("../constants");
66
const getLargeTransactions = async () => {
77
let largeTransactions =
88
nodeCache.get(LARGE_TRANSACTIONS) ||
9-
(await new Promise((resolve, reject) => {
9+
(await new Promise(async (resolve, reject) => {
1010
try {
11-
const database = db.getDatabase();
11+
const database = await db.getDatabase();
1212

1313
if (!database) {
1414
throw new Error("Mongo unavailable for getLargeTransactions");
1515
}
1616

1717
database
1818
.collection(LARGE_TRANSACTIONS)
19-
.find({
20-
$query: {},
21-
})
19+
.find()
2220
.sort({ createdAt: -1 })
23-
.toArray((_err, values = []) => {
21+
.toArray()
22+
.then(values => {
2423
const transactions = values.map(({ value: [transaction] }) => transaction);
2524
nodeCache.set(LARGE_TRANSACTIONS, transactions, 15);
2625

server/api/nodeLocations.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@ const { NODE_LOCATIONS } = require("../constants");
66
const getNodeLocations = async () => {
77
let nodeLocations =
88
nodeCache.get(NODE_LOCATIONS) ||
9-
(await new Promise((resolve, reject) => {
9+
(await new Promise(async (resolve, reject) => {
1010
try {
11-
const database = db.getDatabase();
11+
const database = await db.getDatabase();
1212

1313
if (!database) {
1414
throw new Error("Mongo unavailable for getNodeLocations");
1515
}
1616

1717
database
1818
.collection(NODE_LOCATIONS)
19-
.find({
20-
$query: {},
21-
})
22-
.toArray((_err, values = []) => {
19+
.find()
20+
.toArray()
21+
.then(values => {
2322
nodeCache.set(NODE_LOCATIONS, values);
2423
resolve(values);
2524
});

server/client/mongo/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const { MONGO_URL, MONGO_DB, MONGO_OPTIONS } = require("../../constants");
77
const client = new MongoClient(MONGO_URL, MONGO_OPTIONS);
88

99
function isConnected() {
10-
return !!client && !!client.topology && client.topology.isConnected();
10+
const isConnected = !!client && !!client.topology && client.topology.isConnected();
11+
return isConnected;
1112
}
1213

1314
async function connect() {
@@ -22,8 +23,13 @@ async function connect() {
2223
}
2324

2425
// Function to retrieve the MongoDB database instance
25-
function getDatabase() {
26-
return isConnected() ? client.db(MONGO_DB) : null;
26+
async function getDatabase() {
27+
if (isConnected()) {
28+
return client.db(MONGO_DB);
29+
}
30+
31+
await connect();
32+
return client.db(MONGO_DB);
2733
}
2834

2935
module.exports = {

server/client/mongo/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const {
1616
} = require("../constants");
1717

1818
async function createIndexes() {
19-
const database = db.getDatabase();
19+
const database = await db.getDatabase();
2020
const extraOptions = { unique: true, background: true };
2121

2222
const indexExists1 = await database.collection(LARGE_TRANSACTIONS).indexExists("createdAt");

server/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const MONGO_URL = MONGO_USER
5656
? `mongodb://${MONGO_USER}:${MONGO_PASSWORD}@localhost:27017`
5757
: `mongodb://localhost:27017`;
5858
const MONGO_DB = "nanolooker";
59-
const MONGO_OPTIONS = { useUnifiedTopology: true, poolSize: 10 };
59+
const MONGO_OPTIONS = { family: 4 };
6060

6161
// https://api.coingecko.com/api/v3/simple/supported_vs_currencies
6262
const SUPPORTED_CRYPTOCURRENCY = require("./supported-cryptocurrency");

server/cron/2minersStats.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const processData = async ({ stats }) => {
7070
// Too heaving saving them, instead store uniqueAccounts on the latest date
7171
delete stats.payoutAccounts;
7272

73-
const database = db.getDatabase();
73+
const database = await db.getDatabase();
7474
if (database) {
7575
await database.collection(MINERS_STATS_COLLECTION).insertOne(stats);
7676
}
@@ -113,15 +113,16 @@ const getAccountNonExchangeRepresentative = async account => {
113113

114114
const getLatestEntry = async () => {
115115
// eslint-disable-next-line no-loop-func
116-
const latestEntry = await new Promise((resolve, reject) => {
117-
const database = db.getDatabase();
116+
const latestEntry = await new Promise(async (resolve, reject) => {
117+
const database = await db.getDatabase();
118118
if (database) {
119119
database
120120
.collection(MINERS_STATS_COLLECTION)
121121
.find({ pool: "2Miners" })
122122
.sort({ date: -1 })
123123
.limit(1)
124-
.toArray((_err, [data = {}] = []) => {
124+
.toArray()
125+
.then(([data = {}]) => {
125126
console.log(`Most recent date: ${data.date}`);
126127
resolve(data);
127128
});

server/cron/coingeckoStats.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const getMarketCapStats = async () => {
103103
const top = process.env.NODE_ENV === "production" ? 200 : 10;
104104

105105
try {
106-
const database = db.getDatabase();
106+
const database = await db.getDatabase();
107107

108108
if (!database) {
109109
throw new Error("Mongo unavailable for getMarketCapStats");

server/cron/exchangeTracker.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const getAccountHistory = async (account, latestDate) => {
9191

9292
console.log(`Account history completed: ${account}`);
9393

94-
const database = db.getDatabase();
94+
const database = await db.getDatabase();
9595

9696
if (dailyBalances.length > 1) {
9797
console.log(`Adding: ${dailyBalances.length} day(s)`);
@@ -119,7 +119,7 @@ const getAccountsHistory = async () => {
119119
return;
120120
}
121121

122-
const database = db.getDatabase();
122+
const database = await db.getDatabase();
123123

124124
for (let i = 0; i < accounts.length; i++) {
125125
let latestDate = null;
@@ -136,7 +136,8 @@ const getAccountsHistory = async () => {
136136
})
137137
.sort({ date: -1 })
138138
.limit(1)
139-
.toArray((_err, [data = {}] = []) => {
139+
.toArray()
140+
.then(([data = {}]) => {
140141
console.log(`Most recent date: ${data.date}`);
141142
resolve(data.date);
142143
});
@@ -156,7 +157,7 @@ const getExchangeBalances = async () => {
156157

157158
console.log("Getting balances from collection");
158159

159-
const database = db.getDatabase();
160+
const database = await db.getDatabase();
160161

161162
exchangeBalances = await new Promise((resolve, reject) => {
162163
database
@@ -176,7 +177,8 @@ const getExchangeBalances = async () => {
176177
},
177178
])
178179
.sort({ convertedDate: 1 })
179-
.toArray((_err, data = []) => {
180+
.toArray()
181+
.then(data => {
180182
const balances = {};
181183
accounts.forEach(({ account }) => (balances[account] = []));
182184

server/cron/marketCapRank.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const getNextHour = () => {
1515
// At every 20th minute.
1616
cron.schedule("*/20 * * * *", async () => {
1717
try {
18-
const database = db.getDatabase();
18+
const database = await db.getDatabase();
1919

2020
if (!database) {
2121
throw new Error("Mongo unavailable for marketCapRank");

server/cron/nodeLocations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const doNodeLocations = async () => {
9797
results = results.concat(locationResults);
9898
}
9999

100-
const database = db.getDatabase();
100+
const database = await db.getDatabase();
101101

102102
if (!database) {
103103
throw new Error("Mongo unavailable for doNodeLocations");

0 commit comments

Comments
 (0)