monkeytype/backend/dao/usersDAO.js

27 lines
766 B
JavaScript
Raw Normal View History

2021-06-07 00:32:37 +08:00
const { mongoDB } = require("../init/mongodb");
class UsersDAO {
static async addUser(name, email, uid) {
return await mongoDB()
.collection("users")
.insertOne({ name, email, uid, addedAt: Date.now() });
}
static async updateName(uid, name) {
const nameDoc = await mongoDB()
.collection("users")
.findOne({ name: { $regex: new RegExp(`^${name}$`, "i") } });
if (nameDoc) throw new Error("Username already taken");
return await mongoDB()
.collection("users")
.updateOne({ uid }, { $set: { name } });
}
static async getUser(uid) {
const user = await mongoDB().collection("users").findOne({ uid });
if (!user) throw new Error("User not found");
return user;
}
}
module.exports = UsersDAO;