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");

0 commit comments

Comments
 (0)