const DBName = "EMI_SOCIAL"; let userProfileCache = {}; userDB = (DB) => { DB.profileCols = DB.db.db(DBName).collection("profiles"); DB.newProfile = (profileObj) => { return DB.profileCols.insertOne(profileObj.toObj()).catch((err) => { console.log(err); return false; }); } DB.getProfile = async (profileId) => { if (userProfileCache[profileId]) return userProfileCache[profileId]; const _id = DB.ObjectID(profileId); let r = await DB.profileCols.findOne({ _id }).catch((err) => { console.log(err); return false; }); if (r) userProfileCache[profileId] = r; return r; } DB.getProfiles = async (userId) => { const userid = DB.ObjectID(userId); return await DB.profileCols.find({ userid }).toArray().catch((err) => { console.log(err); return false; }); } DB.latestProfile = async (userId) => { const userid = DB.ObjectID(userId); let r = await DB.profileCols.find({ userid }) .sort({ lastUpdate: -1 }).limit(1) .toArray().catch((err) => { console.log(err); return false; }); if (r[0]) userProfileCache[r[0].id] = r[0]; return r[0]; } } module.exports = userDB;