-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathuserCount.js
More file actions
49 lines (38 loc) · 1.15 KB
/
userCount.js
File metadata and controls
49 lines (38 loc) · 1.15 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
import fs from "fs";
import path from "path";
import dotenv from "dotenv";
import { MongoClient } from "mongodb";
dotenv.config();
const uri = process.env.MONGO_URI;
if (!uri) {
console.error("❌ MONGO_URI is not defined in .env");
process.exit(1);
}
const statsFile = path.join(process.cwd(), "user-stats.json");
const client = new MongoClient(uri);
(async () => {
try {
await client.connect();
const db = client.db();
const collection = db.collection("settings");
const count = await collection.countDocuments({ _id: { $exists: true } });
const today = new Date().toISOString().slice(0, 10);
let stats = [];
if (fs.existsSync(statsFile)) {
stats = JSON.parse(fs.readFileSync(statsFile, "utf-8"));
}
const existingIndex = stats.findIndex(entry => entry.date === today);
if (existingIndex >= 0) {
stats[existingIndex].count = count;
} else {
stats.push({ date: today, count });
}
fs.writeFileSync(statsFile, JSON.stringify(stats, null, 2));
console.log(count);
} catch (err) {
console.error("❌ Error:", err);
process.exit(1);
} finally {
await client.close();
}
})();