How to build this db model

Working on a NoSQL implementation of this discord bot me and my friend are setting up. What it'll do is iterate through all messages and get the amount of emojis each user has across all their messages. I was wondering what a good model for this would be? Should we build a model for each User, and then iterate through all their emojis? I have this so far:
type Emoji = {
emoji: string;
count: number;
};

export default class User {
constructor(
public server: string,
public username: string,
public emojis: Emoji[],
) {}
}
type Emoji = {
emoji: string;
count: number;
};

export default class User {
constructor(
public server: string,
public username: string,
public emojis: Emoji[],
) {}
}
This way, we can just do something like:
// psuedocode
const serverUsers = users.filter((user) => {
return user.server === server;
});

users.sort((a, b) => {
const userACount = a.emojis.find((emoji) => {
return (emoji.emoji === 'chart').count;
});

const userBCount = b.emojis.find((emoji) => {
return (emoji.emoji === 'chart').count;
});

return userACount > userBCount; // I know this won't work lol gotta think about it
});
// psuedocode
const serverUsers = users.filter((user) => {
return user.server === server;
});

users.sort((a, b) => {
const userACount = a.emojis.find((emoji) => {
return (emoji.emoji === 'chart').count;
});

const userBCount = b.emojis.find((emoji) => {
return (emoji.emoji === 'chart').count;
});

return userACount > userBCount; // I know this won't work lol gotta think about it
});
4 Replies
dys 🐙
dys 🐙12h ago
Could you the counts of each emoji in your db? It seems like the list of emojis could get voluminous quickly. You have to do a db update for each emojiing event anyway. It'd definitely speed up the calculation. (Also, your sort should return userBCount - userACount since it's expecting a positive, 0, or negative value.)
vince
vince12h ago
Hey dys! Do you mean I should get the count of a specific emoji in the backend instead of the frontend? e.g have an endpoint like /get/emoji/:emojiId and that way we're limiting the amount of work the bot has to do?
dys 🐙
dys 🐙12h ago
I actually misread your code. Because you used an array, I assumed you were tracking all the emojis in a list. Your structure is actually what I was suggesting. 😸 How I'd change what you have is to use a map, so you have user.emojiCount = { chart: 10, … } (so you can call user.emojiCount['chart']) rather than user.emojis = [{ emoji: 'chart', count: 10 }, …] & have to find the count.
vince
vince11h ago
No worries 😂 and that's smart! I'll try that, thank you!! 🙂
Want results from more Discord servers?
Add your server