-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
40 lines (28 loc) · 1.06 KB
/
db.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
import { MongoClient } from 'mongodb';
const dbHost = process.env.DDBB_HOST ?? 'localhost:27017';
const dbUser = process.env.DDBB_USER ?? null;
const dbPass = process.env.DDBB_PASS ?? null;
const dbName = process.env.DDBB_NAME ?? 'pokeGenAI';
const userSegment = dbUser ? `${dbUser}:${dbPass}@` : '';
const mongoUri = `mongodb://${userSegment}${dbHost}/${dbName}`;
const client = new MongoClient(mongoUri);
const connectDB = async () => {
await client.connect();
const db = client.db(dbName);
console.log('Connected successfully to mongodb server');
const collection = db.collection('tweets');
await collection.createIndex({ createdAt: 1, user: 1 });
return collection;
};
const getUserDailyTweets = async (collection, userId) => {
const startDate = new Date();
startDate.setHours(0, 0, 0, 0);
const endDate = new Date();
endDate.setHours(23, 59, 59, 999);
const dailyTweets = await collection.count({
createdAt: { $gte: startDate, $lt: endDate },
'user.userId': userId,
});
return dailyTweets;
};
export { connectDB, getUserDailyTweets };