Skip to content

Commit

Permalink
Updates for new mongodb package.
Browse files Browse the repository at this point in the history
  • Loading branch information
roncli committed Aug 24, 2021
1 parent 8c15828 commit 5b4043b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 42 deletions.
1 change: 0 additions & 1 deletion node/package.json
Expand Up @@ -50,7 +50,6 @@
"@types/generic-pool": "3.1.10",
"@types/html-minifier": "4.0.1",
"@types/ioredis": "4.26.7",
"@types/mongodb": "4.0.7",
"@types/node-schedule": "1.3.2"
}
}
9 changes: 4 additions & 5 deletions node/src/database/attendee.js
Expand Up @@ -32,14 +32,13 @@ class AttendeeDb {
static async add(data) {
const db = await Db.get();

/** @type {MongoDb.FindAndModifyWriteOpResultObject<AttendeeTypes.AttendeeMongoData>} */
const result = await db.collection("attendee").findOneAndUpdate({
const result = /** @type {MongoDb.ModifyResult<AttendeeTypes.AttendeeMongoData>} */(await db.collection("attendee").findOneAndUpdate({ // eslint-disable-line no-extra-parens
eventId: MongoDb.Long.fromNumber(data.eventId),
userId: MongoDb.Long.fromNumber(data.userId)
}, {$set: {
eventId: MongoDb.Long.fromNumber(data.eventId),
userId: MongoDb.Long.fromNumber(data.userId)
}}, {upsert: true, returnOriginal: false});
}}, {upsert: true, returnDocument: "after"}));

data._id = result.value._id.toHexString();

Expand All @@ -61,7 +60,7 @@ class AttendeeDb {
static async getByEventId(id) {
const db = await Db.get();

return db.collection("attendee").aggregate([
return /** @type {Promise<{discordId: string}[]>} */(db.collection("attendee").aggregate([ // eslint-disable-line no-extra-parens
{
$match: {eventId: Db.toLong(id)}
},
Expand All @@ -87,7 +86,7 @@ class AttendeeDb {
discordId: "$user.discord.id"
}
}
]).toArray();
]).toArray());
}

// ### ## # # ## # # ##
Expand Down
14 changes: 6 additions & 8 deletions node/src/database/event.js
Expand Up @@ -50,10 +50,10 @@ class EventDb {
event.description = data.description;
}

/** @type {MongoDb.InsertOneWriteOpResult<EventTypes.EventMongoData>} */
await db.collection("event").insertOne(event);
/** @type {MongoDb.InsertOneResult<EventTypes.EventMongoData>} */
const result = await db.collection("event").insertOne(event);

data._id = Db.fromLong(event._id);
data._id = result.insertedId.toNumber();

return data;
}
Expand All @@ -73,8 +73,7 @@ class EventDb {
static async get(id) {
const db = await Db.get();

/** @type {EventTypes.EventMongoData} */
const event = await db.collection("event").findOne({_id: MongoDb.Long.fromNumber(id)});
const event = /** @type {EventTypes.EventMongoData} */(await db.collection("event").findOne({_id: MongoDb.Long.fromNumber(id)})); // eslint-disable-line no-extra-parens

if (!event) {
return void 0;
Expand Down Expand Up @@ -113,15 +112,14 @@ class EventDb {
range.start = {$lte: end};
}

/** @type {EventTypes.EventMongoData[]} */
const events = await db.collection("event").find({
const events = /** @type {EventTypes.EventMongoData[]} */(await db.collection("event").find({ // eslint-disable-line no-extra-parens
$and: [
{
start: {$lte: end},
end: {$gte: start}
}
]
}).toArray();
}).toArray());

return events.map((e) => ({
_id: Db.fromLong(e._id),
Expand Down
5 changes: 1 addition & 4 deletions node/src/database/index.js
Expand Up @@ -69,14 +69,11 @@ class Db {
client = new MongoDb.MongoClient(`mongodb://web_sixgg:${process.env.WEB_SIXGG_PASSWORD}@db:27017/sixgg`, {
authMechanism: "SCRAM-SHA-256",
authSource: "admin",
useUnifiedTopology: true,
promoteLongs: false
});
}

if (!client.isConnected()) {
await client.connect();
}
await client.connect();

if (!db) {
db = client.db("sixgg");
Expand Down
4 changes: 2 additions & 2 deletions node/src/database/session.js
Expand Up @@ -32,7 +32,7 @@ class SessionDb {
static async delete(id) {
const db = await Db.get();

await db.collection("session").deleteOne({_id: MongoDb.ObjectID.createFromHexString(id)});
await db.collection("session").deleteOne({_id: MongoDb.ObjectId.createFromHexString(id)});
}

// # #
Expand All @@ -55,7 +55,7 @@ class SessionDb {
refreshToken: Encryption.encryptWithSalt(session.refreshToken)
};

await db.collection("session").findOneAndUpdate({_id: MongoDb.ObjectID.createFromHexString(session._id), ip: session.ip, userId: MongoDb.Long.fromNumber(session.userId)}, {$set: {
await db.collection("session").findOneAndUpdate({_id: MongoDb.ObjectId.createFromHexString(session._id), ip: session.ip, userId: MongoDb.Long.fromNumber(session.userId)}, {$set: {
ip: session.ip,
userId: MongoDb.Long.fromNumber(session.userId),
accessToken: {
Expand Down
3 changes: 1 addition & 2 deletions node/src/database/twitch.js
Expand Up @@ -34,8 +34,7 @@ class TwitchDb {
static async get() {
const db = await Db.get();

/** @type {TwitchTypes.EncryptedMongoTokens} */
const encryptedTokens = await db.collection("twitch").findOne({});
const encryptedTokens = /** @type {TwitchTypes.EncryptedMongoTokens} */(await db.collection("twitch").findOne({})); // eslint-disable-line no-extra-parens

if (!encryptedTokens) {
return void 0;
Expand Down
33 changes: 13 additions & 20 deletions node/src/database/user.js
Expand Up @@ -43,8 +43,7 @@ class UserDb {
static async get(id) {
const db = await Db.get();

/** @type {UserTypes.UserMongoData} */
const user = await db.collection("user").findOne({_id: id});
const user = /** @type {UserTypes.UserMongoData} */(await db.collection("user").findOne({_id: id})); // eslint-disable-line no-extra-parens

if (!user) {
return void 0;
Expand Down Expand Up @@ -77,8 +76,7 @@ class UserDb {

await db.collection("user").deleteMany({"discord.id": {$nin: discordIds}});

/** @type {UserTypes.UserMongoData[]} */
const users = await db.collection("user").find().toArray();
const users = /** @type {UserTypes.UserMongoData[]} */(await db.collection("user").find().toArray()); // eslint-disable-line no-extra-parens

return users.map((u) => ({
_id: Db.fromLong(u._id),
Expand All @@ -105,8 +103,7 @@ class UserDb {
static async getByGuildMember(member) {
const db = await Db.get();

/** @type {{user: UserTypes.UserMongoData, session: SessionTypes.EncryptedMongoSessionData}} */
const encryptedData = await db.collection("session").aggregate([
const encryptedData = /** @type {{user: UserTypes.UserMongoData, session: SessionTypes.EncryptedMongoSessionData}} */(await db.collection("session").aggregate([ // eslint-disable-line no-extra-parens
{
$match: {
"discord.id": member.id
Expand All @@ -129,7 +126,7 @@ class UserDb {
{
$unwind: "$user"
}
]).next();
]).next());

if (!encryptedData) {
return void 0;
Expand Down Expand Up @@ -189,8 +186,7 @@ class UserDb {
static async getByEventAttendees(eventId) {
const db = await Db.get();

/** @type {UserTypes.UserMongoData[]} */
const attendees = await db.collection("attendee").aggregate([
const attendees = /** @type {UserTypes.UserMongoData[]} */(await db.collection("attendee").aggregate([ // eslint-disable-line no-extra-parens
{
$match: {
eventId: MongoDb.Long.fromNumber(eventId)
Expand Down Expand Up @@ -222,7 +218,7 @@ class UserDb {
guildMember: 1
}
}
]).toArray();
]).toArray());

return attendees.map((a) => ({
_id: Db.fromLong(a._id),
Expand All @@ -247,8 +243,7 @@ class UserDb {
static async getBySession(id, ip) {
const db = await Db.get();

/** @type {{user: UserTypes.UserMongoData, session: SessionTypes.EncryptedMongoSessionData}} */
const encryptedData = await db.collection("session").aggregate([
const encryptedData = /** @type {{user: UserTypes.UserMongoData, session: SessionTypes.EncryptedMongoSessionData}} */(await db.collection("session").aggregate([ // eslint-disable-line no-extra-parens
{
$match: {
_id: new MongoDb.ObjectId(id),
Expand All @@ -272,7 +267,7 @@ class UserDb {
{
$unwind: "$user"
}
]).next();
]).next());

if (!encryptedData) {
return void 0;
Expand Down Expand Up @@ -337,8 +332,7 @@ class UserDb {
/** @type {UserTypes.UserMongoData} */
let userResult;
if (await db.collection("user").findOne({"discord.id": user.id})) {
/** @type {MongoDb.FindAndModifyWriteOpResultObject<UserTypes.UserMongoData>} */
const result = await db.collection("user").findOneAndUpdate({"discord.id": user.id}, {$set: {
const result = /** @type {MongoDb.ModifyResult<UserTypes.UserMongoData>} */(await db.collection("user").findOneAndUpdate({"discord.id": user.id}, {$set: { // eslint-disable-line no-extra-parens
discord: {
id: user.id,
username: user.username,
Expand All @@ -353,7 +347,7 @@ class UserDb {
id: c.id,
type: c.type
}))
}});
}}));

userResult = result.value;
userResult._id = Db.toLong(userResult._id);
Expand All @@ -379,7 +373,7 @@ class UserDb {

await Db.id(userResult, "user");

await db.collection("user").insertOne(userResult);
await /** @type {MongoDb.Collection<UserTypes.UserMongoData>} */(db.collection("user")).insertOne(userResult); // eslint-disable-line no-extra-parens
}

const expires = new Date(),
Expand All @@ -390,8 +384,7 @@ class UserDb {

expires.setSeconds(expires.getSeconds() + token.expires_in - 3600);

/** @type {MongoDb.FindAndModifyWriteOpResultObject<SessionTypes.EncryptedMongoSessionData>} */
const sessionResult = await db.collection("session").findOneAndUpdate({ip: req.ip, userId: userResult._id}, {$set: {
const sessionResult = /** @type {MongoDb.ModifyResult<SessionTypes.EncryptedMongoSessionData>} */(await db.collection("session").findOneAndUpdate({ip: req.ip, userId: userResult._id}, {$set: { // eslint-disable-line no-extra-parens
ip: req.ip,
userId: userResult._id,
accessToken: {
Expand All @@ -403,7 +396,7 @@ class UserDb {
encrypted: new MongoDb.Binary(encryptedTokens.refreshToken.encrypted)
},
expires
}}, {upsert: true, returnOriginal: false});
}}, {upsert: true, returnDocument: "after"}));

return {
user: {
Expand Down

0 comments on commit 5b4043b

Please sign in to comment.