Skip to content

Commit

Permalink
refactor: update mongodb to new version
Browse files Browse the repository at this point in the history
  • Loading branch information
jimchen5209 committed Apr 9, 2024
1 parent 9a297e0 commit 93b7891
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
3 changes: 2 additions & 1 deletion dist/Core/MongoDB.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions dist/Core/SetManager.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions dist/Core/TimeManager.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/Core/MongoDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export class MongoDB extends EventEmitter {

const dbConfig = config.database;

MongoClient.connect(dbConfig.host, { useNewUrlParser: true, useUnifiedTopology: true }).then(client => {
const client = new MongoClient(dbConfig.host);

client.connect().then(() => {
console.log('[MongoDB] Connected successfully to server');

this.client = client.db(dbConfig.name);
Expand Down
12 changes: 7 additions & 5 deletions src/Core/SetManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Collection, ObjectID } from 'mongodb';
import { Collection, ObjectId } from 'mongodb';
import { Core } from '..';
import { ERR_DB_NOT_INIT } from './MongoDB';

export interface ISet {
_id: ObjectID;
_id: ObjectId;
serverID: string;
settings: {
rankChannelID: string;
Expand All @@ -28,15 +28,17 @@ export class SetManager {
public async create(serverID: string, rankChannelID: string = '', rankDisplay: boolean = false, continuousChannelID: string = '', continuousDisplay: boolean = false) {
if (!this.database) throw ERR_DB_NOT_INIT;

return (await this.database.insertOne({
const data = {
serverID,
settings: {
rankChannelID,
rankDisplay,
continuousChannelID,
continuousDisplay
}
} as ISet)).ops[0] as ISet;
} as ISet;

return (await this.database.insertOne(data)).acknowledged ? data : null;
}

public async get(serverID: string) {
Expand Down Expand Up @@ -66,6 +68,6 @@ export class SetManager {
{ serverID },
set,
{ upsert: true }
)).value;
));
}
}
12 changes: 7 additions & 5 deletions src/Core/TimeManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Collection, ObjectID } from 'mongodb';
import { Collection, ObjectId } from 'mongodb';
import { Core } from '..';
import { ERR_DB_NOT_INIT } from './MongoDB';

export interface ITime {
_id: ObjectID;
_id: ObjectId;
serverID: string;
userID: string;
timeStamp: number;
Expand All @@ -25,12 +25,14 @@ export class TimeManager {
public async create(serverID: string, userID: string, timeStamp: number, type: string) {
if (!this.database) throw ERR_DB_NOT_INIT;

return (await this.database.insertOne({
const data = {
serverID,
userID,
timeStamp,
type
} as ITime)).ops[0] as ITime;
} as ITime;

return (await this.database.insertOne(data)).acknowledged ? data : null;
}

public async get(serverID: string, startTime: number, endTime: number) {
Expand Down Expand Up @@ -60,6 +62,6 @@ export class TimeManager {
public async getCountByUserAndType(serverID: string, userID: string, startTime: number, endTime: number, type: string) {
if (!this.database) throw ERR_DB_NOT_INIT;

return this.database.find({ serverID, userID, timeStamp: { $gte: startTime, $lt: endTime }, type }).count();
return this.database.countDocuments({ serverID, userID, timeStamp: { $gte: startTime, $lt: endTime }, type });
}
}

0 comments on commit 93b7891

Please sign in to comment.